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

254 lines
6.5 KiB
JavaScript
Raw Permalink 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-05-01 13:42:17 +00:00
export class Strength {
constructor(cipherText, regionName) {
2023-03-17 10:42:11 +00:00
this.cipherText = cipherText;
2023-03-13 14:52:14 +00:00
this.assumedStrength = null;
this.prover = null;
document.addEventListener("PROOF", (ev) => {
const data = ev.detail;
if (
data.region === regionName &&
data.stage === "CHALLENGE" &&
this.prover !== null
) {
let z = this.prover.prove(BigInt(data.challenge));
socket.emit(
"message",
Packet.createProof(regionName, "0x" + z.toString(16))
);
}
});
this.verifier = null;
document.addEventListener("PROOF", (ev) => {
const data = ev.detail;
if (
data.region === regionName &&
data.stage === "PROOF" &&
this.verifier !== null
) {
let result = this.verifier.verify(BigInt(data.z));
if (result > 0) {
this.assumedStrength = this.verifier.plainText;
document.dispatchEvent(new CustomEvent("updateStrengths"));
} else {
console.warn(`Failed to verify ciphertext! ${result}`);
}
}
});
2023-03-13 14:52:14 +00:00
}
2023-04-21 08:50:20 +00:00
transparentUpdate(value) {
this.cipherText.update(new Ciphertext(this.cipherText.pubKey, value, 0n));
if (this.assumedStrength !== null) {
this.assumedStrength += value;
}
}
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;
}
this.prover = this.cipherText.prove();
socket.emit(
"message",
Packet.createProofConjecture(
region,
2023-03-24 16:53:02 +00:00
"0x" + this.cipherText.plainText.toString(),
"0x" + this.prover.a.toString(16)
)
);
}
verify(region, plainText, a) {
if (!this.cipherText.readOnly) {
return;
}
this.verifier = this.cipherText.prove(plainText, a);
socket.emit(
"message",
2023-03-24 16:53:02 +00:00
Packet.createProofChallenge(
region,
"0x" + this.verifier.challenge.toString(16)
2023-03-24 16:53:02 +00:00
)
);
}
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;
this.strength = new Strength(null, name);
2023-02-06 11:04:37 +00:00
this.neighbours = new Set();
this.continent = continent;
2023-04-30 17:42:52 +00:00
this.attackResolver = null;
this.attackerRes = null;
this.defenderRes = null;
2023-02-06 11:04:37 +00:00
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-04-21 08:50:20 +00:00
this.strength.assumedStrength = 1n;
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);
}
2023-04-10 10:19:11 +00:00
requestProof() {
socket.emit("message", Packet.createProofRequest(this.name));
}
verify(plainText, a) {
this.strength.verify(this.name, plainText, a);
}
2023-04-30 17:42:52 +00:00
async handleResolve(resolution) {
await navigator.locks.request(`region-${this.name}`, () => {
if (resolution.author === this.owner.id) {
this.defenderRes = resolution;
} else {
this.attackerRes = resolution;
}
if (
this.attackResolver !== null &&
this.defenderRes !== null &&
this.attackerRes !== null
) {
this.attackResolver({
attackerRes: this.attackerRes,
defenderRes: this.defenderRes,
});
}
});
}
async resolveAttack() {
let promise;
await navigator.locks.request(`region-${this.name}`, () => {
if (this.attackerRes === null || this.defenderRes === null) {
let resolver;
promise = new Promise((resolve) => {
resolver = resolve;
});
this.attackResolver = resolver;
} else {
promise = new Promise((resolve) => {
resolve({
attackerRes: this.attackerRes,
defenderRes: this.defenderRes,
});
});
}
});
return promise;
}
2023-02-06 11:04:37 +00:00
}
2023-04-27 11:52:02 +00:00
window.Region = Region;
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);