Riskless/static/js/modules/interface/main.js

195 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-03-04 10:50:49 +00:00
import { generate_keypair, generate_rsa_keypair } from "../crypto/main.js";
import { Random } from "./random.js";
import { Barrier } from "./barrier.js";
import { Packet } from "./packet.js";
2023-03-04 00:25:54 +00:00
import { Game } from "./game.js";
2023-03-05 17:19:37 +00:00
import { Region } from "./map.js";
2023-03-04 00:25:54 +00:00
import "./dom.js";
export const ID = window.crypto.randomUUID();
export const game = new Game();
2023-03-13 14:52:14 +00:00
window.game = game;
export let socket;
2023-03-05 17:19:37 +00:00
export let random;
let barrier;
2023-03-04 10:50:49 +00:00
window.paillier = generate_keypair();
window.rsa = generate_rsa_keypair();
// Not totally reliable but better than nothing.
window.addEventListener("beforeunload", () => {
socket.emit("message", Packet.createDisconnect());
});
document.addEventListener("DOMContentLoaded", () => {
socket = io();
random = new Random();
barrier = new Barrier();
socket.on("connect", () => {
window.console.log("Connected!");
2023-03-04 00:25:54 +00:00
window.console.log(`We are: ${ID}`);
2023-03-13 14:52:14 +00:00
socket.emit("message", Packet.createAnnounce(window.rsa, window.paillier));
game.addPlayer(ID, true, window.rsa.pubKey, window.paillier.pubKey);
});
2023-03-04 14:19:26 +00:00
socket.on("message", async (packet) => {
let data = packet.payload;
2023-03-13 14:52:14 +00:00
if (data.type !== "KEEPALIVE") window.console.log(data);
2023-03-04 00:25:54 +00:00
2023-03-04 14:19:26 +00:00
const sender = game.players[data.author];
if (sender === undefined) {
window.console.warn(
`Not able to verify signature on packet ${data.id} as the sender is unknown.`
);
} else {
let sig = BigInt(packet.sig);
// decrypt and compare signature
2023-03-05 17:19:37 +00:00
let dehash = sender.rsaPubKey.encrypt(sig);
let hash = BigInt("0x" + CryptoJS.SHA3(JSON.stringify(data)).toString());
2023-03-04 14:19:26 +00:00
if (dehash !== hash) {
window.console.error(`Signature invalid! Ignoring packet ${data.id}.`);
return;
}
2023-03-07 15:43:47 +00:00
// check if the packet is replayed.
if (data.timestamp <= sender.lastPacket) {
window.console.error(`Replay detected! Ignoring packet ${data.id}.`);
return;
}
sender.lastPacket = data.timestamp;
2023-03-04 14:19:26 +00:00
}
switch (data.type) {
case "RANDOM":
if (data.author === ID) {
return;
}
await random.processCooperativeRandom(data);
break;
case "BARRIER":
if (data.author === ID) {
return;
}
barrier.resolve(data);
break;
default:
const event = new CustomEvent(data.type, { detail: data });
document.dispatchEvent(event);
break;
}
});
});
/**
* Process player connect packets: these inform that a new player has joined.
*
* @param data Packet received
*/
2023-03-04 00:25:54 +00:00
document.addEventListener("ANNOUNCE", (ev) => {
const data = ev.detail;
if (data.author === ID) return;
2023-03-13 14:52:14 +00:00
let is_new = game.addPlayer(data.author, false, data.rsaPubKey, data.paillierPubKey);
// When a new player is seen, all announce to ensure they know all players.
if (is_new) {
2023-03-13 14:52:14 +00:00
socket.emit("message", Packet.createAnnounce(window.rsa, window.paillier));
}
});
2023-03-04 00:25:54 +00:00
document.addEventListener("DISCONNECT", (ev) => {
const data = ev.detail;
game.removePlayer(data.author);
});
/**
* Process keep-alive packets: these are packets that check players are still online.
*
* @param data Packet received
*/
2023-03-04 00:25:54 +00:00
document.addEventListener("KEEPALIVE", (ev) => {
const data = ev.detail;
game.keepAlive(data.author);
});
2023-03-04 00:25:54 +00:00
document.addEventListener("ACT", async (ev) => {
const data = ev.detail;
if (game.isWaiting()) {
game.setReady(data.author, data.ready);
2023-03-04 00:25:54 +00:00
} else {
2023-03-17 10:42:11 +00:00
// Throw out our own packets
if (data.author === game.us) {
return;
}
2023-03-04 00:25:54 +00:00
if (data.author !== game.currentPlayer().id) {
if (data.action === "DEFENSE") {
await game.players[data.author].setDefense(data.amount);
}
2023-03-04 00:25:54 +00:00
return;
}
2023-03-04 00:25:54 +00:00
if (game.isPregame()) {
if (!Region.allRegionsClaimed()) {
// Claim a region in the pregame.
if (game.currentPlayer().claim(data)) {
// Increment to next player.
game.currentPlayer().endTurn();
}
2023-03-17 10:42:11 +00:00
} else if (!game.allReinforcementsPlaced()) {
2023-03-04 00:25:54 +00:00
if (game.currentPlayer().reinforce(data)) {
game.currentPlayer().endTurn();
}
}
} else {
if (await game.currentPlayer().act(data)) {
game.currentPlayer().endTurn();
2023-03-05 17:19:37 +00:00
} else {
const event = new CustomEvent("turnProgress");
document.dispatchEvent(event);
2023-03-04 00:25:54 +00:00
}
}
}
});
// todo has to filter by player
document.addEventListener("PROOF", async (ev) => {
const data = ev.detail;
if (data.stage === "CONJECTURE") {
// find the relevant entity
let region = Region.getRegion(data.region);
region.prove(data.plainText, data.noise());
}
});
2023-03-18 15:41:37 +00:00
document.addEventListener("endTurn", () => {
if (game.isPregame() && game.allReinforcementsPlaced()) {
game.incrementState();
}
});
2023-03-04 00:25:54 +00:00
document.addEventListener("gameStateUpdate", async () => {
if (game.isPregame()) {
let firstPlayerIndex = await random.get(
Object.keys(game.players).length,
"first-player"
);
2023-03-04 00:25:54 +00:00
let firstPlayer = Object.values(game.players).sort((a, b) =>
a.id < b.id ? -1 : 1
)[firstPlayerIndex];
2023-03-04 00:25:54 +00:00
firstPlayer.isPlaying = true;
await barrier.wait();
2023-03-05 17:19:37 +00:00
const event = new CustomEvent("playerChange");
document.dispatchEvent(event);
2023-03-04 00:25:54 +00:00
}
});