/
polls.ts
37 lines (30 loc) · 1.02 KB
/
polls.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import type * as Party from "partykit/server";
// room ID is completely arbitrary for this partyserver.
// For the client, room is the object-hash of the questions and options.
// But for us, it's just a string, and what we'll store a tally of id/votes against.
type Votes = {
[option: string]: number;
};
export default class PollParty implements Party.Server {
votes: Votes = {};
constructor(public party: Party.Party) {}
async onStart() {
this.votes = (await this.party.storage.get("votes")) || {};
}
async onConnect(connection: Party.Connection) {
const msg = {
type: "sync",
votes: this.votes,
};
connection.send(JSON.stringify(msg));
}
async onMessage(message: string) {
const msg = JSON.parse(message);
if (msg.type === "vote") {
const { option } = msg;
this.votes[option] = (parseInt(`${this.votes[option]}`) || 0) + 1;
this.party.broadcast(JSON.stringify({ type: "sync", votes: this.votes }));
await this.party.storage.put("votes", this.votes);
}
}
}