#235 game_object : delete custom variant and Stream operator functions

This commit is contained in:
Vlad Dobromyslov 2022-02-16 13:02:52 +03:00
parent 7ae3138863
commit 1f6e6dc65a
4 changed files with 7 additions and 121 deletions

View file

@ -28,8 +28,6 @@
#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>
@ -533,51 +531,6 @@ 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<graphene::chain::game_id_type>( max_depth );
game_obj.match_id = v["match_id"].as<graphene::chain::match_id_type>( max_depth );
game_obj.players = v["players"].as<std::vector<graphene::chain::account_id_type> >( max_depth );
game_obj.winners = v["winners"].as<flat_set<graphene::chain::account_id_type> >( max_depth );
game_obj.game_details = v["game_details"].as<graphene::chain::game_specific_details>( max_depth );
game_obj.next_timeout = v["next_timeout"].as<fc::optional<time_point_sec> >( max_depth );
graphene::chain::game_state state = v["state"].as<graphene::chain::game_state>( max_depth );
const_cast<int*>(game_obj.my->state_machine.current_state())[0] = (int)state;
}
} //end namespace fc

View file

@ -23,22 +23,9 @@
*/
#pragma once
#include <graphene/chain/match_object.hpp>
#include <graphene/chain/rock_paper_scissors.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <graphene/db/flat_index.hpp>
#include <graphene/db/object.hpp>
#include <graphene/db/generic_index.hpp>
#include <fc/crypto/hex.hpp>
#include <sstream>
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;
@ -82,21 +69,6 @@ namespace graphene { namespace chain {
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:
// 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 game_object& game_obj );
template<typename Stream>
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<impl> my;
@ -113,50 +85,6 @@ namespace graphene { namespace chain {
member<object, object_id_type, &object::id> > > >
> game_object_multi_index_type;
typedef generic_index<game_object, game_object_multi_index_type> game_index;
template<typename Stream>
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<Stream, const graphene::db::abstract_object<game_object> >(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<typename Stream>
inline Stream& operator>>( Stream& s, game_object& game_obj )
{
// unpack all fields exposed in the header in the usual way
//fc::raw::unpack<Stream, graphene::db::abstract_object<game_object> >(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,
@ -171,4 +99,6 @@ FC_REFLECT_DERIVED(graphene::chain::game_object, (graphene::db::object),
(players)
(winners)
(game_details)
(next_timeout))
(next_timeout))
GRAPHENE_EXTERNAL_SERIALIZATION( extern, graphene::chain::game_object )

View file

@ -577,6 +577,7 @@ FC_REFLECT_TYPENAME( graphene::chain::fba_accumulator_id_type )
FC_REFLECT_TYPENAME( graphene::chain::betting_market_position_id_type )
FC_REFLECT_TYPENAME( graphene::chain::global_betting_statistics_id_type )
FC_REFLECT_TYPENAME( graphene::chain::tournament_details_id_type )
FC_REFLECT_TYPENAME( graphene::chain::game_id_type )
FC_REFLECT_TYPENAME( graphene::chain::match_id_type )
FC_REFLECT_TYPENAME( graphene::chain::custom_permission_id_type )
FC_REFLECT_TYPENAME( graphene::chain::custom_account_authority_id_type )

View file

@ -43,6 +43,7 @@
#include <graphene/chain/witness_schedule_object.hpp>
#include <graphene/chain/witness_scheduler.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/game_object.hpp>
#include <graphene/chain/tournament_object.hpp>
#include <graphene/chain/match_object.hpp>
@ -74,5 +75,6 @@ GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::witness_object
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::witness_scheduler )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::witness_schedule_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::tournament_object )
GRAPHENE_EXTERNAL_SERIALIZATION( /*not extern*/, graphene::chain::match_object )