Пример #1
0
void openHandler(int clientID){
    
    json msg; // Our first message to the client
    
    // If Player 1 just connected...
    if (server.getClientIDs().size() == 1) {
        // Send msg: you've been assigned player 1
        msg["MESSAGE_TYPE"] = "PLAYER_ASSIGNMENT";
        msg["PLAYER_NUMBER"] = 1;
		msg["UPDATE_CYCLE_LENGTH"] = UPDATE_CYCLE_LENGTH_MS;
        send_message(clientID, msg);
    }
    
    // If Player 2 just connected...
    else if (server.getClientIDs().size() == 2) {
        // Send msg: you've been assigned player 2
        msg["MESSAGE_TYPE"] = "PLAYER_ASSIGNMENT";
        msg["PLAYER_NUMBER"] = 2;
		msg["UPDATE_CYCLE_LENGTH"] = UPDATE_CYCLE_LENGTH_MS;
        send_message(clientID, msg);
    }
    
    // Or if there are too many connections, reject it:
    else {
        msg["MESSAGE_TYPE"] = "CONNECTION_REJECTED";
        send_message(clientID, msg);
        server.wsClose(clientID);
    }
}
Пример #2
0
/* 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;

}
Пример #3
0
/* called when a client disconnects */
void closeHandler(int clientID){
	ostringstream os;
	os << "Stranger " << clientID << " has leaved.";

	vector<int> clientIDs = server.getClientIDs();
	for (int i = 0; i < clientIDs.size(); i++){
		server.wsSend(clientIDs[i], "Disconnected");
		server.wsClose(clientIDs[i]);
	}
	ReceiveQueue.clear();
	SendQueue.clear();

}
Пример #4
0
/* 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;

}
Пример #5
0
void closeHandler(int clientID){
    
    // If game is ongoing, kill it and send out
    // an error to whomever is still connected:
    if (game_p != NULL && game_p->isActive()) {
        json errorMsg;
        errorMsg["MESSAGE_TYPE"] = "ERROR";
        errorMsg["ERROR_MSG"] = "Other player disconnected";
        
        // Send the message to whomever is connected
        vector<int> clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsSend(clientIDs[i], errorMsg.dump()); // Don't buffer
        }
        
        // Close all open connections (must be done separately)
        clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsClose(i);
        }
        resetGame();
    }
}
Пример #6
0
/* called when a client connects */
void openHandler(int clientID){
	ostringstream os;
	vector<int> clientIDs = server.getClientIDs();

	std::cout << "in openhandler " << clientIDs.size() << std::endl;
	if (clientIDs.size() > 3){
		server.wsClose(clientID);
		cout << "Connection rejected: Only two connection allowed at a time.";
	}
	else if (clientIDs.size() == 2)
	{
		std::cout << "size is 2" << std::endl;
		os << "Stranger " << clientID << " has joined.";

		for (int i = 0; i < clientIDs.size(); i++){
			clientSnakes[clientIDs[i]].ID = clientID;
			server.wsSend(clientIDs[i], "Welcome!");
			//SendQueue[std::chrono::steady_clock::now()].push_back(createMessage(clientIDs[i], "Welcome!"));
		}

		gameStartState();

	}
}
Пример #7
0
void periodicHandler(){
    
    // Poll the incoming & outgoing message buffers.
    std::pair <int, std::string> message_pair;
    if (send_buffer.isMessageReady()) {
        message_pair = send_buffer.getMessage();
        if (message_pair.first != -1) {
			json time_check = json::parse(message_pair.second);
			if (time_check["MESSAGE_TYPE"] == "TIME_STAMP_REPLY") {
				time_check["T3"] = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
				message_pair.second = time_check.dump();
			}
            server.wsSend(message_pair.first, message_pair.second);
        }
    }
    if (receive_buffer.isMessageReady()) {
        message_pair = receive_buffer.getMessage();
        if (message_pair.first != -1) {
            read_message(message_pair.first, message_pair.second);
        }
    }
    
    // If the game is active, update the game state.
    // We want this to occur no sooner than UPDATE_CYCLE_LENGTH_MS.
    // WARNING: This sets the pace of the game, so Milestone 4 client features that
    // perform movement prediction MUST use the same clock speed.
    if (game_p != NULL && game_p->isActive()) {
        unsigned long long currentTime = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
        if (currentTime - lastUpdateTime >= UPDATE_CYCLE_LENGTH_MS) {
            // Update the game
            json msg = game_p->update();
            
            // Broadcast the update to all clients
            vector<int> clientIDs = server.getClientIDs();
            for (int i = 0; i < clientIDs.size(); i++) {
                send_message(clientIDs[i], msg);
            }
            
            // Update the clock
            lastUpdateTime = chrono::system_clock::now().time_since_epoch() / chrono::milliseconds(1);
        }
    }
    
    // If there is a game object but the status is INACTIVE,
    // update the clients and then DESTROY the game object.
    if (game_p != NULL && !game_p->isActive()) {

        json msg = game_p->update();
        resetGame();
        
        // Broadcast the final update to all clients
        // and then disconnect clients
        cout << "GAME OVER BROADCASTING FINAL UPDATE" << endl;
        vector<int> clientIDs = server.getClientIDs();
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsSend(clientIDs[i], msg.dump()); // Don't buffer
        }
        for (int i = 0; i < clientIDs.size(); i++) {
            server.wsClose(i);
        }
    }
}