Fix a lot of stuff

This commit is contained in:
2023-04-10 11:19:11 +01:00
parent 848c56ff84
commit 0991c6bff9
8 changed files with 64 additions and 25 deletions

View File

@ -148,6 +148,14 @@ 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

@ -125,7 +125,7 @@ document.addEventListener("ACT", async (ev) => {
game.setReady(data.author, data.ready);
} else {
// Throw out our own packets
if (data.author === game.us) {
if (data.author === game.us.id) {
return;
}
@ -149,6 +149,10 @@ document.addEventListener("ACT", async (ev) => {
game.currentPlayer().endTurn();
}
}
if (game.allReinforcementsPlaced()) {
game.incrementState();
}
} else {
if (await game.currentPlayer().act(data)) {
game.currentPlayer().endTurn();
@ -163,8 +167,16 @@ document.addEventListener("ACT", async (ev) => {
// todo has to filter by player
document.addEventListener("PROOF", async (ev) => {
const data = ev.detail;
if (data.stage === "REQUEST") {
let region = Region.getRegion(data.region);
// todo check if this is a valid request e.g actually has neighbour
if (region.owner === game.us) {
region.prove();
}
}
if (data.stage === "CONJECTURE") {
// find the relevant entity
let region = Region.getRegion(data.region);
region.verify(BigInt(data.plainText), BigInt(data.a));

View File

@ -157,6 +157,10 @@ export class Region {
this.strength.prove(this.name);
}
requestProof() {
socket.emit("message", Packet.createProofRequest(this.name));
}
verify(plainText, a) {
this.strength.verify(this.name, plainText, a);
}

View File

@ -117,6 +117,14 @@ export class Packet {
});
}
static createProofRequest(region) {
return this._sign({
...this._createBase("PROOF"),
stage: "REQUEST",
region: region,
});
}
static createProofConjecture(region, plainText, a) {
return this._sign({
...this._createBase("PROOF"),

View File

@ -1,7 +1,7 @@
import { Packet } from "./packet.js";
import { socket, game, random } from "./main.js";
import { RsaPubKey } from "../crypto/rsa.js";
import { PaillierPubKey, ReadOnlyCyphertext } from "../crypto/paillier.js";
import { PaillierPubKey, ReadOnlyCiphertext } from "../crypto/paillier.js";
import { Region } from "./map.js";
import { showDefenseDom } from "./dom.js";
@ -88,7 +88,7 @@ export class Player {
if (region.owner === null) {
region.claim(
this,
new ReadOnlyCyphertext(this.paillierPubKey, BigInt(data.cipherText))
new ReadOnlyCiphertext(this.paillierPubKey, BigInt(data.cipherText))
);
this.totalStrength += 1;
@ -112,7 +112,7 @@ export class Player {
if (region.owner === this) {
region.reinforce(
new ReadOnlyCyphertext(
new ReadOnlyCiphertext(
this.paillierPubKey,
BigInt(data.regions[regionName])
)
@ -122,6 +122,13 @@ export class Player {
this.totalStrength += 1;
// request proofs
for (let region of this.getRegions()) {
if ([...region.neighbours.values()].find((r) => r.owner === game.us)) {
region.requestProof();
}
}
return true;
}
@ -154,15 +161,13 @@ export class Player {
this.totalStrength += 1;
// send proofs
for (let region of this.getRegions()) {
// eh
if ([...region.neighbours.values()].find((r) => r.owner !== this)) {
region.prove();
if (game.isPlaying()) {
this.reinforcementsPlaced += 1;
if (this.reinforcementsPlaced === this.reinforcementsAvailable) {
this.turnPhase = PHASE_ATTACK;
}
}
this.endTurn();
}
/**
@ -173,7 +178,7 @@ export class Player {
*/
async act(data) {
if (this.turnPhase === PHASE_REINFORCE) {
if (data.region !== undefined) {
if (data.regions !== undefined) {
if (this.reinforce(data)) {
this.reinforcementsPlaced += 1;
}