#235 Add custom funtions for match_object

This commit is contained in:
Vlad Dobromyslov 2022-02-24 09:39:51 +03:00
parent 6f9c6fa8f0
commit 224eedee39
2 changed files with 180 additions and 12 deletions

View file

@ -1,6 +1,18 @@
#pragma once
#include <graphene/db/object.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, uint32_t max_depth = 1);
void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth = 1);
} //end namespace fc
namespace graphene { namespace chain {
class database;
@ -61,6 +73,21 @@ namespace graphene { namespace chain {
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, uint32_t max_depth);
friend void ::fc::from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth);
void pack_impl(std::ostream& stream) const;
void unpack_impl(std::istream& stream);
void on_initiate_match(database& db);
void on_game_complete(database& db, const game_object& game);
game_id_type start_next_game(database& db, match_id_type match_id);
@ -76,6 +103,56 @@ namespace graphene { namespace chain {
> 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.tournament_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.number_of_wins);
fc::raw::pack(s, match_obj.number_of_ties);
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.tournament_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.number_of_wins);
fc::raw::unpack(s, match_obj.number_of_ties);
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,
@ -83,16 +160,52 @@ FC_REFLECT_ENUM(graphene::chain::match_state,
(match_in_progress)
(match_complete))
//FC_REFLECT_TYPENAME(graphene::chain::match_object) // manually serialized
FC_REFLECT_DERIVED(graphene::chain::match_object, (graphene::db::object),
(tournament_id)
(players)
(games)
(game_winners)
(number_of_wins)
(number_of_ties)
(match_winners)
(start_time)
(end_time))
namespace fc {
GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::match_object )
template<>
template<>
inline void if_enum<fc::false_type>::from_variant(const variant &vo, graphene::chain::match_object &v, uint32_t max_depth) {
from_variant(vo, v, max_depth);
}
template<>
template<>
inline void if_enum<fc::false_type>::to_variant(const graphene::chain::match_object &v, variant &vo, uint32_t max_depth) {
to_variant(v, vo, max_depth);
}
namespace raw { namespace detail {
template<>
template<>
inline void if_enum<fc::false_type>::pack(fc::datastream<size_t> &s, const graphene::chain::match_object &v, uint32_t) {
s << v;
}
template<>
template<>
inline void if_enum<fc::false_type>::pack(fc::datastream<char*> &s, const graphene::chain::match_object &v, uint32_t) {
s << v;
}
template<>
template<>
inline void if_enum<fc::false_type>::unpack(fc::datastream<const char*> &s, graphene::chain::match_object &v, uint32_t) {
s >> v;
}
} }
template <>
struct get_typename<graphene::chain::match_object> {
static const char *name() {
return "graphene::chain::match_object";
}
};
template <>
struct reflector<graphene::chain::match_object> {
typedef graphene::chain::match_object type;
typedef fc::true_type is_defined;
typedef fc::false_type is_enum;
};
} // namespace fc

View file

@ -28,6 +28,8 @@
#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>
#include <boost/range/algorithm/copy.hpp>
@ -325,6 +327,18 @@ namespace graphene { namespace chain {
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;
}
void match_object::on_initiate_match(database& db)
{
my->state_machine.process_event(initiate_match(db));
@ -348,4 +362,45 @@ namespace graphene { namespace chain {
} } // 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, uint32_t max_depth)
{ try {
fc_elog(fc::logger::get("tournament"), "In match_obj to_variant");
elog("In match_obj to_variant");
fc::mutable_variant_object o;
o("id", fc::variant(match_obj.id, max_depth))
("tournament_id", fc::variant(match_obj.tournament_id, max_depth))
("players", fc::variant(match_obj.players, max_depth))
("games", fc::variant(match_obj.games, max_depth))
("game_winners", fc::variant(match_obj.game_winners, max_depth))
("number_of_wins", fc::variant(match_obj.number_of_wins, max_depth))
("number_of_ties", fc::variant(match_obj.number_of_ties, max_depth))
("match_winners", fc::variant(match_obj.match_winners, max_depth))
("start_time", fc::variant(match_obj.start_time, max_depth))
("end_time", fc::variant(match_obj.end_time, max_depth))
("state", fc::variant(match_obj.get_state(), max_depth));
v = o;
} FC_RETHROW_EXCEPTIONS(warn, "") }
// Manually reflect match_object to variant to properly reflect "state"
void from_variant(const fc::variant& v, graphene::chain::match_object& match_obj, uint32_t max_depth)
{ try {
fc_elog(fc::logger::get("tournament"), "In match_obj from_variant");
match_obj.id = v["id"].as<graphene::chain::match_id_type>( max_depth );
match_obj.tournament_id = v["tournament_id"].as<graphene::chain::tournament_id_type>( max_depth );
match_obj.players = v["players"].as<std::vector<graphene::chain::account_id_type> >( max_depth );
match_obj.games = v["games"].as<std::vector<graphene::chain::game_id_type> >( max_depth );
match_obj.game_winners = v["game_winners"].as<std::vector<flat_set<graphene::chain::account_id_type> > >( max_depth );
match_obj.number_of_wins = v["number_of_wins"].as<std::vector<uint32_t> >( max_depth );
match_obj.number_of_ties = v["number_of_ties"].as<uint32_t>( max_depth );
match_obj.match_winners = v["match_winners"].as<flat_set<graphene::chain::account_id_type> >( max_depth );
match_obj.start_time = v["start_time"].as<time_point_sec>( max_depth );
match_obj.end_time = v["end_time"].as<optional<time_point_sec> >( max_depth );
graphene::chain::match_state state = v["state"].as<graphene::chain::match_state>( max_depth );
const_cast<int*>(match_obj.my->state_machine.current_state())[0] = (int)state;
} FC_RETHROW_EXCEPTIONS(warn, "") }
} //end namespace fc