#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::disconnect() { //delete connection; //connection = NULL; //We need to make this player dumb? } void Client::reconnect(Connection::p c) { connection = c; } 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 login_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, login_callback)); return; } connection->send(make_shared(true)); nick_ = login_msg->nick; login_callback(shared_from_this()); } void Client::handle_lobby(Message::p msg, boost::function lobby_callback) { if(msg->type != Message::Types::LobbyAction) { return; } Message::LobbyAction::p lobby_msg = dynamic_pointer_cast(msg); lobby_callback(lobby_msg->index); } 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.back()); } } void Client::lobby_status(const std::vector& game_modes, boost::function callback) { Message::LobbyStatus::p msg = make_shared(); msg->game_modes = game_modes; connection->send(msg); connection->recv(boost::bind(&Client::handle_lobby, shared_from_this(), _1, callback)); } unsigned int Client::id() { return id_; } std::string Client::nick() { return nick_; } void Client::game_start(boost::function callback, std::vector players) { Message::GameStart::p msg = make_shared(); msg->players = players; msg->player_id = 0; 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(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a) { connection->send(make_shared(pl_d, pl_r, pl_u, pl_l, g, a)); } void Client::round_end(boost::function callback) { connection->send(make_shared()); // Check if we're waiting for ready. if(callback) { connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); } } void Client::get_action(boost::function callback, Actions expected_actions) { connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); } unsigned int ClientDumb::id() { return 0; } std::string ClientDumb::nick() { return "CPU"; } void ClientDumb::game_start(boost::function callback, std::vector players) { callback(); } void ClientDumb::round_start() { } void ClientDumb::round_state(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a) { } void ClientDumb::round_end(boost::function callback) { callback(); } void ClientDumb::get_action(boost::function callback, Actions expected_actions) { callback(expected_actions.back()); }