state sync; the basic early game works
This commit is contained in:
@@ -21,6 +21,10 @@ async function prepare_gamestate() {
|
||||
}
|
||||
|
||||
tick_meter_running = true;
|
||||
|
||||
if (window.pywebview.api.debug_mode) {
|
||||
dev_toolbox(true);
|
||||
}
|
||||
}
|
||||
|
||||
function save_game() {
|
||||
|
@@ -45,6 +45,44 @@ function record_log(text) {
|
||||
page_elements["div_log"].append(div_logrow);
|
||||
}
|
||||
|
||||
function record_log_with_choices() {
|
||||
const div_logrow = document.createElement("div");
|
||||
if (bool_log_alt) { div_logrow.className = "log-line"; }
|
||||
else { div_logrow.className = "log-line-alt"; }
|
||||
bool_log_alt = !bool_log_alt;
|
||||
|
||||
const div_logtick = document.createElement("div");
|
||||
div_logtick.className = "log-tick"
|
||||
div_logtick.innerHTML = "Day " + gamestate["tick"];
|
||||
div_logrow.append(div_logtick);
|
||||
|
||||
const div_logdata = document.createElement("div");
|
||||
|
||||
const div_logmsg = document.createElement("div");
|
||||
div_logmsg.innerHTML = arguments[0];
|
||||
div_logmsg.className = "log-msg";
|
||||
div_logdata.append(div_logmsg);
|
||||
|
||||
const div_logactions = document.createElement("div");
|
||||
div_logactions.className = "log-button-row";
|
||||
|
||||
for (var i = 1; i < arguments.length; i += 2) {
|
||||
console.log(i)
|
||||
var label = arguments[i];
|
||||
var callback = arguments[i+1];
|
||||
|
||||
var btn_action = document.createElement("button");
|
||||
btn_action.innerHTML = label;
|
||||
btn_action.setAttribute("onclick", callback + "; tick_meter_running = true;");
|
||||
div_logactions.append(btn_action);
|
||||
}
|
||||
div_logdata.append(div_logactions);
|
||||
|
||||
div_logrow.append(div_logdata);
|
||||
page_elements["div_log"].append(div_logrow);
|
||||
tick_meter_running = false;
|
||||
}
|
||||
|
||||
function update_ui() {
|
||||
page_elements["lbl_name"].innerHTML = gamestate["name"];
|
||||
page_elements["lbl_tick"].innerHTML = gamestate["tick"];
|
||||
@@ -78,6 +116,18 @@ function dev_toolbox(open) {
|
||||
dev_toolbox_open = open;
|
||||
}
|
||||
|
||||
async function steal_resource(resource, amount, items) {
|
||||
var stealdata = await fetch("/act/steal/" + resource, {method: "POST", body: JSON.stringify({gamestate: gamestate})})
|
||||
.then(res => { return res.json(); })
|
||||
.catch(e => { throw e; });
|
||||
|
||||
if (stealdata["success"]) {
|
||||
gamestate[resource] += amount;
|
||||
record_log("Stole " + resource + " from a human: " + items.join(", "));
|
||||
}
|
||||
else { record_log("Didn't steal " + resource + " from a human"); }
|
||||
}
|
||||
|
||||
async function game_tick() {
|
||||
gamestate["tick"] += 1;
|
||||
ticks_since_last_save += 1;
|
||||
@@ -102,7 +152,55 @@ async function game_tick() {
|
||||
// Flavor event - no gameplay effect, but occasionally says something fun.
|
||||
record_log(tickdata["log"]);
|
||||
} else if (tickdata["event_type"] == 10) { // ENCHUMAN
|
||||
|
||||
var total_food = 0;
|
||||
var food_descs = [];
|
||||
var total_shinies = 0;
|
||||
var shinies_descs = [];
|
||||
|
||||
switch (page_elements["menu_enc_human"].value) {
|
||||
case "pause":
|
||||
tickdata.items.food.forEach((item) => {
|
||||
total_food += item["amount"];
|
||||
food_descs.push(item["desc"]);
|
||||
})
|
||||
tickdata.items.shinies.forEach((item) => {
|
||||
total_shinies += item["amount"];
|
||||
shinies_descs.push(item["desc"]);
|
||||
})
|
||||
var logstring = "You have encountered a human. It is carrying these resources:\n\n"
|
||||
logstring += "<ol>\n"
|
||||
if (total_food > 0) {
|
||||
logstring += "<li><b>" + total_food + " food:</b> " + food_descs.join(", ") + "</li>\n";
|
||||
}
|
||||
if (total_shinies > 0) {
|
||||
logstring += "<li><b>" + total_shinies + " shinies:</b> " + shinies_descs.join(", ") + "</li>\n";
|
||||
}
|
||||
logstring += "</ol>\nWhat would you like to do?";
|
||||
|
||||
record_log_with_choices(logstring,
|
||||
"Steal food", `steal_resource('food', ${total_food}, ${JSON.stringify(food_descs)})`,
|
||||
"Steal shinies", `steal_resource('shinies', ${total_shinies}, ${JSON.stringify(shinies_descs)})`
|
||||
)
|
||||
|
||||
break;
|
||||
case "steal-food":
|
||||
tickdata.items.food.forEach((item) => {
|
||||
total_food += item["amount"];
|
||||
food_descs.push(item["desc"]);
|
||||
})
|
||||
steal_resource("food", total_food, food_descs);
|
||||
break;
|
||||
case "steal-shinies":
|
||||
tickdata.items.shinies.forEach((item) => {
|
||||
total_shinies += item["amount"];
|
||||
shinies_descs.push(item["desc"]);
|
||||
})
|
||||
steal_resource("shinies", total_shinies, shinies_descs);
|
||||
break;
|
||||
default:
|
||||
console.error("undefined action " + page_elements["menu_enc_human"]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// sanity check
|
||||
|
Reference in New Issue
Block a user