diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-25 03:01:43 +0100 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-11-25 03:01:43 +0100 |
commit | 4282a9d069b3bedc8888a4e6202f2b066233d4ec (patch) | |
tree | 8e0646804a7b33883be5867833e7b02004ddca1c /server/client.cpp | |
parent | d5aae396856ee801eb2e2420b786fda2f13cf391 (diff) |
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.
Diffstat (limited to 'server/client.cpp')
-rw-r--r-- | server/client.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
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 <boost/bind.hpp> + +Client::p Client::create(Connection::p c, boost::function<void (Client::p)> 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<void (Client::p)> f) { + // Send Hello. + connection->send(make_shared<Message::Hello>("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<void (Client::p)> lobby_callback) { + 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(&Client::handle_login, shared_from_this(), _1, lobby_callback)); + return; + } + + connection->send(make_shared<Message::LoginResponse>(true)); + + nick_ = login_msg->nick; + + lobby_callback(shared_from_this()); +} + +void Client::handle_ready(Message::p msg, boost::function<void ()> ready_callback) { + if(msg->type != Message::Types::Ready) { + return; + } + + ready_callback(); +} + +void Client::handle_action(Message::p msg, boost::function<void (Action)> action_callback, Actions possible_actions) { + if(msg->type != Message::Types::RoundAction) { + return; + } + + Message::RoundAction::p action_msg = dynamic_pointer_cast<Message::RoundAction>(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<void ()> callback, std::vector<Client::p> players) { + Message::GameStart::p msg = make_shared<Message::GameStart>(); + + for(std::vector<Client::p>::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<Message::RoundStart>()); +} + +void Client::round_state(State state) { + connection->send(make_shared<Message::RoundState>(state)); +} + +void Client::round_end() { + connection->send(make_shared<Message::RoundEnd>()); +} + +void Client::get_action(boost::function<void (Action)> callback, Actions expected_actions) { + connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); +} + +void Client::kill_action() { + +} |