Working on reimplementing the player characters
I've also added EraseIf() to the manager classes.
This commit is contained in:
@@ -38,12 +38,23 @@ int ClientManager::HandleDisconnection(int i) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientManager::ForEach(Lambda fn) {
|
void ClientManager::ForEach(std::function<void(Iterator)> fn) {
|
||||||
for(Iterator it = clientMap.begin(); it != clientMap.end(); it++) {
|
for(Iterator it = clientMap.begin(); it != clientMap.end(); it++) {
|
||||||
fn(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) {
|
ClientEntry* ClientManager::GetClient(int i) {
|
||||||
for (auto& it : clientMap) {
|
for (auto& it : clientMap) {
|
||||||
if (it.first == i) {
|
if (it.first == i) {
|
||||||
|
|||||||
@@ -34,16 +34,16 @@ struct ClientEntry {
|
|||||||
class ClientManager {
|
class ClientManager {
|
||||||
public:
|
public:
|
||||||
//clarity typedefs
|
//clarity typedefs
|
||||||
typedef std::map<int, ClientEntry> Container;
|
typedef std::map<int, ClientEntry> Container;
|
||||||
typedef Container::iterator Iterator;
|
typedef Container::iterator Iterator;
|
||||||
typedef std::function<void(Iterator)> Lambda;
|
|
||||||
|
|
||||||
//returns the internal index
|
//returns the internal index
|
||||||
int HandleConnection(IPaddress);
|
int HandleConnection(IPaddress);
|
||||||
int HandleDisconnection(int i);
|
int HandleDisconnection(int i);
|
||||||
|
|
||||||
//lambdas
|
//lambdas
|
||||||
void ForEach(Lambda);
|
void ForEach(std::function<void(Iterator)> fn);
|
||||||
|
void EraseIf(std::function<bool(Iterator)> fn);
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
ClientEntry* GetClient(int i);
|
ClientEntry* GetClient(int i);
|
||||||
|
|||||||
@@ -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++) {
|
for(Iterator it = playerMap.begin(); it != playerMap.end(); it++) {
|
||||||
fn(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) {
|
PlayerEntry* PlayerManager::GetPlayer(int uniqueID) {
|
||||||
for (auto& it : playerMap) {
|
for (auto& it : playerMap) {
|
||||||
if (it.first == uniqueID) {
|
if (it.first == uniqueID) {
|
||||||
|
|||||||
@@ -40,9 +40,8 @@ struct PlayerEntry {
|
|||||||
class PlayerManager {
|
class PlayerManager {
|
||||||
public:
|
public:
|
||||||
//clarity typedefs
|
//clarity typedefs
|
||||||
typedef std::map<int, PlayerEntry> Container;
|
typedef std::map<int, PlayerEntry> Container;
|
||||||
typedef Container::iterator Iterator;
|
typedef Container::iterator Iterator;
|
||||||
typedef std::function<void(Iterator)> Lambda;
|
|
||||||
|
|
||||||
//These functions interact with the database
|
//These functions interact with the database
|
||||||
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
|
//*Deletion, *Load and *Unload returns: 0 success, -1 failure
|
||||||
@@ -54,7 +53,8 @@ public:
|
|||||||
int HandlePlayerUnload (int uniqueID);
|
int HandlePlayerUnload (int uniqueID);
|
||||||
|
|
||||||
//lambdas
|
//lambdas
|
||||||
void ForEach(Lambda);
|
void ForEach(std::function<void(Iterator)> fn);
|
||||||
|
void EraseIf(std::function<bool(Iterator)> fn);
|
||||||
|
|
||||||
//accessors
|
//accessors
|
||||||
PlayerEntry* GetPlayer(int uniqueID);
|
PlayerEntry* GetPlayer(int uniqueID);
|
||||||
|
|||||||
@@ -209,23 +209,20 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
|
|||||||
network.Send(&clientMgr.GetClient(packet.clientInfo.index)->address, buffer, sizeof(NetworkPacket));
|
network.Send(&clientMgr.GetClient(packet.clientInfo.index)->address, buffer, sizeof(NetworkPacket));
|
||||||
clientMgr.HandleDisconnection(packet.clientInfo.index);
|
clientMgr.HandleDisconnection(packet.clientInfo.index);
|
||||||
|
|
||||||
/* //delete players
|
//delete players from all clients
|
||||||
erase_if(playerMap, [&](pair<int, Player> it) -> bool {
|
NetworkPacket delPacket;
|
||||||
if (it.second.clientIndex == packet.clientInfo.index) {
|
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
|
||||||
NetworkPacket delPacket;
|
|
||||||
|
|
||||||
//data to delete one specific player
|
playerMgr.EraseIf([&](PlayerManager::Iterator it) -> bool {
|
||||||
delPacket.meta.type = NetworkPacket::Type::PLAYER_DELETE;
|
//find the internal players to delete
|
||||||
delPacket.playerInfo.playerIndex = it.first;
|
if (it->first == packet.clientInfo.index) {
|
||||||
|
delPacket.playerInfo.playerIndex = it->first;
|
||||||
//send to all
|
//send the delete player command to all clients
|
||||||
PumpPacket(delPacket);
|
PumpPacket(delPacket);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
*/
|
|
||||||
|
|
||||||
//finished this routine
|
//finished this routine
|
||||||
cout << "disconnect, total: " << clientMgr.Size() << endl;
|
cout << "disconnect, total: " << clientMgr.Size() << endl;
|
||||||
@@ -233,6 +230,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
|
|||||||
/*
|
/*
|
||||||
void ServerApplication::HandleSynchronize(NetworkPacket packet) {
|
void ServerApplication::HandleSynchronize(NetworkPacket packet) {
|
||||||
//send all the server's data to this client
|
//send all the server's data to this client
|
||||||
|
//TODO: compensate for large distances
|
||||||
NetworkPacket newPacket;
|
NetworkPacket newPacket;
|
||||||
char buffer[sizeof(NetworkPacket)];
|
char buffer[sizeof(NetworkPacket)];
|
||||||
|
|
||||||
@@ -322,3 +320,12 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) {
|
|||||||
PumpPacket(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));
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -62,11 +62,13 @@ private:
|
|||||||
void HandleBroadcastRequest(NetworkPacket);
|
void HandleBroadcastRequest(NetworkPacket);
|
||||||
void HandleJoinRequest(NetworkPacket);
|
void HandleJoinRequest(NetworkPacket);
|
||||||
void HandleDisconnect(NetworkPacket);
|
void HandleDisconnect(NetworkPacket);
|
||||||
// void HandleSynchronize(NetworkPacket);
|
void HandleSynchronize(NetworkPacket);
|
||||||
void HandleShutdown(NetworkPacket);
|
void HandleShutdown(NetworkPacket);
|
||||||
// void HandlePlayerNew(NetworkPacket);
|
void HandlePlayerNew(NetworkPacket);
|
||||||
// void HandlePlayerDelete(NetworkPacket);
|
void HandlePlayerDelete(NetworkPacket);
|
||||||
// void HandlePlayerUpdate(NetworkPacket);
|
void HandlePlayerUpdate(NetworkPacket);
|
||||||
|
|
||||||
|
void PumpPacket(NetworkPacket);
|
||||||
|
|
||||||
//networking
|
//networking
|
||||||
UDPNetworkUtility network;
|
UDPNetworkUtility network;
|
||||||
|
|||||||
Reference in New Issue
Block a user