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