Working on reimplementing the player characters

I've also added EraseIf() to the manager classes.
This commit is contained in:
Kayne Ruse
2014-03-07 22:09:50 +11:00
parent 006a72174f
commit 56d02ad8d4
6 changed files with 56 additions and 25 deletions
+12 -1
View File
@@ -38,12 +38,23 @@ int ClientManager::HandleDisconnection(int i) {
return -1;
}
void ClientManager::ForEach(Lambda fn) {
void ClientManager::ForEach(std::function<void(Iterator)> fn) {
for(Iterator it = clientMap.begin(); it != clientMap.end(); it++) {
fn(it);
}
}
void ClientManager::EraseIf(std::function<bool(Iterator)> fn) {
for(Iterator it = clientMap.begin(); it != clientMap.end(); /* empty */) {
if(fn(it)) {
it = clientMap.erase(it);
}
else {
++it;
}
}
}
ClientEntry* ClientManager::GetClient(int i) {
for (auto& it : clientMap) {
if (it.first == i) {
+4 -4
View File
@@ -34,16 +34,16 @@ struct ClientEntry {
class ClientManager {
public:
//clarity typedefs
typedef std::map<int, ClientEntry> Container;
typedef Container::iterator Iterator;
typedef std::function<void(Iterator)> Lambda;
typedef std::map<int, ClientEntry> Container;
typedef Container::iterator Iterator;
//returns the internal index
int HandleConnection(IPaddress);
int HandleDisconnection(int i);
//lambdas
void ForEach(Lambda);
void ForEach(std::function<void(Iterator)> fn);
void EraseIf(std::function<bool(Iterator)> fn);
//accessors
ClientEntry* GetClient(int i);
+12 -1
View File
@@ -37,12 +37,23 @@ int PlayerManager::HandlePlayerUnload(int uniqueID) {
//
}
void PlayerManager::ForEach(Lambda fn) {
void PlayerManager::ForEach(std::function<void(Iterator)> fn) {
for(Iterator it = playerMap.begin(); it != playerMap.end(); it++) {
fn(it);
}
}
void PlayerManager::EraseIf(std::function<bool(Iterator)> fn) {
for(Iterator it = playerMap.begin(); it != playerMap.end(); /* empty */) {
if(fn(it)) {
it = playerMap.erase(it);
}
else {
++it;
}
}
}
PlayerEntry* PlayerManager::GetPlayer(int uniqueID) {
for (auto& it : playerMap) {
if (it.first == uniqueID) {
+4 -4
View File
@@ -40,9 +40,8 @@ struct PlayerEntry {
class PlayerManager {
public:
//clarity typedefs
typedef std::map<int, PlayerEntry> Container;
typedef Container::iterator Iterator;
typedef std::function<void(Iterator)> Lambda;
typedef std::map<int, PlayerEntry> Container;
typedef Container::iterator Iterator;
//These functions interact with the database
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
@@ -54,7 +53,8 @@ public:
int HandlePlayerUnload (int uniqueID);
//lambdas
void ForEach(Lambda);
void ForEach(std::function<void(Iterator)> fn);
void EraseIf(std::function<bool(Iterator)> fn);
//accessors
PlayerEntry* GetPlayer(int uniqueID);
+18 -11
View File
@@ -209,23 +209,20 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
network.Send(&clientMgr.GetClient(packet.clientInfo.index)->address, buffer, sizeof(NetworkPacket));
clientMgr.HandleDisconnection(packet.clientInfo.index);
/* //delete players
erase_if(playerMap, [&](pair<int, Player> it) -> bool {
if (it.second.clientIndex == packet.clientInfo.index) {
NetworkPacket delPacket;
//delete players from all clients
NetworkPacket delPacket;
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
//data to delete one specific player
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
delPacket.playerInfo.playerIndex = it.first;
//send to all
playerMgr.EraseIf([&](PlayerManager::Iterator it) -> bool {
//find the internal players to delete
if (it->first == packet.clientInfo.index) {
delPacket.playerInfo.playerIndex = it->first;
//send the delete player command to all clients
PumpPacket(delPacket);
return true;
}
return false;
});
*/
//finished this routine
cout << "disconnect, total: " << clientMgr.Size() << endl;
@@ -233,6 +230,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
/*
void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//send all the server's data to this client
//TODO: compensate for large distances
NetworkPacket newPacket;
char buffer[sizeof(NetworkPacket)];
@@ -322,3 +320,12 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
PumpPacket(packet);
}
*/
void ServerApplication::PumpPacket(NetworkPacket packet) {
//I don't really like this, but it'll do for now
char buffer[sizeof(NetworkPacket)];
serialize(&packet, buffer);
clientMgr.ForEach([&](ClientManager::Iterator it) {
network.Send(&it->second.address, buffer, sizeof(NetworkPacket));
});
}
+6 -4
View File
@@ -62,11 +62,13 @@ private:
void HandleBroadcastRequest(NetworkPacket);
void HandleJoinRequest(NetworkPacket);
void HandleDisconnect(NetworkPacket);
// void HandleSynchronize(NetworkPacket);
void HandleSynchronize(NetworkPacket);
void HandleShutdown(NetworkPacket);
// void HandlePlayerNew(NetworkPacket);
// void HandlePlayerDelete(NetworkPacket);
// void HandlePlayerUpdate(NetworkPacket);
void HandlePlayerNew(NetworkPacket);
void HandlePlayerDelete(NetworkPacket);
void HandlePlayerUpdate(NetworkPacket);
void PumpPacket(NetworkPacket);
//networking
UDPNetworkUtility network;