/// <summary> /// Joins an existing game. /// </summary> /// <param name="gameId">The game identifier.</param> /// <param name="username">The username.</param> /// <param name="callback">The callback.</param> public void JoinGame(Guid gameId, string username, IGameConsoleCallback callback) { Game game = null; lock (LockObject) { if (!this.games.ContainsKey(gameId)) { throw new FaultException <GameNotFoundException>(new GameNotFoundException(gameId), "Game not found"); } game = this.games[gameId]; if (game.Status != GameStatus.Open) { throw new FaultException <GameNotOpenException>(new GameNotOpenException(gameId), "Game is not is opening state"); } game.Status = GameStatus.Playing; } // get visible windows from player 1 var player1VisibleWindows = game.Players[0].VisibleWindows; // create second player var player = new Player(); player.Name = username; player.Callback = callback; player.VisibleWindows.AddRange(game.Map.Windows.Count > player1VisibleWindows.Count ? game.Map.Windows.Select(window => window.Id).Where(id => !player1VisibleWindows.Contains(id)) : player1VisibleWindows); game.Players[1] = player; // send startgame callback Task.Run(() => this.StartGameCallback(game)); }
/// <summary> /// Opens a new game. /// </summary> /// <param name="mapId">The map identifier.</param> /// <param name="visibleWindows">The visible windows.</param> /// <param name="username">The username.</param> /// <param name="callback">The callback.</param> /// <returns>The id (guid) of the new game</returns> public Guid OpenGame(long mapId, IEnumerable <long> visibleWindows, string username, IGameConsoleCallback callback) { // load the map var map = this.unitOfWork.MapRepository.GetById(mapId); if (map == null) { throw new FaultException <MapNotFoundException>(new MapNotFoundException(mapId), "Map not found"); } // generate game var game = new Game(); game.Id = Guid.NewGuid(); game.Map = map; // create the player var player = new Player(); player.Name = username; player.VisibleWindows.AddRange(visibleWindows); player.Callback = callback; // set the player as player 1 and start the game game.Players[0] = player; lock (LockObject) { this.games.Add(game.Id, game); } return(game.Id); }