private PlayerJoinGameResponse JoinGame(PlayerJoinGame request) { var result = new PlayerJoinGameResponse(); var game = Cache.Get <CardGame>(request.GameId); var allGames = Cache.Get <AllGames>(AllGamesSummaries); var gameSummary = allGames.Games.Single(x => x.GameId == request.GameId); if (game.PlayerOneId != null && game.PlayerTwoId != null) { throw HttpError.Conflict("Game full"); } if (game.PlayerOneId == null) { game.PlayerOneId = request.PlayerId; game.PlayerOne = new GamePlayer { DisplayName = request.PlayerDisplayerName, Hand = game.Deck.Deal(2) }; gameSummary.HasPlayerOne = true; } else { game.PlayerTwoId = request.PlayerId; game.PlayerTwo = new GamePlayer { DisplayName = request.PlayerDisplayerName, Hand = game.Deck.Deal(2) }; gameSummary.HasPlayerTwo = true; } Cache.Set(AllGamesSummaries, allGames); game.Events.Add(new CardGameEvent { ClientId = request.PlayerId, GameId = request.GameId, Message = "Player {0} has entered the game.".Fmt(request.PlayerDisplayerName), Type = "JOIN" }); Cache.Set(request.GameId, game, TimeSpan.FromHours(1)); return(result); }
private PlayerJoinGameResponse JoinGame(PlayerJoinGame request) { var result = new PlayerJoinGameResponse(); var game = Cache.Get<CardGame>(request.GameId); var allGames = Cache.Get<AllGames>(AllGamesSummaries); var gameSummary = allGames.Games.Single(x => x.GameId == request.GameId); if (game.PlayerOneId != null && game.PlayerTwoId != null) throw HttpError.Conflict("Game full"); if (game.PlayerOneId == null) { game.PlayerOneId = request.PlayerId; game.PlayerOne = new GamePlayer { DisplayName = request.PlayerDisplayerName, Hand = game.Deck.Deal(2) }; gameSummary.HasPlayerOne = true; } else { game.PlayerTwoId = request.PlayerId; game.PlayerTwo = new GamePlayer { DisplayName = request.PlayerDisplayerName, Hand = game.Deck.Deal(2) }; gameSummary.HasPlayerTwo = true; } Cache.Set(AllGamesSummaries, allGames); game.Events.Add(new CardGameEvent { ClientId = request.PlayerId, GameId = request.GameId, Message = "Player {0} has entered the game.".Fmt(request.PlayerDisplayerName), Type = "JOIN" }); Cache.Set(request.GameId, game,TimeSpan.FromHours(1)); return result; }