void InterpretCommand(int clientID, std::string message) { bool named = false; vector<int> clientIDs = server.getClientIDs(); ostringstream os; ostringstream os2; if (message.find("NewPlayer:") == 0) { if (message.length() > 10) { if (player1 == "") player1 = message.substr(10); else player2 = message.substr(10); } named = true; } if (!named) { vector<int> clientIDs = server.getClientIDs(); if (message.length() > 7) { if (clientID == clientIDs[0]) { snakeState.SetPlayerInput(0, message[8]); } else { snakeState.SetPlayerInput(1, message[8]); } } } }
/* called once per select() loop */ void periodicHandler() { vector<int> clientIDs = server.getClientIDs(); if (last_score1 != snakeState.GetPlayerScore(0) || last_score2 != snakeState.GetPlayerScore(1) || last_move[0] != snakeState.GetPlayerDirection(0) || last_move[1] != snakeState.GetPlayerDirection(1)) { if (last_score1 != snakeState.GetPlayerScore(0) || last_score2 != snakeState.GetPlayerScore(1)) { snakeState.SetPlayerInput(0, 'D'); snakeState.SetPlayerInput(1, 'A'); } emptyQueue(); message_to_process[0] = ""; message_to_process[1] = ""; last_score1 = snakeState.GetPlayerScore(0); last_score2 = snakeState.GetPlayerScore(1); last_move[0] = snakeState.GetPlayerDirection(0); last_move[1] = snakeState.GetPlayerDirection(1); snakeState.UpdateBoardState(); } messageDelay(); LARGE_INTEGER li; if (!QueryPerformanceFrequency(&li)) cout << "QueryPerformanceFrequency failed!\n"; static double freq = double(li.QuadPart) / 1000.0; QueryPerformanceCounter(&li); __int64 current = li.QuadPart; static __int64 interval = (double)500 * freq; // 500 ms static __int64 next = current + interval; if (gameStarted) { if (current >= next) { if (message_to_process[0] != "" && message_to_process[1] != "") { ProcessMessages(); message_to_process[0] = ""; message_to_process[1] = ""; ostringstream ss; ostringstream score1; ostringstream score2; ostringstream ssMove; ss << "GB:" << snakeState.GetBoardState(); score1 << "1:" << player1 + " score: " << snakeState.GetPlayerScore(0); score2 << "2:" << player2 + " score: " << snakeState.GetPlayerScore(1); ssMove << "MOVE:" << snakeState.GetPlayerDirection(0) << snakeState.GetPlayerDirection(1); for (int i = 0; i < clientIDs.size(); i++) { server.wsSend(clientIDs[i], ss.str()); server.wsSend(clientIDs[i], score1.str()); server.wsSend(clientIDs[i], score2.str()); server.wsSend(clientIDs[i], ssMove.str()); } snakeState.UpdateBoardState(); next = current + interval; } } } }