Danbias/Code/Game/GameServer/Implementation/GameSession_General.cpp

194 lines
4.6 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
2014-01-28 09:00:02 +01:00
#include "..\GameSession.h"
#include "..\GameClient.h"
#include <Protocols.h>
#include <PostBox\PostBox.h>
#include <GameLogicStates.h>
2014-01-29 10:18:01 +01:00
#define NOMINMAX
#include <Windows.h>
#include <Queue.h>
using namespace Utility::DynamicMemory;
using namespace Oyster;
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace GameLogic;
namespace DanBias
{
GameSession* GameSession::gameSession = nullptr;
2014-01-30 00:19:00 +01:00
GameSession::GameSession()
2014-01-20 15:47:52 +01:00
:gameInstance(GameAPI::Instance())
{
this->owner = 0;
this->isCreated = false;
this->isRunning = false;
this->gameSession = this;
2014-01-31 22:52:52 +01:00
this->logicDeltaTime = 0.0f;
2014-01-30 00:19:00 +01:00
memset(&this->description, 0, sizeof(GameDescription));
}
GameSession::~GameSession()
{
2014-01-29 10:18:01 +01:00
this->worker.Terminate();
this->clients.Clear();
this->gameInstance;
this->owner = 0;
2014-01-29 10:18:01 +01:00
this->isCreated = false;
this->isRunning = false;
}
bool GameSession::Create(GameDescription& desc)
{
2014-01-30 00:19:00 +01:00
this->description = desc;
2014-01-20 15:47:52 +01:00
/* Do some error checking */
if(desc.clients.Size() == 0) return false;
if(!desc.owner) return false;
if(this->isCreated) return false;
2014-01-20 15:47:52 +01:00
/* standard initialization of some data */
NetworkSession::clients = desc.clients;
this->clients.Resize(desc.clients.Size());
this->owner = desc.owner;
2014-01-20 15:47:52 +01:00
/* Initiate the game instance */
if(!this->gameInstance.Initiate())
{
printf("Failed to initiate the game instance\n");
}
/* Create the game level */
if(!(this->levelData = this->gameInstance.CreateLevel()))
{
printf("Level not created!");
return false;
}
/* Create the players in the game instance */
GameLogic::IPlayerData* p = 0;
for (unsigned int i = 0; i < desc.clients.Size(); i++)
{
2014-01-20 15:47:52 +01:00
if( (p = this->gameInstance.CreatePlayer()) )
{
desc.clients[i]->SetOwner(this);
2014-01-29 10:18:01 +01:00
this->clients[i] = new GameClient(desc.clients[i], p);
2014-01-20 15:47:52 +01:00
}
else
{
printf("Failed to create player (%i)\n", i);
}
}
2014-01-20 15:47:52 +01:00
/* Create the worker thread */
2014-01-29 10:18:01 +01:00
if(this->worker.Create(this, false) != OYSTER_THREAD_ERROR_SUCCESS)
2014-01-20 15:47:52 +01:00
return false;
2014-01-20 15:47:52 +01:00
this->worker.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_3);
2014-01-29 10:18:01 +01:00
/* Set some game instance data options */
2014-01-20 15:47:52 +01:00
this->gameInstance.SetSubscription(GameLogic::GameEvent::ObjectEventFunctionType_OnMove, GameSession::ObjectMove);
//this->gameInstance.SetSubscription(GameLogic::GameEvent::ObjectEventFunctionType_OnDead, GameSession::ObjectDead);
this->description.clients.Clear();
2014-01-20 15:47:52 +01:00
this->isCreated = true;
return this->isCreated;
}
void GameSession::Run()
{
if(this->isRunning) return;
if(this->clients.Size() > 0)
{
2014-01-29 10:18:01 +01:00
this->worker.Start();
2014-01-20 15:47:52 +01:00
this->worker.SetPriority(OYSTER_THREAD_PRIORITY_1);
this->isRunning = true;
}
}
void GameSession::ThreadEntry( )
{
//A timer so we dont lock because 1 client disconnected..
Utility::WinTimer t;
DynamicArray<SmartPointer<GameClient>> readyList = this->clients;
//First we need to clean invalid clients, if any, and tell them to start loading game data
for (unsigned int i = 0; i < readyList.Size(); i++)
{
if(!readyList[i])
{
readyList.Remove(i);
}
else
{
Protocol_LobbyStartGame p(readyList[i]->GetPlayer()->GetID());
readyList[i]->GetClient()->Send(p);
}
}
unsigned int readyCounter = readyList.Size();
//Sync with clients
while (readyCounter != 0)
{
this->ProcessClients();
for (unsigned int i = 0; i < readyList.Size(); i++)
{
if(readyList[i] && readyList[i]->IsReady())
{
//Need to send information about other players to all players
for (unsigned int k = 0; k < readyList.Size(); k++)
{
if(k != i && this->clients[k])
{
Protocol_ObjectCreate p(this->clients[k]->GetPlayer()->GetOrientation(), this->clients[k]->GetPlayer()->GetID(), "char_white.dan"); //The model name will be custom later..
readyList[i]->GetClient()->Send(p);
}
}
readyCounter-- ;
readyList[i] = 0;
}
}
Sleep(5); //TODO: This might not be needed here.
}
}
2014-01-29 10:18:01 +01:00
bool GameSession::Attach(Utility::DynamicMemory::SmartPointer<NetworkClient> client)
{
2014-01-29 10:18:01 +01:00
if(!this->isCreated) return false;
2014-01-29 10:18:01 +01:00
client->SetOwner(this);
SmartPointer<GameClient> obj = new GameClient(client, this->gameInstance.CreatePlayer());
for (unsigned int i = 0; i < clients.Size(); i++)
{
if(!clients[i])
{
clients[i] = obj;
2014-01-29 10:18:01 +01:00
return true;
}
}
2014-01-29 10:18:01 +01:00
clients.Push(obj);
return true;
}
void GameSession::CloseSession( bool dissconnectClients )
{
NetworkSession::CloseSession(true);
this->clients.Clear();
}
}//End namespace DanBias