public void ServGameLoop(Action p_callbackFinish) { //Responde os calculos para todos Action p_callbackResponseTurn = delegate { for (int i = 0; i < listClients.Count; i++) { SocketController.ClientData __clientData = listClients[i]; __clientData.clientToSendResponse = ListToSend.ToString(); listClients[listClients.FindIndex(x => x.id == __clientData.id)] = __clientData; } StreamToClients(p_callbackFinish); // ServGameLoop((Action)p_callbackFinish); }; _threadGameLoop = new Thread(new ParameterizedThreadStart(GameLoop)); _threadGameLoop.Start(p_callbackResponseTurn); }
// Lógia da aceitação do cliente private void AcceptTcpClientThread(object p_callbackFinish) { while (_isAcceptingNewClients == true) { if (listClients.Count >= _maxClients) { Console.WriteLine("All clients connected, starting game..."); _isAcceptingNewClients = false; if ((Action)p_callbackFinish != null) { ((Action)p_callbackFinish)(); } //* ---> Novo //lobby de espera for (int i = 0; i < listClients.Count; i++) { SocketController.ClientData __clientData = listClients[i]; __clientData.clientToSendResponse = "1"; listClients[listClients.FindIndex(x => x.id == __clientData.id)] = __clientData; } StreamToClients((Action)p_callbackFinish); } else { ClientData __clientData = new ClientData(); __clientData.tcpClient = _tcpListener.AcceptTcpClient(); __clientData.id = Guid.NewGuid().ToString(); //* ---> Novo //set idPlayer 1/2 setIdOrder(__clientData.id); listClients.Add(__clientData); Console.WriteLine("New Client connected, {0} of {1}\n", listClients.Count, _maxClients); } } }
// Lógica da espera da resposta do cliente private void WaitClientStreamThread(ClientData p_clientData, Action p_callbackFinish) { NetworkStream __networkStream = p_clientData.tcpClient.GetStream(); int __readCount = __networkStream.Read(_bytes, 0, _bytes.Length); if (__readCount != 0) { string __response = Encoding.ASCII.GetString(_bytes, 0, __readCount); Console.WriteLine("Received data from Client [{0}]: \n{1}", p_clientData.id, __response); p_clientData.clientToGetResponse = __response; listClients[listClients.FindIndex(x => x.id == p_clientData.id)] = p_clientData; __networkStream.Flush(); _clientResponseCounter++; //* ---> Novo /////JSON string p_playerID = listClients[listClients.FindIndex(x => x.id == p_clientData.id)].id; var p_JsonResponse = JSON.Parse(__response); if (CURRENT_STATE == GameState.ON) { string getResponse = GameLogic(p_playerID, __response); //RESPONDE PARA O CLIENTE QUE ESTA ESPERANDO O OUTRO JOGADOR if (getResponse != null) { for (int i = 0; i < listClients.Count; i++) { SocketController.ClientData __clientData = listClients[i]; if (__clientData.id == p_playerID) { __clientData.clientToSendResponse = "3"; Console.WriteLine("Dados recebidos do turno : Player " + p_playerID); listClients[listClients.FindIndex(x => x.id == __clientData.id)] = __clientData; StreamToClients(p_callbackFinish); } } } } if (P1.TurnSend && P2.TurnSend) { CURRENT_STATE = GameState.REQUEST_TURN; ServGameLoop((Action)p_callbackFinish); } else { bool PlayerReady = p_JsonResponse["PlayerReady"].AsBool; string PlayerName = p_JsonResponse["PlayerName"].Value; string TeamChosen = p_JsonResponse["TeamChosen"].Value; //Se os jogadores estao conectando - procura pelo o id e seta o nome if (CURRENT_STATE == GameState.CONNECTING_PLAYERS) { if (_PlayersConnected < 2) { JoinnedPlayers(PlayerName, p_playerID); } } //se os jogadores estao no lobby para escolher time else if (CURRENT_STATE == GameState.CHOOSING_TEAM) { if (_PlayersReady < 2) { PlayerTeam(TeamChosen, p_playerID); } } //se os jogadores estao prontos para iniciar if (CURRENT_STATE == GameState.START_GAME) { for (int i = 0; i < listClients.Count; i++) { SocketController.ClientData __clientData = listClients[i]; //pronto pra comecar __clientData.clientToSendResponse = "2"; listClients[listClients.FindIndex(x => x.id == __clientData.id)] = __clientData; } StreamToClients(p_callbackFinish); CURRENT_STATE = GameState.ON; // ServGameLoop((Action)p_callbackFinish); GameLogic(); } } //* ---> Novo if (_clientResponseCounter >= _maxClients) { if (p_callbackFinish != null) { p_callbackFinish(); } } } }