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

216 lines
5.1 KiB
C++
Raw Normal View History

2014-01-28 09:00:02 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "..\GameLobby.h"
#include <PlayerProtocols.h>
#include <PostBox\PostBox.h>
#include <Protocols.h>
2014-01-28 09:00:02 +01:00
using namespace Utility::DynamicMemory;
using namespace Oyster::Network;
using namespace Oyster;
using namespace GameLogic;
2014-01-28 09:00:02 +01:00
2014-02-17 16:17:07 +01:00
using namespace DanBias;
GameLobby::GameLobby()
{ }
2014-02-17 16:17:07 +01:00
GameLobby::~GameLobby()
{
this->gClients.Clear();
2014-02-17 16:17:07 +01:00
}
void GameLobby::Release()
{
NetworkSession::CloseSession(true);
this->gameSession.CloseSession(true);
}
void GameLobby::Update()
2014-01-28 09:00:02 +01:00
{
for (unsigned int i = 0; i < this->gClients.Size(); i++)
2014-01-28 09:00:02 +01:00
{
if(this->gClients[i])
2014-02-17 16:17:07 +01:00
{
this->gClients[i]->GetClient()->Update();
2014-02-17 16:17:07 +01:00
}
2014-01-30 00:19:00 +01:00
}
2014-02-17 16:17:07 +01:00
}
void GameLobby::SetGameDesc(const LobbyLevelData& desc)
{
this->description.gameMode = desc.gameMode;
this->description.gameName = desc.gameName;
this->description.mapName = desc.mapName;
this->description.gameTimeInMinutes = desc.gameTimeInMinutes;
2014-02-17 16:17:07 +01:00
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);
2014-02-17 16:17:07 +01:00
}
void GameLobby::GetGameDesc(LobbyLevelData& desc)
{
desc.gameTimeInMinutes = this->description.gameTimeInMinutes;
2014-02-17 16:17:07 +01:00
desc.maxClients = this->description.maxClients;
desc.mapName = this->description.mapName;
desc.gameName = this->description.gameName;
desc.gameMode = this->description.gameMode;
2014-02-17 16:17:07 +01:00
}
2014-02-18 13:12:08 +01:00
bool GameLobby::StartGameSession( bool forceStart )
2014-02-17 16:17:07 +01:00
{
2014-02-18 13:12:08 +01:00
//Check if all clients is ready, in not force start
if(!forceStart)
2014-01-30 00:19:00 +01:00
{
2014-02-18 13:12:08 +01:00
if(!this->GetClientCount())
{ /*None connected*/ return false;}
else if( this->GetClientCount() != this->readyList.Size() )
{ /*Not enough connected*/ return false; }
}
2014-02-18 13:12:08 +01:00
GameSession::GameDescription desc;
desc.maxClients = this->description.maxClients;
desc.gameMode = this->description.gameMode;
desc.gameTimeMinutes = this->description.gameTimeInMinutes;
desc.mapName = this->description.mapName;
desc.owner = this;
desc.clients = this->gClients;
2014-02-15 09:29:54 +01:00
2014-02-18 13:12:08 +01:00
if(desc.gameTimeMinutes == 0)
desc.gameTimeMinutes = 10; //note: should be fetched from somewhere.
2014-02-15 09:29:54 +01:00
2014-02-18 13:12:08 +01:00
if(desc.maxClients == 0)
desc.maxClients = 10; //note: should be fetched somewhere else..
2014-02-17 16:17:07 +01:00
2014-02-18 13:12:08 +01:00
this->gClients.Clear(); //Remove clients from lobby list
if(this->gameSession.Create(desc, forceStart))
2014-02-17 16:17:07 +01:00
{
2014-02-18 13:12:08 +01:00
this->gameSession.Run();
return true;
2014-02-17 16:17:07 +01:00
}
2014-02-18 13:12:08 +01:00
2014-02-17 16:17:07 +01:00
return false;
}
2014-02-18 13:12:08 +01:00
int GameLobby::GetGameSessionClientCount()
{
return this->gameSession.GetClientCount();
}
2014-01-28 09:00:02 +01:00
2014-02-17 16:17:07 +01:00
void GameLobby::ClientEventCallback(NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e)
{
switch (e.args.type)
2014-01-29 10:18:01 +01:00
{
2014-02-17 16:17:07 +01:00
case NetworkClient::ClientEventArgs::EventType_Disconnect:
2014-02-15 22:33:40 +01:00
2014-02-17 16:17:07 +01:00
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve:
2014-02-15 22:33:40 +01:00
2014-02-17 16:17:07 +01:00
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
//e.sender->Disconnect();
//this->readyList.Remove(e.sender);
//this->gClients.Remove(e.sender);
2014-02-17 16:17:07 +01:00
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str());
this->ParseProtocol(e.args.data.protocol, e.sender);
break;
2014-01-29 10:18:01 +01:00
}
2014-02-17 16:17:07 +01:00
}
void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer<Oyster::Network::NetworkClient> client)
{
printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str());
2014-02-17 16:17:07 +01:00
if(this->gameSession)
{
if(!this->Attach(client))
{
client->Disconnect();
}
2014-02-17 16:17:07 +01:00
}
else
{
if(!this->Attach(client))
{
//Send message that lobby full
client->Disconnect();
return;
}
2014-02-17 16:17:07 +01:00
Protocol_LobbyClientData p1;
Protocol_LobbyGameData p2;
2014-01-31 22:52:52 +01:00
for (unsigned int i = 0; i < this->gClients.Size(); i++)
2014-02-17 16:17:07 +01:00
{
if(this->gClients[i])
{
2014-02-17 16:17:07 +01:00
Protocol_LobbyClientData::PlayerData t;
t.id = client->GetID();
t.ip = client->GetIpAddress();
2014-02-17 16:17:07 +01:00
t.team = 0;
t.name = "Dennis <20>r kung tycker Erik!";
p1.list.Push(t);
}
2014-01-31 22:52:52 +01:00
}
2014-02-17 16:17:07 +01:00
p2.majorVersion = 1;
p2.minorVersion = 0;
p2.mapName = "Dennis <20>r kung tycker Erik!";
client->Send(p1.GetProtocol());
client->Send(p2.GetProtocol());
2014-01-28 09:00:02 +01:00
}
2014-02-17 16:17:07 +01:00
}
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;
client->SetOwner(this);
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;
}
2014-01-28 09:00:02 +01:00