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

157 lines
3.0 KiB
C++
Raw Normal View History

2014-01-28 09:00:02 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#define NOMINMAX
#include <Windows.h>
#include <iostream>
2014-01-29 10:18:01 +01:00
#include <vld.h>
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
#include "..\GameServerAPI.h"
#include "..\GameLobby.h"
2014-01-28 09:00:02 +01:00
#include "..\GameSession.h"
2014-01-29 10:18:01 +01:00
#include <NetworkServer.h>
2014-01-28 09:00:02 +01:00
#include <NetworkClient.h>
2014-01-29 10:18:01 +01:00
#include <WindowShell.h>
#include <Utilities.h>
#include <WinTimer.h>
2014-01-30 00:19:00 +01:00
#include <thread>
2014-01-29 10:18:01 +01:00
using namespace DanBias;
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility;
namespace
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
GameLobby lobby;
NetworkServer server;
WinTimer timer;
}
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc)
2014-01-29 10:18:01 +01:00
{
if(server.Init(desc.listenPort, &lobby) == NetworkServer::ServerReturnCode_Error)
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
return DanBiasServerReturn_Error;
2014-01-28 09:00:02 +01:00
}
2014-01-30 00:19:00 +01:00
GameSession::GameDescription d;
2014-01-28 09:00:02 +01:00
std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort);
2014-01-29 10:18:01 +01:00
return DanBiasServerReturn_Sucess;
}
2014-01-30 00:19:00 +01:00
void GameServerAPI::ServerStart()
2014-01-29 10:18:01 +01:00
{
timer.reset();
2014-01-30 00:19:00 +01:00
server.Start();
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
}
void GameServerAPI::ServerStop()
{
2014-01-31 22:52:52 +01:00
if(!server.IsStarted()) return;
2014-01-30 00:19:00 +01:00
lobby.Release();
server.Shutdown();
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
double total = timer.getElapsedSeconds();
int time = (int)total;
int hour, min, sec;
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
hour=time / 3600;
time=time % 3600;
min=time / 60;
time=time % 60;
sec = time;
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
printf( "Server has been running for: %i:%i:%i - [hh:mm:ss] \n\n", hour, min, sec );
printf( "Terminating in : ");
2014-01-30 00:19:00 +01:00
for (int i = 0; i < 3; i++)
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
printf( "%i ", 3-i );
Sleep(1000);
2014-01-28 09:00:02 +01:00
}
2014-01-30 00:19:00 +01:00
printf( "\nServer terminated!" );
2014-01-29 10:18:01 +01:00
}
2014-01-30 00:19:00 +01:00
void GameServerAPI::ServerUpdate()
2014-01-29 10:18:01 +01:00
{
2014-01-30 00:19:00 +01:00
server.Update();
lobby.Update();
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
}
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
GameServerAPI::GameServerInfo GameServerAPI::ServerGetInfo()
{
GameServerAPI::GameServerInfo i;
i.serverIp = server.GetLanAddress().c_str();
i.listenPort = server.GetPort();
return i;
2014-01-29 10:18:01 +01:00
}
2014-01-30 00:19:00 +01:00
void GameServerAPI::GameSetMapId(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.mapNumber = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetMaxClients(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.maxClients = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetGameMode(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.gameMode = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetGameTime(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.gameTime = val;
lobby.SetGameDesc(d);
}
int GameServerAPI::GameGetMapId()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.mapNumber;
}
int GameServerAPI::GameGetMaxClients()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.maxClients;
}
int GameServerAPI::GameGetGameMode()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameMode;
}
int GameServerAPI::GameGetGameTime()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameTime;
}
const char* GameServerAPI::GameGetGameName()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameName.c_str();
}
bool GameServerAPI::GameStart()
{
return lobby.StartGameSession();
}