summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/player.cpp30
-rw-r--r--server/player.h14
2 files changed, 18 insertions, 26 deletions
diff --git a/server/player.cpp b/server/player.cpp
index ce6a116..b1d8467 100644
--- a/server/player.cpp
+++ b/server/player.cpp
@@ -3,24 +3,24 @@
#include <boost/bind.hpp>
Player::p Player::create(Connection::p c, boost::function<void (Player::p)> f) {
- Player::p p(new Player(c, f));
- p->start();
+ Player::p p(new Player(c));
+ p->start(f);
return p;
}
-Player::Player(Connection::p c, boost::function<void (Player::p)> f) : connection(c), timer(c->socket.get_io_service()), lobby_callback(f) {
+Player::Player(Connection::p c) : connection(c), timer(c->socket.get_io_service()) {
}
-void Player::start() {
+void Player::start(boost::function<void (Player::p)> f) {
// Send Hello.
connection->send(make_shared<Message::Hello>("aotenjoud git"));
// Wait for Login.
- connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1));
+ connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, f));
}
-void Player::handle_login(Message::p msg) {
+void Player::handle_login(Message::p msg, boost::function<void (Player::p)> lobby_callback) {
if(msg->type != Message::Types::Login) {
return;
}
@@ -30,7 +30,7 @@ void Player::handle_login(Message::p 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));
+ connection->recv(boost::bind(&Player::handle_login, shared_from_this(), _1, lobby_callback));
return;
}
@@ -41,7 +41,7 @@ void Player::handle_login(Message::p msg) {
lobby_callback(shared_from_this());
}
-void Player::handle_ready(Message::p msg) {
+void Player::handle_ready(Message::p msg, boost::function<void ()> ready_callback) {
if(msg->type != Message::Types::Ready) {
return;
}
@@ -49,14 +49,14 @@ void Player::handle_ready(Message::p msg) {
ready_callback();
}
-void Player::handle_action(Message::p msg) {
+void Player::handle_action(Message::p msg, boost::function<void (Action)> action_callback) {
if(msg->type != Message::Types::RoundAction) {
return;
}
Message::RoundAction::p action_msg = dynamic_pointer_cast<Message::RoundAction>(msg);
- action_msg->action.player = id;
+ //action_msg->action.player = id;
if(action_callback != 0) {
action_callback(action_msg->action);
}
@@ -67,8 +67,6 @@ std::string Player::nick() {
}
void Player::game_start(boost::function<void ()> callback, std::vector<Player::p> players) {
- ready_callback = callback;
-
Message::GameStart::p msg = make_shared<Message::GameStart>();
for(std::vector<Player::p>::iterator i = players.begin(); i != players.end(); i++) {
@@ -78,7 +76,7 @@ void Player::game_start(boost::function<void ()> callback, std::vector<Player::p
connection->send(msg);
- connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1));
+ connection->recv(boost::bind(&Player::handle_ready, shared_from_this(), _1, callback));
}
void Player::round_start() {
@@ -94,11 +92,9 @@ void Player::round_end() {
}
void Player::get_action(boost::function<void (Action)> callback) {
- action_callback = callback;
-
- connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1));
+ connection->recv(boost::bind(&Player::handle_action, shared_from_this(), _1, callback));
}
void Player::kill_action() {
- action_callback = 0;
+
}
diff --git a/server/player.h b/server/player.h
index af9c59e..d0dc73b 100644
--- a/server/player.h
+++ b/server/player.h
@@ -20,25 +20,21 @@ class Player : public boost::enable_shared_from_this<Player> {
boost::asio::deadline_timer timer;
- boost::function<void (Player::p)> lobby_callback;
- boost::function<void ()> ready_callback;
- boost::function<void (Action)> action_callback;
-
std::string nick_;
- Player(Connection::p c, boost::function<void (Player::p)> f);
+ Player(Connection::p c);
//! Start communicating.
- void start();
+ void start(boost::function<void (Player::p)> f);
//! Handle Login-message.
- void handle_login(Message::p msg);
+ void handle_login(Message::p msg, boost::function<void (Player::p)> lobby_callback);
//! Handle Ready-message.
- void handle_ready(Message::p msg);
+ void handle_ready(Message::p msg, boost::function<void ()> ready_callback);
//! Handle Action-message.
- void handle_action(Message::p msg);
+ void handle_action(Message::p msg, boost::function<void (Action)> action_callback);
public:
//! The ID of the player