Added player support to the server

This code is essentially copied from the old branch, since the two
branches are now functionally identical. How much time have I wasted
rewriting this?
This commit is contained in:
Kayne Ruse
2013-12-06 17:47:03 +11:00
parent f6a4674a2f
commit a3a990cc01
4 changed files with 200 additions and 0 deletions
+53
View File
@@ -35,6 +35,7 @@ using namespace std;
//-------------------------
int Client::counter = 0;
int Player::counter = 0;
//-------------------------
//Define the ServerApplication
@@ -139,6 +140,18 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
HandleShutdown(packet);
break;
case NetworkPacket::Type::PLAYER_NEW:
HandlePlayerNew(packet);
break;
case NetworkPacket::Type::PLAYER_DELETE:
HandlePlayerDelete(packet);
break;
case NetworkPacket::Type::PLAYER_UPDATE:
HandlePlayerUpdate(packet);
break;
//handle errors
default:
throw(runtime_error("Unknown NetworkPacket::Type encountered"));
@@ -195,3 +208,43 @@ void ServerApplication::HandleShutdown(NetworkPacket packet) {
cout << "shutting down" << endl;
}
void ServerApplication::HandlePlayerNew(NetworkPacket packet) {
//create the new player object
Player newPlayer;
newPlayer.clientIndex = packet.playerInfo.clientIndex;
newPlayer.handle = packet.playerInfo.handle;
newPlayer.avatar = packet.playerInfo.avatar;
newPlayer.position = {0,0};
newPlayer.motion = {0,0};
//push this player
playerMap[Player::counter] = newPlayer;
//send the client their info
packet.playerInfo.playerIndex = Player::counter;
packet.playerInfo.position = newPlayer.position;
packet.playerInfo.motion = newPlayer.motion;
network.Send(&clientMap[newPlayer.clientIndex].address, &packet, sizeof(NetworkPacket));
//finish this routine
Player::counter++;
cout << "new player, total: " << playerMap.size() << endl;
}
void ServerApplication::HandlePlayerDelete(NetworkPacket packet) {
if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
throw(std::runtime_error("Cannot delete a non-existant player"));
}
playerMap.erase(packet.playerInfo.playerIndex);
}
void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
throw(std::runtime_error("Cannot update a non-existant player"));
}
playerMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position;
playerMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion;
}
+16
View File
@@ -32,9 +32,11 @@
//misc
#include "config_utility.hpp"
#include "vector2.hpp"
//STL
#include <map>
#include <string>
//hold the client info
struct Client {
@@ -42,6 +44,16 @@ struct Client {
IPaddress address;
};
//hold the player info
struct Player {
static int counter;
int clientIndex;
std::string handle;
std::string avatar;
Vector2 position;
Vector2 motion;
};
//The main application class
class ServerApplication {
public:
@@ -61,6 +73,9 @@ private:
void HandleJoinRequest(NetworkPacket);
void HandleDisconnect(NetworkPacket);
void HandleShutdown(NetworkPacket);
void HandlePlayerNew(NetworkPacket);
void HandlePlayerDelete(NetworkPacket);
void HandlePlayerUpdate(NetworkPacket);
//networking
UDPNetworkUtility network;
@@ -73,6 +88,7 @@ private:
ConfigUtility config;
std::map<int, Client> clientMap;
std::map<int, Player> playerMap;
};
#endif