From c6a83f89ea55ba2071611513ac85b64134ac8f7b Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 15 Nov 2010 08:24:55 +0100 Subject: Added Player class. --- server/lobby.cpp | 28 ++++------------------------ server/lobby.h | 5 +++-- server/player.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++----- server/player.h | 43 +++++++++++++++++++++++++++---------------- 4 files changed, 76 insertions(+), 47 deletions(-) (limited to 'server') 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 void Lobby::handle_connect(Connection::p connection) { - // Send Hello. - connection->send(make_shared("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(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(false)); - connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); - return; - } - - connection->send(make_shared(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 #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 -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 f) { + Player::p p(new Player(c, f)); + p->start(); + return p; +} + +Player::Player(Connection::p c, boost::function f) : connection(c), lobby_callback(f) { + +} + +void Player::start() { + // Send Hello. + connection->send(make_shared("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(msg); + + // Check if nick is invalid. + if(login_msg->nick.size() == 0) { + connection->send(make_shared(false)); + connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1)); + return; + } + + connection->send(make_shared(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 #include -#include "connection.h" - -class Player{ +#include +#include + +#include "connection.h" + +class Player : public boost::enable_shared_from_this { + public: + typedef boost::shared_ptr p; + + static p create(Connection::p c, boost::function f); + private: + Connection::p connection; + + boost::function lobby_callback; + + std::string nick_; + + Player(Connection::p c, boost::function 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 -- cgit v1.2.3