diff --git a/libraries/chain/game_object.cpp b/libraries/chain/game_object.cpp index 19f3613b..88c0cf3c 100644 --- a/libraries/chain/game_object.cpp +++ b/libraries/chain/game_object.cpp @@ -28,6 +28,8 @@ #include #include +#include +#include #include #include @@ -531,6 +533,49 @@ namespace graphene { namespace chain { my->state_machine.process_event(initiate_game(db, players)); } + void game_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 game_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; + } + } } // graphene::chain +namespace fc { +// Manually reflect game_object to variant to properly reflect "state" +void to_variant(const graphene::chain::game_object& game_obj, fc::variant& v, uint32_t max_depth) +{ + fc_elog(fc::logger::get("tournament"), "In game_obj to_variant"); + elog("In game_obj to_variant"); + fc::mutable_variant_object o; + o("id", fc::variant(game_obj.id, max_depth )) + ("match_id", fc::variant(game_obj.match_id, max_depth )) + ("players", fc::variant(game_obj.players, max_depth )) + ("winners", fc::variant(game_obj.winners, max_depth )) + ("game_details", fc::variant(game_obj.game_details, max_depth )) + ("next_timeout", fc::variant(game_obj.next_timeout, max_depth )) + ("state", fc::variant(game_obj.get_state(), max_depth )); + v = o; +} + +// Manually reflect game_object to variant to properly reflect "state" +void from_variant(const fc::variant& v, graphene::chain::game_object& game_obj, uint32_t max_depth) +{ + fc_elog(fc::logger::get("tournament"), "In game_obj from_variant"); + game_obj.id = v["id"].as( max_depth ); + game_obj.match_id = v["match_id"].as( max_depth ); + game_obj.players = v["players"].as >( max_depth ); + game_obj.winners = v["winners"].as >( max_depth ); + game_obj.game_details = v["game_details"].as( max_depth ); + game_obj.next_timeout = v["next_timeout"].as >( max_depth ); + graphene::chain::game_state state = v["state"].as( max_depth ); + const_cast(game_obj.my->state_machine.current_state())[0] = (int)state; +} +} //end namespace fc diff --git a/libraries/chain/include/graphene/chain/game_object.hpp b/libraries/chain/include/graphene/chain/game_object.hpp index 1f326b90..cca9ea5e 100644 --- a/libraries/chain/include/graphene/chain/game_object.hpp +++ b/libraries/chain/include/graphene/chain/game_object.hpp @@ -26,6 +26,17 @@ #include #include #include +#include +#include + +namespace graphene { namespace chain { +class game_object; +} } + +namespace fc { +void to_variant(const graphene::chain::game_object& game_obj, fc::variant& v, uint32_t max_depth = 1); +void from_variant(const fc::variant& v, graphene::chain::game_object& game_obj, uint32_t max_depth = 1); +} //end namespace fc namespace graphene { namespace chain { class database; @@ -70,6 +81,21 @@ namespace graphene { namespace chain { void on_timeout(database& db); void start_game(database& db, const std::vector& players); + // serialization functions: + // for serializing to raw, go through a temporary sstream object to avoid + // having to implement serialization in the header file + template + friend Stream& operator<<( Stream& s, const game_object& game_obj ); + + template + friend Stream& operator>>( Stream& s, game_object& game_obj ); + + friend void ::fc::to_variant(const graphene::chain::game_object& game_obj, fc::variant& v, uint32_t max_depth); + friend void ::fc::from_variant(const fc::variant& v, graphene::chain::game_object& game_obj, uint32_t max_depth); + + void pack_impl(std::ostream& stream) const; + void unpack_impl(std::istream& stream); + class impl; std::unique_ptr my; }; @@ -85,6 +111,49 @@ namespace graphene { namespace chain { member > > > > game_object_multi_index_type; typedef generic_index game_index; + + template + inline Stream& operator<<( Stream& s, const game_object& game_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 >(s, game_obj); + fc::raw::pack(s, game_obj.id); + fc::raw::pack(s, game_obj.match_id); + 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; + game_obj.pack_impl(stream); + std::string stringified_stream(stream.str()); + fc::raw::pack(s, stream.str()); + + return s; + } + + template + inline Stream& operator>>( Stream& s, game_object& game_obj ) + { + // unpack all fields exposed in the header in the usual way + //fc::raw::unpack >(s, game_obj); + fc::raw::unpack(s, game_obj.id); + fc::raw::unpack(s, game_obj.match_id); + 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; + fc::raw::unpack(s, stringified_stream); + std::istringstream stream(stringified_stream); + game_obj.unpack_impl(stream); + + return s; + } } } FC_REFLECT_ENUM(graphene::chain::game_state, @@ -93,12 +162,52 @@ FC_REFLECT_ENUM(graphene::chain::game_state, (expecting_reveal_moves) (game_complete)) -//FC_REFLECT_TYPENAME(graphene::chain::game_object) // manually serialized -FC_REFLECT_DERIVED(graphene::chain::game_object, (graphene::db::object), - (match_id) - (players) - (winners) - (game_details) - (next_timeout)) +namespace fc { -GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::game_object ) \ No newline at end of file +template<> +template<> +inline void if_enum::from_variant(const variant &vo, graphene::chain::game_object &v, uint32_t max_depth) { + from_variant(vo, v, max_depth); +} + +template<> +template<> +inline void if_enum::to_variant(const graphene::chain::game_object &v, variant &vo, uint32_t max_depth) { + to_variant(v, vo, max_depth); +} + +namespace raw { namespace detail { + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::game_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::pack(fc::datastream &s, const graphene::chain::game_object &v, uint32_t) { + s << v; +} + +template<> +template<> +inline void if_enum::unpack(fc::datastream &s, graphene::chain::game_object &v, uint32_t) { + s >> v; +} + +} } + +template <> +struct get_typename { + static const char *name() { + return "graphene::chain::game_object"; + } +}; +template <> +struct reflector { + typedef graphene::chain::game_object type; + typedef fc::true_type is_defined; + typedef fc::false_type is_enum; +}; +} // namespace fc \ No newline at end of file