now catching the game crash and sending to the client

This commit is contained in:
jhalitaksoy 2021-07-04 00:44:15 +03:00
parent fb12557ed7
commit 5a08e30df1
3 changed files with 27 additions and 12 deletions

View File

@ -4,3 +4,4 @@ export const channel_game_move = "game_move"
export const channel_on_game_update = "on_game_update" export const channel_on_game_update = "on_game_update"
export const channel_leave_game = "leave_game" export const channel_leave_game = "leave_game"
export const channel_on_game_end = "on_game_end" export const channel_on_game_end = "on_game_end"
export const channel_on_game_crashed = "on_game_crashed"

View File

@ -1,13 +1,11 @@
import express, { Request, Response } from "express"; import express, { Request, Response } from "express";
import * as http from 'http'; import * as http from 'http';
import WebSocket from "ws"
import { decodeText, encodeText } from "./rtmt/byte_util"; import { decodeText, encodeText } from "./rtmt/byte_util";
import { encode } from "./rtmt/encode_decode_message";
import { RTMTWS } from "./rtmt/rtmt_websocket"; import { RTMTWS } from "./rtmt/rtmt_websocket";
import cors from "cors" import cors from "cors"
import { generateKey } from "./key_factory"; import { generateKey } from "./key_factory";
import { MatchMaker } from "./matcmaker"; import { MatchMaker } from "./matcmaker";
import { channel_game_move, channel_leave_game, channel_new_game, channel_on_game_start, channel_on_game_update } from "./channel_names"; import { channel_game_move, channel_leave_game, channel_new_game, channel_on_game_crashed, channel_on_game_start, channel_on_game_update } from "./channel_names";
import { Bytes } from "./rtmt/rtmt"; import { Bytes } from "./rtmt/rtmt";
import { createGame, Game, GameMove } from "./mancala"; import { createGame, Game, GameMove } from "./mancala";
@ -62,6 +60,7 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
const gameMove: GameMove = JSON.parse(decodeText(message)) const gameMove: GameMove = JSON.parse(decodeText(message))
const game = gameStore.get(userKey) const game = gameStore.get(userKey)
if (game) { if (game) {
try {
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey)) game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
const data = encodeText(JSON.stringify(game)) const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data) rtmt.sendMessage(game.player1, channel_on_game_update, data)
@ -71,6 +70,14 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
gameStore.delete(game.player1) gameStore.delete(game.player1)
gameStore.delete(game.player2) gameStore.delete(game.player2)
} }
} catch (err) {
console.log(err);
deleteGame(game)
const data = encodeText(err.toString())
rtmt.sendMessage(game.player1, channel_on_game_crashed, data)
rtmt.sendMessage(game.player2, channel_on_game_crashed, data)
}
} else { } else {
console.log("Game not found!"); console.log("Game not found!");
} }
@ -79,10 +86,16 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => { rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => {
const game = gameStore.get(userKey) const game = gameStore.get(userKey)
if (game) { if (game) {
gameStore.delete(game.player1) deleteGame(game)
gameStore.delete(game.player2)
const data = encodeText(JSON.stringify(game)) const data = encodeText(JSON.stringify(game))
rtmt.sendMessage(game.player1, channel_on_game_update, data) rtmt.sendMessage(game.player1, channel_on_game_update, data)
rtmt.sendMessage(game.player2, channel_on_game_update, data) rtmt.sendMessage(game.player2, channel_on_game_update, data)
} }
}) })
const deleteGame = (game: Game) => {
if (game) {
gameStore.delete(game.player1)
gameStore.delete(game.player2)
}
}

View File

@ -14,6 +14,7 @@ export class Game {
} }
public moveByIndex(holeIndex: number, player: "player1" | "player2") { public moveByIndex(holeIndex: number, player: "player1" | "player2") {
throw new Error("afdad")
let hole = this.board.player1Holes[holeIndex] let hole = this.board.player1Holes[holeIndex]
if (player == "player2") { if (player == "player2") {