From 125bbd657586dfe58e66e884da73bc3a85790c39 Mon Sep 17 00:00:00 2001 From: jude Date: Fri, 21 Apr 2023 09:50:20 +0100 Subject: [PATCH] repaired some attack related code --- README.md | 4 ++++ static/js/modules/interface/dom.js | 9 --------- static/js/modules/interface/main.js | 2 +- static/js/modules/interface/map.js | 10 +++++++++- static/js/modules/interface/player.js | 24 +++++++++++++++--------- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1e097b3..61b895e 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,7 @@ The key size is determined by the variable `KEY_SIZE` in `random_primes.js`. Thi variable dictates the size of _each_ prime used in the modulus, so the total key size is twice this value. Recommend selecting one of 512, 1024, or 2048. In fact, I recommend selecting 512 if you want the program to run in less than 3-5 business days. + +Key generation will still take some time. The browser may tell you the tab is not +responding, but just wait and it'll be fine eventually. If not, short-circuit the +`generate_safe_prime` function in `random_primes.js` to just return a normal prime. diff --git a/static/js/modules/interface/dom.js b/static/js/modules/interface/dom.js index df2342f..09fe466 100644 --- a/static/js/modules/interface/dom.js +++ b/static/js/modules/interface/dom.js @@ -57,7 +57,6 @@ function updateMapDom() { } showStrengths(); - showRemainingReinforcements(); } @@ -148,14 +147,6 @@ document.addEventListener("DOMContentLoaded", () => { el.addEventListener("click", (ev) => { let region = ev.target.closest(".node").dataset.name; game.us.sendReinforce(region); - - if (game.isPregame()) { - game.us.endTurn(); - - if (game.allReinforcementsPlaced()) { - game.incrementState(); - } - } }) ); diff --git a/static/js/modules/interface/main.js b/static/js/modules/interface/main.js index f3d8306..3a244f7 100644 --- a/static/js/modules/interface/main.js +++ b/static/js/modules/interface/main.js @@ -126,7 +126,7 @@ document.addEventListener("ACT", async (ev) => { game.setReady(data.author, data.ready); } else { // Throw out our own packets - if (data.author === game.us.id) { + if (!game.isPlaying() && data.author === game.us.id) { return; } diff --git a/static/js/modules/interface/map.js b/static/js/modules/interface/map.js index 69e8b4b..72b93fa 100644 --- a/static/js/modules/interface/map.js +++ b/static/js/modules/interface/map.js @@ -53,6 +53,14 @@ class Strength { }); } + transparentUpdate(value) { + this.cipherText.update(new Ciphertext(this.cipherText.pubKey, value, 0n)); + + if (this.assumedStrength !== null) { + this.assumedStrength += value; + } + } + update(cipherText) { if (this.cipherText === null) { this.cipherText = cipherText; @@ -130,7 +138,7 @@ export class Region { claim(player, cipherText) { this.owner = player; this.strength.update(cipherText); - this.strength.assumedStrength = 1; + this.strength.assumedStrength = 1n; } reinforce(cipherText) { diff --git a/static/js/modules/interface/player.js b/static/js/modules/interface/player.js index ca912e5..f192826 100644 --- a/static/js/modules/interface/player.js +++ b/static/js/modules/interface/player.js @@ -109,8 +109,8 @@ export class Player { */ reinforce(data) { if (!verifyRegions(data.verification, this.paillierPubKey)) { - console.log("Failed to verify Protocol 4.5!"); - return; + console.log("Failed to verify reinforcements!"); + return false; } for (let regionName of Object.keys(data.regions)) { @@ -169,11 +169,11 @@ export class Player { this.totalStrength += 1; - if (game.isPlaying()) { - this.reinforcementsPlaced += 1; + if (game.isPregame()) { + game.us.endTurn(); - if (this.reinforcementsPlaced === this.reinforcementsAvailable) { - this.turnPhase = PHASE_ATTACK; + if (game.allReinforcementsPlaced()) { + game.incrementState(); } } } @@ -187,7 +187,9 @@ export class Player { async act(data) { if (this.turnPhase === PHASE_REINFORCE) { if (data.regions !== undefined) { - if (this.reinforce(data)) { + if (this === game.us) { + this.reinforcementsPlaced += 1; + } else if (this.reinforce(data)) { this.reinforcementsPlaced += 1; } @@ -250,9 +252,12 @@ export class Player { defenderStrength <= 0 || defenderStrength > Math.min(2, defender.strength) ) { + console.log("waiting"); defenderStrength = await defender.owner.getDefense(); } + console.log(defenderStrength); + /* How do Risk attacks work? - Offender signs 1-3 armies, defender signs 1-2 armies - Both roll respective dice @@ -289,11 +294,12 @@ export class Player { let defenderResult = defenderRolls.pop(); if (offenderResult > defenderResult) { - defender.strength -= 1; + defender.strength.transparentUpdate(-1n); } else { - offender.strength -= 1; + offender.strength.transparentUpdate(-1n); } + // Handle region capture. if (defender.strength === 0) { defender.strength = offenderRolls.length + 1; offender.strength -= offenderRolls.length + 1;