1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#!/usr/bin/env lua
local foods = require'foods'
local qs = require'qs'
local days = {
"mon","tue","wed","thu","fri","sat","sun"
}
print"Content-type: text/html"
print""
local prev_foods = {}
local Q = {}
if os.getenv'REQUEST_METHOD' == 'POST' then
Q = qs.parse_qs(io.read())
local log = assert(io.open("/home/rebecca/pr/food/LOG", "a+"))
log:write(os.date("%Y-%m-%dT%H:%M:%S"))
for i=0,13 do
local prev_food = assert(tonumber(Q[tostring(i)]))
prev_foods[i] = prev_food
log:write(" "..prev_food)
end
log:write("\n")
log:close()
else
for i=0,13 do
prev_foods[i] = 1
end
end
local counts = {}
for i = 0,13 do
counts[i] = (counts[i] or 0) + 1
end
local start_day = tonumber(Q.start or 1)
local function ok(fi, si)
return true
end
local function sel(sel_ix)
local opts = {}
for food_ix,food in ipairs(foods) do
if food_ix == prev_foods[sel_ix] then
table.insert(opts,
'<option selected value='..food_ix..'>'
.. food_ix .. ' ' .. food.name .. '</option>'
)
elseif ok(food_ix, sel_ix) then
table.insert(opts,
'<option '..'value='..food_ix..'>'
.. food_ix .. ' ' .. food.name .. '</option>'
)
end
end
return '<select name='..sel_ix..'>'
.. table.concat(opts,'\n') .. '</select>'
end
local function row(n)
local d = 1+((n+start_day-1)%7)
return '<tr>'
.. '<th>' .. days[d] .. '</th>'
.. '<td>' .. sel(2*n) .. '</td>'
.. '<td>' .. sel(2*n+1) .. '</td>'
end
print[[
<form method=post>
<label for=start>start on:</label>
<select id=start name=start>
]]
for i,d in ipairs(days) do
local s = i == start_day and 'selected ' or ''
print('<option '..s..'value='..i..'>'..d..'</option>')
end
print[[
</select>
<table>
<tr>
<th>\</th>
<th>lunch</th>
<th>dinner</th>
</tr>
]]
for n=0,6 do print(row(n)) end
print[[
</table>
<input type=submit value=check name=submit>
<br>
<hr>
]]
local ings = {}
for _,fx in pairs(prev_foods) do
local food = foods[fx]
for k,v in pairs(foods[fx].ing) do
ings[k] = (ings[k] or 0) + v
end
end
local i2 = {}
for k,v in pairs(ings) do
table.insert(i2,k)
end
table.sort(i2)
print'<dl>'
for _,g in ipairs(i2) do
print('<dt>'..g..'</dt>')
print('<dd>'..ings[g]..'x</dd>')
end
print'<hr>'
for _,g in ipairs(i2) do
print(g..', ')
end
|