Skip to content

Commit

Permalink
Begun working on the issues (see #2, #3).
Browse files Browse the repository at this point in the history
  • Loading branch information
brodenino committed Mar 7, 2013
1 parent 0893cbe commit 330b9b6
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 26 deletions.
25 changes: 25 additions & 0 deletions Projects/Gameplay/Src/DisconnectPacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "DisconnectPacket.h"

DisconnectPacket::DisconnectPacket()
{
clientNetworkIdentity = -1;
playerID = -1;
}

DisconnectPacket::~DisconnectPacket()
{

}

Packet DisconnectPacket::pack()
{
Packet packet(static_cast<char>(PacketType::ClientDisconnect));
packet << clientNetworkIdentity << playerID;

return packet;
}

void DisconnectPacket::unpack( Packet& p_packet )
{
p_packet >> clientNetworkIdentity >> playerID;
}
28 changes: 28 additions & 0 deletions Projects/Gameplay/Src/DisconnectPacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include "Packetizer.h"

// =======================================================================================
// DisconnectPacket
// =======================================================================================

///---------------------------------------------------------------------------------------
/// \brief Handles the packing and unpacking of DisconnectPacket
///
/// # WelcomePacket
/// Detailed description.....
/// Created on: 14-1-2013
///---------------------------------------------------------------------------------------

class DisconnectPacket : public Packetizer
{
public:
DisconnectPacket();
~DisconnectPacket();

Packet pack();

void unpack( Packet& p_packet );
public:
int clientNetworkIdentity;
int playerID;
};
2 changes: 2 additions & 0 deletions Projects/Gameplay/Src/Gameplay.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ClInclude Include="ColorTone.h" />
<ClInclude Include="DestroyOnParticlesDeath.h" />
<ClInclude Include="DestroyOnParticlesDeathSystem.h" />
<ClInclude Include="DisconnectPacket.h" />
<ClInclude Include="EditSphereSystem.h" />
<ClInclude Include="EditSphereUpdatePacket.h" />
<ClInclude Include="EmpRocketLancherModuleSystem.h" />
Expand Down Expand Up @@ -288,6 +289,7 @@
<ClCompile Include="DamageVisualizerSystem.cpp" />
<ClCompile Include="DestroyOnParticlesDeath.cpp" />
<ClCompile Include="DestroyOnParticlesDeathSystem.cpp" />
<ClCompile Include="DisconnectPacket.cpp" />
<ClCompile Include="DisplayGameStats.cpp" />
<ClCompile Include="DisplayPlayerScoreSystem.cpp" />
<ClCompile Include="EditSphereSystem.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Projects/Gameplay/Src/Gameplay.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@
<ClInclude Include="ModuleOnChamberStartPoint.h">
<Filter>Components\Modules</Filter>
</ClInclude>
<ClInclude Include="DisconnectPacket.h">
<Filter>Systems\Network\Packet Translation\Packetizer</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Transform.cpp">
Expand Down Expand Up @@ -1405,5 +1408,8 @@
<ClCompile Include="SoundComponent.cpp">
<Filter>Components\Audio</Filter>
</ClCompile>
<ClCompile Include="DisconnectPacket.cpp">
<Filter>Systems\Network\Packet Translation\Packetizer</Filter>
</ClCompile>
</ItemGroup>
</Project>
38 changes: 36 additions & 2 deletions Projects/Gameplay/Src/PlayerSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "PlayerSystem.h"
#include "PlayerComponent.h"
#include <Globals.h>
#include <string>
using namespace std;

PlayerSystem::PlayerSystem() : EntitySystem( SystemType::PlayerSystem, 1,
ComponentType::PlayerComponent)
{

m_playerComponents.resize(MAXPLAYERS, NULL);
}

PlayerSystem::~PlayerSystem()
Expand All @@ -14,6 +17,8 @@ PlayerSystem::~PlayerSystem()

PlayerComponent* PlayerSystem::getPlayerCompFromNetworkComp(NetworkSynced* p_netComponent)
{


vector<Entity*> activeEntities = getActiveEntities();

Entity* player = activeEntities[p_netComponent->getPlayerID()];
Expand All @@ -36,7 +41,7 @@ void PlayerSystem::inserted( Entity* p_entity )
m_playerComponents.push_back(playerComponent);
}

std::string PlayerSystem::getPlayerNameFromID( int p_playerID ) const
std::string PlayerSystem::getPlayerNameFromID( int p_playerID )
{
if(p_playerID <= m_playerComponents.size()){
for (unsigned int i = 0; i < m_playerComponents.size(); i++)
Expand All @@ -46,5 +51,34 @@ std::string PlayerSystem::getPlayerNameFromID( int p_playerID ) const
}
}
}
//if (m_playerComps.hasValue(p_playerID))
// return m_playerComps[p_playerID]->m_playerName;
return "";
}

