added ending game to mancala
This commit is contained in:
parent
30953e3464
commit
a5b9ec2513
25
src/index.ts
25
src/index.ts
@ -33,9 +33,9 @@ server.listen(port, () => {
|
||||
|
||||
const rtmt = new RTMTWS()
|
||||
|
||||
rtmt.initWebSocket(server, (userKey : string) => {
|
||||
rtmt.initWebSocket(server, (userKey: string) => {
|
||||
const game = gameStore.get(userKey)
|
||||
if(game){
|
||||
if (game) {
|
||||
rtmt.sendMessage(userKey, channel_on_game_update, encodeText(JSON.stringify(game)))
|
||||
}
|
||||
})
|
||||
@ -53,8 +53,9 @@ matchmaker.onPlayersPaired = (userKey1: string, userKey2: string) => {
|
||||
gameStore.set(userKey1, game)
|
||||
gameStore.set(userKey2, game)
|
||||
|
||||
rtmt.sendMessage(userKey1, channel_on_game_start, encodeText(JSON.stringify(game)))
|
||||
rtmt.sendMessage(userKey2, channel_on_game_start, encodeText(JSON.stringify(game)))
|
||||
const data = encodeText(JSON.stringify(game))
|
||||
rtmt.sendMessage(userKey1, channel_on_game_start, data)
|
||||
rtmt.sendMessage(userKey2, channel_on_game_start, data)
|
||||
}
|
||||
|
||||
rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
|
||||
@ -62,9 +63,14 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
|
||||
const game = gameStore.get(userKey)
|
||||
if (game) {
|
||||
game.moveByIndex(gameMove.index, game.getPlayerNameByKey(userKey))
|
||||
const data = encodeText(JSON.stringify(game))
|
||||
rtmt.sendMessage(game.player1, channel_on_game_update, data)
|
||||
rtmt.sendMessage(game.player2, channel_on_game_update, data)
|
||||
|
||||
rtmt.sendMessage(game.player1, channel_on_game_update, encodeText(JSON.stringify(game)))
|
||||
rtmt.sendMessage(game.player2, channel_on_game_update, encodeText(JSON.stringify(game)))
|
||||
if (game.state == "ended") {
|
||||
gameStore.delete(game.player1)
|
||||
gameStore.delete(game.player2)
|
||||
}
|
||||
} else {
|
||||
console.log("Game not found!");
|
||||
}
|
||||
@ -72,8 +78,11 @@ rtmt.listenMessage(channel_game_move, (userKey: string, message: Bytes) => {
|
||||
|
||||
rtmt.listenMessage(channel_leave_game, (userKey: string, message: Bytes) => {
|
||||
const game = gameStore.get(userKey)
|
||||
if(game){
|
||||
if (game) {
|
||||
gameStore.delete(game.player1)
|
||||
gameStore.delete(game.player2)
|
||||
const data = encodeText(JSON.stringify(game))
|
||||
rtmt.sendMessage(game.player1, channel_on_game_update, data)
|
||||
rtmt.sendMessage(game.player2, channel_on_game_update, data)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@ -3,18 +3,20 @@ export class Game {
|
||||
player2: string
|
||||
board: Board
|
||||
turn: "player1" | "player2"
|
||||
state: "playing" | "ended"
|
||||
|
||||
constructor(player1: string, player2: string, board: Board, turn: "player1" | "player2") {
|
||||
constructor(player1: string, player2: string, board: Board, turn: "player1" | "player2", state: "playing" | "ended") {
|
||||
this.player1 = player1
|
||||
this.player2 = player2
|
||||
this.board = board
|
||||
this.turn = turn
|
||||
this.state = state
|
||||
}
|
||||
|
||||
public moveByIndex(holeIndex: number, player: "player1" | "player2") {
|
||||
let hole = this.board.player1Holes[holeIndex]
|
||||
|
||||
if(player == "player2"){
|
||||
if (player == "player2") {
|
||||
hole = this.board.player2Holes[holeIndex]
|
||||
}
|
||||
|
||||
@ -64,7 +66,7 @@ export class Game {
|
||||
}
|
||||
})
|
||||
hole.ballCount = holeEndBallCount
|
||||
console.log((stopIndex) );
|
||||
console.log((stopIndex));
|
||||
console.log(checkIsOwnHole(stopIndex, circleSize));
|
||||
|
||||
if (checkIsOwnHole(stopIndex, circleSize)) {
|
||||
@ -73,17 +75,35 @@ export class Game {
|
||||
const ownStore = getOwnStore(circle)
|
||||
const opponentHole = getOpponentHoleByIndex(stopIndex, circle)
|
||||
|
||||
ownStore.ballCount++
|
||||
stopHole.ballCount = 0;
|
||||
if (opponentHole.ballCount > 0) {
|
||||
ownStore.ballCount++
|
||||
stopHole.ballCount = 0;
|
||||
|
||||
ownStore.ballCount += opponentHole.ballCount
|
||||
opponentHole.ballCount = 0
|
||||
ownStore.ballCount += opponentHole.ballCount
|
||||
opponentHole.ballCount = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!checkIsOwnStore(stopIndex, circleSize)) {
|
||||
this.changeTurn()
|
||||
}
|
||||
|
||||
const ownHoles = circle.slice(0, circle.length / 2 - 1)
|
||||
|
||||
let sumBallCount = 0;
|
||||
for (const hole of ownHoles) sumBallCount += hole.ballCount
|
||||
|
||||
if (sumBallCount == 0) {
|
||||
const opppentHoles = circle.slice(circle.length / 2, circle.length - 1)
|
||||
let sumOppenentBallCount = 0;
|
||||
for (const hole of opppentHoles) {
|
||||
sumOppenentBallCount += hole.ballCount
|
||||
hole.ballCount = 0
|
||||
}
|
||||
getOwnStore(circle).ballCount += sumOppenentBallCount
|
||||
this.state = "ended"
|
||||
}
|
||||
}
|
||||
|
||||
public changeTurn() {
|
||||
@ -94,23 +114,23 @@ export class Game {
|
||||
}
|
||||
}
|
||||
|
||||
public clone() { return new Game(this.player1, this.player2, this.board, this.turn) }
|
||||
public clone() { return new Game(this.player1, this.player2, this.board, this.turn, this.state) }
|
||||
|
||||
//todo throw expection
|
||||
public getPlayerNameByKey(key : string) : "player1" | "player2" {
|
||||
if(this.player1 == key){
|
||||
public getPlayerNameByKey(key: string): "player1" | "player2" {
|
||||
if (this.player1 == key) {
|
||||
return "player1"
|
||||
}else{
|
||||
} else {
|
||||
return "player2"
|
||||
}
|
||||
}
|
||||
|
||||
public checkGameTurn(user : string) : boolean{
|
||||
public checkGameTurn(user: string): boolean {
|
||||
return this.getPlayerNameByKey(user) == this.turn
|
||||
}
|
||||
}
|
||||
|
||||
export function createGame( player1: string, player2: string){
|
||||
export function createGame(player1: string, player2: string) {
|
||||
// const board = new Board(
|
||||
// [new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)],
|
||||
// [new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)],
|
||||
@ -119,7 +139,11 @@ export function createGame( player1: string, player2: string){
|
||||
[new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4)],
|
||||
[new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4)],
|
||||
new Store(0), new Store(0))
|
||||
return new Game(player1, player2, board, "player1")
|
||||
// const board = new Board(
|
||||
// [new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(1)],
|
||||
// [new Hole(1), new Hole(0), new Hole(0), new Hole(0), new Hole(1), new Hole(0)],
|
||||
// new Store(0), new Store(0))
|
||||
return new Game(player1, player2, board, "player1", "playing")
|
||||
}
|
||||
|
||||
export class Hole {
|
||||
@ -191,5 +215,5 @@ function getOpponentHoleByIndex(index: number, circle: Array<Hole>) {
|
||||
}
|
||||
|
||||
export interface GameMove {
|
||||
index : number,
|
||||
index: number,
|
||||
}
|
||||
@ -6,6 +6,7 @@ export class MatchMaker {
|
||||
public onPlayersPaired!: (userKey1: string, userKey2: string) => void
|
||||
|
||||
public find(userKey : string) : void{
|
||||
if(this.waitingUserKey == userKey) return;
|
||||
if(this.waitingUserKey){
|
||||
const user1 = this.waitingUserKey as string
|
||||
this.waitingUserKey = undefined
|
||||
|
||||
@ -12,11 +12,20 @@ describe('mancala', function () {
|
||||
});
|
||||
it('zero hole test 1', function () {
|
||||
var board = new mancala_1.Board([new mancala_1.Hole(0), new mancala_1.Hole(1), new mancala_1.Hole(2), new mancala_1.Hole(3), new mancala_1.Hole(4), new mancala_1.Hole(5)], [new mancala_1.Hole(1), new mancala_1.Hole(0), new mancala_1.Hole(4), new mancala_1.Hole(4), new mancala_1.Hole(4), new mancala_1.Hole(4)], new mancala_1.Store(0), new mancala_1.Store(0));
|
||||
var game = new mancala_1.Game(player1, player2, board, "player2");
|
||||
var game = new mancala_1.Game(player1, player2, board, "player2", "playing");
|
||||
game.moveByIndex(0, "player2");
|
||||
expect(game.turn).toBe("player1");
|
||||
expect(game.board.player2Store.ballCount).toBe(5);
|
||||
expect(game.board.player1Holes[game.board.player1Holes.length - 1 - 1].ballCount).toBe(0);
|
||||
});
|
||||
it('game end test 1', function () {
|
||||
var board = new mancala_1.Board([new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(4), new mancala_1.Hole(4)], [new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(0), new mancala_1.Hole(1)], new mancala_1.Store(0), new mancala_1.Store(0));
|
||||
var game = new mancala_1.Game(player1, player2, board, "player2", "playing");
|
||||
game.moveByIndex(5, "player2");
|
||||
expect(game.turn).toBe("player2");
|
||||
expect(game.board.player2Store.ballCount).toBe(9);
|
||||
expect(game.board.player1Holes[game.board.player1Holes.length / 2].ballCount).toBe(0);
|
||||
expect(game.state).toBe("ended");
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=mancala.test.js.map
|
||||
@ -1 +1 @@
|
||||
{"version":3,"file":"mancala.test.js","sourceRoot":"","sources":["mancala.test.ts"],"names":[],"mappings":";;AAAA,0CAAqE;AAErE,QAAQ,CAAC,SAAS,EAAE;IAClB,IAAM,OAAO,GAAG,GAAG,CAAA;IACnB,IAAM,OAAO,GAAG,GAAG,CAAA;IACnB,EAAE,CAAC,WAAW,EAAE;QACd,IAAM,IAAI,GAAG,oBAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACzC,IAAI,CAAC,IAAI,GAAG,SAAS,CAAA;QACrB,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;QAC9B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;IACF,EAAE,CAAC,kBAAkB,EAAE;QACrB,IAAM,KAAK,GAAG,IAAI,eAAK,CACrB,CAAC,IAAI,cAAI,CAAC,CAAC,CAAC,EAAE,I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AM,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACrF,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
||||
@ -14,10 +14,23 @@ describe('mancala', function () {
|
||||
[new Hole(0), new Hole(1), new Hole(2), new Hole(3), new Hole(4), new Hole(5)],
|
||||
[new Hole(1), new Hole(0), new Hole(4), new Hole(4), new Hole(4), new Hole(4)],
|
||||
new Store(0), new Store(0))
|
||||
const game = new Game(player1, player2, board, "player2")
|
||||
const game = new Game(player1, player2, board, "player2", "playing")
|
||||
game.moveByIndex(0, "player2")
|
||||
expect(game.turn).toBe("player1")
|
||||
expect(game.board.player2Store.ballCount).toBe(5)
|
||||
expect(game.board.player1Holes[game.board.player1Holes.length - 1 - 1].ballCount).toBe(0)
|
||||
})
|
||||
|
||||
it('game end test 1', function () {
|
||||
const board = new Board(
|
||||
[new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(4), new Hole(4)],
|
||||
[new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(0), new Hole(1)],
|
||||
new Store(0), new Store(0))
|
||||
const game = new Game(player1, player2, board, "player2", "playing")
|
||||
game.moveByIndex(5, "player2")
|
||||
expect(game.turn).toBe("player2")
|
||||
expect(game.board.player2Store.ballCount).toBe(9)
|
||||
expect(game.board.player1Holes[game.board.player1Holes.length / 2].ballCount).toBe(0)
|
||||
expect(game.state).toBe("ended")
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user