コード例 #1
0
ファイル: main.cpp プロジェクト: jellesse92/MileStone1
/* called when a client connects */
void openHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	server.wsSend(clientID, "Welcome!");
	
	ostringstream game_width;
	ostringstream game_height;
	ostringstream game_board;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();

	server.wsSend(clientID, game_width.str());
	server.wsSend(clientID, game_height.str());
	server.wsSend(clientID, game_board.str());
	

	if (clientIDs.size() == 2) {
		gameStarted = true;
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

}
コード例 #2
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;
			}
		}
}
コード例 #3
0
ファイル: main.cpp プロジェクト: jellesse92/MileStone1
/* called when a client connects */
void openHandler(int clientID) {
	vector<int> clientIDs = server.getClientIDs();

	server.wsSend(clientID, "Welcome!");
	
	ostringstream game_width;
	ostringstream game_height;
	ostringstream game_board;
	ostringstream ssMove;
	ostringstream playerID;

	game_width << "GW:" << snakeState.GetBoardWidth();
	game_height << "GH:" << snakeState.GetBoardHeight();
	game_board << "GB:" << snakeState.GetBoardState();
	ssMove << "MOVE:" << snakeState.GetPlayerDirection(0) << snakeState.GetPlayerDirection(1);
	playerID << "ID:" << clientID;


	server.wsSend(clientID, playerID.str());
	server.wsSend(clientID, game_width.str());
	server.wsSend(clientID, game_height.str());
	server.wsSend(clientID, game_board.str());
	server.wsSend(clientID, ssMove.str());

	if (clientIDs.size() == 2) {
		gameStarted = true;
		message_to_process[0] = "";
		message_to_process[1] = "";
		emptyQueue();
		last_move[0] = 'D';
		last_move[1] = 'A';
		snakeState.StartNewGame();
		return;
	}
	else if (clientIDs.size() > 2)
		server.wsClose(clientID);
	else
		gameStarted = false;

}
コード例 #4
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;
				}

	
			}
		}
}