Separate match object out from tournament and give it a fsm,
start changing genesis json
This commit is contained in:
parent
5186ef4076
commit
eb8929f6fa
10 changed files with 524 additions and 57 deletions
|
|
@ -76,6 +76,7 @@ add_library( graphene_chain
|
||||||
vesting_balance_evaluator.cpp
|
vesting_balance_evaluator.cpp
|
||||||
tournament_evaluator.cpp
|
tournament_evaluator.cpp
|
||||||
tournament_object.cpp
|
tournament_object.cpp
|
||||||
|
match_object.cpp
|
||||||
withdraw_permission_evaluator.cpp
|
withdraw_permission_evaluator.cpp
|
||||||
worker_evaluator.cpp
|
worker_evaluator.cpp
|
||||||
confidential_evaluator.cpp
|
confidential_evaluator.cpp
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@
|
||||||
#include <graphene/chain/witness_schedule_object.hpp>
|
#include <graphene/chain/witness_schedule_object.hpp>
|
||||||
#include <graphene/chain/worker_object.hpp>
|
#include <graphene/chain/worker_object.hpp>
|
||||||
#include <graphene/chain/tournament_object.hpp>
|
#include <graphene/chain/tournament_object.hpp>
|
||||||
|
#include <graphene/chain/match_object.hpp>
|
||||||
|
|
||||||
#include <graphene/chain/account_evaluator.hpp>
|
#include <graphene/chain/account_evaluator.hpp>
|
||||||
#include <graphene/chain/asset_evaluator.hpp>
|
#include <graphene/chain/asset_evaluator.hpp>
|
||||||
|
|
|
||||||
|
|
@ -476,44 +476,93 @@ void database::update_withdraw_permissions()
|
||||||
remove(*permit_index.begin());
|
remove(*permit_index.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
void database::update_tournaments()
|
void process_finished_games(database& db)
|
||||||
|
{
|
||||||
|
//auto& games_index = db.get_index_type<game_index>().indices().get<by_id>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_finished_matches(database& db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void process_in_progress_tournaments(database& db)
|
||||||
|
{
|
||||||
|
auto& start_time_index = db.get_index_type<tournament_index>().indices().get<by_start_time>();
|
||||||
|
auto start_iter = start_time_index.lower_bound(boost::make_tuple(tournament_state::in_progress));
|
||||||
|
while (start_iter != start_time_index.end() &&
|
||||||
|
start_iter->get_state() == tournament_state::in_progress)
|
||||||
|
{
|
||||||
|
auto next_iter = std::next(start_iter);
|
||||||
|
start_iter->check_for_new_matches_to_start(db);
|
||||||
|
start_iter = next_iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel_expired_tournaments(database& db)
|
||||||
{
|
{
|
||||||
// First, cancel any tournaments that didn't get enough players
|
// First, cancel any tournaments that didn't get enough players
|
||||||
auto& registration_deadline_index = get_index_type<tournament_index>().indices().get<by_registration_deadline>();
|
auto& registration_deadline_index = db.get_index_type<tournament_index>().indices().get<by_registration_deadline>();
|
||||||
// this index is sorted on state and deadline, so the tournaments awaiting registrations with the earliest
|
// this index is sorted on state and deadline, so the tournaments awaiting registrations with the earliest
|
||||||
// deadlines will be at the beginning
|
// deadlines will be at the beginning
|
||||||
while (!registration_deadline_index.empty() &&
|
while (!registration_deadline_index.empty() &&
|
||||||
registration_deadline_index.begin()->get_state() == tournament_state::accepting_registrations &&
|
registration_deadline_index.begin()->get_state() == tournament_state::accepting_registrations &&
|
||||||
registration_deadline_index.begin()->options.registration_deadline <= head_block_time())
|
registration_deadline_index.begin()->options.registration_deadline <= db.head_block_time())
|
||||||
{
|
{
|
||||||
const tournament_object& tournament_obj = *registration_deadline_index.begin();
|
const tournament_object& tournament_obj = *registration_deadline_index.begin();
|
||||||
fc_ilog(fc::logger::get("tournament"),
|
fc_ilog(fc::logger::get("tournament"),
|
||||||
"Canceling tournament ${id} because its deadline expired",
|
"Canceling tournament ${id} because its deadline expired",
|
||||||
("id", tournament_obj.id));
|
("id", tournament_obj.id));
|
||||||
// cancel this tournament
|
// cancel this tournament
|
||||||
modify(tournament_obj, [&](tournament_object& t) {
|
db.modify(tournament_obj, [&](tournament_object& t) {
|
||||||
t.on_registration_deadline_passed(*this);
|
t.on_registration_deadline_passed(db);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_fully_registered_tournaments(database& db)
|
||||||
|
{
|
||||||
// Next, start any tournaments that have enough players and whose start time just arrived
|
// Next, start any tournaments that have enough players and whose start time just arrived
|
||||||
auto& start_time_index = get_index_type<tournament_index>().indices().get<by_start_time>();
|
auto& start_time_index = db.get_index_type<tournament_index>().indices().get<by_start_time>();
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// find the first tournament waiting to start; if its start time has arrived, start it
|
// find the first tournament waiting to start; if its start time has arrived, start it
|
||||||
auto start_iter = start_time_index.lower_bound(boost::make_tuple(tournament_state::awaiting_start));
|
auto start_iter = start_time_index.lower_bound(boost::make_tuple(tournament_state::awaiting_start));
|
||||||
if (start_iter != start_time_index.end() &&
|
if (start_iter != start_time_index.end() &&
|
||||||
start_iter->get_state() == tournament_state::awaiting_start &&
|
start_iter->get_state() == tournament_state::awaiting_start &&
|
||||||
*start_iter->start_time <= head_block_time())
|
*start_iter->start_time <= db.head_block_time())
|
||||||
{
|
{
|
||||||
modify(*start_iter, [&](tournament_object& t) {
|
db.modify(*start_iter, [&](tournament_object& t) {
|
||||||
t.on_start_time_arrived(*this);
|
t.on_start_time_arrived(db);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void initiate_next_round_of_matches(database& db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void initiate_next_games(database& db)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void database::update_tournaments()
|
||||||
|
{
|
||||||
|
// Process as follows:
|
||||||
|
// - Process games
|
||||||
|
// - Process matches
|
||||||
|
// - Process tournaments
|
||||||
|
// - Process matches
|
||||||
|
// - Process games
|
||||||
|
process_finished_games(*this);
|
||||||
|
process_finished_matches(*this);
|
||||||
|
cancel_expired_tournaments(*this);
|
||||||
|
start_fully_registered_tournaments(*this);
|
||||||
|
process_in_progress_tournaments(*this);
|
||||||
|
initiate_next_round_of_matches(*this);
|
||||||
|
initiate_next_games(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
} }
|
} }
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,27 @@ struct genesis_state_type {
|
||||||
public_key_type active_key;
|
public_key_type active_key;
|
||||||
bool is_lifetime_member = false;
|
bool is_lifetime_member = false;
|
||||||
};
|
};
|
||||||
|
struct initial_bts_account_type {
|
||||||
|
struct initial_authority {
|
||||||
|
uint32_t weight_threshold;
|
||||||
|
flat_map<string, weight_type> account_auths; // uses account name instead of account id
|
||||||
|
flat_map<public_key_type, weight_type> key_auths;
|
||||||
|
flat_map<address, weight_type> address_auths;
|
||||||
|
};
|
||||||
|
initial_bts_account_type(const string& name = string(),
|
||||||
|
const initial_authority& owner_authority = initial_authority(),
|
||||||
|
const initial_authority& active_authority = initial_authority(),
|
||||||
|
const share_type& core_balance = share_type())
|
||||||
|
: name(name),
|
||||||
|
owner_authority(owner_authority),
|
||||||
|
active_authority(active_authority),
|
||||||
|
core_balance(core_balance)
|
||||||
|
{}
|
||||||
|
string name;
|
||||||
|
initial_authority owner_authority;
|
||||||
|
initial_authority active_authority;
|
||||||
|
share_type core_balance;
|
||||||
|
};
|
||||||
struct initial_asset_type {
|
struct initial_asset_type {
|
||||||
struct initial_collateral_position {
|
struct initial_collateral_position {
|
||||||
address owner;
|
address owner;
|
||||||
|
|
@ -103,6 +124,7 @@ struct genesis_state_type {
|
||||||
share_type max_core_supply = GRAPHENE_MAX_SHARE_SUPPLY;
|
share_type max_core_supply = GRAPHENE_MAX_SHARE_SUPPLY;
|
||||||
chain_parameters initial_parameters;
|
chain_parameters initial_parameters;
|
||||||
immutable_chain_parameters immutable_parameters;
|
immutable_chain_parameters immutable_parameters;
|
||||||
|
vector<initial_bts_account_type> initial_bts_accounts;
|
||||||
vector<initial_account_type> initial_accounts;
|
vector<initial_account_type> initial_accounts;
|
||||||
vector<initial_asset_type> initial_assets;
|
vector<initial_asset_type> initial_assets;
|
||||||
vector<initial_balance_type> initial_balances;
|
vector<initial_balance_type> initial_balances;
|
||||||
|
|
@ -147,8 +169,20 @@ FC_REFLECT(graphene::chain::genesis_state_type::initial_committee_member_type, (
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_worker_type, (owner_name)(daily_pay))
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_worker_type, (owner_name)(daily_pay))
|
||||||
|
|
||||||
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_bts_account_type::initial_authority,
|
||||||
|
(weight_threshold)
|
||||||
|
(account_auths)
|
||||||
|
(key_auths)
|
||||||
|
(address_auths))
|
||||||
|
|
||||||
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_bts_account_type,
|
||||||
|
(name)
|
||||||
|
(owner_authority)
|
||||||
|
(active_authority)
|
||||||
|
(core_balance))
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type,
|
FC_REFLECT(graphene::chain::genesis_state_type,
|
||||||
(initial_timestamp)(max_core_supply)(initial_parameters)(initial_accounts)(initial_assets)(initial_balances)
|
(initial_timestamp)(max_core_supply)(initial_parameters)(initial_bts_accounts)(initial_accounts)(initial_assets)(initial_balances)
|
||||||
(initial_vesting_balances)(initial_active_witnesses)(initial_witness_candidates)
|
(initial_vesting_balances)(initial_active_witnesses)(initial_witness_candidates)
|
||||||
(initial_committee_candidates)(initial_worker_candidates)
|
(initial_committee_candidates)(initial_worker_candidates)
|
||||||
(initial_chain_id)
|
(initial_chain_id)
|
||||||
|
|
|
||||||
152
libraries/chain/include/graphene/chain/match_object.hpp
Normal file
152
libraries/chain/include/graphene/chain/match_object.hpp
Normal file
|
|
@ -0,0 +1,152 @@
|
||||||
|
#pragma once
|
||||||
|
#include <graphene/chain/protocol/tournament.hpp>
|
||||||
|
#include <graphene/chain/rock_paper_scissors.hpp>
|
||||||
|
#include <boost/multi_index/composite_key.hpp>
|
||||||
|
#include <graphene/db/flat_index.hpp>
|
||||||
|
#include <graphene/db/generic_index.hpp>
|
||||||
|
#include <fc/crypto/hex.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace graphene { namespace chain {
|
||||||
|
class match_object;
|
||||||
|
} }
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
void to_variant(const graphene::chain::match_object& match_obj, fc::variant& v);
|
||||||
|
void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj);
|
||||||
|
} //end namespace fc
|
||||||
|
|
||||||
|
namespace graphene { namespace chain {
|
||||||
|
class database;
|
||||||
|
using namespace graphene::db;
|
||||||
|
|
||||||
|
enum class match_state
|
||||||
|
{
|
||||||
|
waiting_on_previous_matches,
|
||||||
|
match_in_progress,
|
||||||
|
match_complete
|
||||||
|
};
|
||||||
|
|
||||||
|
class match_object : public graphene::db::abstract_object<match_object>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const uint8_t space_id = protocol_ids;
|
||||||
|
static const uint8_t type_id = match_object_type;
|
||||||
|
|
||||||
|
tournament_id_type tournament_id;
|
||||||
|
|
||||||
|
/// The players in the match
|
||||||
|
vector<account_id_type> players;
|
||||||
|
|
||||||
|
/// The list of games in the match
|
||||||
|
/// Unlike tournaments where the list of matches is known at the start,
|
||||||
|
/// the list of games will start with one game and grow until we have played
|
||||||
|
/// enough games to declare a winner for the match.
|
||||||
|
vector<game_id_type> games;
|
||||||
|
|
||||||
|
/// A list of the winners of each round of the game. This information is
|
||||||
|
/// also stored in the game object, but is duplicated here to allow displaying
|
||||||
|
/// information about a match without having to request all game objects
|
||||||
|
vector<flat_set<account_id_type> > game_winners;
|
||||||
|
|
||||||
|
// If the match is not yet complete, this will be empty
|
||||||
|
// If the match is in the "match_complete" state, it will contain the
|
||||||
|
// list of winners.
|
||||||
|
// For Rock-paper-scissors, there will be one winner, unless there is
|
||||||
|
// a stalemate (in that case, there are no winners)
|
||||||
|
flat_set<account_id_type> match_winners;
|
||||||
|
|
||||||
|
/// the time the match started
|
||||||
|
time_point_sec start_time;
|
||||||
|
|
||||||
|
/// If the match has ended, the time it ended
|
||||||
|
optional<time_point_sec> end_time;
|
||||||
|
|
||||||
|
match_object();
|
||||||
|
match_object(const match_object& rhs);
|
||||||
|
~match_object();
|
||||||
|
match_object& operator=(const match_object& rhs);
|
||||||
|
|
||||||
|
match_state get_state() const;
|
||||||
|
|
||||||
|
// serialization functions:
|
||||||
|
// for serializing to raw, go through a temporary sstream object to avoid
|
||||||
|
// having to implement serialization in the header file
|
||||||
|
template<typename Stream>
|
||||||
|
friend Stream& operator<<( Stream& s, const match_object& match_obj );
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
friend Stream& operator>>( Stream& s, match_object& match_obj );
|
||||||
|
|
||||||
|
friend void ::fc::to_variant(const graphene::chain::match_object& match_obj, fc::variant& v);
|
||||||
|
friend void ::fc::from_variant(const fc::variant& v, graphene::chain::match_object& match_obj);
|
||||||
|
|
||||||
|
void pack_impl(std::ostream& stream) const;
|
||||||
|
void unpack_impl(std::istream& stream);
|
||||||
|
game_id_type start_next_game(database& db, match_id_type match_id);
|
||||||
|
|
||||||
|
class impl;
|
||||||
|
std::unique_ptr<impl> my;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef multi_index_container<
|
||||||
|
match_object,
|
||||||
|
indexed_by<
|
||||||
|
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > > >
|
||||||
|
> match_object_multi_index_type;
|
||||||
|
typedef generic_index<match_object, match_object_multi_index_type> match_index;
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
inline Stream& operator<<( Stream& s, const match_object& match_obj )
|
||||||
|
{
|
||||||
|
// pack all fields exposed in the header in the usual way
|
||||||
|
// instead of calling the derived pack, just serialize the one field in the base class
|
||||||
|
// fc::raw::pack<Stream, const graphene::db::abstract_object<match_object> >(s, match_obj);
|
||||||
|
fc::raw::pack(s, match_obj.id);
|
||||||
|
fc::raw::pack(s, match_obj.players);
|
||||||
|
fc::raw::pack(s, match_obj.games);
|
||||||
|
fc::raw::pack(s, match_obj.game_winners);
|
||||||
|
fc::raw::pack(s, match_obj.match_winners);
|
||||||
|
fc::raw::pack(s, match_obj.start_time);
|
||||||
|
fc::raw::pack(s, match_obj.end_time);
|
||||||
|
|
||||||
|
// fc::raw::pack the contents hidden in the impl class
|
||||||
|
std::ostringstream stream;
|
||||||
|
match_obj.pack_impl(stream);
|
||||||
|
std::string stringified_stream(stream.str());
|
||||||
|
fc::raw::pack(s, stream.str());
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
inline Stream& operator>>( Stream& s, match_object& match_obj )
|
||||||
|
{
|
||||||
|
// unpack all fields exposed in the header in the usual way
|
||||||
|
//fc::raw::unpack<Stream, graphene::db::abstract_object<match_object> >(s, match_obj);
|
||||||
|
fc::raw::unpack(s, match_obj.id);
|
||||||
|
fc::raw::unpack(s, match_obj.players);
|
||||||
|
fc::raw::unpack(s, match_obj.games);
|
||||||
|
fc::raw::unpack(s, match_obj.game_winners);
|
||||||
|
fc::raw::unpack(s, match_obj.match_winners);
|
||||||
|
fc::raw::unpack(s, match_obj.start_time);
|
||||||
|
fc::raw::unpack(s, match_obj.end_time);
|
||||||
|
|
||||||
|
// fc::raw::unpack the contents hidden in the impl class
|
||||||
|
std::string stringified_stream;
|
||||||
|
fc::raw::unpack(s, stringified_stream);
|
||||||
|
std::istringstream stream(stringified_stream);
|
||||||
|
match_obj.unpack_impl(stream);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
} }
|
||||||
|
|
||||||
|
FC_REFLECT_ENUM(graphene::chain::match_state,
|
||||||
|
(waiting_on_previous_matches)
|
||||||
|
(match_in_progress)
|
||||||
|
(match_complete))
|
||||||
|
|
||||||
|
FC_REFLECT_TYPENAME(graphene::chain::match_object) // manually serialized
|
||||||
|
|
||||||
|
|
@ -40,7 +40,9 @@ namespace graphene { namespace chain {
|
||||||
bool insurance_enabled;
|
bool insurance_enabled;
|
||||||
/// The number of seconds users are given to commit their next move, counted from the beginning
|
/// The number of seconds users are given to commit their next move, counted from the beginning
|
||||||
/// of the hand (during the game, a hand begins immediately on the block containing the
|
/// of the hand (during the game, a hand begins immediately on the block containing the
|
||||||
/// second player's reveal or where the time_per_reveal move has expired)
|
/// second player's reveal or where the time_per_reveal move has expired).
|
||||||
|
/// Note, if these times aren't an even multiple of the block interval, they will be rounded
|
||||||
|
/// up.
|
||||||
uint32_t time_per_commit_move;
|
uint32_t time_per_commit_move;
|
||||||
|
|
||||||
/// The number of seconds users are given to reveal their move, counted from the time of the
|
/// The number of seconds users are given to reveal their move, counted from the time of the
|
||||||
|
|
@ -48,13 +50,6 @@ namespace graphene { namespace chain {
|
||||||
uint32_t time_per_reveal_move;
|
uint32_t time_per_reveal_move;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct rock_paper_scissors_game_details
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
typedef fc::static_variant<rock_paper_scissors_game_options> game_specific_details;
|
|
||||||
|
|
||||||
} }
|
} }
|
||||||
|
|
||||||
FC_REFLECT( graphene::chain::rock_paper_scissors_game_options, (insurance_enabled)(time_per_commit_move)(time_per_reveal_move) )
|
FC_REFLECT( graphene::chain::rock_paper_scissors_game_options, (insurance_enabled)(time_per_commit_move)(time_per_reveal_move) )
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <graphene/chain/protocol/tournament.hpp>
|
#include <graphene/chain/protocol/tournament.hpp>
|
||||||
|
#include <graphene/chain/rock_paper_scissors.hpp>
|
||||||
#include <boost/multi_index/composite_key.hpp>
|
#include <boost/multi_index/composite_key.hpp>
|
||||||
#include <graphene/db/flat_index.hpp>
|
#include <graphene/db/flat_index.hpp>
|
||||||
#include <graphene/db/generic_index.hpp>
|
#include <graphene/db/generic_index.hpp>
|
||||||
|
|
@ -112,32 +113,12 @@ namespace graphene { namespace chain {
|
||||||
void on_start_time_arrived(database& db);
|
void on_start_time_arrived(database& db);
|
||||||
void on_final_game_completed();
|
void on_final_game_completed();
|
||||||
|
|
||||||
|
void check_for_new_matches_to_start(database& db) const;
|
||||||
private:
|
private:
|
||||||
class impl;
|
class impl;
|
||||||
std::unique_ptr<impl> my;
|
std::unique_ptr<impl> my;
|
||||||
};
|
};
|
||||||
|
|
||||||
class match_object : public graphene::db::abstract_object<match_object>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static const uint8_t space_id = protocol_ids;
|
|
||||||
static const uint8_t type_id = match_object_type;
|
|
||||||
|
|
||||||
tournament_id_type tournament_id;
|
|
||||||
///
|
|
||||||
vector<account_id_type> players;
|
|
||||||
|
|
||||||
vector<game_id_type> games;
|
|
||||||
vector<flat_set<account_id_type> > game_winners;
|
|
||||||
|
|
||||||
/// the time the match started
|
|
||||||
time_point_sec start_time;
|
|
||||||
/// If the match has ended, the time it ended
|
|
||||||
optional<time_point_sec> end_time;
|
|
||||||
|
|
||||||
game_id_type start_next_game(database& db, match_id_type match_id);
|
|
||||||
};
|
|
||||||
|
|
||||||
class game_object : public graphene::db::abstract_object<game_object>
|
class game_object : public graphene::db::abstract_object<game_object>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -149,10 +130,10 @@ namespace graphene { namespace chain {
|
||||||
vector<account_id_type> players;
|
vector<account_id_type> players;
|
||||||
|
|
||||||
flat_set<account_id_type> winners;
|
flat_set<account_id_type> winners;
|
||||||
|
|
||||||
|
game_specific_details game_details;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct by_registration_deadline {};
|
struct by_registration_deadline {};
|
||||||
struct by_start_time {};
|
struct by_start_time {};
|
||||||
typedef multi_index_container<
|
typedef multi_index_container<
|
||||||
|
|
@ -178,13 +159,6 @@ namespace graphene { namespace chain {
|
||||||
> tournament_details_object_multi_index_type;
|
> tournament_details_object_multi_index_type;
|
||||||
typedef generic_index<tournament_details_object, tournament_details_object_multi_index_type> tournament_details_index;
|
typedef generic_index<tournament_details_object, tournament_details_object_multi_index_type> tournament_details_index;
|
||||||
|
|
||||||
typedef multi_index_container<
|
|
||||||
match_object,
|
|
||||||
indexed_by<
|
|
||||||
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > > >
|
|
||||||
> match_object_multi_index_type;
|
|
||||||
typedef generic_index<match_object, match_object_multi_index_type> match_index;
|
|
||||||
|
|
||||||
typedef multi_index_container<
|
typedef multi_index_container<
|
||||||
game_object,
|
game_object,
|
||||||
indexed_by<
|
indexed_by<
|
||||||
|
|
@ -258,15 +232,9 @@ FC_REFLECT_ENUM(graphene::chain::tournament_state,
|
||||||
(registration_period_expired)
|
(registration_period_expired)
|
||||||
(concluded))
|
(concluded))
|
||||||
|
|
||||||
FC_REFLECT_DERIVED(graphene::chain::match_object, (graphene::db::object),
|
|
||||||
(tournament_id)
|
|
||||||
(players)
|
|
||||||
(games)
|
|
||||||
(game_winners)
|
|
||||||
(start_time)
|
|
||||||
(end_time))
|
|
||||||
FC_REFLECT_DERIVED(graphene::chain::game_object, (graphene::db::object),
|
FC_REFLECT_DERIVED(graphene::chain::game_object, (graphene::db::object),
|
||||||
(match_id)
|
(match_id)
|
||||||
(players)
|
(players)
|
||||||
(winners))
|
(winners)
|
||||||
|
(game_details))
|
||||||
|
|
||||||
|
|
|
||||||
266
libraries/chain/match_object.cpp
Normal file
266
libraries/chain/match_object.cpp
Normal file
|
|
@ -0,0 +1,266 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
|
||||||
|
*
|
||||||
|
* The MIT License
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include <graphene/chain/database.hpp>
|
||||||
|
#include <graphene/chain/tournament_object.hpp>
|
||||||
|
#include <graphene/chain/match_object.hpp>
|
||||||
|
|
||||||
|
#include <boost/msm/back/state_machine.hpp>
|
||||||
|
#include <boost/msm/front/state_machine_def.hpp>
|
||||||
|
#include <boost/archive/binary_oarchive.hpp>
|
||||||
|
#include <boost/archive/binary_iarchive.hpp>
|
||||||
|
#include <boost/msm/back/tools.hpp>
|
||||||
|
|
||||||
|
namespace graphene { namespace chain {
|
||||||
|
|
||||||
|
namespace msm = boost::msm;
|
||||||
|
namespace mpl = boost::mpl;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
// Events
|
||||||
|
struct previous_round_complete
|
||||||
|
{
|
||||||
|
database& db;
|
||||||
|
vector<account_id_type> players;
|
||||||
|
previous_round_complete(database& db, const vector<account_id_type> players) :
|
||||||
|
db(db), players(players)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
struct game_complete
|
||||||
|
{
|
||||||
|
database& db;
|
||||||
|
game_id_type game_id;
|
||||||
|
game_complete(database& db, game_id_type game_id) : db(db), game_id(game_id) {};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct match_state_machine_ : public msm::front::state_machine_def<match_state_machine_>
|
||||||
|
{
|
||||||
|
// disable a few state machine features we don't use for performance
|
||||||
|
typedef int no_exception_thrown;
|
||||||
|
typedef int no_message_queue;
|
||||||
|
|
||||||
|
// States
|
||||||
|
struct waiting_on_previous_matches : public msm::front::state<>{};
|
||||||
|
struct match_in_progress : public msm::front::state<>
|
||||||
|
{
|
||||||
|
void on_entry(const game_complete& event, match_state_machine_& fsm)
|
||||||
|
{
|
||||||
|
fc_ilog(fc::logger::get("tournament"),
|
||||||
|
"Match ${id} is now in progress",
|
||||||
|
("id", fsm.match_obj->id));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
struct match_complete : public msm::front::state<>
|
||||||
|
{
|
||||||
|
void on_entry(const game_complete& event, match_state_machine_& fsm)
|
||||||
|
{
|
||||||
|
fc_ilog(fc::logger::get("tournament"),
|
||||||
|
"Match ${id} is complete",
|
||||||
|
("id", fsm.match_obj->id));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
typedef waiting_on_previous_matches initial_state;
|
||||||
|
|
||||||
|
typedef match_state_machine_ x; // makes transition table cleaner
|
||||||
|
|
||||||
|
// Guards
|
||||||
|
bool was_final_game(const game_complete& event)
|
||||||
|
{
|
||||||
|
fc_ilog(fc::logger::get("tournament"),
|
||||||
|
"In was_final_game guard, returning ${value}",
|
||||||
|
("value", false));// match_obj->registered_players == match_obj->options.number_of_players - 1));
|
||||||
|
return false;
|
||||||
|
//return match_obj->registered_players == match_obj->options.number_of_players - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_next_game(const game_complete& event)
|
||||||
|
{
|
||||||
|
fc_ilog(fc::logger::get("tournament"),
|
||||||
|
"In start_next_game action");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transition table for tournament
|
||||||
|
struct transition_table : mpl::vector<
|
||||||
|
// Start Event Next Action Guard
|
||||||
|
// +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+
|
||||||
|
_row < waiting_on_previous_matches, previous_round_complete, match_in_progress >,
|
||||||
|
// +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+
|
||||||
|
a_row < match_in_progress, game_complete, match_in_progress, &x::start_next_game >,
|
||||||
|
g_row < match_in_progress, game_complete, match_complete, &x::was_final_game >
|
||||||
|
// +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+
|
||||||
|
> {};
|
||||||
|
|
||||||
|
|
||||||
|
match_object* match_obj;
|
||||||
|
match_state_machine_(match_object* match_obj) : match_obj(match_obj) {}
|
||||||
|
};
|
||||||
|
typedef msm::back::state_machine<match_state_machine_> match_state_machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
class match_object::impl {
|
||||||
|
public:
|
||||||
|
match_state_machine state_machine;
|
||||||
|
|
||||||
|
impl(match_object* self) : state_machine(self) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
match_object::match_object() :
|
||||||
|
my(new impl(this))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
match_object::match_object(const match_object& rhs) :
|
||||||
|
graphene::db::abstract_object<match_object>(rhs),
|
||||||
|
players(rhs.players),
|
||||||
|
games(rhs.games),
|
||||||
|
game_winners(rhs.game_winners),
|
||||||
|
match_winners(rhs.match_winners),
|
||||||
|
start_time(rhs.start_time),
|
||||||
|
end_time(rhs.end_time),
|
||||||
|
my(new impl(this))
|
||||||
|
{
|
||||||
|
my->state_machine = rhs.my->state_machine;
|
||||||
|
my->state_machine.match_obj = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
match_object& match_object::operator=(const match_object& rhs)
|
||||||
|
{
|
||||||
|
//graphene::db::abstract_object<match_object>::operator=(rhs);
|
||||||
|
id = rhs.id;
|
||||||
|
players = rhs.players;
|
||||||
|
games = rhs.games;
|
||||||
|
game_winners = rhs.game_winners;
|
||||||
|
match_winners = rhs.match_winners;
|
||||||
|
start_time = rhs.start_time;
|
||||||
|
end_time = rhs.end_time;
|
||||||
|
my->state_machine = rhs.my->state_machine;
|
||||||
|
my->state_machine.match_obj = this;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
match_object::~match_object()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool verify_match_state_constants()
|
||||||
|
{
|
||||||
|
unsigned error_count = 0;
|
||||||
|
typedef msm::back::generate_state_set<match_state_machine::stt>::type all_states;
|
||||||
|
static char const* filled_state_names[mpl::size<all_states>::value];
|
||||||
|
mpl::for_each<all_states,boost::msm::wrap<mpl::placeholders::_1> >
|
||||||
|
(msm::back::fill_state_names<match_state_machine::stt>(filled_state_names));
|
||||||
|
for (unsigned i = 0; i < mpl::size<all_states>::value; ++i)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// this is an approximate test, the state name provided by typeinfo will be mangled, but should
|
||||||
|
// at least contain the string we're looking for
|
||||||
|
const char* fc_reflected_value_name = fc::reflector<match_state>::to_string((match_state)i);
|
||||||
|
if (!strcmp(fc_reflected_value_name, filled_state_names[i]))
|
||||||
|
fc_elog(fc::logger::get("match"),
|
||||||
|
"Error, state string mismatch between fc and boost::msm for int value ${int_value}: boost::msm -> ${boost_string}, fc::reflect -> ${fc_string}",
|
||||||
|
("int_value", i)("boost_string", filled_state_names[i])("fc_string", fc_reflected_value_name));
|
||||||
|
}
|
||||||
|
catch (const fc::bad_cast_exception&)
|
||||||
|
{
|
||||||
|
fc_elog(fc::logger::get("match"),
|
||||||
|
"Error, no reflection for value ${int_value} in enum match_state",
|
||||||
|
("int_value", i));
|
||||||
|
++error_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return error_count == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
match_state match_object::get_state() const
|
||||||
|
{
|
||||||
|
static bool state_constants_are_correct = verify_match_state_constants();
|
||||||
|
(void)&state_constants_are_correct;
|
||||||
|
match_state state = (match_state)my->state_machine.current_state()[0];
|
||||||
|
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void match_object::pack_impl(std::ostream& stream) const
|
||||||
|
{
|
||||||
|
boost::archive::binary_oarchive oa(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking);
|
||||||
|
oa << my->state_machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
void match_object::unpack_impl(std::istream& stream)
|
||||||
|
{
|
||||||
|
boost::archive::binary_iarchive ia(stream, boost::archive::no_header|boost::archive::no_codecvt|boost::archive::no_xml_tag_checking);
|
||||||
|
ia >> my->state_machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
game_id_type match_object::start_next_game(database& db, match_id_type match_id)
|
||||||
|
{
|
||||||
|
const game_object& game =
|
||||||
|
db.create<game_object>( [&]( game_object& game ) {
|
||||||
|
game.match_id = match_id;
|
||||||
|
game.players = players;
|
||||||
|
});
|
||||||
|
return game.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
} } // graphene::chain
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
// Manually reflect match_object to variant to properly reflect "state"
|
||||||
|
void to_variant(const graphene::chain::match_object& match_obj, fc::variant& v)
|
||||||
|
{
|
||||||
|
fc_elog(fc::logger::get("tournament"), "In match_obj to_variant");
|
||||||
|
elog("In match_obj to_variant");
|
||||||
|
fc::mutable_variant_object o;
|
||||||
|
o("id", match_obj.id)
|
||||||
|
("players", match_obj.players)
|
||||||
|
("games", match_obj.games)
|
||||||
|
("game_winners", match_obj.game_winners)
|
||||||
|
("match_winners", match_obj.match_winners)
|
||||||
|
("start_time", match_obj.start_time)
|
||||||
|
("end_time", match_obj.end_time)
|
||||||
|
("state", match_obj.get_state());
|
||||||
|
|
||||||
|
v = o;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Manually reflect match_object to variant to properly reflect "state"
|
||||||
|
void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj)
|
||||||
|
{
|
||||||
|
fc_elog(fc::logger::get("tournament"), "In match_obj from_variant");
|
||||||
|
match_obj.id = v["id"].as<graphene::chain::match_id_type>();
|
||||||
|
match_obj.players = v["players"].as<std::vector<graphene::chain::account_id_type> >();
|
||||||
|
match_obj.games = v["games"].as<std::vector<graphene::chain::game_id_type> >();
|
||||||
|
match_obj.game_winners = v["game_winners"].as<std::vector<flat_set<graphene::chain::account_id_type> > >();
|
||||||
|
match_obj.match_winners = v["match_winners"].as<flat_set<graphene::chain::account_id_type> >();
|
||||||
|
match_obj.start_time = v["start_time"].as<time_point_sec>();
|
||||||
|
match_obj.end_time = v["end_time"].as<optional<time_point_sec> >();
|
||||||
|
graphene::chain::match_state state = v["state"].as<graphene::chain::match_state>();
|
||||||
|
const_cast<int*>(match_obj.my->state_machine.current_state())[0] = (int)state;
|
||||||
|
}
|
||||||
|
} //end namespace fc
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -64,6 +64,7 @@
|
||||||
#include <graphene/app/api.hpp>
|
#include <graphene/app/api.hpp>
|
||||||
#include <graphene/chain/asset_object.hpp>
|
#include <graphene/chain/asset_object.hpp>
|
||||||
#include <graphene/chain/tournament_object.hpp>
|
#include <graphene/chain/tournament_object.hpp>
|
||||||
|
#include <graphene/chain/match_object.hpp>
|
||||||
#include <graphene/chain/protocol/fee_schedule.hpp>
|
#include <graphene/chain/protocol/fee_schedule.hpp>
|
||||||
#include <graphene/utilities/git_revision.hpp>
|
#include <graphene/utilities/git_revision.hpp>
|
||||||
#include <graphene/utilities/key_conversion.hpp>
|
#include <graphene/utilities/key_conversion.hpp>
|
||||||
|
|
@ -2257,7 +2258,6 @@ public:
|
||||||
|
|
||||||
unsigned match_number = player_number / 2;
|
unsigned match_number = player_number / 2;
|
||||||
unsigned player_in_match = player_number % 2;
|
unsigned player_in_match = player_number % 2;
|
||||||
idump((match_number)(player_in_match));
|
|
||||||
|
|
||||||
match_object match = _remote_db->get_objects({tournament_details.matches[match_number]})[0].as<match_object>();
|
match_object match = _remote_db->get_objects({tournament_details.matches[match_number]})[0].as<match_object>();
|
||||||
std::string player_name;
|
std::string player_name;
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@
|
||||||
#include <graphene/chain/withdraw_permission_object.hpp>
|
#include <graphene/chain/withdraw_permission_object.hpp>
|
||||||
#include <graphene/chain/witness_object.hpp>
|
#include <graphene/chain/witness_object.hpp>
|
||||||
#include <graphene/chain/worker_object.hpp>
|
#include <graphene/chain/worker_object.hpp>
|
||||||
|
#include <graphene/chain/tournament_object.hpp>
|
||||||
|
|
||||||
#include <fc/smart_ref_impl.hpp>
|
#include <fc/smart_ref_impl.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue