public bool CheckPlayerInGame(Guid playerId, out GameWrapper Game) { Game = null; var game = GameStore.Values.FirstOrDefault(x => x.Player1.Id == playerId || x.Player2.Id == playerId); if (game == null) { return(false); } Game = game; return(true); }
public Guid CreateNewGame(Guid player1Id, Guid player2Id) { Guid gameId; var retries = 0; do { if (retries < 3) { gameId = Guid.NewGuid(); retries += 1; } else { throw new InvalidOperationException("Could not generate valid Guid within 3 tries"); } } while (GameStore.ContainsKey(gameId)); var personGenerator = new PersonNameGenerator(); var names = personGenerator.GenerateMultipleFirstAndLastNames(2).Select(x => x.Split(' ').First()); var player1 = new PlayerWrapper { Player = new Player(names.First()), Id = player1Id }; var player2 = new PlayerWrapper { Player = new Player(names.Last()), Id = player2Id }; var game = new GameWrapper { Id = gameId, Game = new Game(), Player1 = player1, Player2 = player2 }; GameStore.Add(gameId, game); PlayerStore.Add(player1.Id, player1); PlayerStore.Add(player2.Id, player2); GameCreated?.Invoke(this, new GameCreatedEvent(gameId, player1, player2)); return(gameId); }