From 719b7f1cd0917980021f8715c7f5bf8a642254f6 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 3 Nov 2024 01:57:36 +0100 Subject: Propose deals and list accepted deals in political display. --- play.js | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) (limited to 'play.js') diff --git a/play.js b/play.js index 0decf5f..76aeb9e 100644 --- a/play.js +++ b/play.js @@ -92,6 +92,12 @@ const turn_name = [ "Winter 1744", ] +const DI_TURN = 0 +const DI_A_POWER = 1 +const DI_B_POWER = 2 +const DI_A_PROMISE = 3 +const DI_B_PROMISE = 4 + const F_EMPEROR_FRANCE = 1 const F_EMPEROR_AUSTRIA = 2 const F_ITALY_FRANCE = 4 @@ -1523,11 +1529,21 @@ function on_update() { layout_combat_marker() } - action_menu(document.getElementById("subsidy_menu"), [ + window.subsidy_menu.classList.toggle("hide", is_intro()) + action_menu(window.subsidy_menu, [ "propose_subsidy", "cancel_subsidy", + "propose_deal", ]) + update_deal_list(view.deals, window.active_deal_list, "Active Deals") + if (view.proposed_deal) { + update_deal_list([ view.proposed_deal ], window.proposed_deal_list, "Proposed Deal") + setTimeout(() => scroll_into_view(window.proposed_deal_list), 333) + } else { + update_deal_list(null, window.proposed_deal_list, "Proposed Deal") + } + action_button_with_argument("suit", SPADES, colorize_S) action_button_with_argument("suit", CLUBS, colorize_C) action_button_with_argument("suit", HEARTS, colorize_H) @@ -1679,6 +1695,101 @@ function on_log(text) { return p } +/* DEAL DIALOGS */ + +function propose_subsidy() { send_action("propose_subsidy") } +function cancel_subsidy() { send_action("cancel_subsidy") } + +function html_escape(str) { + return str.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">") +} + +function update_deal_item(deal) { + let [ turn, a_power, b_power, a_promise, b_promise ] = deal + let str = "" + str += "" + power_image[a_power] + str += "" + html_escape(a_promise) + str += "" + power_image[b_power] + str += "" + html_escape(b_promise) + str += "" + turn_name[turn] + return str +} + +function update_deal_list(deals, elt, title) { + if (deals && deals.length > 0) { + let str = "
" + title + for (let deal of deals) + str += update_deal_item(deal) + str += "
" + elt.innerHTML = str + elt.style.display = "block" + } else { + elt.style.display = "none" + } +} + +function propose_deal() { + if (!is_action("propose_deal")) + return + + let form = window.propose_deal_form + + form.a_power.value = view.power + switch (view.power) { + case P_FRANCE: + form.b_power.value = P_PRUSSIA + break + case P_PRUSSIA: + form.b_power.value = P_FRANCE + break + case P_PRAGMATIC: + form.b_power.value = P_AUSTRIA + break + case P_AUSTRIA: + form.b_power.value = P_PRAGMATIC + break + } + form.turn.value = view.turn + + for (let opt of form.turn.options) + opt.disabled = (opt.value < view.turn) + + window.propose_deal_dlog.showModal() +} + +function propose_deal_submit(evt) { + evt.preventDefault() + + let data = Object.fromEntries(new FormData(window.propose_deal_form)) + + data.turn = Number(data.turn) + data.a_power = Number(data.a_power) + data.b_power = Number(data.b_power) + + if (!data.a_promise || !data.b_promise) { + alert("Each side must promise something!") + return + } + + console.log(data, player_from_power(data.a_power), player_from_power(data.b_power)) + if (player_from_power(data.a_power) === player_from_power(data.b_power)) { + alert("Cannot create deals between powers controlled by the same player.") + return + } + + let deal = [ data.turn, data.a_power, data.b_power, data.a_promise, data.b_promise ] + + if (view.actions.propose_deal) + send_message("action", [ "propose_deal", deal, game_cookie ]) + + window.propose_deal_dlog.close() +} + +function propose_deal_cancel(evt) { + evt.preventDefault() + window.propose_deal_dlog.close() +} + /* COMMON LIBRARY */ function array_insert(array, index, item) { -- cgit v1.2.3