From 4282a9d069b3bedc8888a4e6202f2b066233d4ec Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 25 Nov 2010 03:01:43 +0100 Subject: Rename Player to Client, to better reflect what it represents. A Player should be specific to a game, while a Client may participate in several. --- server/client.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server/client.h | 66 ++++++++++++++++++++++++++++++++++ server/connection.h | 2 +- server/game.cpp | 4 +-- server/game.h | 8 ++--- server/lobby.cpp | 10 +++--- server/lobby.h | 6 ++-- server/player.cpp | 102 ---------------------------------------------------- server/player.h | 66 ---------------------------------- server/standard.h | 1 - 10 files changed, 183 insertions(+), 184 deletions(-) create mode 100644 server/client.cpp create mode 100644 server/client.h delete mode 100644 server/player.cpp delete mode 100644 server/player.h (limited to 'server') diff --git a/server/client.cpp b/server/client.cpp new file mode 100644 index 0000000..533f6ee --- /dev/null +++ b/server/client.cpp @@ -0,0 +1,102 @@ +#include "client.h" + +#include + +Client::p Client::create(Connection::p c, boost::function f) { + Client::p p(new Client(c)); + p->start(f); + return p; +} + +Client::Client(Connection::p c) : connection(c), timer(c->socket.get_io_service()) { + +} + +void Client::start(boost::function f) { + // Send Hello. + connection->send(make_shared("aotenjoud git")); + + // Wait for Login. + connection->recv(boost::bind(&Client::handle_login, shared_from_this(), _1, f)); +} + +void Client::handle_login(Message::p msg, boost::function lobby_callback) { + 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(&Client::handle_login, shared_from_this(), _1, lobby_callback)); + return; + } + + connection->send(make_shared(true)); + + nick_ = login_msg->nick; + + lobby_callback(shared_from_this()); +} + +void Client::handle_ready(Message::p msg, boost::function ready_callback) { + if(msg->type != Message::Types::Ready) { + return; + } + + ready_callback(); +} + +void Client::handle_action(Message::p msg, boost::function action_callback, Actions possible_actions) { + if(msg->type != Message::Types::RoundAction) { + return; + } + + Message::RoundAction::p action_msg = dynamic_pointer_cast(msg); + + // Check if the action is valid. + if(possible_actions.contains(action_msg->action)) { + action_callback(action_msg->action); + } else { + action_callback(possible_actions[0]); + } +} + +std::string Client::nick() { + return nick_; +} + +void Client::game_start(boost::function callback, std::vector players) { + Message::GameStart::p msg = make_shared(); + + for(std::vector::iterator i = players.begin(); i != players.end(); i++) { + msg->players.push_back((*i)->nick()); + } + msg->player_id = id; + + connection->send(msg); + + connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); +} + +void Client::round_start() { + connection->send(make_shared()); +} + +void Client::round_state(State state) { + connection->send(make_shared(state)); +} + +void Client::round_end() { + connection->send(make_shared()); +} + +void Client::get_action(boost::function callback, Actions expected_actions) { + connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); +} + +void Client::kill_action() { + +} diff --git a/server/client.h b/server/client.h new file mode 100644 index 0000000..ad5df64 --- /dev/null +++ b/server/client.h @@ -0,0 +1,66 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include +#include +#include +#include + +#include "connection.h" + +class Client : 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::asio::deadline_timer timer; + + std::string nick_; + + Client(Connection::p c); + + //! Start communicating. + void start(boost::function f); + + //! Handle Login-message. + void handle_login(Message::p msg, boost::function lobby_callback); + + //! Handle Ready-message. + void handle_ready(Message::p msg, boost::function ready_callback); + + //! Handle Action-message. + void handle_action(Message::p msg, boost::function action_callback, Actions expected_actions); + + public: + //! The ID of the client + int id; + + //! Return client's nick. + std::string nick(); + + //! Notify client of a game start. + void game_start(boost::function callback, std::vector players); + + //! Notify client of a round start. + void round_start(); + + //! Send round state. + void round_state(State state); + + //! Send round end. + void round_end(); + + //! Get action. + void get_action(boost::function callback, Actions expected_actions); + + void kill_action(); +}; + +typedef std::vector Clients; + +#endif diff --git a/server/connection.h b/server/connection.h index 0f16022..248817b 100644 --- a/server/connection.h +++ b/server/connection.h @@ -10,7 +10,7 @@ class Connection : public ConnectionBase, public boost::enable_shared_from_this { private: friend class TCPServer; - friend class Player; + friend class Client; boost::asio::ip::tcp::socket socket; diff --git a/server/game.cpp b/server/game.cpp index 4d102a7..07373ac 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -11,7 +11,7 @@ #include "../common/set.h" -Game::p Game::create(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { +Game::p Game::create(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4) { Game::p p(new Game(player_1, player_2, player_3, player_4)); p->start(); return p; @@ -22,7 +22,7 @@ Game::~Game() { delete ruleset; } -Game::Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) { +Game::Game(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4) { players.push_back(player_1); players.push_back(player_2); players.push_back(player_3); diff --git a/server/game.h b/server/game.h index f0f1564..2574dc3 100644 --- a/server/game.h +++ b/server/game.h @@ -7,7 +7,7 @@ #include #include "wall.h" -#include "player.h" +#include "client.h" #include "../common/action.h" #include "gamevariant.h" @@ -17,16 +17,16 @@ class Game : public boost::enable_shared_from_this { public: typedef boost::shared_ptr p; - static p create(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4); + static p create(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4); ~Game(); private: - std::vector players; + std::vector players; int waiting_players; - Game(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4); + Game(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4); GameVariant *ruleset; diff --git a/server/lobby.cpp b/server/lobby.cpp index 748d70f..79c207b 100644 --- a/server/lobby.cpp +++ b/server/lobby.cpp @@ -8,20 +8,20 @@ void Lobby::handle_connect(Connection::p connection) { // Create player. - Player::create(connection, boost::bind(&Lobby::handle_action, this, _1)); + Client::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_action(Player::p player) { - std::cout << "Player " << player->nick() << " entered the lobby." << std::endl; +void Lobby::handle_action(Client::p client) { + std::cout << "Client " << client->nick() << " entered the lobby." << std::endl; if(waiting.size() >= 3) { - Game::create(waiting[0], waiting[1], waiting[2], player); + Game::create(waiting[0], waiting[1], waiting[2], client); waiting.clear(); } else { - waiting.push_back(player); + waiting.push_back(client); } } diff --git a/server/lobby.h b/server/lobby.h index 6f5572e..0e25975 100644 --- a/server/lobby.h +++ b/server/lobby.h @@ -6,20 +6,20 @@ #include #include "tcpserver.h" -#include "player.h" +#include "client.h" class Lobby { private: boost::asio::io_service io_service; TCPServer server; - std::vector waiting; + std::vector waiting; //! Handle new connection. void handle_connect(Connection::p connection); //! Handle action. - void handle_action(Player::p player); + void handle_action(Client::p client); public: Lobby(); diff --git a/server/player.cpp b/server/player.cpp deleted file mode 100644 index d8baaf8..0000000 --- a/server/player.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "player.h" - -#include - -Player::p Player::create(Connection::p c, boost::function f) { - Player::p p(new Player(c)); - p->start(f); - return p; -} - -Player::Player(Connection::p c) : connection(c), timer(c->socket.get_io_service()) { - -} - -void Player::start(boost::function f) { - // Send Hello. - connection->send(make_shared("aotenjoud git")); - - // Wait for Login. - connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, f)); -} - -void Player::handle_login(Message::p msg, boost::function lobby_callback) { - 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, lobby_callback)); - return; - } - - connection->send(make_shared(true)); - - nick_ = login_msg->nick; - - lobby_callback(shared_from_this()); -} - -void Player::handle_ready(Message::p msg, boost::function ready_callback) { - if(msg->type != Message::Types::Ready) { - return; - } - - ready_callback(); -} - -void Player::handle_action(Message::p msg, boost::function action_callback, Actions possible_actions) { - if(msg->type != Message::Types::RoundAction) { - return; - } - - Message::RoundAction::p action_msg = dynamic_pointer_cast(msg); - - // Check if the action is valid. - if(possible_actions.contains(action_msg->action)) { - action_callback(action_msg->action); - } else { - action_callback(possible_actions[0]); - } -} - -std::string Player::nick() { - return nick_; -} - -void Player::game_start(boost::function callback, std::vector players) { - Message::GameStart::p msg = make_shared(); - - for(std::vector::iterator i = players.begin(); i != players.end(); i++) { - msg->players.push_back((*i)->nick()); - } - msg->player_id = id; - - connection->send(msg); - - connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1, callback)); -} - -void Player::round_start() { - connection->send(make_shared()); -} - -void Player::round_state(State state) { - connection->send(make_shared(state)); -} - -void Player::round_end() { - connection->send(make_shared()); -} - -void Player::get_action(boost::function callback, Actions expected_actions) { - connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback, expected_actions)); -} - -void Player::kill_action() { - -} diff --git a/server/player.h b/server/player.h deleted file mode 100644 index 25eddd3..0000000 --- a/server/player.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef PLAYER_H -#define PLAYER_H - -#include -#include -#include -#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::asio::deadline_timer timer; - - std::string nick_; - - Player(Connection::p c); - - //! Start communicating. - void start(boost::function f); - - //! Handle Login-message. - void handle_login(Message::p msg, boost::function lobby_callback); - - //! Handle Ready-message. - void handle_ready(Message::p msg, boost::function ready_callback); - - //! Handle Action-message. - void handle_action(Message::p msg, boost::function action_callback, Actions expected_actions); - - public: - //! The ID of the player - int id; - - //! Return player's nick. - std::string nick(); - - //! Notify client of a game start. - void game_start(boost::function callback, std::vector players); - - //! Notify client of a round start. - void round_start(); - - //! Send round state. - void round_state(State state); - - //! Send round end. - void round_end(); - - //! Get action. - void get_action(boost::function callback, Actions expected_actions); - - void kill_action(); -}; - -typedef std::vector Players; - -#endif diff --git a/server/standard.h b/server/standard.h index 4e5e4fb..9ad73ec 100644 --- a/server/standard.h +++ b/server/standard.h @@ -2,7 +2,6 @@ #define STANDARD_H #include "gamevariant.h" -#include "player.h" #include "wall.h" #include "../common/set.h" -- cgit v1.2.3