void PlayerSystem::removed( Entity* p_entity )
{
auto playerComponent = static_cast<PlayerComponent*>(
p_entity->getComponent(ComponentType::PlayerComponent) );

// Recycles the id. Note, this is only useful on the server.
recyclePlayerId(playerComponent->m_playerID);
}
//
int PlayerSystem::createPlayerId( int p_fromNetworkOwnerId )
{
return m_playerIds.add(p_fromNetworkOwnerId);
}

void PlayerSystem::recyclePlayerId( int p_playerId )
{
if (m_playerIds.hasValue(p_playerId))
m_playerIds.removeAt(p_playerId);
}

//PlayerComponent* PlayerSystem::serverCreatePlayerComponent()
//{
// PlayerComponent* playerComp = new PlayerComponent();
// playerComp->m_playerID = m_playerComps.add(playerComp);
// return playerComp;
//}
12 changes: 11 additions & 1 deletion Projects/Gameplay/Src/PlayerSystem.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <EntitySystem.h>
#include "NetworkSynced.h"
#include <UniqueIndexList.h>

class NetworkSynced;
class PlayerComponent;
Expand All @@ -24,8 +25,17 @@ class PlayerSystem : public EntitySystem
~PlayerSystem();
PlayerComponent* getPlayerCompFromNetworkComp(NetworkSynced* p_netComponent);
const vector<PlayerComponent*>& getPlayerComponents() const;
string getPlayerNameFromID(int p_playerID) const;
string getPlayerNameFromID(int p_playerID);
void inserted( Entity* p_entity );
void removed( Entity* p_entity );

//PlayerComponent* serverCreatePlayerComponent(int p_fromNetworkOwnerId);

int createPlayerId(int p_fromNetworkOwnerId);
void recyclePlayerId(int p_playerId);
private:
vector<PlayerComponent*> m_playerComponents;

UniqueIndexList<PlayerComponent*> m_playerComps;
UniqueIndexList<int> m_playerIds;
};
52 changes: 31 additions & 21 deletions Projects/Gameplay/Src/ServerWelcomeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "EntityCreationPacket.h"
#include "WelcomePacket.h"
#include "NewlyConnectedPlayerPacket.h"
#include "DisconnectPacket.h"

