"
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) {
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_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])
}
|