public void ConnectMsg(string msg, string id, Newtonsoft.Json.Linq.JContainer data) { //Program.awaitingPlayersID.Add(id); UserVoter newplayer; if (!Program.ConnIDtoUser.TryGetValue(id, out newplayer)) { newplayer = new UserVoter(id, data["workerId"] != null ? data["workerId"].ToString() : "", data["assignmentId"] != null ? data["assignmentId"].ToString() : ""); Program.ConnIDtoUser.Add(id, newplayer); } if (WaitingRoom.joinWaitingRnStart(newplayer)) { foreach (Game playingGame in Program.PlayingGames.Where(playingGame => playingGame.status != "playing")) { sendStartToPlayers(playingGame); } } if (newplayer.CurrGame == null) { Clients.Client(id).StartGameMsg("wait"); waitingRoomStats(); } }
public static Boolean joinWaitingRnStart(UserVoter Player) { if (!waitingPlayers.Contains(Player)) { waitingPlayers.Add(Player); } return(checkToStartGame() && Program.PlayingGames.Any()); }
//sent when the client loads the game page public void GameDetailsMsg(string connectionId) { UserVoter playerUser = Program.ConnIDtoUser[connectionId]; Game thegame = Program.getplayersGame(connectionId); int playerIndex = playerUser.inGameIndex; int turn = thegame.getTurn(connectionId); Clients.Client(connectionId).GameDetails(playerIndex, thegame.numOfCandidates, thegame.getNumOfPlayers(), thegame.getTurnsLeft(), thegame.createCandNamesString(playerUser), playerUser.currPriToString(), thegame.createPointsString(), thegame.createNumOfVotesString(playerUser), thegame.whoVoted, thegame.createWhoVotedString(playerIndex), ("p" + (playerIndex + 1)), turn, thegame.turn, thegame.getCurrentWinner(playerIndex), thegame.turnsToWait(playerIndex), thegame.prioritiesJSON); }
private static void splitPlayersToGames(List <UserVoter> playersList) { playersList.Shuffle(); int numPlayersinGame = Program.gameDetails.numOfHumanPlayers; //while (playersList.Count > 0) //foreach (Game currGame in Program.PlayingGames) foreach (List <UserVoter> BatchofPlayers in sortedPlayers) { if (Program.gameDetails.numOfHumanPlayers > BatchofPlayers.Count) { for (int x = 0; (playersList.Count > 0) && (x == 0 || x < numPlayersinGame); x++) { UserVoter newP = playersList.First(); BatchofPlayers.Add(newP); playersList.Remove(newP); RemoveFromWaitingR(newP.connectionID); } } } }
public override Task OnDisconnected(bool stopCalled) { Game theGame = Program.getplayersGame(Context.ConnectionId); UserVoter removedVoter = null; int playerIndex = -1; var sb = new StringBuilder(); if (Program.ConnIDtoUser.TryGetValue(Context.ConnectionId, out removedVoter)) { playerIndex = removedVoter.inGameIndex; sb.AppendFormat("[{0}] Disconnected: Player {1} ({2}). ", DateTime.Now.ToString("HH:mm:ss"), removedVoter.userID, removedVoter.mTurkID != "" ? removedVoter.mTurkID : Context.ConnectionId); if (removedVoter.CurrGame != null) { sb.AppendFormat("Left game {0}.", removedVoter.CurrGame.gameID); } Console.WriteLine(sb); WaitingRoom.RemoveFromWaitingR(Context.ConnectionId); Program.ConnIDtoUser.Remove(Context.ConnectionId); } waitingRoomStats(); if (Program.PlayingGames.Count > 0) { if (theGame != null && !theGame.gameOver) { theGame.replacePlayer(playerIndex, removedVoter); theGame.deletePlayerID(Context.ConnectionId); if (theGame.turn == playerIndex) { theGame.GameOver(); sendGameOver(theGame); } else { theGame.endNextVote = true; } if (false) { if (theGame.playersID.Count == 0) { theGame.endGame(); } else if (theGame.playersID.Count > 0 && theGame.turn == playerIndex) { int next = theGame.getNextTurn(); if (next != -1 && theGame.playersID.Count > 0) { if (theGame.playersID.Count > 0) { if (theGame.playersID.Count - 1 == theGame.humanTurn || theGame.playersID.Count <= theGame.humanTurn) { theGame.humanTurn = 0; updateOtherPlayers(theGame, Context.ConnectionId, playerIndex, next); UpdatePlayer(theGame, Context.ConnectionId, playerIndex, next); } else { updateOtherPlayers(theGame, Context.ConnectionId, playerIndex, next); UpdatePlayer(theGame, Context.ConnectionId, playerIndex, next); } } } else { sendGameOver(theGame); } } else { if (theGame.playersID.Count <= theGame.humanTurn) { theGame.humanTurn = 0; } } } } } WaitingRoom.checkToStartGame(); return(null); }