Skip to content

Commit

Permalink
More implementation in ServerList #10
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 9, 2019
1 parent a2bb625 commit 99074b1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/ui/loginwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <guisan.hpp>
#include <guisan/sdl.hpp>

/**
* Utility window to get identification details from the user
*/
class LoginWindow final : public gcn::Window, public gcn::ActionListener {

public:
Expand Down
57 changes: 55 additions & 2 deletions src/views/serverlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@
* as defined by the Mozilla Public License, v. 2.0.
*/

#include <SDL2/SDL_net.h>
#include "../protocol.hpp"
#include "../utils.hpp"
#include "serverlist.hpp"
using namespace std;

ServerList::ServerList(ContextName name, gcn::Container * p) : Context(name, p),
state(State::NONE), currentIP(0x0),
btn_back("< Back"), btn_manualIP("Enter IP"), btn_rescan("Rescan local network")
{
btn_back.setActionEventId("back");
btn_back.addActionListener(this);
btn_manualIP.setActionEventId("ip");
btn_manualIP.addActionListener(this);
btn_rescan.setActionEventId("rescan");
w_login.setActionEventId("login");
w_login.addActionListener(this);
w_login.setVisible(false);

if (name == ContextName::SERVER_LIST_WEB) {
btn_rescan.setVisible(false);
btn_rescan.setVisible(false); // this is useless for a web game
}

addWidgets();
Expand All @@ -42,6 +52,15 @@ void ServerList::action(const gcn::ActionEvent & actionEvent) {
else if (actionEvent.getId() == "rescan") {
scanNetwork();
}
else if (actionEvent.getId() == "connect") {
currentIP = linbound::stringToIP("127.0.0.1"); // To be replaced once InputBox is in
if (currentIP != 0) {
sendRequest(currentIP);
}
}
else if (actionEvent.getId() == "login") {
login(currentIP, w_login.getLogin(), w_login.getPassword());
}
}

void ServerList::drawBackground(SDL_Renderer * screen) {
Expand All @@ -54,22 +73,56 @@ void ServerList::drawBackground(SDL_Renderer * screen) {
void ServerList::processMessage(const Uint8 code, const std::string & message) {
switch (code) {
case SERVER_INFO:
// Analyze message and create button
// Analyze message and create button, store in vector
break;
case LOGIN_MSG:
// Analyze message, warn of errors
setNextContext(ContextName::ROOM_LIST);
break;
default:
break;
}
}

/**
* Getter so the RoomList can know where to connect to when changing Context
* \return IP of the server for which connection was accepted
*/
Uint32 ServerList::getIP() const
{
return currentIP;
}

void ServerList::addWidgets() {
addWidget(&btn_back, 40, background->h - 40);
addWidget(&btn_manualIP, 700, background->h / 2);
addWidget(&btn_rescan, 700, btn_manualIP.getY() + 40);

addCenteredWidget(&w_login);
}

/**
* Send a broadcast message to find other servers
*/
void ServerList::scanNetwork() {
// TBD

state = State::RECEIVING_BC;
}

/**
* Try to see if a server exists at a given IP
* \param ip IPv4 as an Uint32, fetched from the user
*/
void ServerList::sendRequest(Uint32 ip) {
// Create message

// Send it

state = State::RECEIVING_IP;
}

void ServerList::login(Uint32 ip, const std::string & login, const std::string & password) {

state = State::LOGIN;
}
30 changes: 30 additions & 0 deletions src/views/serverlist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@
#include <guisan.hpp>
#include <SDL2/SDL.h>
#include "../context.hpp"
#include "../ui/loginwindow.hpp"

struct ServerInfo {
Uint32 ip;
std::string name;
Uint8 levelMin;
Uint8 levelMax;
Uint8 busy; // level of attendance
};

/**
* View representing the list of servers, used in LAN and web game modes
*/
class ServerList : public Context, public gcn::ActionListener {
public:
enum State { NONE,
LOGIN, // Login sent, waiting for answer
RECEIVING_BC, // Receiving server data from LAN
RECEIVING_IP // Receiving server data from manual IP
};

ServerList(ContextName name, gcn::Container *p);
virtual ~ServerList();
void action(const gcn::ActionEvent &actionEvent) override;
Expand All @@ -31,17 +49,29 @@ class ServerList : public Context, public gcn::ActionListener {
// No specific handling to be done here, GUI handles everything
};

Uint32 getIP() const;

private:
State state;
Uint32 currentIP;

gcn::Button btn_back;
gcn::Button btn_manualIP;
gcn::Button btn_rescan;
// gcn::InputBox input_ip;
LoginWindow w_login;

SDL_Texture *backTexture = nullptr;
SDL_Surface *background = nullptr;

const int REQUEST_TIMEOUT = 5000; // 5 seconds

std::vector<ServerInfo> serversFound;

void addWidgets();
void scanNetwork();
void sendRequest(Uint32 ip);
void login(Uint32 ip, const std::string &login, const std::string &password);
};

#endif // !_H_SERVERLIST_

0 comments on commit 99074b1

Please sign in to comment.