コード例 #1
0
ファイル: main.cpp プロジェクト: jellesse92/MileStone1
/* called once per select() loop */
void periodicHandler() {
	vector<int> clientIDs = server.getClientIDs();

		static time_t next = time(NULL)+1;
		time_t current = time(NULL);
		messageDelay();
		
		if (gameStarted) {
			if (current   >= next) {
				snakeState.UpdateBoardState();

				ostringstream ss;
				ostringstream score1;
				ostringstream score2;
				ss << "GB:" << snakeState.GetBoardState();
				score1 << "1:" << player1 + " score: " << snakeState.GetPlayerScore(0);
				score2 << "2:" << player2 + " score: " << snakeState.GetPlayerScore(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());
				}

				next = time(NULL) + 1;
			}
		}
}
コード例 #2
0
ファイル: main.cpp プロジェクト: jellesse92/MileStone1
/* 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;
				}

	
			}
		}
}