[begin preamble]
linebreak = {
};
var events = retrieve({select priority as `day`, body as `changes` from entries where parent=[_id] and priority <= [int(_subtitle)] order by priority asc, id asc});
var timeline = list();
null(each(events) ?(e) {[
if(timeline has e.day) {[
timeline[e.day] &= list(e.changes);
]} else {[
timeline[e.day] = list(e.changes);
]};
]});
var resources = split(_body, linebreak);
var resource_map = list();
null(each(resources) ?(x) {[
xs = split(x, " ");
var r = list();
r.growth = float(xs[1]);
r.delta = float(xs[2]);
resource_map[xs[0]] = r;
]});
var out_log = "";
get_resource = function(resource_name, day) {[
var day = int(day);
var amount = 0;
var resource = resource_map[resource_name];
var di = 1;
while(di <= day) {[
var amount_override = 0;
out_log = out_log & "<br>" & di & " less than or equal to " & day;
if(timeline has di)
null(each(timeline[di]) ?(c) {[
out_log = out_log & "<br>" & c;
change = split(c, " ");
if(change[0] != resource_name) {[
// continue
]} else if(change[1] == "add") {[
amount = amount + float(change[2]);
out_log = out_log & "<br>" & "added " & change[2] & " " & change[0];
]} else if(change[1] == "set") {[
amount_override = 1;
out_log = out_log & "<br>" & "set " & change[2] & " " & change[0];
amount = float(change[2]);
]} else if(change[1] == "growth") {[
out_log = out_log & "<br>" & "growth " & change[2] & " " & change[0];
resource.growth = float(change[2]);
]} else if(change[1] == "accumulation") {[
resource.delta = float(change[2]);
out_log = out_log & "<br>" & "acc " & change[2] & " " & change[0];
]};
]});
if(amount_override == 0)
amount = (amount * resource.growth) + resource.delta;
out_log = out_log & "<br>" & resource_name & " now " & amount;
di = di + 1;
]};
resource_map[resource_name] = resource;
return amount;
]};
[end preamble]
[begin mode view][
suppress_template();
today = int(_subtitle);
override_title("Resource Calendar: Day " & today, "");
override_parent(_id, _title);
resource_names = list();
r = each(resources) ?(resource_initial) {
[
axs = split(resource_initial, " ");
today_value = get_resource(axs[0], today);
var resource_info = resource_map[axs[0]];
resource_names = merge(resource_names, list(axs[0]));
var growth = int((resource_info.growth * 100.0) - 99.5);
var delta = resource_info.delta;
]<ul class="resource">
<li>[replace(axs[0], "_", " ")]</li>
<ul class="numbers">
<li>[
var decimal = strpos(today_value, ".");
if(decimal >= 0)
substr(today_value, 0, decimal + 3)
else
today_value;
if(delta > 0) delta = {+[delta]};
if(delta != 0) "<li>" & delta & "/day";
if(growth > 0) growth = {+[growth]};
if(growth != 0) "<li>" & growth & "%/day";
]
</ul>
</ul>
};
]
<html><head>
<title>[_title]: Day [today]</title>
<style>
#resources th, #resources td {
padding: 2em 5em;
text-transform: uppercase;
background: rgba(255, 255, 255, 0.2);
text-align: center;
}
#resources {
margin: 2em auto;
}
.edit_link {
float: right;
margin-left: 1em;
}
</style>
<link rel="stylesheet" type="text/css" href="/theme/tabletop.css">
</head><body><article><article><article>
<h1>[_title]</h1>
<h2>Day [today]</h2>
<ul id="resources"><li>[concat(r, "<li>")]</ul>
<h3>Events</h3>
<div class="info">
[
show("view", retrieve({select * from entries where parent=[_id] order by priority asc, id asc}));
]
</div>
[
if(can_edit()) {
<div id="edit-controls">
<div>
<h4>Add Event</h4>[
form(_id, "add_event", list(
list("text", "date", _subtitle, "Date"),
list("@", "", _select("", concat(resource_names, linebreak), "resource"), "Resource"),
list("@", "", _select("", {add<!>One-time increase/decrease
set<!>Set exact value
growth<!>Change growth rate
accumulation<!>Change daily increase}, "type"), "Event type"),
list("text", "amount", "0", "Amount"),
list("text", "comment", "", "Comment")
));
]
</div>
<div>
<h4>Set Current Date</h4>[
form(_id, "set_date", list(
list("text", "date", _subtitle, "Date")
));
]
</div>
<div>
<h4>Change Resources</h4>
<a href="/?id=[_id]&mode=edit">Edit this page</a>
</div>
</div>
};
]</article></article></article></body></html>[end mode view]
[begin mode add_event][
day = int(_params.date);
e = new_page("new event on date " & day, _id);
event = import entry e;
event._template = title2id("Resource Calendar Event");
event._title = "event " & e & " on date " & day;
event._priority = day;
event._body = _params.resource & " " & _params.type & " " & _params.amount & " " & _params.comment;
refresh_to({/?id=[_id]}, 1);
]<a href="/?id=[_id]">Continue</a>[end mode add_event]
[begin mode set_date][
_subtitle = int(_params.date);
refresh_to({/?id=[_id]}, 1);
]<a href="/?id=[_id]">Continue</a>[end mode set_date]
[begin mode edit][
form(_id, "submit", list(
list("text", "title", _title, "Title"),
list("textarea", "body", _body, "Resources")
));
today = int(_subtitle);
override_title("Resource Calendar: Day " & today, "");
override_parent(_id, _title);
resource_names = list();
var r = each(resources) ?(resource_initial) {[
axs = split(resource_initial, " ");
axs[0];
]};
]
<ul><li>[concat(r, "<li>")]</ul>
<p>Format is <tt>resource_name growth_rate daily_increase</tt>. Examples:</p>
<ul>
<li>a static balance would be <tt>money 1.00 0.0</tt>
<li>a steady paycheck of $10 per day would be <tt>money 1.00 10</tt>
<li>a 5% per-day growth rate would be <tt>debt 1.05 0</tt>
</ul>
<p>Daily increase is applied after growth rate.</p>
[end mode edit]
[begin mode submit][
_title = _params.title;
_body = _params.body;
refresh_to({/?id=[_id]}, 1);
]<a href="/?id=[_id]">Continue</a>[end mode submit]