Smoothed naming and other conventions

This commit is contained in:
Kayne Ruse
2013-05-24 00:51:04 +10:00
parent cce1a874bf
commit 0b2af1d80f
21 changed files with 176 additions and 257 deletions
+28
View File
@@ -0,0 +1,28 @@
#ifndef CLIENTDATA_HPP_
#define CLIENTDATA_HPP_
#include "vector2.hpp"
#include <string>
struct ClientData {
ClientData() = default;
ClientData(int ID, int ch, std::string h, std::string a) {
playerID = ID;
channel = ch;
handle = h;
avatar = a;
}
void Update(int delta) {
position += motion * delta;
}
int playerID = -1;
int channel = -1;
Vector2 position;
Vector2 motion;
std::string handle;
std::string avatar;
};
#endif
+2 -2
View File
@@ -1,4 +1,4 @@
#include "server.hpp"
#include "server_application.hpp"
#include <stdexcept>
#include <iostream>
@@ -10,7 +10,7 @@ int main(int, char**) {
cout << "Beginning server" << endl;
#endif
try {
Server app;
ServerApplication app;
app.Init();
app.Proc();
app.Quit();
-12
View File
@@ -1,12 +0,0 @@
#include "player.hpp"
Player::Player(int ID, int ch, std::string h, std::string a) {
playerID = ID;
channel = ch;
handle = h;
avatar = a;
}
void Player::Update(int delta) {
position += motion * delta;
}
-39
View File
@@ -1,39 +0,0 @@
#ifndef PLAYER_HPP_
#define PLAYER_HPP_
#include "vector2.hpp"
#include <string>
class Player {
public:
Player() = default;
Player(int playerID, int channel, std::string handle, std::string avatar);
void Update(int delta);
int SetPlayerID(int id) {return playerID = id;}
int GetPlayerID() const {return playerID;}
int SetChannel(int ch) {return channel = ch;}
int GetChannel() const {return channel;}
Vector2 SetPosition(Vector2 v) {return position = v;}
Vector2 ShiftPosition(Vector2 v) {return position += v;}
Vector2 GetPosition() const {return position;}
Vector2 SetMotion(Vector2 v) {return motion = v;}
Vector2 ShiftMotion(Vector2 v) {return motion += v;}
Vector2 GetMotion() const {return motion;}
std::string SetHandle(std::string s) {return handle = s;}
std::string GetHandle() const {return handle;}
std::string SetAvatar(std::string s) {return avatar = s;}
std::string GetAvatar() const {return avatar;}
private:
int playerID = -1;
int channel = -1; //for networking
Vector2 position;
Vector2 motion;
std::string handle;
std::string avatar;
};
#endif
-42
View File
@@ -1,42 +0,0 @@
#include "player_manager.hpp"
#include <stdexcept>
void PlayerManager::UpdateAll(int delta) {
for (auto it : playerMap) {
it.second->Update(delta);
}
}
Player* PlayerManager::New(int playerID, int channel, std::string handle, std::string avatar) {
if (playerMap.find(playerID) != playerMap.end()) {
throw(std::runtime_error("Player ID already exists"));
}
return playerMap[playerID] = new Player(playerID, channel, handle, avatar);
}
Player* PlayerManager::Get(int playerID) {
for (auto it : playerMap) {
if (it.second->GetPlayerID() == playerID) {
return it.second;
}
}
return nullptr;
}
void PlayerManager::Delete(int playerID) {
for (auto it : playerMap) {
if (it.second->GetPlayerID() == playerID) {
delete it.second;
playerMap.erase(playerID);
return;
}
}
}
void PlayerManager::DeleteAll() {
for (auto it : playerMap) {
delete it.second;
}
playerMap.clear();
}
-38
View File
@@ -1,38 +0,0 @@
#ifndef PLAYERMANAGER_H_
#define PLAYERMANAGER_H_
#include "player.hpp"
#include <map>
#include <string>
class PlayerManager {
private:
//utilities
typedef std::map<int, Player*> PlayerMap;
public:
PlayerManager() = default;
PlayerManager(int max) {SetMaxPlayers(max);}
~PlayerManager() {DeleteAll();}
void UpdateAll(int delta);
Player* New(int playerID, int channel, std::string handle, std::string avatar);
Player* Get(int playerID);
void Delete(int playerID);
void DeleteAll();
PlayerMap* GetPlayerMap() {return &playerMap;}
int SetMaxPlayers(int max) {return maxPlayers = max;}
int GetMaxPlayers() const {return maxPlayers;}
private:
//utilities
//...
//members
PlayerMap playerMap;
int maxPlayers = 0;
};
#endif
-41
View File
@@ -1,41 +0,0 @@
#ifndef SERVER_HPP_
#define SERVER_HPP_
#include "delta.hpp"
#include "packet_list.hpp"
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "player_manager.hpp"
class Server {
public:
Server() = default;
~Server() = default;
void Init();
void Proc();
void Quit();
private:
void HandleInput();
void UpdateWorld();
void HandleOutput();
//network commands
void Ping(Packet*);
void JoinRequest(Packet*);
void Disconnect(Packet*);
void Movement(Packet*);
bool running = false;
Delta delta;
ConfigUtility configUtil;
UDPNetworkUtility netUtil;
PlayerManager playerMgr;
int uniqueIndex = 0;
};
#endif
@@ -1,21 +1,20 @@
#include "server.hpp"
#include "server_application.hpp"
#include <stdexcept>
#include <iostream>
using namespace std;
void Server::Init() {
void ServerApplication::Init() {
if (SDLNet_Init()) {
throw(runtime_error("Failed to initialize SDL_net"));
}
configUtil.Load("config.cfg");
netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet));
playerMgr.SetMaxPlayers(SDLNET_MAX_UDPCHANNELS);
netUtil.Open(configUtil.Integer("server.port"), sizeof(PacketData));
running = true;
}
void Server::Proc() {
void ServerApplication::Proc() {
while(running) {
HandleInput();
UpdateWorld();
@@ -27,18 +26,18 @@ void Server::Proc() {
}
}
void Server::Quit() {
void ServerApplication::Quit() {
netUtil.Close();
SDLNet_Quit();
}
void Server::HandleInput() {
void ServerApplication::HandleInput() {
//accept new connections
//accept updates from the clients
//read the updates from the clients into internal containers
Packet packet;
PacketData packet;
while(netUtil.Receive()) {
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet));
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(PacketData));
switch(packet.type) {
case PacketList::PING:
Ping(&packet);
@@ -56,15 +55,17 @@ void Server::HandleInput() {
}
}
void Server::UpdateWorld() {
void ServerApplication::UpdateWorld() {
//update internals ie.
// ai
// loot drops
delta.Calculate();
playerMgr.UpdateAll(delta.GetDelta());
for (auto it : clientMap) {
it.second.Update(delta.GetDelta());
}
}
void Server::HandleOutput() {
void ServerApplication::HandleOutput() {
//send all information to new connections
//selective updates to existing connectons
}
@@ -73,14 +74,14 @@ void Server::HandleOutput() {
//network commands
//-------------------------
void Server::Ping(Packet* packet) {
void ServerApplication::Ping(PacketData* packet) {
//respond to pings with the server name
packet->type = PacketList::PONG;
sprintf(packet->pong.metadata, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(Packet));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(PacketData));
}
void Server::JoinRequest(Packet* packet) {
void ServerApplication::JoinRequest(PacketData* packet) {
//TODO
cout << "Join request..." << endl;
// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) {
@@ -91,10 +92,10 @@ void Server::JoinRequest(Packet* packet) {
// cout << ch << endl;
}
void Server::Disconnect(Packet* packet) {
void ServerApplication::Disconnect(PacketData* packet) {
//TODO
}
void Server::Movement(Packet* packet) {
void ServerApplication::Movement(PacketData* packet) {
//TODO
}
+47
View File
@@ -0,0 +1,47 @@
#ifndef SERVERAPPLICATION_HPP_
#define SERVERAPPLICATION_HPP_
#include "client_data.hpp"
#include "delta.hpp"
#include "packet_list.hpp"
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include <map>
#include <string>
class ServerApplication {
public:
ServerApplication() = default;
~ServerApplication() = default;
void Init();
void Proc();
void Quit();
private:
void HandleInput();
void UpdateWorld();
void HandleOutput();
//network commands
void Ping(PacketData*);
void JoinRequest(PacketData*);
void Disconnect(PacketData*);
void Movement(PacketData*);
bool running = false;
Delta delta;
//globals
ConfigUtility configUtil;
UDPNetworkUtility netUtil;
//members
std::map<int, ClientData> clientMap;
int maxClients = SDLNET_MAX_UDPCHANNELS;
int uniqueIndex = 0;
};
#endif