diff options
-rw-r--r-- | conflicts.txt | 22 | ||||
-rw-r--r-- | conflicts_orig.txt | 15 | ||||
-rw-r--r-- | foods.lua | 95 | ||||
-rw-r--r-- | index.php | 188 | ||||
-rwxr-xr-x | page.cgi | 120 | ||||
-rw-r--r-- | qs.lua | 26 |
6 files changed, 466 insertions, 0 deletions
diff --git a/conflicts.txt b/conflicts.txt new file mode 100644 index 0000000..1d7a968 --- /dev/null +++ b/conflicts.txt @@ -0,0 +1,22 @@ +--- +soup: bean soup, tomato soup, squash soup +curry: chickpea curry, mj curry +potato: mash, jacket beans +greens: ramen, fast ramen, stir fry +tomato: tomato soup, mj spghet, mj curry +meat: chilli, gurger +beans: bean soup, chilli + +bean soup: soup, beans +tomato soup: soup, tomato +squash soup: soup +chickpea curry: curry +mj curry: curry, tomato +mash: potato +jacket beans: potato +ramen: greens +fast ramen: greens +stir fry: greens +mj spghet: tomato +chilli: meat, beans +gurger: meat diff --git a/conflicts_orig.txt b/conflicts_orig.txt new file mode 100644 index 0000000..92998e5 --- /dev/null +++ b/conflicts_orig.txt @@ -0,0 +1,15 @@ +bean soup: tomato soup, squash soup, bean soup, chilli +chickpea curry: mj curry, chickpea curry +chilli: chilli, gurger +fast ramen: fast ramen, ramen +gurger: jacket beans, chilli, gurger +jacket beans: jacket beans, mash, gurger +mash: jacket beans, mash +mj curry: mj spghet, mj curry, tomato soup, mj penne, chickpea curry +mj penne: mj spghet, mj curry, tomato soup, mj penne +mj spghet: mj spghet, mj curry, tomato soup, mj penne +ramen: fast ramen, ramen +squash soup: tomato soup, squash soup, bean soup +stir fry: stir fry, fast ramen, ramen +tomato soup: tomato soup, squash soup, bean soup, mj spghet, mj curry +vesto/egg: vesto/egg diff --git a/foods.lua b/foods.lua new file mode 100644 index 0000000..663e3da --- /dev/null +++ b/foods.lua @@ -0,0 +1,95 @@ +local foods = {} +local function f(name,tags,ing) + table.insert(foods,{ + name=name, + tags=tags, + ing=ing or {}, + }) +end + +f('*',{}) +f('mj spghet',{'tomato'},{ + broccoli=.5, + passata=1, + spaghetti=.3}) +f('mj curry',{'curry','tomato'},{ + broccoli=.5, + passata=1, + rice=.3}) +f('chilli',{'meat','beans'}) +f('stir fry',{'greens'},{ + kale=.3, + broccoli=.5, + noodles=1.5, + sweetcorn=.5, + mangetout=.5}) +f('jacket beans',{'potato'},{ + potatoes=5, + beans=2}) +f('mash',{'potato'},{ + potatoes=5, + peas=.3}) +f('vesto/egg',{},{ + egg=2, + vesto=.5, + ['black beans']=1, + spaghetti=.2}) +f('fast ramen',{'greens'},{ + ['instant ramen']=2, + broccoli=.5, + sweetcorn=.5}) +f('ramen',{'greens'},{ + ['ramen ingredients']=1}) +f('tomato soup',{'soup','tomato'},{ + ['tinned tomatoes']=3, + ['coconut milk']=1, + garlic=.5}) +f('squash soup',{'soup'},{ + squash=1, + onion=1, + garlic=.5, + ['vegetable stock']=.15, + ['coconut milk']=1}) +f('bean soup',{'soup','beans'},{ + onion=1, + garlic=.5, + pepper=1, + ['black beans']=3, + ['tinned tomatoes']=1, + ['vegetable stock']=.15, + lime=1}) +f('gurger',{'meat'},{ + ['bean burger']=2, + ['minced meat']=.25, + chips=1, + egg=1, + bun=4}) +f('chickpea curry',{'curry','chickpea'},{ + chickpea=2, + ['chopped tomato']=2, + lemon=1, + onion=2, + garlic=.5, + ginger=.5, + chilli=1}) +f('chickpea thing',{'chickpea'},{ + chickpea=1, + garlic=.5, + ginger=.5, + chilli=1, + rice=.3, +}) +f('pizza',{},{ + pizza=2, +}) +f('daniel beans',{},{ + ['black beans']=1, + pepper=.5, + garlic=.5, + rice=.3, +}) + + + + +return foods diff --git a/index.php b/index.php new file mode 100644 index 0000000..840d987 --- /dev/null +++ b/index.php @@ -0,0 +1,188 @@ +<?php +error_reporting(E_ALL); +$GLOBALS["days"]= array("Mon","Tue","Wed","Thur","Fri","Sat","Sun"); +$GLOBALS["meals"] = array("*" ,"mj spghet", "mj curry","chilli", + "stir fry","jacket beans","mash", + "vesto/egg", "fast ramen", "ramen", + "tomato soup","squash soup","bean soup", + "gurger", "mj penne","chickpea curry"); +$GLOBALS["conflicts"] = [[],[1,2,10,14],[1,2,10,14,15],[3,13], + [4,8,9],[5,6,13],[5,6], + [7],[8,9],[8,9], + [10,11,12,1,2],[10,11,12],[10,11,12,3], + [5,3,13],[1,2,10,14], [2,15]]; + + +// 3-4x tinned tomatoes +// olive oil (ideally) +// 2x tins coconut milk +// garlic +// herbs (oregano, basil, thyme(?), rosemary(??), sage(???)) +// tomato puree + + +$GLOBALS["mealingredients"] = [ + 0 => [], + 1 => ["brocolli" => .5, + "passata" => 1, + "spaghetti" => .3], + 2 => ["brocolli" => .5, + "passata" => 1, + "rice" => .3], + 3 => [], + 4 => ["kale" => .3, + "pak choi" => .5, + "brocolli" => .5, + "rice" => .3, + "sweetcorn" => .5, + "mangetout" => .5], + 5 => ["potatoes" => 5, + "beans" => 2], + 6 => ["potatoes" => 5, + "peas" => .3], + 7 => ["egg" => 2, + "vesto" => .5, + "black beans" => 1, + "spaghetti" => .2], + 8 => ["instant ramen" => 2, + "brocolli" => .5, + "sweetcorn" => .5], + 9 => ["noodles" => .5, + "pak choi" => .5, + "brocolli" => .5, + "chilli" => .5, + "sweetcorn" => .5, + "mangetout" => .5, + "garlic" => 1 + ], + 10 => ["tinned tomatoes" => 3, + "coconut milk" => 1, + "garlic" => .5], + 11 => ["squash" => 1, + "onion" => 1, + "garlic" => .5, + "vegetable stock" => .15, + "coconut milk" => 1], + 12 => ["onion" => 1, + "garlic" => .5, + "pepper" => 1, + "black beans" => 3, + "tinned tomatoes" => 1, + "vegetable stock" => .15, + "lime" => 1], + 13 => ["bean burger"=>2, + "minced meat" => .25, + "chips" => 1, + "egg" => 1, + "bun" => 4], + 14 => ["brocolli" => .5, + "passata" => 1, + "penne" => .5], + 15 => ["chickpea" => 2, + "chopped tomato" => 2, + "lemon" => 1, + "onion" => 2, + "garlic" => .5, + "ginger" => .5, + "chilli" => 1] +]; + +$GLOBALS["ingredientsList"] = []; +$GLOBALS["ingredients"] = []; + + +$GLOBALS["allowance"] = [999,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]; + +for($i =0;$i<14;$i++){ + if ($_POST[$i]==""){ + $_POST[$i]=0; + } + $GLOBALS["allowance"][$_POST[$i]]-=1; +} + + +function genSel($dayNum,$meal){ + $p = (int)$_POST[$dayNum*2+$meal]; + $dm = $dayNum*2 + $meal; + $o = "<select name= \"" . ($dm) . "\" id=" . ($dm) . ">"; + $incompat = []; + for($i=max(0,$dm-1);$i<=min($dm+1,13); $i++){ + $incompat = array_merge($incompat,$GLOBALS["conflicts"][$_POST[$i]]); + } + for ($i = 0; $i < count($GLOBALS["meals"]); $i++) { + if(($_POST[$dm]=="" || !in_array($i,$incompat)) && $GLOBALS["allowance"][$i]>0){ + $o = $o . "<option value=" . $i . ">" . $GLOBALS["meals"][$i] . "</option>"; + }else if($p==$i){ + $o = $o . "<option selected=\"selected\" value=" . $i . ">" . $GLOBALS["meals"][$i] . "</option>"; + } + } + return $o . "</select>"; +} + +function genRow($dayNum){ + return "<tr><th>" . $GLOBALS["days"][($dayNum+5) % 7] . "</th><th>" . genSel($dayNum,0) . "</th><th>" . genSel($dayNum,1) . "</th></tr>"; +} + +for($i =0;$i<14;$i++){ + foreach($GLOBALS["mealingredients"][$_POST[$i]] as $key => $val){ + if(isset($GLOBALS["ingredients"][$key])){ + $GLOBALS["ingredients"][$key] += $val; + }else{ + $GLOBALS["ingredients"][$key] = $val; + } + } +} + +?> + +<html class="no-js" lang=""> + <head> + <meta charset="utf-8"> + <meta http-equiv="x-ua-compatible" content="ie=edge"> + <title>Food solver</title> + <meta name="description" content=""> + <meta name="viewport" content="width=device-width, initial-scale=1"> + + <!-- Place favicon.ico in the root directory --> + <style> + th {text-align: left;} + </style> + </head> + <body onload=""> + <!--[if lt IE 8]> + <p class="browserupgrade"> + You are using an <strong>outdated</strong> browser. Please + <a href="http://browsehappy.com/">upgrade your browser</a> to improve + your experience. + </p> + <![endif]--> + + <form method="post"> + <table> + <tr> + <th scope="col">\</th> + <th scope="col">Lunch</th> + <th scope="col">Dinner</th> + </tr> + <?php + for($x = 0;$x<7; $x++){ + echo genRow($x % 7); + } + ?> + </table> + <input type="submit" class="button" value="Check" name="submit" /> + <input type="submit" class="button" value="Print" name="submit" /> + </form> + <?php + if($_POST["submit"]=="Print"){ + echo "<table><tr><th>Ingredient</th><th>Qty</th></tr>"; + + foreach($GLOBALS["ingredients"] as $key => $val){ + echo "<tr><th>" . $key . "</th><th>" . $val . "</th></tr>"; + } + + echo "</table>"; + } + ?> + </body> +</html> diff --git a/page.cgi b/page.cgi new file mode 100755 index 0000000..6743f91 --- /dev/null +++ b/page.cgi @@ -0,0 +1,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 @@ -0,0 +1,26 @@ +-- querystring and post body parsing + +local function urldecode(s) + return s:gsub('+',' ') + :gsub('%%(%x%x)',function(x) + return string.char(tonumber(x,16)) + end) +end + +local function parse_qs(s) + local ix = 1 + local res = {} + while true do + local _,next_ix,ek,ev = s:find("([^=]+)=([^&]*)&?",ix) + if not ek then break end + local k,v = urldecode(ek), urldecode(ev) + res[k] = v + ix = next_ix + 1 + end + return res +end + +return { + urldecode = urldecode, + parse_qs = parse_qs, +} |