GameServer - Added inititiate protocols

This commit is contained in:
dean11 2014-02-18 11:34:24 +01:00
parent 5b4758d2ae
commit f6f183d6e7
13 changed files with 311 additions and 159 deletions

View File

@ -5,6 +5,7 @@
#define DANBIASSERVER_CLIENT_OBJECT_H #define DANBIASSERVER_CLIENT_OBJECT_H
#include <NetworkClient.h> #include <NetworkClient.h>
#include <NetworkSession.h>
#include <PostBox\PostBox.h> #include <PostBox\PostBox.h>
#include <GameAPI.h> #include <GameAPI.h>
#include <Utilities.h> #include <Utilities.h>
@ -17,22 +18,21 @@ namespace DanBias
class GameClient class GameClient
{ {
public: public:
GameClient(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client, GameLogic::IPlayerData* player); GameClient(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> nwClient);
virtual~GameClient(); virtual~GameClient();
GameLogic::IPlayerData* GetPlayer();
GameLogic::IPlayerData* ReleasePlayer();
Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> GetClient();
Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> ReleaseClient();
inline bool operator==(const GameLogic::IPlayerData* c) { return (this->player) ? (c->GetID() == this->player->GetID()) : false ; } inline bool operator==(const GameLogic::IPlayerData* c) { return (this->player) ? (c->GetID() == this->player->GetID()) : false ; }
inline bool operator==(const Oyster::Network::NetworkClient* c) { return (c->GetID() == this->GetID()); } inline bool operator==(const Oyster::Network::NetworkClient* c) { return (c->GetID() == this->client->GetID()); }
inline float GetSinceLastResponse() const { return this->secondsSinceLastResponse; } inline bool Equals(const GameLogic::IPlayerData* c) { return (this->player) ? (c->GetID() == this->player->GetID()) : false ; }
inline std::wstring GetAlias() const { return this->alias; } inline bool Equals(const Oyster::Network::NetworkClient* c) { return (c->GetID() == this->client->GetID()); }
inline std::wstring GetCharacter() const { return this->character; }
inline bool IsReady() const { return this->isReady; } inline float GetSinceLastResponse() const { return this->secondsSinceLastResponse; }
inline GameLogic::IPlayerData* GetPlayer() const { return this->player; } inline std::wstring GetAlias() const { return this->alias; }
inline std::wstring GetCharacter() const { return this->character; }
inline bool IsReady() const { return this->isReady; }
inline GameLogic::IPlayerData* GetPlayer() const { return this->player; }
Oyster::Network::NetClient GetClient() const { return this->client; }
void SetPlayer(GameLogic::IPlayerData* player); void SetPlayer(GameLogic::IPlayerData* player);
@ -41,6 +41,13 @@ namespace DanBias
void SetCharacter(std::wstring character); void SetCharacter(std::wstring character);
void SetSinceLastResponse(float seconds); void SetSinceLastResponse(float seconds);
GameLogic::IPlayerData* ReleasePlayer();
Oyster::Network::NetClient ReleaseClient();
//NetworkSpecific
void SetOwner(Oyster::Network::NetworkSession* owner);
void UpdateClient();
private: private:
GameLogic::IPlayerData* player; GameLogic::IPlayerData* player;
Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client; Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client;
@ -52,4 +59,7 @@ namespace DanBias
std::wstring character; std::wstring character;
}; };
}//End namespace DanBias }//End namespace DanBias
typedef Utility::DynamicMemory::SmartPointer<DanBias::GameClient> gClient;
#endif // !DANBIASSERVER_CLIENT_OBJECT_H #endif // !DANBIASSERVER_CLIENT_OBJECT_H

View File

