Skip to content

Instantly share code, notes, and snippets.

@nipundavid
Created November 17, 2021 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nipundavid/fbb598a60987d8d248f3903e90af21bb to your computer and use it in GitHub Desktop.
Save nipundavid/fbb598a60987d8d248f3903e90af21bb to your computer and use it in GitHub Desktop.
import { Room, Client } from "colyseus";
import { Schema, type, MapSchema } from "@colyseus/schema";
export class Player extends Schema {
@type("number") score = 0;
}
export class State extends Schema {
@type("string") currentTurn: string;
@type({ map: Player })
players = new MapSchema<Player>();
@type("string") winner: string;
@type("boolean") draw: boolean;
@type("boolean") shotArrow: boolean;
}
export class MyRoom extends Room<State> {
maxClients = 2;
// When room is initialized
onCreate(options: any) {
console.log(`${JSON.stringify(options)} created`);
this.setState(new State());
this.onMessage("action", (client, data) => {
console.log("action received from", client.sessionId, ":", data);
if (data.data == "arrowShot")
this.arrowShotAction(client.sessionId, data);
});
this.onMessage("score", (client, data) => {
console.log("score received from", client.sessionId, ":", data);
this.updateScoreAction(client.sessionId, data);
});
}
// When client successfully join the room
onJoin(client: Client, options: any, auth: any) {
console.log(`${client.sessionId} Joined`);
this.state.players.set(client.sessionId, new Player());
if (this.state.players.size === 2) {
this.state.currentTurn = client.sessionId;
// lock this room for new users
this.lock();
}
}
arrowShotAction(sessionId: string, data: any) {
if (this.state.winner || this.state.draw) {
return false;
}
// since arrow is fired
if (this.state.shotArrow) return;
if (sessionId === this.state.currentTurn) {
this.state.shotArrow = true;
this.clock.setTimeout(() => {
this.state.shotArrow = false;
//change turn
this.changePlayerTurnAction(sessionId, data);
}, 3000);
}
}
changePlayerTurnAction(sessionId: string, data: any) {
console.log(data);
if (this.state.winner || this.state.draw) {
return false;
}
if (sessionId === this.state.currentTurn) {
const playerIds = Array.from(this.state.players.keys());
const otherPlayerSessionId =
sessionId === playerIds[0] ? playerIds[1] : playerIds[0];
this.state.currentTurn = otherPlayerSessionId;
}
}
updateScoreAction(sessionId: string, data: any) {
if (this.state.winner || this.state.draw) {
return false;
}
if (sessionId === this.state.currentTurn) {
this.state.players.get(sessionId).score += data.score;
}
const playerIds = Array.from(this.state.players.keys());
console.log(
`Score of ${playerIds[0]}: ${JSON.stringify(
this.state.players.get(playerIds[0])
)}`
);
console.log(
`Score of ${playerIds[1]}: ${JSON.stringify(
this.state.players.get(playerIds[1])
)}`
);
}
// When a client leaves the room
onLeave(client: Client, consented: boolean) {
console.log(`${client.sessionId} Leaving`);
this.state.players.delete(client.sessionId);
}
// Cleanup callback, called after there are no more clients in the room. (see `autoDispose`)
onDispose() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment