bookie_plugin: added localized_event_strings ....

This commit is contained in:
Roman Olearski 2017-07-31 14:26:20 +02:00 committed by Roman Olearski
parent b96459045b
commit 6ca6413d88
10 changed files with 144 additions and 16 deletions

3
.gitignore vendored
View file

@ -41,3 +41,6 @@ object_database/*
*.pyc *.pyc
*.pyo *.pyo
.romek
.*

View file

@ -254,9 +254,9 @@
"scale": 10000 "scale": 10000
}, },
"block_interval": 5, "block_interval": 5,
"maintenance_interval": 86400, "maintenance_interval": 6000,
"maintenance_skip_slots": 3, "maintenance_skip_slots": 3,
"committee_proposal_review_period": 1209600, "committee_proposal_review_period": 900,
"maximum_transaction_size": 2048, "maximum_transaction_size": 2048,
"maximum_block_size": 2048000000, "maximum_block_size": 2048000000,
"maximum_time_until_expiration": 86400, "maximum_time_until_expiration": 86400,
@ -391,7 +391,12 @@
"name": "nathan", "name": "nathan",
"owner_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "owner_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV", "active_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"is_lifetime_member": false "is_lifetime_member": true
},{
"name": "romek",
"owner_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "PPY6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"is_lifetime_member": true
} }
], ],
"initial_assets": [{ "initial_assets": [{

View file

@ -36,8 +36,7 @@ void_result event_create_evaluator::do_evaluate(const event_create_operation& op
{ try { { try {
FC_ASSERT(trx_state->_is_proposed_trx); FC_ASSERT(trx_state->_is_proposed_trx);
database& d = db(); //database& d = db();
// the event_group_id in the operation can be a relative id. If it is, // the event_group_id in the operation can be a relative id. If it is,
// resolve it and verify that it is truly an event_group // resolve it and verify that it is truly an event_group
object_id_type resolved_event_group_id = op.event_group_id; object_id_type resolved_event_group_id = op.event_group_id;

View file

@ -28,6 +28,7 @@ class bookie_api_impl
binned_order_book get_binned_order_book(graphene::chain::betting_market_id_type betting_market_id, int32_t precision); binned_order_book get_binned_order_book(graphene::chain::betting_market_id_type betting_market_id, int32_t precision);
std::shared_ptr<graphene::bookie::bookie_plugin> get_plugin(); std::shared_ptr<graphene::bookie::bookie_plugin> get_plugin();
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
void get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language);
graphene::app::application& app; graphene::app::application& app;
}; };
@ -113,6 +114,11 @@ asset bookie_api_impl::get_total_matched_bet_amount_for_betting_market_group(bet
return get_plugin()->get_total_matched_bet_amount_for_betting_market_group(group_id); return get_plugin()->get_total_matched_bet_amount_for_betting_market_group(group_id);
} }
void bookie_api_impl::get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language)
{
get_plugin()->get_events_containing_sub_string(events, sub_string, language);
}
} // detail } // detail
bookie_api::bookie_api(graphene::app::application& app) : bookie_api::bookie_api(graphene::app::application& app) :
@ -130,6 +136,14 @@ asset bookie_api::get_total_matched_bet_amount_for_betting_market_group(betting_
return my->get_total_matched_bet_amount_for_betting_market_group(group_id); return my->get_total_matched_bet_amount_for_betting_market_group(group_id);
} }
std::vector<event_object> bookie_api::get_events_containing_sub_string(const std::string& sub_string, const std::string& language)
{
std::vector<event_object> events;
my->get_events_containing_sub_string(events, sub_string, language);
return events;
}
} } // graphene::bookie } } // graphene::bookie

View file

@ -36,6 +36,7 @@
#include <graphene/chain/operation_history_object.hpp> #include <graphene/chain/operation_history_object.hpp>
#include <graphene/chain/transaction_evaluation_state.hpp> #include <graphene/chain/transaction_evaluation_state.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <fc/smart_ref_impl.hpp> #include <fc/smart_ref_impl.hpp>
#include <fc/thread/thread.hpp> #include <fc/thread/thread.hpp>
@ -145,11 +146,29 @@ class bookie_plugin_impl
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
void fill_localized_event_strings();
void get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language);
graphene::chain::database& database() graphene::chain::database& database()
{ {
return _self.database(); return _self.database();
} }
// 1.18. "Washington Capitals/Chicago Blackhawks"
typedef std::pair<event_id_type, std::string> event_string;
struct event_string_less : public std::less<const event_string&>
{
bool operator()(const event_string &_left, const event_string &_right) const
{
return (_left.first.instance < _right.first.instance);
}
};
typedef flat_set<event_string, event_string_less> event_string_set;
// "en"
std::map<std::string, event_string_set> localized_event_strings;
bookie_plugin& _self; bookie_plugin& _self;
flat_set<account_id_type> _tracked_accounts; flat_set<account_id_type> _tracked_accounts;
}; };
@ -240,9 +259,69 @@ void bookie_plugin_impl::on_block_applied( const signed_block& )
obj.total_matched_bets_amount += amount_bet.amount; obj.total_matched_bets_amount += amount_bet.amount;
}); });
} }
else if( op.op.which() == operation::tag< event_create_operation >::value )
{
FC_ASSERT(op.result.which() == operation_result::tag< object_id_type >::value);
//object_id_type object_id = op.result.get<object_id_type>();
event_id_type object_id = op.result.get<object_id_type>();
FC_ASSERT( db.find_object(object_id), "invalid event specified" );
const event_create_operation& event_create_op = op.op.get<event_create_operation>();
for(const std::pair<std::string, std::string>& pair : event_create_op.name)
{
localized_event_strings[pair.first].insert(event_string(object_id, pair.second));
}
}
else if( op.op.which() == operation::tag< event_update_operation >::value )
{
const event_update_operation& event_create_op = op.op.get<event_update_operation>();
if (!event_create_op.new_name.valid())
continue;
event_id_type event_id = event_create_op.event_id;
for(const std::pair<std::string, std::string>& pair : *event_create_op.new_name)
{
// try insert
std::pair<event_string_set::iterator, bool> result =
localized_event_strings[pair.first].insert(event_string(event_id, pair.second));
if (!result.second)
// update string only
result.first->second = pair.second;
}
}
} }
} }
void bookie_plugin_impl::fill_localized_event_strings()
{
graphene::chain::database& db = database();
const auto& event_index = db.get_index_type<event_object_index>().indices().get<by_id>();
auto event_itr = event_index.cbegin();
while (event_itr != event_index.cend())
{
const event_object& event_obj = *event_itr;
++event_itr;
for(const std::pair<std::string, std::string>& pair : event_obj.name)
{
localized_event_strings[pair.first].insert(event_string(event_obj.id, pair.second));
}
}
}
void bookie_plugin_impl::get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language)
{
graphene::chain::database& db = database();
if (localized_event_strings.find(language) != localized_event_strings.end())
{
std::string lower_case_sub_string = boost::algorithm::to_lower_copy(sub_string);
const event_string_set& language_set = localized_event_strings[language];
for (const event_string& pair : language_set)
{
std::string lower_case_string = boost::algorithm::to_lower_copy(pair.second);
if (lower_case_string.find(lower_case_sub_string) != std::string::npos)
events.push_back(pair.first(db));
}
}
}
asset bookie_plugin_impl::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id) asset bookie_plugin_impl::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id)
{ {
graphene::chain::database& db = database(); graphene::chain::database& db = database();
@ -279,19 +358,21 @@ void bookie_plugin::plugin_set_program_options(
void bookie_plugin::plugin_initialize(const boost::program_options::variables_map& options) void bookie_plugin::plugin_initialize(const boost::program_options::variables_map& options)
{ {
ilog("bookie plugin: plugin_startup() begin"); ilog("bookie plugin: plugin_startup() begin");
database().applied_block.connect( [&]( const signed_block& b){ my->on_block_applied(b); } ); database().applied_block.connect( [&]( const signed_block& b){ my->on_block_applied(b); } );
database().changed_objects.connect([&](const vector<object_id_type>& changed_object_ids, const fc::flat_set<graphene::chain::account_id_type>& impacted_accounts){ my->on_objects_changed(changed_object_ids); }); database().changed_objects.connect([&](const vector<object_id_type>& changed_object_ids, const fc::flat_set<graphene::chain::account_id_type>& impacted_accounts){ my->on_objects_changed(changed_object_ids); });
auto event_index = database().add_index<primary_index<detail::persistent_event_object_index> >(); //auto event_index =
//event_index->add_secondary_index<detail::events_by_competitor_index>(); database().add_index<primary_index<detail::persistent_event_object_index> >();
//event_index->add_secondary_index<detail::events_by_competitor_index>();
//LOAD_VALUE_SET(options, "tracked-accounts", my->_tracked_accounts, graphene::chain::account_id_type);
//LOAD_VALUE_SET(options, "tracked-accounts", my->_tracked_accounts, graphene::chain::account_id_type); ilog("bookie plugin: plugin_startup() end");
ilog("bookie plugin: plugin_startup() end"); }
}
void bookie_plugin::plugin_startup() void bookie_plugin::plugin_startup()
{ {
ilog("bookie plugin: plugin_startup()"); ilog("bookie plugin: plugin_startup()");
my->fill_localized_event_strings();
} }
flat_set<account_id_type> bookie_plugin::tracked_accounts() const flat_set<account_id_type> bookie_plugin::tracked_accounts() const
@ -301,9 +382,14 @@ flat_set<account_id_type> bookie_plugin::tracked_accounts() const
asset bookie_plugin::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id) asset bookie_plugin::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id)
{ {
ilog("bookie plugin: get_total_matched_bet_amount_for_betting_market_group($group_id)", ("group_d", group_id)); ilog("bookie plugin: get_total_matched_bet_amount_for_betting_market_group($group_id)", ("group_d", group_id));
return my->get_total_matched_bet_amount_for_betting_market_group(group_id); return my->get_total_matched_bet_amount_for_betting_market_group(group_id);
} }
void bookie_plugin::get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language)
{
ilog("bookie plugin: get_events_containing_sub_string($s, $l)", ("s", sub_string)("l", language));
my->get_events_containing_sub_string(events, sub_string, language);
}
} } } }
FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_event_object, (graphene::db::object), (event_object_id)(name)(season)(start_time)(event_group_id)(status)(scores) ) FC_REFLECT_DERIVED( graphene::bookie::detail::persistent_event_object, (graphene::db::object), (event_object_id)(name)(season)(start_time)(event_group_id)(status)(scores) )

View file

@ -8,6 +8,7 @@
#include <graphene/chain/protocol/types.hpp> #include <graphene/chain/protocol/types.hpp>
#include <graphene/chain/protocol/asset.hpp> #include <graphene/chain/protocol/asset.hpp>
#include <graphene/chain/event_object.hpp>
using namespace graphene::chain; using namespace graphene::chain;
@ -43,6 +44,7 @@ class bookie_api
*/ */
binned_order_book get_binned_order_book(graphene::chain::betting_market_id_type betting_market_id, int32_t precision); binned_order_book get_binned_order_book(graphene::chain::betting_market_id_type betting_market_id, int32_t precision);
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
std::shared_ptr<detail::bookie_api_impl> my; std::shared_ptr<detail::bookie_api_impl> my;
}; };
@ -55,5 +57,6 @@ FC_REFLECT(graphene::bookie::binned_order_book, (aggregated_back_bets)(aggregate
FC_API(graphene::bookie::bookie_api, FC_API(graphene::bookie::bookie_api,
(get_binned_order_book) (get_binned_order_book)
(get_total_matched_bet_amount_for_betting_market_group) (get_total_matched_bet_amount_for_betting_market_group)
(get_events_containing_sub_string)
) )

View file

@ -71,6 +71,7 @@ class bookie_plugin : public graphene::app::plugin
flat_set<account_id_type> tracked_accounts()const; flat_set<account_id_type> tracked_accounts()const;
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
void get_events_containing_sub_string(std::vector<event_object>& events, const std::string& sub_string, const std::string& language);
friend class detail::bookie_plugin_impl; friend class detail::bookie_plugin_impl;
std::unique_ptr<detail::bookie_plugin_impl> my; std::unique_ptr<detail::bookie_plugin_impl> my;

View file

@ -29,6 +29,7 @@
using namespace graphene::app; using namespace graphene::app;
using namespace graphene::chain; using namespace graphene::chain;
using namespace graphene::utilities; using namespace graphene::utilities;
using namespace graphene::bookie;
using namespace std; using namespace std;
namespace fc namespace fc
@ -1571,6 +1572,7 @@ class wallet_api
order_book get_order_book( const string& base, const string& quote, unsigned limit = 50); order_book get_order_book( const string& base, const string& quote, unsigned limit = 50);
asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id); asset get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id);
std::vector<event_object> get_events_containing_sub_string(const std::string& sub_string, const std::string& language);
vector<sport_object> list_sports() const; vector<sport_object> list_sports() const;
vector<event_group_object> list_event_groups(sport_id_type sport_id) const; vector<event_group_object> list_event_groups(sport_id_type sport_id) const;
@ -1983,4 +1985,5 @@ FC_API( graphene::wallet::wallet_api,
(get_tournament) (get_tournament)
(get_order_book) (get_order_book)
(get_total_matched_bet_amount_for_betting_market_group) (get_total_matched_bet_amount_for_betting_market_group)
(get_events_containing_sub_string)
) )

View file

@ -72,6 +72,8 @@
#include <graphene/chain/protocol/rock_paper_scissors.hpp> #include <graphene/chain/protocol/rock_paper_scissors.hpp>
#include <graphene/chain/rock_paper_scissors.hpp> #include <graphene/chain/rock_paper_scissors.hpp>
#include <graphene/bookie/bookie_api.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp> #include <graphene/chain/protocol/fee_schedule.hpp>
#include <graphene/utilities/git_revision.hpp> #include <graphene/utilities/git_revision.hpp>
#include <graphene/utilities/key_conversion.hpp> #include <graphene/utilities/key_conversion.hpp>
@ -525,7 +527,8 @@ public:
_remote_api(rapi), _remote_api(rapi),
_remote_db(rapi->database()), _remote_db(rapi->database()),
_remote_net_broadcast(rapi->network_broadcast()), _remote_net_broadcast(rapi->network_broadcast()),
_remote_hist(rapi->history()) _remote_hist(rapi->history()),
_remote_bookie(rapi->bookie())
{ {
chain_id_type remote_chain_id = _remote_db->get_chain_id(); chain_id_type remote_chain_id = _remote_db->get_chain_id();
if( remote_chain_id != _chain_id ) if( remote_chain_id != _chain_id )
@ -3159,6 +3162,7 @@ public:
fc::api<database_api> _remote_db; fc::api<database_api> _remote_db;
fc::api<network_broadcast_api> _remote_net_broadcast; fc::api<network_broadcast_api> _remote_net_broadcast;
fc::api<history_api> _remote_hist; fc::api<history_api> _remote_hist;
fc::api<bookie_api> _remote_bookie;
optional< fc::api<network_node_api> > _remote_net_node; optional< fc::api<network_node_api> > _remote_net_node;
optional< fc::api<graphene::debug_witness::debug_api> > _remote_debug; optional< fc::api<graphene::debug_witness::debug_api> > _remote_debug;
@ -5630,7 +5634,12 @@ order_book wallet_api::get_order_book( const string& base, const string& quote,
asset wallet_api::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id) asset wallet_api::get_total_matched_bet_amount_for_betting_market_group(betting_market_group_id_type group_id)
{ {
return asset(); return( my->_remote_bookie->get_total_matched_bet_amount_for_betting_market_group(group_id) );
}
std::vector<event_object> wallet_api::get_events_containing_sub_string(const std::string& sub_string, const std::string& language)
{
return( my->_remote_bookie->get_events_containing_sub_string(sub_string, language) );
} }
// default ctor necessary for FC_REFLECT // default ctor necessary for FC_REFLECT

View file

@ -864,6 +864,11 @@ BOOST_AUTO_TEST_CASE( wimbledon_2017_gentelmen_singles_final_test )
auto cilic_wins_final_market_id = cilic_wins_final_market.id; auto cilic_wins_final_market_id = cilic_wins_final_market.id;
auto federer_wins_final_market_id = federer_wins_final_market.id; auto federer_wins_final_market_id = federer_wins_final_market.id;
update_event(cilic_vs_federer.id,
fc::optional<object_id_type>(),
internationalized_string_type({{"en", "R. Federer vs. M. Cilic"}}),
fc::optional<internationalized_string_type>());
generate_blocks(13); generate_blocks(13);
const betting_market_group_object& betting_market_group = moneyline_cilic_vs_federer_id(db); const betting_market_group_object& betting_market_group = moneyline_cilic_vs_federer_id(db);