done????????????????

This commit is contained in:
jude 2023-05-01 14:42:17 +01:00
parent 0507b2e820
commit 6d83ccfb5b
4 changed files with 60 additions and 18 deletions

View File

@ -165,9 +165,11 @@ document.addEventListener("ACT", async (ev) => {
}
});
document.addEventListener("RESOLVE", (ev) => {
document.addEventListener("RESOLVE", async (ev) => {
const data = ev.detail;
game.contendedRegion.handleResolve(data);
if (game.contendedRegion !== null) {
await game.contendedRegion.handleResolve(data);
}
});
// todo has to filter by player

View File

@ -10,7 +10,7 @@ class Continent {
}
}
class Strength {
export class Strength {
constructor(cipherText, regionName) {
this.cipherText = cipherText;
this.assumedStrength = null;

View File

@ -174,7 +174,6 @@ export class Packet {
return this._sign({
...this._createBase("RESOLVE"),
action: "YIELD",
proof: proof,
});
}

View File

@ -2,12 +2,11 @@ import { Packet } from "./packet.js";
import { socket, game, random } from "./main.js";
import { RsaPubKey } from "../crypto/rsa.js";
import { PaillierPubKey, ReadOnlyCiphertext } from "../crypto/paillier.js";
import { Region } from "./map.js";
import { Region, Strength } from "./map.js";
import { showDefenseDom } from "./dom.js";
import {
proveBitLength,
proveFortify,
proveRange,
proveRegions,
verifyBitLength,
verifyFortify,
@ -392,7 +391,7 @@ export class Player {
socket.emit("message", Packet.createRegionYield());
} else {
let ct = defender.strength.cipherText.clone();
ct.update(new Ciphertext(ct.pubKey, -2n, 0n));
ct.update(new Ciphertext(ct.pubKey, -1n, 0n));
// Prove we still control the region
let proof = proveBitLength(ct);
@ -402,25 +401,67 @@ export class Player {
} else if (this === game.us) {
if (defender.strength.assumedStrength === 0n) {
// Handle region gain
defender.owner = this;
defender.strength = new Strength(
new Ciphertext(this.paillierPubKey, offenderRolls.length + 1),
defender.name
let newStrength = new Ciphertext(
this.paillierPubKey,
BigInt(offenderRolls.length) + 1n
);
defender.owner = this;
defender.strength = new Strength(newStrength, defender.name);
// Send the new ciphertext
socket.emit(
"message",
Packet.createRegionCapture(defender.strength.cipherText)
);
socket.emit("message", Packet.createRegionCapture(newStrength));
} else {
// State we didn't capture. Again, makes programming easier.
socket.emit("message", Packet.createRegionYield());
}
} else {
}
let resolutions = await defender.resolveAttack();
console.log(resolutions);
if (
resolutions.attackerRes.action === "CAPTURE" &&
resolutions.defenderRes.action === "YIELD"
) {
// Capture occurred.
if (this === game.us) {
// Already done above.
} else {
defender.owner = this;
defender.strength = new Strength(
new ReadOnlyCiphertext(
this.paillierPubKey,
BigInt(resolutions.attackerRes.cipherText)
),
defender.name
);
}
} else if (resolutions.attackerRes.action === "YIELD") {
// Do nothing, no capture occurred.
} else {
// Conflict res. Need to check proof.
let v = verifyBitLength(
resolutions.defenderRes.proof,
defender.owner.paillierPubKey
);
if (v !== null && v <= 8) {
// Accept defender (do nothing)
} else {
// Accept attacker
if (this === game.us) {
// Already done above.
} else {
defender.owner = this;
defender.strength = new Strength(
new ReadOnlyCiphertext(
this.paillierPubKey,
BigInt(resolutions.attackerRes.cipherText)
),
defender.name
);
}
}
}
// Reset the promises in case they attack again.