Join request is sent, but I need to plan the next section out in detail

This commit is contained in:
Kayne Ruse
2013-05-22 17:31:13 +10:00
parent 66f54dca0b
commit cce1a874bf
8 changed files with 68 additions and 50 deletions
+14 -7
View File
@@ -70,7 +70,9 @@ void Lobby::Receive() {
case PacketList::PONG:
PushServer(&packet);
break;
//...
case PacketList::JOINCONFIRM:
//TODO: enter the game
break;
}
}
}
@@ -115,10 +117,7 @@ void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
PingNetwork();
}
if (buttonMap["join"]->MouseButtonUp(button) == Button::State::HOVER) {
//join...
if (selectedServer) {
cout << "joining server: " << selectedServer->name << endl;
}
JoinRequest(selectedServer);
}
if (buttonMap["back"]->MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU);
@@ -159,7 +158,15 @@ void Lobby::PingNetwork() {
void Lobby::PushServer(Packet* packet) {
Server s;
s.name = packet->pong.serverName;
s.name = packet->pong.metadata;
s.add = netUtil->GetInPacket()->address;
serverVector.push_back(s);
}
}
void Lobby::JoinRequest(Server* server) {
Packet packet;
packet.type = PacketList::JOINREQUEST;
snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
snprintf(packet.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
netUtil->Send(&server->add, reinterpret_cast<void*>(&packet), sizeof(Packet));
}
+1
View File
@@ -44,6 +44,7 @@ protected:
//utilities
void PingNetwork();
void PushServer(Packet*);
void JoinRequest(Server*);
typedef std::map<std::string, Button*> ButtonMap;
//members
+11 -4
View File
@@ -17,7 +17,8 @@ enum class PacketList {
JOINCONFIRM,
DISCONNECT,
//player controls
//information control
SYNCHRONIZE,
NEWPLAYER,
DELETEPLAYER,
MOVEMENT,
@@ -29,11 +30,12 @@ enum class PacketList {
struct Ping {
PacketList type = PacketList::PING;
char metadata[PACKET_STRING_SIZE];
};
struct Pong {
PacketList type = PacketList::PONG;
char serverName[PACKET_STRING_SIZE];
char metadata[PACKET_STRING_SIZE];
};
struct JoinRequest {
@@ -52,9 +54,13 @@ struct Disconnect {
};
//-------------------------
//player controls
//information control
//-------------------------
struct Synchronize {
PacketList type = PacketList::SYNCHRONIZE;
};
struct NewPlayer {
PacketList type = PacketList::NEWPLAYER;
int playerID;
@@ -92,7 +98,8 @@ union Packet {
JoinConfirm joinConfirm;
Disconnect disconnect;
//player controls
//information control
Synchronize synchronize;
NewPlayer newPlayer;
DeletePlayer deletePlayer;
Movement movement;
+3 -3
View File
@@ -23,17 +23,17 @@ public:
Vector2 ShiftMotion(Vector2 v) {return motion += v;}
Vector2 GetMotion() const {return motion;}
std::string SetAvatar(std::string s) {return avatar = s;}
std::string GetAvatar() const {return avatar;}
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 avatar;
std::string handle;
std::string avatar;
};
#endif
+17 -22
View File
@@ -1,47 +1,42 @@
#include "player_manager.hpp"
PlayerManager::PlayerManager(int max) {
maxPlayers = max;
}
PlayerManager::~PlayerManager() {
DeleteAll();
}
#include <stdexcept>
void PlayerManager::UpdateAll(int delta) {
for (auto it : playerList) {
it->Update(delta);
for (auto it : playerMap) {
it.second->Update(delta);
}
}
Player* PlayerManager::New(int playerID, int channel, std::string handle, std::string avatar) {
Player* p = new Player(playerID, channel, handle, avatar);
playerList.push_back(p);
return p;
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 : playerList) {
if (it->GetPlayerID() == playerID) {
return it;
for (auto it : playerMap) {
if (it.second->GetPlayerID() == playerID) {
return it.second;
}
}
return nullptr;
}
void PlayerManager::Delete(int playerID) {
for (auto it : playerList) {
if (it->GetPlayerID() == playerID) {
delete it;
playerList.remove(it);
for (auto it : playerMap) {
if (it.second->GetPlayerID() == playerID) {
delete it.second;
playerMap.erase(playerID);
return;
}
}
}
void PlayerManager::DeleteAll() {
for (auto it : playerList) {
delete it;
for (auto it : playerMap) {
delete it.second;
}
playerList.clear();
playerMap.clear();
}
+8 -9
View File
@@ -3,17 +3,17 @@
#include "player.hpp"
#include <list>
#include <map>
#include <string>
class PlayerManager {
private:
//utilities
typedef std::list<Player*> PlayerList;
typedef std::map<int, Player*> PlayerMap;
public:
PlayerManager() = default;
PlayerManager(int maxPlayers);
~PlayerManager();
PlayerManager(int max) {SetMaxPlayers(max);}
~PlayerManager() {DeleteAll();}
void UpdateAll(int delta);
@@ -23,17 +23,16 @@ public:
void DeleteAll();
PlayerList* GetPlayerList() {
return &playerList;
}
PlayerMap* GetPlayerMap() {return &playerMap;}
int SetMaxPlayers(int max) {return maxPlayers = max;}
int GetMaxPlayers() const {return maxPlayers;}
private:
//utilities
//...
//members
PlayerList playerList;
PlayerMap playerMap;
int maxPlayers = 0;
int ticker = 0;
};
#endif
+12 -5
View File
@@ -11,6 +11,7 @@ void Server::Init() {
}
configUtil.Load("config.cfg");
netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet));
playerMgr.SetMaxPlayers(SDLNET_MAX_UDPCHANNELS);
running = true;
}
@@ -74,20 +75,26 @@ void Server::HandleOutput() {
void Server::Ping(Packet* packet) {
//respond to pings with the server name
cout << "responding to ping..." << endl;
packet->type = PacketList::PONG;
sprintf(packet->pong.serverName, "%s",configUtil.CString("servername"));
sprintf(packet->pong.metadata, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(Packet));
}
void Server::JoinRequest(Packet* packet) {
//
//TODO
cout << "Join request..." << endl;
// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) {
// //rejection
// return;
// }
// int ch = netUtil.Bind(&netUtil.GetInPacket()->address, -1);
// cout << ch << endl;
}
void Server::Disconnect(Packet* packet) {
//
//TODO
}
void Server::Movement(Packet* packet) {
//
//TODO
}
+2
View File
@@ -34,6 +34,8 @@ private:
ConfigUtility configUtil;
UDPNetworkUtility netUtil;
PlayerManager playerMgr;
int uniqueIndex = 0;
};
#endif