Skip to content

Commit

Permalink
Wesnothd/Player Connection: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Aug 10, 2019
1 parent 4819846 commit ed7d60c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 56 deletions.
28 changes: 14 additions & 14 deletions src/server/player_connection.cpp
@@ -1,23 +1,23 @@
/*
Copyright (C) 2016 - 2018 by Sergey Popov <loonycyborg@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
Copyright (C) 2016 - 2018 by Sergey Popov <loonycyborg@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
See the COPYING file for more details.
*/

#include "server/player_connection.hpp"

#include "server/game.hpp"

namespace wesnothd
{

const std::shared_ptr<game> player_record::get_game() const
{
return game_;
Expand All @@ -33,14 +33,14 @@ int player_record::game_id() const
return game_ ? game_->id() : 0;
}

void player_record::set_game(player_record& record, std::shared_ptr<game> new_game)
void player_record::set_game(std::shared_ptr<game> new_game)
{
record.game_ = new_game;
game_ = new_game;
}

void player_record::enter_lobby(player_record& record)
void player_record::enter_lobby()
{
record.game_.reset();
game_.reset();
}

}
99 changes: 60 additions & 39 deletions src/server/player_connection.hpp
Expand Up @@ -14,63 +14,84 @@

#pragma once

#include "simple_wml.hpp"
#include "player.hpp"
#include "server_base.hpp"
#include "server/player.hpp"
#include "server/server_base.hpp"
#include "server/simple_wml.hpp"

#ifndef _WIN32
#define BOOST_ASIO_DISABLE_THREADS
#define BOOST_ASIO_DISABLE_THREADS
#endif
#include <boost/asio.hpp>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index_container.hpp>

namespace wesnothd
{

class player_record
{
const socket_ptr socket_;
mutable player player_;
std::shared_ptr<game> game_;

std::string ip_address;
public:
player_record(const socket_ptr socket, const player& player)
: socket_(socket)
, player_(player)
, game_()
, ip_address(client_address(socket))
{
}

const socket_ptr socket() const
{
return socket_;
}

std::string saved_client_ip() const
{
return ip_address;
}

player& info() const
{
return player_;
}

const std::string& name() const
{
return player_.name();
}

public:

const socket_ptr socket() const { return socket_; }
std::string saved_client_ip() const { return ip_address; }
player& info() const { return player_; }
const std::string& name() const { return player_.name(); }
const std::shared_ptr<game> get_game() const;

std::shared_ptr<game>& get_game();

int game_id() const;
static void set_game(player_record&, std::shared_ptr<game>);
static void enter_lobby(player_record&);

player_record(const socket_ptr socket, const player& player) : socket_(socket), player_(player), ip_address(client_address(socket)) {}
void set_game(std::shared_ptr<game> new_game);

void enter_lobby();

private:
const socket_ptr socket_;
mutable player player_;
std::shared_ptr<game> game_;
std::string ip_address;
};

struct socket_t{};
struct name_t{};
struct game_t{};

using namespace boost::multi_index;

typedef multi_index_container<
player_record,
indexed_by<
ordered_unique<
tag<socket_t>, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record,const socket_ptr,socket)>,
hashed_unique<
tag<name_t>, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record,const std::string&,name)>,
ordered_non_unique<
tag<game_t>, BOOST_MULTI_INDEX_CONST_MEM_FUN(player_record,int,game_id)>
>
> player_connections;
struct socket_t {};
struct name_t {};
struct game_t {};

namespace bmi = boost::multi_index;

using player_connections = bmi::multi_index_container<player_record, bmi::indexed_by<
bmi::ordered_unique<bmi::tag<socket_t>,
bmi::const_mem_fun<player_record, const socket_ptr, &player_record::socket>>,
bmi::hashed_unique<bmi::tag<name_t>,
bmi::const_mem_fun<player_record, const std::string&, &player_record::name>>,
bmi::ordered_non_unique<bmi::tag<game_t>,
bmi::const_mem_fun<player_record, int, &player_record::game_id>>
>>;

} // namespace wesnothd
6 changes: 3 additions & 3 deletions src/server/server.cpp
Expand Up @@ -1724,7 +1724,7 @@ void server::handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml
} else {
auto description = g.description();

player_connections_.modify(player_connections_.find(socket), player_record::enter_lobby);
player_connections_.modify(player_connections_.find(socket), std::bind(&player_record::enter_lobby, _1));
if(!g_ptr.expired()) {
g.describe_slots();
}
Expand Down Expand Up @@ -1801,7 +1801,7 @@ void server::handle_player_in_game(socket_ptr socket, std::shared_ptr<simple_wml
: g.kick_member(*data.child("kick"), socket));

if(user) {
player_connections_.modify(player_connections_.find(user), player_record::enter_lobby);
player_connections_.modify(player_connections_.find(user), std::bind(&player_record::enter_lobby, _1));
if(g.describe_slots()) {
update_game_in_lobby(g, user);
}
Expand Down Expand Up @@ -2927,7 +2927,7 @@ void server::delete_game(int gameid)
// This will call cleanup_game() deleter since there won't
// be any references to that game from player_connections_ anymore
for(const auto& it : range_vctor) {
player_connections_.get<game_t>().modify(it, player_record::enter_lobby);
player_connections_.get<game_t>().modify(it, std::bind(&player_record::enter_lobby, _1));
}

// send users in the game a notification to leave the game since it has ended
Expand Down

0 comments on commit ed7d60c

Please sign in to comment.