summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/lobby.cpp28
-rw-r--r--server/lobby.h5
-rw-r--r--server/player.cpp47
-rw-r--r--server/player.h43
4 files changed, 76 insertions, 47 deletions
diff --git a/server/lobby.cpp b/server/lobby.cpp
index f74a4f2..a3b50d7 100644
--- a/server/lobby.cpp
+++ b/server/lobby.cpp
@@ -5,35 +5,15 @@
#include <iostream>
void Lobby::handle_connect(Connection::p connection) {
- // Send Hello.
- connection->send(make_shared<Message::Hello>("aotenjoud git"));
-
- // Wait for Login.
- connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1));
+ // Create player.
+ Player::create(connection, boost::bind(&Lobby::handle_action, this, _1));
// Get another connection.
server.get_connection(boost::bind(&Lobby::handle_connect, this, _1));
}
-void Lobby::handle_login(Connection::p connection, Message::p msg) {
- if(msg->type != Message::Types::Login) {
- return;
- }
-
- Message::Login::p login_msg = dynamic_pointer_cast<Message::Login>(msg);
-
- std::cout << "Player " << login_msg->nick << " entered the lobby." << std::endl;
-
- // Check if nick is invalid.
- if(login_msg->nick.size() == 0) {
- connection->send(make_shared<Message::LoginResponse>(false));
- connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1));
- return;
- }
-
- connection->send(make_shared<Message::LoginResponse>(true));
-
- // Do something with the connection to keep it around.
+void Lobby::handle_action(Player::p player) {
+ std::cout << "Player " << player->nick() << " entered the lobby." << std::endl;
}
Lobby::Lobby() : server(io_service) {
diff --git a/server/lobby.h b/server/lobby.h
index 07ed13d..d266a99 100644
--- a/server/lobby.h
+++ b/server/lobby.h
@@ -4,6 +4,7 @@
#include <boost/asio.hpp>
#include "tcpserver.h"
+#include "player.h"
class Lobby {
private:
@@ -13,8 +14,8 @@ class Lobby {
//! Handle new connection.
void handle_connect(Connection::p connection);
- //! Handle login.
- void handle_login(Connection::p connection, Message::p msg);
+ //! Handle action.
+ void handle_action(Player::p player);
public:
Lobby();
diff --git a/server/player.cpp b/server/player.cpp
index e1796d8..7243d81 100644
--- a/server/player.cpp
+++ b/server/player.cpp
@@ -1,9 +1,46 @@
#include "player.h"
-Player::Player(){}
-Player::~Player(){}
+#include <boost/bind.hpp>
-Player::Player(const Connection::p& connection, const std::string& nick){
- this->connection_pointer = connection;
- this->nick = nick;
+Player::p Player::create(Connection::p c, boost::function<void (Player::p)> f) {
+ Player::p p(new Player(c, f));
+ p->start();
+ return p;
+}
+
+Player::Player(Connection::p c, boost::function<void (Player::p)> f) : connection(c), lobby_callback(f) {
+
+}
+
+void Player::start() {
+ // Send Hello.
+ connection->send(make_shared<Message::Hello>("aotenjoud git"));
+
+ // Wait for Login.
+ connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1));
+}
+
+void Player::handle_login(Message::p msg) {
+ if(msg->type != Message::Types::Login) {
+ return;
+ }
+
+ Message::Login::p login_msg = dynamic_pointer_cast<Message::Login>(msg);
+
+ // Check if nick is invalid.
+ if(login_msg->nick.size() == 0) {
+ connection->send(make_shared<Message::LoginResponse>(false));
+ connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1));
+ return;
+ }
+
+ connection->send(make_shared<Message::LoginResponse>(true));
+
+ nick_ = login_msg->nick;
+
+ lobby_callback(shared_from_this());
+}
+
+std::string Player::nick() {
+ return nick_;
}
diff --git a/server/player.h b/server/player.h
index 8e04ab5..8b75885 100644
--- a/server/player.h
+++ b/server/player.h
@@ -1,26 +1,37 @@
-#ifndef PLAYER_H
+#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <boost/shared_ptr.hpp>
-#include "connection.h"
-
-class Player{
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/function.hpp>
+
+#include "connection.h"
+
+class Player : public boost::enable_shared_from_this<Player> {
+ public:
+ typedef boost::shared_ptr<Player> p;
+
+ static p create(Connection::p c, boost::function<void (Player::p)> f);
+
private:
+ Connection::p connection;
+
+ boost::function<void (Player::p)> lobby_callback;
+
+ std::string nick_;
+
+ Player(Connection::p c, boost::function<void (Player::p)> f);
- //! A smart pointer to the players connection
- Connection::p connection_pointer;
+ //! Start communicating.
+ void start();
- //! The players nick
- std::string nick;
+ //! Handle login.
+ void handle_login(Message::p msg);
- //! Either generated or fetched from database. Not used yet.
- unsigned int id;
public:
- Player();
- ~Player();
+ //! Return player's nick.
+ std::string nick();
+};
- Player(const Connection::p& connection, const std::string& nick);
-};
-
-#endif // PLAYER_H_INCLUDED
+#endif