diff --git a/src/GameServer.spec.ts b/src/GameServer.spec.ts index 20f7f0a..7e98dc5 100644 --- a/src/GameServer.spec.ts +++ b/src/GameServer.spec.ts @@ -105,15 +105,12 @@ const mapID = "lvl1"; describe("When a user starts an existing valid level", function(){ const games = createGameServer(); - const position = games.startGame(userID, mapID); - - it("then the start position must exist", function(){ - expect(position).to.be.not.null; - }); + games.startGame(userID, mapID); it("then the start position must be remembered for the user", function(){ const current = games.currentPosition(userID); - expect(current.id).to.equal(position.id); + expect(current, "Position object is null.").to.be.not.null; + expect(current.id, "Id of the tile is null.").to.be.not.null; }); }); @@ -158,13 +155,59 @@ }); }); +describe("Scenario: Ending the game", function(){ + const userID = "UserID"; + const mapID = "lvl1"; + + describe("Given a started game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + + describe("When the games is ended", function(){ + const notBreakingFunction = () => { + games.endGame(userID); + }; + it("then no exeption should be thrown.", function(){ + expect(notBreakingFunction).to.not.throw; + }); + }); + }); + + describe("Given a started and ended game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + games.endGame(userID); + + describe("When the current position is queried", function(){ + const position = games.currentPosition(userID); + + it("then current position should be null.", function(){ + expect(position).to.be.null; + }); + }); + }); + + describe("Given no started game.", function(){ + const games = createGameServer(); + + describe("When the game is ended", function(){ + const result = games.endGame(userID); + + it("then it should return true.", function(){ + expect(result).to.equal(true); + }); + }); + }); +}); + describe("Scenario: Navigating in ongoing game", function(){ const userID = "UserID"; const mapID = "lvl1"; describe("When player navigates in existing direction", function(){ const games = createGameServer(); - const startPosition = games.startGame(userID, mapID); + games.startGame(userID, mapID); + const startPosition = games.currentPosition(userID); const naviagtionDirection = startPosition.paths[0]; const nextPosition = games.navigate(userID, naviagtionDirection); it("then the new position has to have a different id", function(){ diff --git a/src/GameServer.spec.ts b/src/GameServer.spec.ts index 20f7f0a..7e98dc5 100644 --- a/src/GameServer.spec.ts +++ b/src/GameServer.spec.ts @@ -105,15 +105,12 @@ const mapID = "lvl1"; describe("When a user starts an existing valid level", function(){ const games = createGameServer(); - const position = games.startGame(userID, mapID); - - it("then the start position must exist", function(){ - expect(position).to.be.not.null; - }); + games.startGame(userID, mapID); it("then the start position must be remembered for the user", function(){ const current = games.currentPosition(userID); - expect(current.id).to.equal(position.id); + expect(current, "Position object is null.").to.be.not.null; + expect(current.id, "Id of the tile is null.").to.be.not.null; }); }); @@ -158,13 +155,59 @@ }); }); +describe("Scenario: Ending the game", function(){ + const userID = "UserID"; + const mapID = "lvl1"; + + describe("Given a started game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + + describe("When the games is ended", function(){ + const notBreakingFunction = () => { + games.endGame(userID); + }; + it("then no exeption should be thrown.", function(){ + expect(notBreakingFunction).to.not.throw; + }); + }); + }); + + describe("Given a started and ended game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + games.endGame(userID); + + describe("When the current position is queried", function(){ + const position = games.currentPosition(userID); + + it("then current position should be null.", function(){ + expect(position).to.be.null; + }); + }); + }); + + describe("Given no started game.", function(){ + const games = createGameServer(); + + describe("When the game is ended", function(){ + const result = games.endGame(userID); + + it("then it should return true.", function(){ + expect(result).to.equal(true); + }); + }); + }); +}); + describe("Scenario: Navigating in ongoing game", function(){ const userID = "UserID"; const mapID = "lvl1"; describe("When player navigates in existing direction", function(){ const games = createGameServer(); - const startPosition = games.startGame(userID, mapID); + games.startGame(userID, mapID); + const startPosition = games.currentPosition(userID); const naviagtionDirection = startPosition.paths[0]; const nextPosition = games.navigate(userID, naviagtionDirection); it("then the new position has to have a different id", function(){ diff --git a/src/GameServer.ts b/src/GameServer.ts index 4d59d2f..03753ad 100644 --- a/src/GameServer.ts +++ b/src/GameServer.ts @@ -53,7 +53,7 @@ } } - public startGame(userID: string, mapID: string): MazeTile + public startGame(userID: string, mapID: string): boolean { this.userIdGuard(userID); const map = this.mapList.find(m => m.id === mapID); @@ -74,7 +74,17 @@ state.tileID = start.id; } - return start; + return true; + } + + public endGame(userID: string): boolean + { + const stateIndex = this.states.findIndex(s => s.userID === userID); + if(stateIndex !== -1) + { + this.states.splice(stateIndex, 1); + } + return true; } public navigate(userID: string, direction:Direction): MazeTile diff --git a/src/GameServer.spec.ts b/src/GameServer.spec.ts index 20f7f0a..7e98dc5 100644 --- a/src/GameServer.spec.ts +++ b/src/GameServer.spec.ts @@ -105,15 +105,12 @@ const mapID = "lvl1"; describe("When a user starts an existing valid level", function(){ const games = createGameServer(); - const position = games.startGame(userID, mapID); - - it("then the start position must exist", function(){ - expect(position).to.be.not.null; - }); + games.startGame(userID, mapID); it("then the start position must be remembered for the user", function(){ const current = games.currentPosition(userID); - expect(current.id).to.equal(position.id); + expect(current, "Position object is null.").to.be.not.null; + expect(current.id, "Id of the tile is null.").to.be.not.null; }); }); @@ -158,13 +155,59 @@ }); }); +describe("Scenario: Ending the game", function(){ + const userID = "UserID"; + const mapID = "lvl1"; + + describe("Given a started game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + + describe("When the games is ended", function(){ + const notBreakingFunction = () => { + games.endGame(userID); + }; + it("then no exeption should be thrown.", function(){ + expect(notBreakingFunction).to.not.throw; + }); + }); + }); + + describe("Given a started and ended game.", function(){ + const games = createGameServer(); + games.startGame(userID, mapID); + games.endGame(userID); + + describe("When the current position is queried", function(){ + const position = games.currentPosition(userID); + + it("then current position should be null.", function(){ + expect(position).to.be.null; + }); + }); + }); + + describe("Given no started game.", function(){ + const games = createGameServer(); + + describe("When the game is ended", function(){ + const result = games.endGame(userID); + + it("then it should return true.", function(){ + expect(result).to.equal(true); + }); + }); + }); +}); + describe("Scenario: Navigating in ongoing game", function(){ const userID = "UserID"; const mapID = "lvl1"; describe("When player navigates in existing direction", function(){ const games = createGameServer(); - const startPosition = games.startGame(userID, mapID); + games.startGame(userID, mapID); + const startPosition = games.currentPosition(userID); const naviagtionDirection = startPosition.paths[0]; const nextPosition = games.navigate(userID, naviagtionDirection); it("then the new position has to have a different id", function(){ diff --git a/src/GameServer.ts b/src/GameServer.ts index 4d59d2f..03753ad 100644 --- a/src/GameServer.ts +++ b/src/GameServer.ts @@ -53,7 +53,7 @@ } } - public startGame(userID: string, mapID: string): MazeTile + public startGame(userID: string, mapID: string): boolean { this.userIdGuard(userID); const map = this.mapList.find(m => m.id === mapID); @@ -74,7 +74,17 @@ state.tileID = start.id; } - return start; + return true; + } + + public endGame(userID: string): boolean + { + const stateIndex = this.states.findIndex(s => s.userID === userID); + if(stateIndex !== -1) + { + this.states.splice(stateIndex, 1); + } + return true; } public navigate(userID: string, direction:Direction): MazeTile diff --git a/src/LabyrinthMiddleware.ts b/src/LabyrinthMiddleware.ts index fb7b857..db93e45 100644 --- a/src/LabyrinthMiddleware.ts +++ b/src/LabyrinthMiddleware.ts @@ -22,7 +22,10 @@ type Mutation { "Start a new game" - startGame(userID: String!, mapID: String!): MazeTileQL + startGame(userID: String!, mapID: String!): Boolean! + + "Cancels a currently running game" + endGame(userID: String!): Boolean! } type Query { @@ -61,8 +64,9 @@ { return { currentPostition: ({userID}) => new MazeTileQL(games.currentPosition(userID)), + endGame: ({userID}) => games.endGame(userID), listMaps: () => games.listMaps(), - startGame: ({userID, mapID}) => new MazeTileQL(games.startGame(userID, mapID)), + startGame: ({userID, mapID}) => games.startGame(userID, mapID), }; }