#include <Globals.h>
#include "EntityFactory.h"
Expand Down Expand Up @@ -65,25 +66,26 @@ void ServerWelcomeSystem::processEntities( const vector<Entity*>& p_entities )
{
int id = m_server->popNewDisconnection();

for (unsigned int index = 0; index < p_entities.size(); index++)
{
NetworkSynced* netSync = static_cast<NetworkSynced*>(
m_world->getComponentManager()->getComponent( p_entities[index],
ComponentType::NetworkSynced ) );
}

// If a client has disconnected, then the clientinfo should be removed?
auto clientInfoSys = static_cast<ServerClientInfoSystem*>(
m_world->getSystem(SystemType::ServerClientInfoSystem));
vector<Entity*> clientInfoEntities = clientInfoSys->getActiveEntities();
for (unsigned int i = 0; i < clientInfoEntities.size(); i++)
{
auto clientInfo = static_cast<ClientInfo*>(
clientInfoEntities[i]->getComponent(ComponentType::ClientInfo));

if (clientInfo->id == id)
m_world->deleteEntity(clientInfoEntities[i]);
}
sendDisconnectPacket(id);
//for (unsigned int index = 0; index < p_entities.size(); index++)
//{
// NetworkSynced* netSync = static_cast<NetworkSynced*>(
// m_world->getComponentManager()->getComponent( p_entities[index],
// ComponentType::NetworkSynced ) );
//}

//// If a client has disconnected, then the clientinfo should be removed?
//auto clientInfoSys = static_cast<ServerClientInfoSystem*>(
// m_world->getSystem(SystemType::ServerClientInfoSystem));
//vector<Entity*> clientInfoEntities = clientInfoSys->getActiveEntities();
//for (unsigned int i = 0; i < clientInfoEntities.size(); i++)
//{
// auto clientInfo = static_cast<ClientInfo*>(
// clientInfoEntities[i]->getComponent(ComponentType::ClientInfo));

// if (clientInfo->id == id)
// m_world->deleteEntity(clientInfoEntities[i]);
//}
}

/************************************************************************/
Expand Down Expand Up @@ -119,7 +121,8 @@ void ServerWelcomeSystem::sendWelcomePacket(int p_newlyConnectedClientId)
// Give the new client its Network Identity.
WelcomePacket welcomePacket;
welcomePacket.clientNetworkIdentity = p_newlyConnectedClientId;
welcomePacket.playerID = m_playerSystem->getActiveEntities().size();
//welcomePacket.playerID = m_playerSystem->getActiveEntities().size();
welcomePacket.playerID = m_playerSystem->createPlayerId(p_newlyConnectedClientId);
m_server->unicastPacket( welcomePacket.pack(), p_newlyConnectedClientId );
}

Expand All @@ -139,4 +142,11 @@ void ServerWelcomeSystem::sendBrodcastAllPlayers()
connectedPacket.playerName = playerComps.at(i)->m_playerName;
m_server->broadcastPacket( connectedPacket.pack() );
}
}
}

void ServerWelcomeSystem::sendDisconnectPacket( int p_newlyDisconnectClientId )
{
DisconnectPacket dcPacket;
dcPacket.clientNetworkIdentity = p_newlyDisconnectClientId;
// dcPacket.playerID = m_playerSystem->get
}
1 change: 1 addition & 0 deletions Projects/Gameplay/Src/ServerWelcomeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ServerWelcomeSystem: public EntitySystem
void sendBrodcastAllPlayers();
private:
void sendWelcomePacket(int p_newlyConnectedClientId);
void sendDisconnectPacket(int p_newlyDisconnectClientId);
void createClientInfoEntity(int p_newlyConnectedClientId);
private:
TcpServer* m_server;
Expand Down
13 changes: 11 additions & 2 deletions Projects/Util/Src/UniqueIndexList.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class UniqueIndexList
T at(unsigned int p_index);
T operator[](unsigned int p_index);

bool hasValue(unsigned int p_index);

unsigned int getSize();

void clear();
Expand All @@ -35,8 +37,6 @@ class UniqueIndexList
stack<unsigned int> m_freeIndices;
};



template<class T>
unsigned int UniqueIndexList<T>::add(T p_valueRef)
{
Expand Down Expand Up @@ -79,6 +79,15 @@ T UniqueIndexList<T>::operator[](unsigned int p_index)
return m_list[p_index];
}

template<class T>
bool UniqueIndexList<T>::hasValue( unsigned int p_index )
{
if (p_index < m_list.size())
return m_list[p_index] != NULL;
else
return false;
}

template<class T>
unsigned int UniqueIndexList<T>::getSize()
{
Expand Down

0 comments on commit 330b9b6

Please sign in to comment.