Riskless/static/js/modules/interface/map.js

196 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-03-24 16:53:02 +00:00
import { socket } from "./main.js";
import { Packet } from "./packet.js";
2023-03-05 17:19:37 +00:00
const REGIONS = {};
2023-02-06 11:04:37 +00:00
class Continent {
constructor(name) {
this.name = name;
this.yield = 0;
}
}
2023-03-13 14:52:14 +00:00
class Strength {
2023-03-17 10:42:11 +00:00
constructor(cipherText) {
this.cipherText = cipherText;
2023-03-13 14:52:14 +00:00
this.assumedStrength = null;
}
2023-03-17 10:42:11 +00:00
update(cipherText) {
if (this.cipherText === null) {
this.cipherText = cipherText;
2023-03-13 14:52:14 +00:00
} else {
2023-03-17 10:42:11 +00:00
this.cipherText.update(cipherText);
2023-03-13 14:52:14 +00:00
}
this.assumedStrength = null;
}
prove(region) {
if (this.cipherText.readOnly) {
return;
}
const controller = new AbortController();
let proofSessionProver = this.cipherText.prove();
document.addEventListener(
"PROOF",
(ev) => {
const data = ev.detail;
if (data.region === region && data.stage === "CHALLENGE") {
2023-03-24 16:53:02 +00:00
let z = proofSessionProver.prove(BigInt(data.challenge));
2023-03-24 16:53:02 +00:00
socket.emit(
"message",
Packet.createProof(region, "0x" + z.toString(16))
);
controller.abort();
}
},
{ signal: controller.signal }
);
socket.emit(
"message",
Packet.createProofConjecture(
region,
2023-03-24 16:53:02 +00:00
"0x" + this.cipherText.plainText.toString(),
"0x" + proofSessionProver.a.toString(16)
)
);
}
verify(region, plainText, a) {
if (!this.cipherText.readOnly) {
return;
}
const controller = new AbortController();
let proofSessionVerifier = this.cipherText.prove(plainText, a);
document.addEventListener(
"PROOF",
(ev) => {
const data = ev.detail;
if (data.region === region && data.stage === "PROOF") {
let result = proofSessionVerifier.verify(BigInt(data.z));
if (result > 0) {
this.assumedStrength = plainText;
document.dispatchEvent(new CustomEvent("updateStrengths"));
controller.abort();
} else {
console.warn(`Failed to verify ciphertext! ${result}`);
}
}
},
{ signal: controller.signal }
);
socket.emit(
"message",
2023-03-24 16:53:02 +00:00
Packet.createProofChallenge(
region,
"0x" + proofSessionVerifier.challenge.toString(16)
)
);
}
2023-03-13 14:52:14 +00:00
}
export class Region {
2023-02-06 11:04:37 +00:00
constructor(name, continent) {
this.name = name;
this.owner = null;
2023-03-13 14:52:14 +00:00
this.strength = new Strength(null);
2023-02-06 11:04:37 +00:00
this.neighbours = new Set();
this.continent = continent;
REGIONS[name] = this;
}
static setNeighbours(region1, region2) {
region1.neighbours.add(region2);
region2.neighbours.add(region1);
}
static allRegionsClaimed() {
return (
Object.values(REGIONS).find((region) => region.owner === null) === undefined
);
}
2023-02-06 11:04:37 +00:00
static getRegion(name) {
return REGIONS[name];
}
static getAllRegions() {
return Object.values(REGIONS);
}
2023-03-17 10:42:11 +00:00
claim(player, cipherText) {
2023-02-06 11:04:37 +00:00
this.owner = player;
2023-03-17 10:42:11 +00:00
this.strength.update(cipherText);
2023-03-13 14:52:14 +00:00
this.strength.assumedStrength = 1;
2023-02-06 11:04:37 +00:00
}
2023-03-17 10:42:11 +00:00
reinforce(cipherText) {
this.strength.update(cipherText);
2023-03-13 14:52:14 +00:00
}
isClaimed() {
2023-03-17 10:42:11 +00:00
return this.strength.cipherText !== null;
2023-03-13 14:52:14 +00:00
}
displayStrength() {
if (this.owner === null) {
return "";
2023-03-17 10:42:11 +00:00
} else if (!this.strength.cipherText.readOnly) {
return this.strength.cipherText.plainText.toString();
2023-03-13 14:52:14 +00:00
} else if (this.strength.assumedStrength !== null) {
return this.strength.assumedStrength.toString();
2023-03-13 14:52:14 +00:00
} else {
return "?";
}
2023-02-06 11:04:37 +00:00
}
2023-03-24 16:53:02 +00:00
prove() {
this.strength.prove(this.name);
}
verify(plainText, a) {
this.strength.verify(this.name, plainText, a);
}
2023-02-06 11:04:37 +00:00
}
2023-02-06 12:30:24 +00:00
const EAST = new Continent("East");
const WEST = new Continent("West");
const A = new Region("A", EAST);
const B = new Region("B", EAST);
const C = new Region("C", EAST);
const D = new Region("D", EAST);
2023-02-06 13:03:25 +00:00
const J = new Region("J", EAST);
2023-02-06 12:30:24 +00:00
const F = new Region("F", WEST);
const G = new Region("G", WEST);
const H = new Region("H", WEST);
const I = new Region("I", WEST);
2023-02-06 13:03:25 +00:00
const E = new Region("E", WEST);
2023-02-08 17:55:45 +00:00
Region.setNeighbours(A, B);
Region.setNeighbours(A, C);
Region.setNeighbours(B, C);
Region.setNeighbours(B, J);
Region.setNeighbours(C, D);
Region.setNeighbours(C, F);
Region.setNeighbours(E, J);
Region.setNeighbours(E, I);
Region.setNeighbours(E, H);
Region.setNeighbours(F, J);
Region.setNeighbours(F, G);
Region.setNeighbours(G, H);
Region.setNeighbours(G, I);
Region.setNeighbours(H, I);