public Task ChangeState() { var existingPerson2 = _context.Players.SingleOrDefault(p => p.ConnectionId == Context.ConnectionId); existingPerson2.ReadyState = !existingPerson2.ReadyState; _context.SaveChanges(); bool allTrue = _context.Players.All(i => i.ReadyState == true); if (allTrue) { gameStart(); } return(ConnectedPeople()); }
//On disconnected task //Removes player and related elements from database and calls for connectedPeople method public override Task OnDisconnectedAsync(Exception exception) { int lobbyId = _context.Lobbies.Include(l => l.Players) .Where(p => p.Players .Where(c => c.ConnectionId == Context.ConnectionId).FirstOrDefault() != null) .FirstOrDefault().ID; string group = "Lobby" + lobbyId; Player player = GetPlayerById(Context.ConnectionId); int curr = _context.Lobbies.Include(l => l.Players).Where(p => p.Players.Where(pp => pp.Name == player.Name) != null).FirstOrDefault().CurrPlayers; curr--; _context.Lobbies.Include(l => l.Players).Where(p => p.Players.Where(pp => pp.Name == player.Name) != null).FirstOrDefault().CurrPlayers = curr; _context.Lobbies.Include(l => l.Players).Where(p => p.Players.Where(pp => pp.Name == player.Name) != null).FirstOrDefault().Players.Remove(player); //_context.Tanks.Remove(player.Tank); //_context.TankStates.Remove(player.TankState); _context.Players.Where(p => p.Name == player.Name).FirstOrDefault().ReadyState = false; _context.SaveChanges(); Groups.RemoveFromGroupAsync(Context.ConnectionId, group); ConnectedPeople(lobbyId).Wait(); Clients.All.SendAsync("Disconnected", Context.ConnectionId); return(base.OnDisconnectedAsync(exception)); }