repaired some attack related code

This commit is contained in:
jude 2023-04-21 09:50:20 +01:00
parent 4c1bcf370f
commit 125bbd6575
5 changed files with 29 additions and 20 deletions

View File

@ -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.

View File

@ -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();
}
}
})
);

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;