-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyRoom.ts
41 lines (31 loc) · 1.09 KB
/
MyRoom.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
38
39
40
41
import firebaseAdmin from "firebase-admin";
import { DecodedIdToken, getAuth } from "firebase-admin/auth";
import { applicationDefault } from "firebase-admin/app";
import { Room, Client } from "colyseus";
import { MyRoomState, Player } from "./schema/MyRoomState";
/**
* Initialize 'firebase-admin'
*/
firebaseAdmin.initializeApp({ credential: applicationDefault() });
export class MyRoom extends Room<MyRoomState> {
onCreate (options: any) {
this.setState(new MyRoomState());
console.log("room", this.roomId, "created!");
}
async onAuth(client: Client, options: any) {
return await getAuth().verifyIdToken(options.accessToken);
}
onJoin (client: Client, options: any, authData: DecodedIdToken) {
const player = new Player();
player.uid = authData.uid;
this.state.players.set(client.sessionId, player);
console.log(client.sessionId, "joined!");
}
onLeave (client: Client, consented: boolean) {
this.state.players.delete(client.sessionId);
console.log(client.sessionId, "left!");
}
onDispose() {
console.log("room", this.roomId, "disposing...");
}
}