Fix build error in member_enumerator when an operation includes an enum.
Implement the commit/reveal timeouts in RPS games, and generate automatic
("insurance") moves. Make the CLI wallet watch for new games in
tournaments you're registered for.
This commit is contained in:
parent
7c30da6b6d
commit
c4ad900026
11 changed files with 323 additions and 77 deletions
|
|
@ -35,7 +35,8 @@
|
|||
|
||||
namespace graphene { namespace chain {
|
||||
|
||||
database::database()
|
||||
database::database() :
|
||||
_random_number_generator(fc::ripemd160().data())
|
||||
{
|
||||
initialize_indexes();
|
||||
initialize_evaluators();
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
#include <graphene/chain/withdraw_permission_object.hpp>
|
||||
#include <graphene/chain/witness_object.hpp>
|
||||
#include <graphene/chain/tournament_object.hpp>
|
||||
#include <graphene/chain/game_object.hpp>
|
||||
|
||||
#include <graphene/chain/protocol/fee_schedule.hpp>
|
||||
|
||||
|
|
@ -70,6 +71,8 @@ void database::update_global_dynamic_data( const signed_block& b )
|
|||
fc::raw::pack( enc, b.previous_secret );
|
||||
dgp.random = enc.result();
|
||||
|
||||
_random_number_generator = fc::hash_ctr_rng<secret_hash_type, 20>(dgp.random.data());
|
||||
|
||||
if( BOOST_UNLIKELY( b.block_num() == 1 ) )
|
||||
dgp.recently_missed_count = 0;
|
||||
else if( _checkpoints.size() && _checkpoints.rbegin()->first >= b.block_num() )
|
||||
|
|
@ -476,6 +479,11 @@ void database::update_withdraw_permissions()
|
|||
remove(*permit_index.begin());
|
||||
}
|
||||
|
||||
uint64_t database::get_random_bits( uint64_t bound )
|
||||
{
|
||||
return _random_number_generator(bound);
|
||||
}
|
||||
|
||||
void process_finished_games(database& db)
|
||||
{
|
||||
//auto& games_index = db.get_index_type<game_index>().indices().get<by_id>();
|
||||
|
|
@ -546,6 +554,24 @@ void initiate_next_round_of_matches(database& db)
|
|||
|
||||
void initiate_next_games(database& db)
|
||||
{
|
||||
// Next, trigger timeouts on any games which have been waiting too long for commit or
|
||||
// reveal moves
|
||||
auto& next_timeout_index = db.get_index_type<game_index>().indices().get<by_next_timeout>();
|
||||
while (1)
|
||||
{
|
||||
// empty time_points are sorted to the beginning, so upper_bound takes us to the first
|
||||
// non-empty time_point
|
||||
auto start_iter = next_timeout_index.upper_bound(boost::make_tuple(optional<time_point_sec>()));
|
||||
if (start_iter != next_timeout_index.end() &&
|
||||
*start_iter->next_timeout <= db.head_block_time())
|
||||
{
|
||||
db.modify(*start_iter, [&](game_object& game) {
|
||||
game.on_timeout(db);
|
||||
});
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void database::update_tournaments()
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@
|
|||
#include <boost/msm/back/tools.hpp>
|
||||
#include <boost/range/algorithm/copy.hpp>
|
||||
|
||||
#include <fc/crypto/hash_ctr_rng.hpp>
|
||||
|
||||
namespace graphene { namespace chain {
|
||||
|
||||
namespace msm = boost::msm;
|
||||
|
|
@ -59,6 +61,14 @@ namespace graphene { namespace chain {
|
|||
{}
|
||||
};
|
||||
|
||||
struct timeout
|
||||
{
|
||||
database& db;
|
||||
timeout(database& db) :
|
||||
db(db)
|
||||
{}
|
||||
};
|
||||
|
||||
struct game_state_machine_ : public msm::front::state_machine_def<game_state_machine_>
|
||||
{
|
||||
// disable a few state machine features we don't use for performance
|
||||
|
|
@ -69,6 +79,13 @@ namespace graphene { namespace chain {
|
|||
struct waiting_for_game_to_start : public msm::front::state<> {};
|
||||
struct expecting_commit_moves : public msm::front::state<>
|
||||
{
|
||||
void set_next_timeout(database& db, game_object& game)
|
||||
{
|
||||
const match_object& match_obj = game.match_id(db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
game.next_timeout = db.head_block_time() + game_options.time_per_commit_move;
|
||||
}
|
||||
void on_entry(const initiate_game& event, game_state_machine_& fsm)
|
||||
{
|
||||
game_object& game = *fsm.game_obj;
|
||||
|
|
@ -80,6 +97,7 @@ namespace graphene { namespace chain {
|
|||
"game ${id} is associtated with match ${match_id}",
|
||||
("id", game.id)
|
||||
("match_id", game.match_id));
|
||||
set_next_timeout(event.db, game);
|
||||
}
|
||||
void on_entry(const game_move& event, game_state_machine_& fsm)
|
||||
{
|
||||
|
|
@ -88,10 +106,26 @@ namespace graphene { namespace chain {
|
|||
fc_ilog(fc::logger::get("tournament"),
|
||||
"game ${id} received a commit move, still expecting another commit move",
|
||||
("id", game.id));
|
||||
set_next_timeout(event.db, game);
|
||||
}
|
||||
};
|
||||
struct expecting_reveal_moves : public msm::front::state<>
|
||||
{
|
||||
void set_next_timeout(database& db, game_object& game)
|
||||
{
|
||||
const match_object& match_obj = game.match_id(db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
game.next_timeout = db.head_block_time() + game_options.time_per_reveal_move;
|
||||
}
|
||||
void on_entry(const timeout& event, game_state_machine_& fsm)
|
||||
{
|
||||
game_object& game = *fsm.game_obj;
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
"game ${id} timed out waiting for commit moves, now expecting reveal move",
|
||||
("id", game.id));
|
||||
set_next_timeout(event.db, game);
|
||||
}
|
||||
void on_entry(const game_move& event, game_state_machine_& fsm)
|
||||
{
|
||||
game_object& game = *fsm.game_obj;
|
||||
|
|
@ -104,10 +138,31 @@ namespace graphene { namespace chain {
|
|||
fc_ilog(fc::logger::get("tournament"),
|
||||
"game ${id} received a reveal move, still expecting reveal moves",
|
||||
("id", game.id));
|
||||
set_next_timeout(event.db, game);
|
||||
}
|
||||
};
|
||||
|
||||
struct game_complete : public msm::front::state<>
|
||||
{
|
||||
void clear_next_timeout(database& db, game_object& game)
|
||||
{
|
||||
const match_object& match_obj = game.match_id(db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
game.next_timeout = fc::optional<fc::time_point_sec>();
|
||||
}
|
||||
void on_entry(const timeout& event, game_state_machine_& fsm)
|
||||
{
|
||||
game_object& game = *fsm.game_obj;
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
"timed out waiting for commits or reveals, game ${id} is complete",
|
||||
("id", game.id));
|
||||
|
||||
game.make_automatic_moves(event.db);
|
||||
game.determine_winner(event.db);
|
||||
clear_next_timeout(event.db, game);
|
||||
}
|
||||
|
||||
void on_entry(const game_move& event, game_state_machine_& fsm)
|
||||
{
|
||||
game_object& game = *fsm.game_obj;
|
||||
|
|
@ -115,31 +170,10 @@ namespace graphene { namespace chain {
|
|||
"received a reveal move, game ${id} is complete",
|
||||
("id", fsm.game_obj->id));
|
||||
|
||||
// we now know who played what, figure out if we have a winner
|
||||
const rock_paper_scissors_game_details& game_details = game.game_details.get<rock_paper_scissors_game_details>();
|
||||
if (game_details.reveal_moves[0]->gesture == game_details.reveal_moves[1]->gesture)
|
||||
ilog("The game was a tie, both players threw ${gesture}", ("gesture", game_details.reveal_moves[0]->gesture));
|
||||
else
|
||||
{
|
||||
const match_object& match_obj = game.match_id(event.db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(event.db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
|
||||
unsigned winner = ((((int)game_details.reveal_moves[0]->gesture -
|
||||
(int)game_details.reveal_moves[1]->gesture +
|
||||
game_options.number_of_gestures) % game_options.number_of_gestures) + 1) % 2;
|
||||
ilog("${gesture1} vs ${gesture2}, ${winner} wins",
|
||||
("gesture1", game_details.reveal_moves[1]->gesture)
|
||||
("gesture2", game_details.reveal_moves[0]->gesture)
|
||||
("winner", game_details.reveal_moves[winner]->gesture));
|
||||
game.winners.insert(game.players[winner]);
|
||||
}
|
||||
|
||||
|
||||
const match_object& match_obj = game.match_id(event.db);
|
||||
event.db.modify(match_obj, [&](match_object& match) {
|
||||
match.on_game_complete(event.db, game);
|
||||
});
|
||||
// if one player didn't commit a move we might need to make their "insurance" move now
|
||||
game.make_automatic_moves(event.db);
|
||||
game.determine_winner(event.db);
|
||||
clear_next_timeout(event.db, game);
|
||||
}
|
||||
};
|
||||
typedef waiting_for_game_to_start initial_state;
|
||||
|
|
@ -158,15 +192,23 @@ namespace graphene { namespace chain {
|
|||
return game_details.commit_moves.at(other_player_index).valid();
|
||||
}
|
||||
|
||||
bool already_have_other_reveal(const game_move& event)
|
||||
bool now_have_reveals_for_all_commits(const game_move& event)
|
||||
{
|
||||
auto iter = std::find(game_obj->players.begin(), game_obj->players.end(),
|
||||
event.move.player_account_id);
|
||||
unsigned player_index = std::distance(game_obj->players.begin(), iter);
|
||||
// hard-coded here for two-player games
|
||||
unsigned other_player_index = player_index == 0 ? 1 : 0;
|
||||
unsigned this_reveal_index = std::distance(game_obj->players.begin(), iter);
|
||||
|
||||
const rock_paper_scissors_game_details& game_details = game_obj->game_details.get<rock_paper_scissors_game_details>();
|
||||
return game_details.reveal_moves.at(other_player_index).valid();
|
||||
for (unsigned i = 0; i < game_details.commit_moves.size(); ++i)
|
||||
if (!game_details.reveal_moves[i] && i != this_reveal_index)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool have_at_least_one_commit_move(const timeout& event)
|
||||
{
|
||||
const rock_paper_scissors_game_details& game_details = game_obj->game_details.get<rock_paper_scissors_game_details>();
|
||||
return game_details.commit_moves[0] || game_details.commit_moves[1];
|
||||
}
|
||||
|
||||
void apply_commit_move(const game_move& event)
|
||||
|
|
@ -203,9 +245,12 @@ namespace graphene { namespace chain {
|
|||
// +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+
|
||||
a_row < expecting_commit_moves, game_move, expecting_commit_moves, &x::apply_commit_move >,
|
||||
row < expecting_commit_moves, game_move, expecting_reveal_moves, &x::apply_commit_move, &x::already_have_other_commit >,
|
||||
_row < expecting_commit_moves, timeout, game_complete >,
|
||||
g_row < expecting_commit_moves, timeout, expecting_reveal_moves, &x::have_at_least_one_commit_move >,
|
||||
// +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+
|
||||
_row < expecting_reveal_moves, timeout, game_complete >,
|
||||
a_row < expecting_reveal_moves, game_move, expecting_reveal_moves, &x::apply_reveal_move >,
|
||||
row < expecting_reveal_moves, game_move, game_complete, &x::apply_reveal_move, &x::already_have_other_reveal >
|
||||
row < expecting_reveal_moves, game_move, game_complete, &x::apply_reveal_move, &x::now_have_reveals_for_all_commits >
|
||||
// +-------------------------------+-------------------------+----------------------------+---------------------+----------------------+
|
||||
//a_row < game_in_progress, game_complete, game_in_progress, &x::start_next_game >,
|
||||
//g_row < game_in_progress, game_complete, game_complete, &x::was_final_game >
|
||||
|
|
@ -237,6 +282,7 @@ namespace graphene { namespace chain {
|
|||
players(rhs.players),
|
||||
winners(rhs.winners),
|
||||
game_details(rhs.game_details),
|
||||
next_timeout(rhs.next_timeout),
|
||||
my(new impl(this))
|
||||
{
|
||||
my->state_machine = rhs.my->state_machine;
|
||||
|
|
@ -251,6 +297,7 @@ namespace graphene { namespace chain {
|
|||
players = rhs.players;
|
||||
winners = rhs.winners;
|
||||
game_details = rhs.game_details;
|
||||
next_timeout = rhs.next_timeout;
|
||||
my->state_machine = rhs.my->state_machine;
|
||||
my->state_machine.game_obj = this;
|
||||
|
||||
|
|
@ -384,11 +431,95 @@ namespace graphene { namespace chain {
|
|||
FC_THROW("Game of type ${type} not supported", ("type", game_details.which()));
|
||||
}
|
||||
|
||||
void game_object::make_automatic_moves(database& db)
|
||||
{
|
||||
rock_paper_scissors_game_details& rps_game_details = game_details.get<rock_paper_scissors_game_details>();
|
||||
|
||||
unsigned players_without_commit_moves = 0;
|
||||
bool no_player_has_reveal_move = true;
|
||||
for (unsigned i = 0; i < 2; ++i)
|
||||
{
|
||||
if (!rps_game_details.commit_moves[i])
|
||||
++players_without_commit_moves;
|
||||
if (rps_game_details.reveal_moves[i])
|
||||
no_player_has_reveal_move = false;
|
||||
}
|
||||
|
||||
if (players_without_commit_moves || no_player_has_reveal_move)
|
||||
{
|
||||
const match_object& match_obj = match_id(db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
for (unsigned i = 0; i < 2; ++i)
|
||||
{
|
||||
if (!rps_game_details.commit_moves[i] ||
|
||||
no_player_has_reveal_move)
|
||||
{
|
||||
struct rock_paper_scissors_throw_reveal reveal;
|
||||
reveal.nonce2 = 0;
|
||||
reveal.gesture = (rock_paper_scissors_gesture)db.get_random_bits(game_options.number_of_gestures);
|
||||
rps_game_details.reveal_moves[i] = reveal;
|
||||
ilog("Player ${player} failed to commit a move, generating a random move for them: ${gesture}",
|
||||
("player", i)("gesture", reveal.gesture));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void game_object::determine_winner(database& db)
|
||||
{
|
||||
// we now know who played what, figure out if we have a winner
|
||||
const rock_paper_scissors_game_details& rps_game_details = game_details.get<rock_paper_scissors_game_details>();
|
||||
if (rps_game_details.reveal_moves[0]->gesture == rps_game_details.reveal_moves[1]->gesture)
|
||||
ilog("The game was a tie, both players threw ${gesture}", ("gesture", rps_game_details.reveal_moves[0]->gesture));
|
||||
else
|
||||
{
|
||||
const match_object& match_obj = match_id(db);
|
||||
const tournament_object& tournament_obj = match_obj.tournament_id(db);
|
||||
const rock_paper_scissors_game_options& game_options = tournament_obj.options.game_options.get<rock_paper_scissors_game_options>();
|
||||
|
||||
if (rps_game_details.reveal_moves[0] && rps_game_details.reveal_moves[1])
|
||||
{
|
||||
unsigned winner = ((((int)rps_game_details.reveal_moves[0]->gesture -
|
||||
(int)rps_game_details.reveal_moves[1]->gesture +
|
||||
game_options.number_of_gestures) % game_options.number_of_gestures) + 1) % 2;
|
||||
ilog("${gesture1} vs ${gesture2}, ${winner} wins",
|
||||
("gesture1", rps_game_details.reveal_moves[1]->gesture)
|
||||
("gesture2", rps_game_details.reveal_moves[0]->gesture)
|
||||
("winner", rps_game_details.reveal_moves[winner]->gesture));
|
||||
winners.insert(players[winner]);
|
||||
}
|
||||
else if (rps_game_details.reveal_moves[0])
|
||||
{
|
||||
ilog("Player 1 didn't commit or reveal their move, player 0 wins");
|
||||
winners.insert(players[0]);
|
||||
}
|
||||
else if (rps_game_details.reveal_moves[1])
|
||||
{
|
||||
ilog("Player 0 didn't commit or reveal their move, player 1 wins");
|
||||
winners.insert(players[1]);
|
||||
}
|
||||
else if (rps_game_details.reveal_moves[1])
|
||||
ilog("Neither player made a move, both players lose");
|
||||
}
|
||||
|
||||
|
||||
const match_object& match_obj = match_id(db);
|
||||
db.modify(match_obj, [&](match_object& match) {
|
||||
match.on_game_complete(db, *this);
|
||||
});
|
||||
}
|
||||
|
||||
void game_object::on_move(database& db, const game_move_operation& op)
|
||||
{
|
||||
my->state_machine.process_event(game_move(db, op));
|
||||
}
|
||||
|
||||
void game_object::on_timeout(database& db)
|
||||
{
|
||||
my->state_machine.process_event(timeout(db));
|
||||
}
|
||||
|
||||
void game_object::start_game(database& db, const std::vector<account_id_type>& players)
|
||||
{
|
||||
my->state_machine.process_event(initiate_game(db, players));
|
||||
|
|
@ -420,6 +551,7 @@ namespace fc {
|
|||
("players", game_obj.players)
|
||||
("winners", game_obj.winners)
|
||||
("game_details", game_obj.game_details)
|
||||
("next_timeout", game_obj.next_timeout)
|
||||
("state", game_obj.get_state());
|
||||
|
||||
v = o;
|
||||
|
|
@ -434,6 +566,7 @@ namespace fc {
|
|||
game_obj.players = v["players"].as<std::vector<graphene::chain::account_id_type> >();
|
||||
game_obj.winners = v["winners"].as<flat_set<graphene::chain::account_id_type> >();
|
||||
game_obj.game_details = v["game_details"].as<graphene::chain::game_specific_details>();
|
||||
game_obj.next_timeout = v["next_timeout"].as<fc::optional<time_point_sec> >();
|
||||
graphene::chain::game_state state = v["state"].as<graphene::chain::game_state>();
|
||||
const_cast<int*>(game_obj.my->state_machine.current_state())[0] = (int)state;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@
|
|||
#include <graphene/db/simple_index.hpp>
|
||||
#include <fc/signals.hpp>
|
||||
|
||||
#include <fc/crypto/hash_ctr_rng.hpp>
|
||||
|
||||
#include <graphene/chain/protocol/protocol.hpp>
|
||||
|
||||
#include <fc/log/logger.hpp>
|
||||
|
|
@ -250,6 +252,8 @@ namespace graphene { namespace chain {
|
|||
const node_property_object& get_node_properties()const;
|
||||
const fee_schedule& current_fee_schedule()const;
|
||||
|
||||
uint64_t get_random_bits( uint64_t bound );
|
||||
|
||||
time_point_sec head_block_time()const;
|
||||
uint32_t head_block_num()const;
|
||||
block_id_type head_block_id()const;
|
||||
|
|
@ -488,6 +492,7 @@ namespace graphene { namespace chain {
|
|||
flat_map<uint32_t,block_id_type> _checkpoints;
|
||||
|
||||
node_property_object _node_property_object;
|
||||
fc::hash_ctr_rng<secret_hash_type, 20> _random_number_generator;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ namespace graphene { namespace chain {
|
|||
flat_set<account_id_type> winners;
|
||||
|
||||
game_specific_details game_details;
|
||||
|
||||
fc::optional<time_point_sec> next_timeout;
|
||||
|
||||
game_state get_state() const;
|
||||
|
||||
|
|
@ -50,7 +52,11 @@ namespace graphene { namespace chain {
|
|||
game_object& operator=(const game_object& rhs);
|
||||
|
||||
void evaluate_move_operation(const database& db, const game_move_operation& op) const;
|
||||
void make_automatic_moves(database& db);
|
||||
void determine_winner(database& db);
|
||||
|
||||
void on_move(database& db, const game_move_operation& op);
|
||||
void on_timeout(database& db);
|
||||
void start_game(database& db, const std::vector<account_id_type>& players);
|
||||
|
||||
// serialization functions:
|
||||
|
|
@ -72,10 +78,15 @@ namespace graphene { namespace chain {
|
|||
std::unique_ptr<impl> my;
|
||||
};
|
||||
|
||||
struct by_next_timeout {};
|
||||
typedef multi_index_container<
|
||||
game_object,
|
||||
indexed_by<
|
||||
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > > >
|
||||
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
|
||||
ordered_unique< tag<by_next_timeout>,
|
||||
composite_key<game_object,
|
||||
member<game_object, optional<time_point_sec>, &game_object::next_timeout>,
|
||||
member<object, object_id_type, &object::id> > > >
|
||||
> game_object_multi_index_type;
|
||||
typedef generic_index<game_object, game_object_multi_index_type> game_index;
|
||||
|
||||
|
|
@ -90,6 +101,7 @@ namespace graphene { namespace chain {
|
|||
fc::raw::pack(s, game_obj.players);
|
||||
fc::raw::pack(s, game_obj.winners);
|
||||
fc::raw::pack(s, game_obj.game_details);
|
||||
fc::raw::pack(s, game_obj.next_timeout);
|
||||
|
||||
// fc::raw::pack the contents hidden in the impl class
|
||||
std::ostringstream stream;
|
||||
|
|
@ -110,6 +122,7 @@ namespace graphene { namespace chain {
|
|||
fc::raw::unpack(s, game_obj.players);
|
||||
fc::raw::unpack(s, game_obj.winners);
|
||||
fc::raw::unpack(s, game_obj.game_details);
|
||||
fc::raw::unpack(s, game_obj.next_timeout);
|
||||
|
||||
// fc::raw::unpack the contents hidden in the impl class
|
||||
std::string stringified_stream;
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ namespace graphene { namespace chain {
|
|||
void on_registration_deadline_passed(database& db);
|
||||
void on_player_registered(database& db, account_id_type payer_id, account_id_type player_id);
|
||||
void on_start_time_arrived(database& db);
|
||||
void on_final_game_completed();
|
||||
void on_match_completed(database& db, const match_object& match);
|
||||
|
||||
void check_for_new_matches_to_start(database& db) const;
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ namespace graphene { namespace chain {
|
|||
|
||||
const tournament_object& tournament_obj = match.tournament_id(event.db);
|
||||
event.db.modify(tournament_obj, [&](tournament_object& tournament) {
|
||||
tournament.on_final_game_completed();
|
||||
tournament.on_match_completed(event.db, match);
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,8 +31,6 @@
|
|||
#include <boost/archive/binary_iarchive.hpp>
|
||||
#include <boost/msm/back/tools.hpp>
|
||||
|
||||
#include <fc/crypto/hash_ctr_rng.hpp>
|
||||
|
||||
namespace graphene { namespace chain {
|
||||
|
||||
namespace msm = boost::msm;
|
||||
|
|
@ -60,7 +58,13 @@ namespace graphene { namespace chain {
|
|||
database& db;
|
||||
start_time_arrived(database& db) : db(db) {};
|
||||
};
|
||||
struct final_game_completed {};
|
||||
|
||||
struct match_completed
|
||||
{
|
||||
database& db;
|
||||
const match_object& match;
|
||||
match_completed(database& db, const match_object& match) : db(db), match(match) {}
|
||||
};
|
||||
|
||||
struct tournament_state_machine_ : public msm::front::state_machine_def<tournament_state_machine_>
|
||||
{
|
||||
|
|
@ -120,9 +124,6 @@ namespace graphene { namespace chain {
|
|||
("id", fsm.tournament_obj->id));
|
||||
const tournament_details_object& tournament_details_obj = fsm.tournament_obj->tournament_details_id(event.db);
|
||||
|
||||
// TODO hoist the rng to reset once per block?
|
||||
fc::hash_ctr_rng<secret_hash_type, 20> rng(event.db.get_dynamic_global_properties().random.data());
|
||||
|
||||
// Create the "seeding" order for the tournament as a random shuffle of the players.
|
||||
//
|
||||
// If this were a game of skill where players were ranked, this algorithm expects the
|
||||
|
|
@ -131,7 +132,7 @@ namespace graphene { namespace chain {
|
|||
tournament_details_obj.registered_players.end());
|
||||
for (unsigned i = seeded_players.size() - 1; i >= 1; --i)
|
||||
{
|
||||
unsigned j = (unsigned)rng(i + 1);
|
||||
unsigned j = (unsigned)event.db.get_random_bits(i + 1);
|
||||
std::swap(seeded_players[i], seeded_players[j]);
|
||||
}
|
||||
|
||||
|
|
@ -178,6 +179,12 @@ namespace graphene { namespace chain {
|
|||
tournament_details_obj.matches = matches;
|
||||
});
|
||||
}
|
||||
void on_entry(const match_completed& event, tournament_state_machine_& fsm)
|
||||
{
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
"Tournament ${id} is still in progress, maybe should start a new match here",
|
||||
("id", fsm.tournament_obj->id));
|
||||
}
|
||||
};
|
||||
struct registration_period_expired : public msm::front::state<>
|
||||
{
|
||||
|
|
@ -199,7 +206,17 @@ namespace graphene { namespace chain {
|
|||
}
|
||||
}
|
||||
};
|
||||
struct concluded : public msm::front::state<>{};
|
||||
|
||||
struct concluded : public msm::front::state<>
|
||||
{
|
||||
void on_entry(const match_completed& event, tournament_state_machine_& fsm)
|
||||
{
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
"Tournament ${id} is complete",
|
||||
("id", fsm.tournament_obj->id));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
typedef accepting_registrations initial_state;
|
||||
|
||||
|
|
@ -214,6 +231,15 @@ namespace graphene { namespace chain {
|
|||
return tournament_obj->registered_players == tournament_obj->options.number_of_players - 1;
|
||||
}
|
||||
|
||||
bool was_final_match(const match_completed& event)
|
||||
{
|
||||
const tournament_details_object& tournament_details_obj = tournament_obj->tournament_details_id(event.db);
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
"In was_final_match guard, returning ${value}",
|
||||
("value", event.match.id == tournament_details_obj.matches[tournament_details_obj.matches.size()]));
|
||||
return event.match.id == tournament_details_obj.matches[tournament_details_obj.matches.size() - 1];
|
||||
}
|
||||
|
||||
void register_player(const player_registered& event)
|
||||
{
|
||||
fc_ilog(fc::logger::get("tournament"),
|
||||
|
|
@ -235,12 +261,13 @@ namespace graphene { namespace chain {
|
|||
// Start Event Next Action Guard
|
||||
// +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+
|
||||
a_row < accepting_registrations, player_registered, accepting_registrations, &x::register_player >,
|
||||
row < accepting_registrations, player_registered, awaiting_start, &x::register_player, &x::will_be_fully_registered >,
|
||||
row < accepting_registrations, player_registered, awaiting_start, &x::register_player, &x::will_be_fully_registered >,
|
||||
_row < accepting_registrations, registration_deadline_passed, registration_period_expired >,
|
||||
// +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+
|
||||
_row < awaiting_start, start_time_arrived, in_progress >,
|
||||
// +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+
|
||||
_row < in_progress, final_game_completed, concluded >
|
||||
_row < in_progress, match_completed, in_progress >,
|
||||
g_row < in_progress, match_completed, concluded, &x::was_final_match >
|
||||
// +---------------------------+-----------------------------+----------------------------+---------------------+----------------------+
|
||||
> {};
|
||||
|
||||
|
|
@ -366,9 +393,9 @@ namespace graphene { namespace chain {
|
|||
my->state_machine.process_event(start_time_arrived(db));
|
||||
}
|
||||
|
||||
void tournament_object::on_final_game_completed()
|
||||
void tournament_object::on_match_completed(database& db, const match_object& match)
|
||||
{
|
||||
my->state_machine.process_event(final_game_completed());
|
||||
my->state_machine.process_event(match_completed(db, match));
|
||||
}
|
||||
|
||||
void tournament_object::check_for_new_matches_to_start(database& db) const
|
||||
|
|
|
|||
|
|
@ -406,7 +406,8 @@ private:
|
|||
if (match_cache_iter != match_cache.end())
|
||||
{
|
||||
const match_object& cached_match_obj = *match_cache_iter;
|
||||
if (cached_match_obj.get_state() != current_match_obj.get_state())
|
||||
if (cached_match_obj.get_state() != current_match_obj.get_state() ||
|
||||
cached_match_obj.games.size() != current_match_obj.games.size())
|
||||
{
|
||||
ilog("match ${id} changed state from ${old} to ${new}",
|
||||
("id", id)
|
||||
|
|
|
|||
|
|
@ -110,6 +110,37 @@ void class_processor::process_class( const static_variant< T... >* dummy )
|
|||
}
|
||||
}
|
||||
|
||||
template<typename IsEnum = fc::false_type>
|
||||
struct if_enum
|
||||
{
|
||||
template< typename T >
|
||||
static void process_class( class_processor* proc, const T* dummy )
|
||||
{
|
||||
std::string tname = fc::get_typename<T>::name();
|
||||
if( proc->result.find( tname ) != proc->result.end() )
|
||||
return;
|
||||
ilog( "processing class ${c}", ("c", tname) );
|
||||
// need this to keep from recursing on same class
|
||||
proc->result.emplace( tname, std::vector< std::string >() );
|
||||
|
||||
member_visitor<T> vtor( proc );
|
||||
fc::reflector<T>::visit( vtor );
|
||||
ilog( "members of class ${c} are ${m}", ("c", tname)("m", vtor.members) );
|
||||
proc->result[tname] = vtor.members;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct if_enum<fc::true_type>
|
||||
{
|
||||
template< typename T >
|
||||
static void process_class( class_processor* proc, const T* dummy )
|
||||
{
|
||||
std::string tname = fc::get_typename<T>::name();
|
||||
std::cerr << "skipping reflected enum " << tname << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename IsReflected=fc::false_type>
|
||||
struct if_reflected
|
||||
{
|
||||
|
|
@ -127,17 +158,7 @@ struct if_reflected<fc::true_type>
|
|||
template< typename T >
|
||||
static void process_class( class_processor* proc, const T* dummy )
|
||||
{
|
||||
std::string tname = fc::get_typename<T>::name();
|
||||
if( proc->result.find( tname ) != proc->result.end() )
|
||||
return;
|
||||
ilog( "processing class ${c}", ("c", tname) );
|
||||
// need this to keep from recursing on same class
|
||||
proc->result.emplace( tname, std::vector< std::string >() );
|
||||
|
||||
member_visitor<T> vtor( proc );
|
||||
fc::reflector<T>::visit( vtor );
|
||||
ilog( "members of class ${c} are ${m}", ("c", tname)("m", vtor.members) );
|
||||
proc->result[tname] = vtor.members;
|
||||
if_enum< typename fc::reflector<T>::is_enum >::process_class(proc, dummy);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@
|
|||
#include <graphene/chain/witness_object.hpp>
|
||||
#include <graphene/chain/worker_object.hpp>
|
||||
#include <graphene/chain/tournament_object.hpp>
|
||||
#include <graphene/chain/match_object.hpp>
|
||||
#include <graphene/chain/game_object.hpp>
|
||||
|
||||
#include <fc/smart_ref_impl.hpp>
|
||||
#include <iostream>
|
||||
|
|
@ -344,32 +346,49 @@ class register_member_visitor
|
|||
}
|
||||
};
|
||||
|
||||
template <typename T, typename IsEnum = fc::false_type>
|
||||
struct serializer_init_helper {
|
||||
static void init()
|
||||
{
|
||||
auto name = js_name<T>::name();
|
||||
if( st.find(name) == st.end() )
|
||||
{
|
||||
fc::reflector<T>::visit( register_member_visitor() );
|
||||
register_serializer( name, [=](){ generate(); } );
|
||||
}
|
||||
}
|
||||
static void generate()
|
||||
{
|
||||
auto name = remove_namespace( js_name<T>::name() );
|
||||
if( name == "int64" ) return;
|
||||
std::cout << "" << name
|
||||
<< " = new Serializer( \n"
|
||||
<< " \"" + name + "\"\n";
|
||||
|
||||
fc::reflector<T>::visit( serialize_member_visitor() );
|
||||
|
||||
std::cout <<")\n\n";
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct serializer_init_helper<T, fc::true_type> {
|
||||
static void init()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T, bool reflected>
|
||||
struct serializer
|
||||
{
|
||||
static_assert( fc::reflector<T>::is_defined::value == reflected, "invalid template arguments" );
|
||||
|
||||
static void init()
|
||||
{
|
||||
auto name = js_name<T>::name();
|
||||
if( st.find(name) == st.end() )
|
||||
{
|
||||
fc::reflector<T>::visit( register_member_visitor() );
|
||||
register_serializer( name, [=](){ generate(); } );
|
||||
}
|
||||
serializer_init_helper< T, typename fc::reflector<T>::is_enum >::init();
|
||||
}
|
||||
|
||||
static void generate()
|
||||
{
|
||||
auto name = remove_namespace( js_name<T>::name() );
|
||||
if( name == "int64" ) return;
|
||||
std::cout << "" << name
|
||||
<< " = new Serializer( \n"
|
||||
<< " \"" + name + "\"\n";
|
||||
|
||||
fc::reflector<T>::visit( serialize_member_visitor() );
|
||||
|
||||
std::cout <<")\n\n";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail_ns
|
||||
|
|
|
|||
Loading…
Reference in a new issue