@ -15,11 +15,18 @@ namespace DanBias
{ {
struct LobbyLevelData struct LobbyLevelData
{ {
int mapNumber;
int maxClients; int maxClients;
int gameMode; int gameTimeInMinutes;
int gameTime; std::wstring gameMode;
std::string gameName; std::wstring mapName;
std::wstring gameName;
LobbyLevelData()
: maxClients(10)
, gameTimeInMinutes(10)
, gameMode(L"unknown")
, mapName(L"unknown")
, gameName(L"unknown")
{ }
}; };
class GameLobby :public Oyster::Network::NetworkSession class GameLobby :public Oyster::Network::NetworkSession
{ {
@ -49,6 +56,8 @@ namespace DanBias
private: private:
void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override; void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override;
void ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client) override; void ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client) override;
void ProcessClients() override;
bool Attach(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client) override;
private: private:
//Utility::WinTimer timer; //Utility::WinTimer timer;
@ -58,7 +67,7 @@ namespace DanBias
GameSession gameSession; GameSession gameSession;
LobbyLevelData description; LobbyLevelData description;
Utility::DynamicMemory::SmartPointer<DanBias::GameClient> sessionOwner; Utility::DynamicMemory::SmartPointer<DanBias::GameClient> sessionOwner;
Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<GameClient>> gClients;
}; };
}//End namespace DanBias }//End namespace DanBias
#endif // !DANBIASGAME_GAMELOBBY_H #endif // !DANBIASGAME_GAMELOBBY_H

View File

@ -55,15 +55,18 @@ namespace DanBias
static void NotifyWhenClientConnect(ClientConnectedNotify func); static void NotifyWhenClientConnect(ClientConnectedNotify func);
static void NotifyWhenClientDisconnect(ClientDisconnectedNotify func); static void NotifyWhenClientDisconnect(ClientDisconnectedNotify func);
static void GameSetMapName(const wchar_t* val);
static void GameSetMaxClients(const int& val);
static void GameSetGameMode(const wchar_t* val);
static void GameSetGameTime(const int& val); static void GameSetGameTime(const int& val);
static int GameGetMapId(); static void GameSetMaxClients(const int& val);
static int GameGetMaxClients(); static void GameSetGameName(const wchar_t* val);
static int GameGetGameMode(); static void GameSetMapName(const wchar_t* val);
static void GameSetGameMode(const wchar_t* val);
static int GameGetGameTime(); static int GameGetGameTime();
static const char* GameGetGameName(); static int GameGetMaxClients();
static const wchar_t* GameGetGameMode();
static const wchar_t* GameGetGameName();
static const wchar_t* GameGetMapName();
static bool GameStart(); static bool GameStart();

View File

@ -32,11 +32,11 @@ namespace DanBias
struct GameDescription struct GameDescription
{ {
unsigned int maxClients; unsigned int maxClients;
std::string mapName; std::wstring mapName;
std::string gameMode; std::wstring gameMode;
int gameTimeMinutes; int gameTimeMinutes;
Oyster::Network::NetworkSession* owner; Oyster::Network::NetworkSession* owner;
Utility::DynamicMemory::DynamicArray<Oyster::Network::NetClient> clients; Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<GameClient>> clients;
}; };
public: public:
@ -52,17 +52,19 @@ namespace DanBias
/** Join an existing/running game session /** Join an existing/running game session
* @param client The client to attach to the session * @param client The client to attach to the session
*/ */
bool Attach(Oyster::Network::NetClient client) override; bool Join(gClient client);
void CloseSession( bool dissconnectClients ) override;
//void CloseSession( bool dissconnectClients ) override;
inline bool IsCreated() const { return this->isCreated; } inline bool IsCreated() const { return this->isCreated; }
inline bool IsRunning() const { return this->isRunning; } inline bool IsRunning() const { return this->isRunning; }
operator bool() { return (this->isCreated && this->isCreated); } operator bool() { return (this->isCreated && this->isRunning); }
//Private member functions //Private member functions
private: private:
// Client event callback function // Client event callback function
void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override; void ClientEventCallback(Oyster::Network::NetEvent<Oyster::Network::NetworkClient*, Oyster::Network::NetworkClient::ClientEventArgs> e) override;
void ProcessClients() override;
//Sends a client to the owner, if param is NULL then all clients is sent //Sends a client to the owner, if param is NULL then all clients is sent
void SendToOwner(DanBias::GameClient* obj); void SendToOwner(DanBias::GameClient* obj);
@ -98,8 +100,8 @@ namespace DanBias
//Private member variables //Private member variables
private: private:
Utility::DynamicMemory::DynamicArray<Utility::DynamicMemory::SmartPointer<GameClient>> clients; Utility::DynamicMemory::DynamicArray<gClient> gClients;
Utility::DynamicMemory::SmartPointer<DanBias::GameClient> sessionOwner; gClient sessionOwner;
Oyster::Thread::OysterThread worker; Oyster::Thread::OysterThread worker;
GameLogic::GameAPI& gameInstance; GameLogic::GameAPI& gameInstance;
GameLogic::ILevelData *levelData; GameLogic::ILevelData *levelData;
@ -115,6 +117,7 @@ namespace DanBias
//TODO: Remove this uggly hax //TODO: Remove this uggly hax
static GameSession* gameSession; static GameSession* gameSession;
};//End GameSession };//End GameSession
}//End namespace DanBias }//End namespace DanBias
#endif // !DANBIASSERVER_GAME_SESSION_H #endif // !DANBIASSERVER_GAME_SESSION_H

View File

@ -12,8 +12,9 @@ using namespace DanBias;
using namespace GameLogic; using namespace GameLogic;
GameClient::GameClient() GameClient::GameClient(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> nwClient)
{ {
this->client = nwClient;
this->player = 0; this->player = 0;
isReady = false; isReady = false;
this->character = L"Unknown"; this->character = L"Unknown";
@ -51,3 +52,25 @@ void GameClient::SetCharacter(std::wstring character)
} }
void GameClient::SetOwner(Oyster::Network::NetworkSession* owner)
{
this->client->SetOwner(owner);
}
void GameClient::UpdateClient()
{
this->client->Update();
}
IPlayerData* GameClient::ReleasePlayer()
{
IPlayerData* temp = this->player;
this->player = 0;
return temp;
}
NetClient GameClient::ReleaseClient()
{
NetClient temp = this->client;
this->client = 0;
return temp;
}

View File

@ -14,7 +14,7 @@ using namespace GameLogic;
using namespace DanBias; using namespace DanBias;
GameLobby::GameLobby() GameLobby::GameLobby()
{ } { }
GameLobby::~GameLobby() GameLobby::~GameLobby()
{ {
this->clients.Clear(); this->clients.Clear();
@ -37,16 +37,33 @@ void GameLobby::Update()
void GameLobby::SetGameDesc(const LobbyLevelData& desc) void GameLobby::SetGameDesc(const LobbyLevelData& desc)
{ {
this->description.gameMode = desc.gameMode; this->description.gameMode = desc.gameMode;
this->description.gameTime = desc.gameTime; this->description.gameName = desc.gameName;
this->description.mapNumber = desc.mapNumber; this->description.mapName = desc.mapName;
this->description.gameTimeInMinutes = desc.gameTimeInMinutes;
this->description.maxClients = desc.maxClients; this->description.maxClients = desc.maxClients;
if(this->gClients.Size() > (unsigned int)desc.maxClients)
{
//Kick overflow
for (unsigned int i = (unsigned int)desc.maxClients - 1; i < this->gClients.Size(); i++)
{
if(this->gClients[i])
{
this->gClients[i]->GetClient()->Disconnect();
}
}
}
this->gClients.Resize((unsigned int)desc.maxClients);
} }
void GameLobby::GetGameDesc(LobbyLevelData& desc) void GameLobby::GetGameDesc(LobbyLevelData& desc)
{ {
desc.gameMode = this->description.gameMode; desc.gameTimeInMinutes = this->description.gameTimeInMinutes;
desc.gameTime = this->description.gameTime;
desc.mapNumber = this->description.mapNumber;
desc.maxClients = this->description.maxClients; desc.maxClients = this->description.maxClients;
desc.mapName = this->description.mapName;
desc.gameName = this->description.gameName;
desc.gameMode = this->description.gameMode;
} }
bool GameLobby::StartGameSession( ) bool GameLobby::StartGameSession( )
{ {
@ -56,10 +73,10 @@ bool GameLobby::StartGameSession( )
GameSession::GameDescription desc; GameSession::GameDescription desc;
desc.maxClients = this->description.maxClients; desc.maxClients = this->description.maxClients;
desc.gameMode = this->description.gameMode; desc.gameMode = this->description.gameMode;
desc.gameTimeMinutes = this->description.gameTime; desc.gameTimeMinutes = this->description.gameTimeInMinutes;
//desc.mapName = this->description.mapNumber; //desc.mapName = this->description.mapNumber;
desc.owner = this; desc.owner = this;
desc.clients = this->clients; desc.clients = this->gClients;
if(desc.gameTimeMinutes == 0) if(desc.gameTimeMinutes == 0)
desc.gameTimeMinutes = 10; //note: should be fetched from somewhere. desc.gameTimeMinutes = 10; //note: should be fetched from somewhere.
@ -67,7 +84,7 @@ bool GameLobby::StartGameSession( )
if(desc.maxClients == 0) if(desc.maxClients == 0)
desc.maxClients = 10; //note: should be fetched somewhere else.. desc.maxClients = 10; //note: should be fetched somewhere else..
this->clients.Clear(); //Remove clients from lobby list this->gClients.Clear(); //Remove clients from lobby list
if(this->gameSession.Create(desc)) if(this->gameSession.Create(desc))
{ {
@ -96,8 +113,8 @@ void GameLobby::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::Clie
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
e.sender->Disconnect(); e.sender->Disconnect();
this->readyList.Remove(e.sender); //this->readyList.Remove(e.sender);
this->clients.Remove(e.sender); //this->gClients.Remove(e.sender);
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
@ -111,21 +128,30 @@ void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster
if(this->gameSession) if(this->gameSession)
{ {
Attach(client); if(!this->Attach(client))
{
client->Disconnect();
}
} }
else else
{ {
Attach(client); if(!this->Attach(client))
{
//Send message that lobby full
client->Disconnect();
return;
}
Protocol_LobbyClientData p1; Protocol_LobbyClientData p1;
Protocol_LobbyGameData p2; Protocol_LobbyGameData p2;
for (unsigned int i = 0; i < this->clients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(this->clients[i]) if(this->gClients[i])
{ {
Protocol_LobbyClientData::PlayerData t; Protocol_LobbyClientData::PlayerData t;
t.id = this->clients[i]->GetID(); t.id = client->GetID();
t.ip = this->clients[i]->GetIpAddress(); t.ip = client->GetIpAddress();
t.team = 0; t.team = 0;
t.name = "Dennis är kung tycker Erik!"; t.name = "Dennis är kung tycker Erik!";
p1.list.Push(t); p1.list.Push(t);
@ -139,4 +165,42 @@ void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster
client->Send(p2.GetProtocol()); client->Send(p2.GetProtocol());
} }
} }
void GameLobby::ProcessClients()
{
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(this->gClients[i])
{
this->gClients[i]->UpdateClient();
}
}
}
bool GameLobby::Attach(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client)
{
if(this->clientCount = this->description.maxClients) return false;
bool added = false;
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(!this->gClients[i])
{
added = true;
this->gClients[i] = new GameClient(client);
}
}
if(!added)
{
this->gClients.Push(new GameClient(client));
}
return true;
}

View File

@ -116,27 +116,34 @@ void GameLobby::LobbyReady(GameLogic::Protocol_LobbyClientReadyState& p, Oyster:
} }
void GameLobby::LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyster::Network::NetworkClient* c) void GameLobby::LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyster::Network::NetworkClient* c)
{ {
NetClient temp; if(this->gameSession)
bool found = false;
//find client in waiting list
for (unsigned int i = 0; !found && i < this->clients.Size(); i++)
{ {
if(this->clients[i]->GetID() == c->GetID()) gClient temp;
bool found = false;
//find client in waiting list
for (unsigned int i = 0; !found && i < this->clients.Size(); i++)
{ {
temp = this->clients[i]; if(this->gClients[i]->GetClient()->GetID() == c->GetID())
found = true; {
temp = this->gClients[i];
found = true;
}
} }
}
//Something is wrong //Something is wrong
if(!found) if(!found)
{ {
c->Disconnect(); c->Disconnect();
}
else
{
//Send game data
this->gameSession.Join(temp);
}
} }
else else
{ {
//Send game data
this->gameSession.Attach(temp);
} }
} }

View File

@ -118,65 +118,74 @@ void GameServerAPI::NotifyWhenClientDisconnect(ClientDisconnectedNotify func)
else clientDisconnectedCallback = func; else clientDisconnectedCallback = func;
} }
void GameServerAPI::GameSetMapName(const wchar_t* val) void GameServerAPI::GameSetMapName(const wchar_t* val)
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
//d.mapNumber = val; //TODO: implement d.mapName = val;
lobby.SetGameDesc(d); lobby.SetGameDesc(d);
} }
void GameServerAPI::GameSetMaxClients(const int& val) void GameServerAPI::GameSetGameMode(const wchar_t* val)
{
LobbyLevelData d;
lobby.GetGameDesc(d);
d.gameMode = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetGameName(const wchar_t* val)
{
LobbyLevelData d;
lobby.GetGameDesc(d);
d.gameName = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetMaxClients(const int& val)
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
d.maxClients = val; d.maxClients = val;
lobby.SetGameDesc(d); lobby.SetGameDesc(d);
} }
void GameServerAPI::GameSetGameMode(const wchar_t* val) void GameServerAPI::GameSetGameTime(const int& val)
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
//d.gameMode = val; //TODO: implement d.gameTimeInMinutes = val;
lobby.SetGameDesc(d); lobby.SetGameDesc(d);
} }
void GameServerAPI::GameSetGameTime(const int& val)
const wchar_t* GameServerAPI::GameGetMapName()
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
d.gameTime = val; return d.mapName.c_str();
lobby.SetGameDesc(d);
} }
int GameServerAPI::GameGetMapId() int GameServerAPI::GameGetMaxClients()
{
LobbyLevelData d;
lobby.GetGameDesc(d);
return d.mapNumber;
}
int GameServerAPI::GameGetMaxClients()
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
return d.maxClients; return d.maxClients;
} }
int GameServerAPI::GameGetGameMode() const wchar_t* GameServerAPI::GameGetGameMode()
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
return d.gameMode; return d.gameMode.c_str();
} }
int GameServerAPI::GameGetGameTime() int GameServerAPI::GameGetGameTime()
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
return d.gameTime; return d.gameTimeInMinutes;
} }
const char* GameServerAPI::GameGetGameName() const wchar_t* GameServerAPI::GameGetGameName()
{ {
LobbyLevelData d; LobbyLevelData d;
lobby.GetGameDesc(d); lobby.GetGameDesc(d);
return d.gameName.c_str(); return d.gameName.c_str();
} }
bool GameServerAPI::GameStart()
bool GameServerAPI::GameStart()
{ {
if(lobby.StartGameSession()) if(lobby.StartGameSession())
{ {

View File

@ -43,9 +43,9 @@ using namespace DanBias;
{ {
int temp = -1; int temp = -1;
//Find the idiot //Find the idiot
for (unsigned int i = 0; i < this->clients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(this->clients[i]->Equals(e.sender)) if(this->gClients[i]->Equals(e.sender))
{ {
temp = i; temp = i;
} }
@ -56,7 +56,7 @@ using namespace DanBias;
this->Detach(e.sender)->Disconnect(); this->Detach(e.sender)->Disconnect();
return; return;
} }
SmartPointer<GameClient> cl = this->clients[temp]; SmartPointer<GameClient> cl = this->gClients[temp];
switch (e.args.type) switch (e.args.type)
{ {
@ -70,15 +70,21 @@ using namespace DanBias;
break; break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str()); printf("\t(%i : %s) - EventType_ProtocolRecieved\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str());
testID = 2;
if(cl->GetPlayer()->GetID() == testID)//TODO: TEST
{
testTimer.reset();
}
this->ParseProtocol(e.args.data.protocol, cl); this->ParseProtocol(e.args.data.protocol, cl);
break; break;
} }
} }
void GameSession::ProcessClients()
{
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(this->gClients[i])
{
this->gClients[i]->UpdateClient();
}
}
}
void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) void GameSession::ObjectMove(GameLogic::IObjectData* movedObject)
{ {

View File

@ -3,6 +3,7 @@
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
#include "..\GameSession.h" #include "..\GameSession.h"
#include "..\GameClient.h" #include "..\GameClient.h"
#include "..\GameLobby.h"
#include <Protocols.h> #include <Protocols.h>
#include <PostBox\PostBox.h> #include <PostBox\PostBox.h>
#include <GameLogicStates.h> #include <GameLogicStates.h>
@ -62,9 +63,16 @@ bool GameSession::Create(GameDescription& desc)
if(this->isCreated) return false; if(this->isCreated) return false;
/* standard initialization of some data */ /* standard initialization of some data */
NetworkSession::clients = desc.clients; this->gClients.Resize((unsigned int)desc.maxClients);
NetworkSession::clients.Resize((unsigned int)desc.maxClients); for (unsigned int i = 0; i < desc.clients.Size(); i++)
this->clients.Resize((unsigned int)desc.maxClients); {
if(desc.clients[i])
{
this->clientCount++;
this->gClients[i] = desc.clients[i];
this->gClients[i]->SetOwner(this);
}
}
this->owner = desc.owner; this->owner = desc.owner;
/* Initiate the game instance */ /* Initiate the game instance */
@ -75,14 +83,13 @@ bool GameSession::Create(GameDescription& desc)
/* Create the players in the game instance */ /* Create the players in the game instance */
GameLogic::IPlayerData* p = 0; GameLogic::IPlayerData* p = 0;
for (unsigned int i = 0; i < desc.clients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(desc.clients[i]) if(this->gClients[i])
{ {
if( (p = this->gameInstance.CreatePlayer()) ) if( (p = this->gameInstance.CreatePlayer()) )
{ {
desc.clients[i]->SetOwner(this); this->gClients[i]->SetPlayer(p);
this->clients[i] = (new GameClient(desc.clients[i], p));
} }
else else
{ {
@ -129,15 +136,15 @@ void GameSession::Run()
void GameSession::ThreadEntry( ) void GameSession::ThreadEntry( )
{ {
//List with clients that we are waiting on.. //List with clients that we are waiting on..
DynamicArray<SmartPointer<GameClient>> readyList;// = this->clients; DynamicArray<gClient> readyList;// = this->clients;
//First we need to clean invalid clients, if any, and tell them to start loading game data //First we need to clean invalid clients, if any, and tell them to start loading game data
for (unsigned int i = 0; i < this->clients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(this->clients[i]) if(this->gClients[i])
{ {
readyList.Push(this->clients[i]); readyList.Push(this->gClients[i]);
Protocol_LobbyCreateGame p((char)1, (char)0, this->description.mapName); Protocol_LobbyCreateGame p((char)1, (char)0, Utility::String::WStringToString(this->description.mapName, std::string()));
readyList[readyList.Size() - 1]->GetClient()->Send(p); readyList[readyList.Size() - 1]->GetClient()->Send(p);
} }
} }
@ -153,13 +160,13 @@ void GameSession::ThreadEntry( )
if(readyList[i] && readyList[i]->IsReady()) if(readyList[i] && readyList[i]->IsReady())
{ {
//Need to send information about other players, to all players //Need to send information about other players, to all players
for (unsigned int k = 0; k < this->clients.Size(); k++) for (unsigned int k = 0; k < this->gClients.Size(); k++)
{ {
if((this->clients[k] && readyList[i]) && readyList[i]->GetClient()->GetID() != this->clients[k]->GetClient()->GetID()) if((this->gClients[k] && readyList[i]) && readyList[i]->GetClient()->GetID() != this->gClients[k]->GetClient()->GetID())
{ {
IPlayerData* pl = this->clients[k]->GetPlayer(); IPlayerData* pl = this->gClients[k]->GetPlayer();
Protocol_ObjectCreatePlayer p( pl->GetPosition(), pl->GetRotation(), pl->GetScale(), Protocol_ObjectCreatePlayer p( pl->GetPosition(), pl->GetRotation(), pl->GetScale(),
pl->GetID(), true, this->clients[k]->GetPlayer()->GetTeamID(), pl->GetID(), true, this->gClients[k]->GetPlayer()->GetTeamID(),
/*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan"); /*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan");
readyList[i]->GetClient()->Send(p); readyList[i]->GetClient()->Send(p);
} }
@ -173,32 +180,31 @@ void GameSession::ThreadEntry( )
} }
//Sync with clients before starting countdown //Sync with clients before starting countdown
for (unsigned int i = 0; i < this->gClients.Size(); i++)
for (unsigned int i = 0; i < this->clients.Size(); i++)
{ {
if(this->clients[i]) if(this->gClients[i])
{ {
this->clients[i]->GetClient()->Send(GameLogic::Protocol_LobbyStartGame(5)); this->gClients[i]->GetClient()->Send(GameLogic::Protocol_LobbyStartGame(5.0f));
} }
} }
} }
bool GameSession::Attach(Utility::DynamicMemory::SmartPointer<NetworkClient> networkClient) bool GameSession::Join(gClient gameClient)
{ {
if(!this->isCreated) return false; if(!this->isCreated) return false;
if(this->GetClientCount() == this->clients.Capacity()) return false; if(this->GetClientCount() == this->gClients.Capacity()) return false;
networkClient->SetOwner(this); gameClient->SetOwner(this);
IPlayerData* playerData = this->gameInstance.CreatePlayer(); IPlayerData* playerData = this->gameInstance.CreatePlayer();
if(!playerData) return false; if(!playerData) return false;
SmartPointer<GameClient> gameClient = new GameClient(networkClient, playerData); gameClient->SetPlayer(playerData);
NetworkClient* nwClient = gameClient->GetClient(); NetworkClient* nwClient = gameClient->GetClient();
// Send the level information // Send the level information
{ {
Protocol_LobbyCreateGame lcg((char)1, (char)0, this->description.mapName); Protocol_LobbyCreateGame lcg((char)1, (char)0, Utility::String::WStringToString(this->description.mapName, std::string()));
nwClient->Send(lcg); nwClient->Send(lcg);
} }
@ -212,11 +218,11 @@ bool GameSession::Attach(Utility::DynamicMemory::SmartPointer<NetworkClient> net
// Send information about other clients // Send information about other clients
{ {
for (unsigned int i = 0; i < this->clients.Size(); i++) for (unsigned int i = 0; i < this->gClients.Size(); i++)
{ {
if(clients[i]) if(this->gClients[i])
{ {
IPlayerData* temp = clients[i]->GetPlayer(); IPlayerData* temp = this->gClients[i]->GetPlayer();
Protocol_ObjectCreatePlayer oc( temp->GetPosition(), temp->GetRotation(), temp->GetScale(), Protocol_ObjectCreatePlayer oc( temp->GetPosition(), temp->GetRotation(), temp->GetScale(),
temp->GetID(), false, temp->GetTeamID(), temp->GetID(), false, temp->GetTeamID(),
/*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan"); /*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan");
@ -227,40 +233,44 @@ bool GameSession::Attach(Utility::DynamicMemory::SmartPointer<NetworkClient> net
//TODO: Need to be able to get the current gameplay data from the logic, to sync it with the client //TODO: Need to be able to get the current gameplay data from the logic, to sync it with the client
{ {
} }
// Insert the new client to the update list // Insert the new client to the update list
bool added = false;
{ {
bool added = false; for (unsigned int i = 0; !added && i < this->gClients.Size(); i++)
for (unsigned int i = 0; !added && i < this->clients.Size(); i++)
{ {
if(!clients[i]) if(!this->gClients[i])
{ {
NetworkSession::clients[i] = networkClient; this->gClients[i] = gameClient;
clients[i] = gameClient; // Send the start signal
{
nwClient->Send(GameLogic::Protocol_LobbyStartGame(0));
}
added = true; added = true;
this->clientCount++;
} }
} }
if(!added)
{
NetworkSession::clients.Push( networkClient );
clients.Push( gameClient );
}
}
// Send the start signal
{
nwClient->Send(GameLogic::Protocol_LobbyStartGame(0));
} }
return true; return added;
} }
void GameSession::CloseSession( bool dissconnectClients ) //DynamicArray<gClient> GameSession::CloseSession( bool dissconnectClients )
{ //{
this->worker.Terminate(); // this->worker.Terminate();
NetworkSession::CloseSession(true); // //TODO: Send clients to lobby
this->clients.Clear(); //
} // //for (unsigned int i = 0; i < this->gClients.Size(); i++)
// //{
// // if(this->gClients[i])
// // {
// // ((GameLobby*)this->owner)-> this->gClients[i]
// // }
// //}
//
// this->gClients.Clear();
//}

View File

@ -72,26 +72,32 @@ void StandaloneGameServerCLI::GameSetMapName(String^ value)
pin_ptr<const wchar_t> wch = PtrToStringChars(value); pin_ptr<const wchar_t> wch = PtrToStringChars(value);
DanBias::GameServerAPI::GameSetMapName(wch); DanBias::GameServerAPI::GameSetMapName(wch);
} }
void StandaloneGameServerCLI::GameSetGameMode(String^ value)
{
pin_ptr<const wchar_t> wch = PtrToStringChars(value);
DanBias::GameServerAPI::GameSetGameMode(wch);
}
void StandaloneGameServerCLI::GameSetGameName(String^ value)
{
pin_ptr<const wchar_t> wch = PtrToStringChars(value);
DanBias::GameServerAPI::GameSetGameName(wch);
}
void StandaloneGameServerCLI::GameSetMaxClients(const int val) void StandaloneGameServerCLI::GameSetMaxClients(const int val)
{ {
DanBias::GameServerAPI::GameSetMaxClients(val); DanBias::GameServerAPI::GameSetMaxClients(val);
} }
void StandaloneGameServerCLI::GameSetGameMode(String^ value)
{
pin_ptr<const wchar_t> wch = PtrToStringChars(value);
DanBias::GameServerAPI::GameSetGameMode(wch);
}
void StandaloneGameServerCLI::GameSetGameTime(const int val) void StandaloneGameServerCLI::GameSetGameTime(const int val)
{ {
DanBias::GameServerAPI::GameSetGameTime(val); DanBias::GameServerAPI::GameSetGameTime(val);
} }
int StandaloneGameServerCLI::GameGetMapId() String^ StandaloneGameServerCLI::GameGetMapName()
{ {
return DanBias::GameServerAPI::GameGetMapId(); return gcnew String( DanBias::GameServerAPI::GameGetMapName());
} }
int StandaloneGameServerCLI::GameGetMaxClients() int StandaloneGameServerCLI::GameGetMaxClients()
@ -99,9 +105,9 @@ int StandaloneGameServerCLI::GameGetMaxClients()
return DanBias::GameServerAPI::GameGetMaxClients(); return DanBias::GameServerAPI::GameGetMaxClients();
} }
int StandaloneGameServerCLI::GameGetGameMode() String^ StandaloneGameServerCLI::GameGetGameMode()
{ {
return DanBias::GameServerAPI::GameGetGameMode(); return gcnew String( DanBias::GameServerAPI::GameGetGameMode());
} }
int StandaloneGameServerCLI::GameGetGameTime() int StandaloneGameServerCLI::GameGetGameTime()
@ -111,7 +117,7 @@ int StandaloneGameServerCLI::GameGetGameTime()
String^ StandaloneGameServerCLI::GameGetGameName() String^ StandaloneGameServerCLI::GameGetGameName()
{ {
return gcnew String(DanBias::GameServerAPI::GameGetGameName()); return gcnew String( DanBias::GameServerAPI::GameGetGameName());
} }
bool StandaloneGameServerCLI::GameStart() bool StandaloneGameServerCLI::GameStart()

View File

@ -47,13 +47,15 @@ namespace System { namespace Windows { namespace Interop
bool ServerIsRunning(); bool ServerIsRunning();
void GameSetMapName(String^ val); void GameSetMapName(String^ val);
void GameSetMaxClients(const int val);
void GameSetGameMode(String^ val); void GameSetGameMode(String^ val);
void GameSetGameName(String^ val);
void GameSetMaxClients(const int val);
void GameSetGameTime(const int val); void GameSetGameTime(const int val);
int GameGetMapId();
int GameGetMaxClients(); int GameGetMaxClients();
int GameGetGameMode();
int GameGetGameTime(); int GameGetGameTime();
System::String^ GameGetMapName();
System::String^ GameGetGameMode();
System::String^ GameGetGameName(); System::String^ GameGetGameName();
bool GameStart(); bool GameStart();
}; };

View File

@ -98,9 +98,9 @@ namespace Oyster
protected: protected:
NetClientList clients; NetClientList clients;
private:
int clientCount; int clientCount;
private:
struct PrivateSessionData; struct PrivateSessionData;
PrivateSessionData* data; PrivateSessionData* data;
}; };