public async Task <ActionResult <Guid> > StartGame(string login) { _logger.LogInformation($"{login} try to find a room for random play "); var room = _rooms.TakeLastRoom(); if (room.Value == null) { var newRoom = new Room() { Guid = Guid.NewGuid(), Player1 = login, Player2 = null }; await _rooms.AddWithGuidAsync(newRoom.Guid, newRoom); _logger.LogInformation($"new room created by {login} he is waiting for player "); return(newRoom.Guid); } room.Value.Player2 = login; await _session.AddWithGuidAsync(room.Value.Guid, room.Value); _logger.LogInformation($" {login} joined to {room.Value.Player1} , and they created a session "); await _rooms.DeleteAsync(room.Key); return(room.Value.Guid); }
public async Task <ActionResult <string> > WaitingEnemy(Guid linkOfGuid) { var room = await _sessionPlayRooms.Get(linkOfGuid); if (room == null) { return(NotFound()); } if (room.FirstPlayer == null) { await _sessionPlayRooms.DeleteAsync(linkOfGuid); await _playRooms.DeleteAsync(linkOfGuid); await _rooms.DeleteAsync(linkOfGuid); return("Exit"); } var game = new GameProcess(room.FirstPlayer, room.SecondPlayer); var winner = await game.PlayersPlay(); if (winner.Value == "Exit") { _jsonUpdaterLeaderBoard.UpdateFile("Leaderboard.json", _leaderBoard.GetDictionary()); room.FirstPlayer = null; return("Exit"); } _logger.LogInformation($"{winner.Value} Win"); if (winner.Value == room.FirstPlayer.Login) { await _leaderBoard.AddWins(room.FirstPlayer.Login); await _leaderBoard.AddLoses(room.SecondPlayer.Login); } else if (winner.Value == room.SecondPlayer.Login) { await _leaderBoard.AddWins(room.SecondPlayer.Login); await _leaderBoard.AddLoses(room.FirstPlayer.Login); } if (winner.Value == "Draw") { await _leaderBoard.AddDraws(room.FirstPlayer.Login); await _leaderBoard.AddDraws(room.SecondPlayer.Login); } return(winner); }