#235 Add custom funtions for event_object

This commit is contained in:
Vlad Dobromyslov 2022-02-23 12:54:09 +03:00
parent a685a63453
commit 6f9c6fa8f0
3 changed files with 700 additions and 549 deletions

View file

@ -31,6 +31,8 @@
#include <graphene/chain/betting_market_object.hpp> #include <graphene/chain/betting_market_object.hpp>
#include <boost/msm/back/state_machine.hpp> #include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.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/msm/back/tools.hpp>
namespace graphene { namespace chain { namespace graphene { namespace chain {
@ -474,6 +476,18 @@ namespace graphene { namespace chain {
}; };
} }
void event_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 event_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 event_object::on_upcoming_event(database& db) void event_object::on_upcoming_event(database& db)
{ {
my->state_machine.process_event(upcoming_event(db)); my->state_machine.process_event(upcoming_event(db));
@ -537,4 +551,34 @@ namespace graphene { namespace chain {
} } // graphene::chain } } // graphene::chain
namespace fc {
// Manually reflect event_object to variant to properly reflect "state"
void to_variant(const graphene::chain::event_object& event_obj, fc::variant& v, uint32_t max_depth)
{
fc::mutable_variant_object o;
o("id", fc::variant(event_obj.id, max_depth))
("name", fc::variant(event_obj.name, max_depth))
("season", fc::variant(event_obj.season, max_depth))
("start_time", fc::variant(event_obj.start_time, max_depth))
("event_group_id", fc::variant(event_obj.event_group_id, max_depth))
("scores", fc::variant(event_obj.scores, max_depth))
("status", fc::variant(event_obj.get_status(), max_depth));
v = o;
}
// Manually reflect event_object to variant to properly reflect "state"
void from_variant(const fc::variant& v, graphene::chain::event_object& event_obj, uint32_t max_depth)
{
event_obj.id = v["id"].as<graphene::chain::event_id_type>( max_depth );
event_obj.name = v["name"].as<graphene::chain::internationalized_string_type>( max_depth );
event_obj.season = v["season"].as<graphene::chain::internationalized_string_type>( max_depth );
event_obj.start_time = v["start_time"].as<optional<time_point_sec> >( max_depth );
event_obj.event_group_id = v["event_group_id"].as<graphene::chain::event_group_id_type>( max_depth );
event_obj.scores = v["scores"].as<std::vector<std::string>>( max_depth );
graphene::chain::event_status status = v["status"].as<graphene::chain::event_status>( max_depth );
const_cast<int*>(event_obj.my->state_machine.current_state())[0] = (int)status;
}
} //end namespace fc

View file

@ -24,12 +24,22 @@
#pragma once #pragma once
#include <graphene/chain/protocol/types.hpp> #include <graphene/chain/protocol/types.hpp>
#include <graphene/chain/protocol/event.hpp>
#include <graphene/db/object.hpp> #include <graphene/db/object.hpp>
#include <graphene/db/generic_index.hpp> #include <graphene/db/generic_index.hpp>
#include <graphene/chain/protocol/event.hpp>
#include <sstream>
#include <boost/multi_index/composite_key.hpp> #include <boost/multi_index/composite_key.hpp>
namespace graphene { namespace chain {
class event_object;
} }
namespace fc {
void to_variant(const graphene::chain::event_object& event_obj, fc::variant& v, uint32_t max_depth = 1);
void from_variant(const fc::variant& v, graphene::chain::event_object& event_obj, uint32_t max_depth = 1);
} //end namespace fc
namespace graphene { namespace chain { namespace graphene { namespace chain {
class database; class database;
@ -58,6 +68,21 @@ class event_object : public graphene::db::abstract_object< event_object >
event_status get_status() const; event_status get_status() const;
vector<string> scores; vector<string> scores;
// 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 event_object& event_obj );
template<typename Stream>
friend Stream& operator>>( Stream& s, event_object& event_obj );
friend void ::fc::to_variant(const graphene::chain::event_object& event_obj, fc::variant& v, uint32_t max_depth);
friend void ::fc::from_variant(const fc::variant& v, graphene::chain::event_object& event_obj, uint32_t max_depth);
void pack_impl(std::ostream& stream) const;
void unpack_impl(std::istream& stream);
void on_upcoming_event(database& db); void on_upcoming_event(database& db);
void on_in_progress_event(database& db); void on_in_progress_event(database& db);
void on_frozen_event(database& db); void on_frozen_event(database& db);
@ -86,16 +111,100 @@ typedef multi_index_container<
member<object, object_id_type, &object::id> > > > > event_object_multi_index_type; member<object, object_id_type, &object::id> > > > > event_object_multi_index_type;
typedef generic_index<event_object, event_object_multi_index_type> event_object_index; typedef generic_index<event_object, event_object_multi_index_type> event_object_index;
template<typename Stream>
inline Stream& operator<<( Stream& s, const event_object& event_obj )
{
fc_elog(fc::logger::get("event"), "In event_obj to_raw");
// 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<event_object> >(s, event_obj);
fc::raw::pack(s, event_obj.id);
fc::raw::pack(s, event_obj.name);
fc::raw::pack(s, event_obj.season);
fc::raw::pack(s, event_obj.start_time);
fc::raw::pack(s, event_obj.event_group_id);
fc::raw::pack(s, event_obj.at_least_one_betting_market_group_settled);
fc::raw::pack(s, event_obj.scores);
// fc::raw::pack the contents hidden in the impl class
std::ostringstream stream;
event_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, event_object& event_obj )
{
fc_elog(fc::logger::get("event"), "In event_obj from_raw");
// unpack all fields exposed in the header in the usual way
//fc::raw::unpack<Stream, graphene::db::abstract_object<event_object> >(s, event_obj);
fc::raw::unpack(s, event_obj.id);
fc::raw::unpack(s, event_obj.name);
fc::raw::unpack(s, event_obj.season);
fc::raw::unpack(s, event_obj.start_time);
fc::raw::unpack(s, event_obj.event_group_id);
fc::raw::unpack(s, event_obj.at_least_one_betting_market_group_settled);
fc::raw::unpack(s, event_obj.scores);
// 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);
event_obj.unpack_impl(stream);
return s;
}
} } // graphene::chain } } // graphene::chain
FC_REFLECT_DERIVED(graphene::chain::event_object, (graphene::db::object), namespace fc {
(name)
(season)
(start_time)
(event_group_id)
(at_least_one_betting_market_group_settled)
(scores))
GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::event_object ) template<>
template<>
inline void if_enum<fc::false_type>::from_variant(const variant &vo, graphene::chain::event_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::event_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::event_object &v, uint32_t) {
s << v;
}
template<>
template<>
inline void if_enum<fc::false_type>::pack(fc::datastream<char*> &s, const graphene::chain::event_object &v, uint32_t) {
s << v;
}
template<>
template<>
inline void if_enum<fc::false_type>::unpack(fc::datastream<const char*> &s, graphene::chain::event_object &v, uint32_t) {
s >> v;
}
} }
template <>
struct get_typename<graphene::chain::event_object> {
static const char *name() {
return "graphene::chain::event_object";
}
};
template <>
struct reflector<graphene::chain::event_object> {
typedef graphene::chain::event_object type;
typedef fc::true_type is_defined;
typedef fc::false_type is_enum;
};
} // namespace fc

View file

@ -45,7 +45,6 @@
#include <graphene/chain/worker_object.hpp> #include <graphene/chain/worker_object.hpp>
#include <graphene/chain/game_object.hpp> #include <graphene/chain/game_object.hpp>
#include <graphene/chain/tournament_object.hpp> #include <graphene/chain/tournament_object.hpp>
#include <graphene/chain/event_object.hpp>
#include <graphene/chain/match_object.hpp> #include <graphene/chain/match_object.hpp>
#include <graphene/chain/betting_market_object.hpp> #include <graphene/chain/betting_market_object.hpp>
@ -79,7 +78,6 @@ GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::witness_schedu
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::worker_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::worker_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::game_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::game_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::tournament_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::tournament_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::event_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::match_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::match_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::betting_market_group_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::betting_market_group_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::betting_market_object ) GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::betting_market_object )