" + power_image[from]
str += " | " + power_image[to]
if (n > 1)
str += " | " + n + " turns"
else
str += " | 1 turn"
})
}
if (show) {
elt.innerHTML = str
elt.style.display = "block"
} else {
elt.style.display = "none"
}
}
function update_deal_item(deal) {
let [ a_power, b_power, a_turn, b_turn, a_phase, b_phase, a_promise, b_promise ] = deal
let str = " |
"
str += "" + power_image[b_power]
str += " | " + html_escape(b_promise)
str += " | " + phase_list(b_phase) + ""
str += " | " + turn_name[b_turn]
return str
}
function update_deal_list(deals, elt, title) {
if (deals && deals.length > 0) {
//let str = " | " + title + " | phase | until"
let str = "" + title
str += deals.map(update_deal_item).join(" |
---|
")
str += " | "
elt.innerHTML = str
elt.style.display = "block"
} else {
elt.style.display = "none"
}
}
function update_deal_options() {
let a_power = window.propose_deal_form.elements.a_power.value | 0
let b_power = window.propose_deal_form.elements.b_power.value | 0
window.a_hussars_label.classList.toggle("hide", a_power !== P_AUSTRIA)
window.b_hussars_label.classList.toggle("hide", b_power !== P_AUSTRIA)
}
function propose_deal() {
if (!is_action("propose_deal"))
return
let form = window.propose_deal_form
window.a_phase_politics.checked = false
window.b_phase_politics.checked = false
window.a_phase_hussars.checked = false
window.b_phase_hussars.checked = false
window.a_phase_movement.checked = false
window.b_phase_movement.checked = false
window.a_phase_retreat.checked = false
window.b_phase_retreat.checked = false
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.a_turn.value = view.turn
form.b_turn.value = view.turn
for (let opt of form.a_turn.options)
opt.disabled = (opt.value < view.turn)
for (let opt of form.b_turn.options)
opt.disabled = (opt.value < view.turn)
update_deal_options()
window.propose_deal_dlog.showModal()
}
function propose_deal_submit(evt) {
evt.preventDefault()
let form = new FormData(window.propose_deal_form)
let data = {
a_power: Number(form.get("a_power")),
b_power: Number(form.get("b_power")),
a_turn: Number(form.get("a_turn")),
b_turn: Number(form.get("b_turn")),
a_phase: form.getAll("a_phase").reduce((acc,x)=>acc|x, 0),
b_phase: form.getAll("b_phase").reduce((acc,x)=>acc|x, 0),
a_promise: form.get("a_promise"),
b_promise: form.get("b_promise"),
}
if (!data.a_promise || !data.b_promise) {
alert("Each side must promise something!")
return
}
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.a_power, data.b_power, data.a_turn, data.b_turn, data.a_phase, data.b_phase, 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) {
for (let i = array.length; i > index; --i)
array[i] = array[i - 1]
array[index] = item
}
function set_has(set, item) {
if (set === item) return true
if (set === undefined) return false
if (set === null) return false
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return true
}
return false
}
function set_add(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return
}
array_insert(set, a, item)
}
function set_add_all(set, other) {
for (let item of other)
set_add(set, item)
}
function set_union(one, two) {
let set = []
for (let item of one)
set_add(set, item)
for (let item of two)
set_add(set, item)
return set
}
function set_intersect(one, two) {
let set = []
for (let item of one)
if (set_has(two, item))
set_add(set, item)
return set
}
function map_has(map, key) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return true
}
return false
}
function map_get(map, key, missing) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return map[(m<<1)+1]
}
return missing
}
function map_for_each(map, f) {
for (let i = 0; i < map.length; i += 2)
f(map[i], map[i+1])
}
|
---|
|