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; const data = ev.detail;
game.contendedRegion.handleResolve(data); if (game.contendedRegion !== null) {
await game.contendedRegion.handleResolve(data);
}
}); });
// todo has to filter by player // todo has to filter by player

View File

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

View File

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

View File

@ -2,12 +2,11 @@ import { Packet } from "./packet.js";
import { socket, game, random } from "./main.js"; import { socket, game, random } from "./main.js";
import { RsaPubKey } from "../crypto/rsa.js"; import { RsaPubKey } from "../crypto/rsa.js";
import { PaillierPubKey, ReadOnlyCiphertext } from "../crypto/paillier.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 { showDefenseDom } from "./dom.js";
import { import {
proveBitLength, proveBitLength,
proveFortify, proveFortify,
proveRange,
proveRegions, proveRegions,
verifyBitLength, verifyBitLength,
verifyFortify, verifyFortify,
@ -392,7 +391,7 @@ export class Player {
socket.emit("message", Packet.createRegionYield()); socket.emit("message", Packet.createRegionYield());
} else { } else {
let ct = defender.strength.cipherText.clone(); 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 // Prove we still control the region
let proof = proveBitLength(ct); let proof = proveBitLength(ct);
@ -402,25 +401,67 @@ export class Player {
} else if (this === game.us) { } else if (this === game.us) {
if (defender.strength.assumedStrength === 0n) { if (defender.strength.assumedStrength === 0n) {
// Handle region gain // Handle region gain
defender.owner = this; let newStrength = new Ciphertext(
defender.strength = new Strength( this.paillierPubKey,
new Ciphertext(this.paillierPubKey, offenderRolls.length + 1), BigInt(offenderRolls.length) + 1n
defender.name
); );
defender.owner = this;
defender.strength = new Strength(newStrength, defender.name);
// Send the new ciphertext // Send the new ciphertext
socket.emit( socket.emit("message", Packet.createRegionCapture(newStrength));
"message",
Packet.createRegionCapture(defender.strength.cipherText)
);
} else { } else {
// State we didn't capture. Again, makes programming easier. // State we didn't capture. Again, makes programming easier.
socket.emit("message", Packet.createRegionYield()); socket.emit("message", Packet.createRegionYield());
} }
} else { }
let resolutions = await defender.resolveAttack();
console.log(resolutions); let resolutions = await defender.resolveAttack();
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. // Reset the promises in case they attack again.