From 2be531a5305bc59a20f0e12ccaa805ecae4000cc Mon Sep 17 00:00:00 2001 From: allerria Date: Tue, 2 Feb 2021 16:30:06 +0300 Subject: [PATCH 1/2] add player ban mechanic --- contracts/casino/include/casino/casino.hpp | 18 ++++++++++++++++ contracts/casino/src/casino.cpp | 19 +++++++++++++++- tests/casino_test.cpp | 25 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/contracts/casino/include/casino/casino.hpp b/contracts/casino/include/casino/casino.hpp index ca520c9..0cf3834 100644 --- a/contracts/casino/include/casino/casino.hpp +++ b/contracts/casino/include/casino/casino.hpp @@ -182,6 +182,14 @@ struct [[eosio::table("gameparams"), eosio::contract("casino")]] game_params_row }; using game_params_table = eosio::multi_index<"gameparams"_n, game_params_row>; + +struct [[eosio::table("banlist"), eosio::contract("casino")]] ban_list_row { + name player; + + uint64_t primary_key() const { return player.value; } +}; + +using ban_list_table = eosio::multi_index<"banlist"_n, ban_list_row>; class [[eosio::contract("casino")]] casino: public eosio::contract { public: @@ -292,6 +300,14 @@ class [[eosio::contract("casino")]] casino: public eosio::contract { [[eosio::action("setgameparam2")]] void set_game_param_token(uint64_t game_id, std::string token, game_params_type params); + // ========================== + // ban + [[eosio::action("banplayer")]] + void ban_player(name player); + + [[eosio::action("unbanplayer")]] + void unban_player(name player); + // ========================== // constants static constexpr int64_t seconds_per_day = 24 * 3600; @@ -328,6 +344,8 @@ class [[eosio::contract("casino")]] casino: public eosio::contract { global_tokens_state gtokens; player_tokens_table player_tokens; game_params_table game_params; + + ban_list_table ban_list; name get_owner() const { return gstate.owner; diff --git a/contracts/casino/src/casino.cpp b/contracts/casino/src/casino.cpp index ddbb7fc..9b856ae 100644 --- a/contracts/casino/src/casino.cpp +++ b/contracts/casino/src/casino.cpp @@ -17,7 +17,8 @@ casino::casino(name receiver, name code, eosio::datastream ds): game_tokens(_self, _self.value), _gtokens(_self, _self.value), player_tokens(_self, _self.value), - game_params(_self, _self.value) { + game_params(_self, _self.value), + ban_list(_self, _self.value) { version.set(version_row {CONTRACT_VERSION}, _self); @@ -561,4 +562,20 @@ void casino::set_game_param_token(uint64_t game_id, std::string token, game_para }); } +void casino::ban_player(name player) { + require_auth(get_owner()); + const auto it = ban_list.find(player.value); + eosio::check(it == ban_list.end(), "player is already banned"); + ban_list.emplace(get_self(), [&](auto& row) { + row.player = player; + }); +} + +void casino::unban_player(name player) { + require_auth(get_owner()); + const auto it = ban_list.find(player.value); + eosio::check(it != ban_list.end(), "player is not banned"); + ban_list.erase(it); +} + } // namespace casino diff --git a/tests/casino_test.cpp b/tests/casino_test.cpp index dfdddee..782b65c 100644 --- a/tests/casino_test.cpp +++ b/tests/casino_test.cpp @@ -239,6 +239,11 @@ class casino_tester : public basic_tester { ("quantity", amount) ("memo", memo) ); } + + bool is_player_banned(const name player) { + vector data = get_row_by_account(casino_account, casino_account, N(banlist), player.value ); + return data.empty() ? false : true; + } }; const account_name casino_tester::casino_account = N(dao.casino); @@ -1859,6 +1864,26 @@ BOOST_FIXTURE_TEST_CASE(set_params_test, casino_tester) try { BOOST_REQUIRE_EQUAL(params_kek, expected_params_kek); } FC_LOG_AND_RETHROW() +BOOST_FIXTURE_TEST_CASE(ban_player_test, casino_tester) try { + const name player = N(player); + + BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); + + BOOST_REQUIRE_EQUAL( + success(), + push_action(casino_account, N(banplayer), casino_account, mvo()("player", player)) + ); + + BOOST_REQUIRE_EQUAL(true, is_player_banned(player)); + + BOOST_REQUIRE_EQUAL( + success(), + push_action(casino_account, N(unbanplayer), casino_account, mvo()("player", player)) + ); + + BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); +} FC_LOG_AND_RETHROW() + BOOST_AUTO_TEST_SUITE_END() } // namespace testing From be2660c8bedd1d7485e7102a4cb717e08bed3a86 Mon Sep 17 00:00:00 2001 From: allerria Date: Tue, 2 Feb 2021 17:12:05 +0300 Subject: [PATCH 2/2] move ban from casino into platform --- contracts/casino/include/casino/casino.hpp | 18 ------------- contracts/casino/src/casino.cpp | 19 +------------- .../platform/include/platform/platform.hpp | 15 +++++++++++ contracts/platform/src/platform.cpp | 19 +++++++++++++- tests/casino_test.cpp | 25 ------------------- tests/platform_test.cpp | 25 +++++++++++++++++++ 6 files changed, 59 insertions(+), 62 deletions(-) diff --git a/contracts/casino/include/casino/casino.hpp b/contracts/casino/include/casino/casino.hpp index 0cf3834..1b93f6f 100644 --- a/contracts/casino/include/casino/casino.hpp +++ b/contracts/casino/include/casino/casino.hpp @@ -183,14 +183,6 @@ struct [[eosio::table("gameparams"), eosio::contract("casino")]] game_params_row using game_params_table = eosio::multi_index<"gameparams"_n, game_params_row>; -struct [[eosio::table("banlist"), eosio::contract("casino")]] ban_list_row { - name player; - - uint64_t primary_key() const { return player.value; } -}; - -using ban_list_table = eosio::multi_index<"banlist"_n, ban_list_row>; - class [[eosio::contract("casino")]] casino: public eosio::contract { public: using eosio::contract::contract; @@ -300,14 +292,6 @@ class [[eosio::contract("casino")]] casino: public eosio::contract { [[eosio::action("setgameparam2")]] void set_game_param_token(uint64_t game_id, std::string token, game_params_type params); - // ========================== - // ban - [[eosio::action("banplayer")]] - void ban_player(name player); - - [[eosio::action("unbanplayer")]] - void unban_player(name player); - // ========================== // constants static constexpr int64_t seconds_per_day = 24 * 3600; @@ -345,8 +329,6 @@ class [[eosio::contract("casino")]] casino: public eosio::contract { player_tokens_table player_tokens; game_params_table game_params; - ban_list_table ban_list; - name get_owner() const { return gstate.owner; } diff --git a/contracts/casino/src/casino.cpp b/contracts/casino/src/casino.cpp index 9b856ae..ddbb7fc 100644 --- a/contracts/casino/src/casino.cpp +++ b/contracts/casino/src/casino.cpp @@ -17,8 +17,7 @@ casino::casino(name receiver, name code, eosio::datastream ds): game_tokens(_self, _self.value), _gtokens(_self, _self.value), player_tokens(_self, _self.value), - game_params(_self, _self.value), - ban_list(_self, _self.value) { + game_params(_self, _self.value) { version.set(version_row {CONTRACT_VERSION}, _self); @@ -562,20 +561,4 @@ void casino::set_game_param_token(uint64_t game_id, std::string token, game_para }); } -void casino::ban_player(name player) { - require_auth(get_owner()); - const auto it = ban_list.find(player.value); - eosio::check(it == ban_list.end(), "player is already banned"); - ban_list.emplace(get_self(), [&](auto& row) { - row.player = player; - }); -} - -void casino::unban_player(name player) { - require_auth(get_owner()); - const auto it = ban_list.find(player.value); - eosio::check(it != ban_list.end(), "player is not banned"); - ban_list.erase(it); -} - } // namespace casino diff --git a/contracts/platform/include/platform/platform.hpp b/contracts/platform/include/platform/platform.hpp index e05e862..c65d9dd 100644 --- a/contracts/platform/include/platform/platform.hpp +++ b/contracts/platform/include/platform/platform.hpp @@ -82,6 +82,14 @@ struct [[eosio::table("token"), eosio::contract("platform")]] token_row { using token_table = eosio::multi_index<"token"_n, token_row>; +struct [[eosio::table("banlist"), eosio::contract("platform")]] ban_list_row { + name player; + + uint64_t primary_key() const { return player.value; } +}; + +using ban_list_table = eosio::multi_index<"banlist"_n, ban_list_row>; + class [[eosio::contract("platform")]] platform: public eosio::contract { public: using eosio::contract::contract; @@ -137,12 +145,19 @@ class [[eosio::contract("platform")]] platform: public eosio::contract { [[eosio::action("deltoken")]] void del_token(std::string token_name); + [[eosio::action("banplayer")]] + void ban_player(name player); + + [[eosio::action("unbanplayer")]] + void unban_player(name player); + private: version_singleton version; global_singleton global; casino_table casinos; game_table games; token_table tokens; + ban_list_table ban_list; }; diff --git a/contracts/platform/src/platform.cpp b/contracts/platform/src/platform.cpp index 57fe84e..67ddd49 100644 --- a/contracts/platform/src/platform.cpp +++ b/contracts/platform/src/platform.cpp @@ -9,7 +9,8 @@ platform::platform(name receiver, name code, eosio::datastream ds): global(_self, _self.value), casinos(_self, _self.value), games(_self, _self.value), - tokens(_self, _self.value) + tokens(_self, _self.value), + ban_list(_self, _self.value) { version.set(version_row {CONTRACT_VERSION}, _self); } @@ -162,4 +163,20 @@ void platform::del_token(std::string token_name) { tokens.erase(tokens.require_find(get_token_pk(token_name), "del token: no token found")); } +void platform::ban_player(name player) { + require_auth(get_self()); + const auto it = ban_list.find(player.value); + eosio::check(it == ban_list.end(), "player is already banned"); + ban_list.emplace(get_self(), [&](auto& row) { + row.player = player; + }); +} + +void platform::unban_player(name player) { + require_auth(get_self()); + const auto it = ban_list.find(player.value); + eosio::check(it != ban_list.end(), "player is not banned"); + ban_list.erase(it); +} + } // namespace platform diff --git a/tests/casino_test.cpp b/tests/casino_test.cpp index 782b65c..dfdddee 100644 --- a/tests/casino_test.cpp +++ b/tests/casino_test.cpp @@ -239,11 +239,6 @@ class casino_tester : public basic_tester { ("quantity", amount) ("memo", memo) ); } - - bool is_player_banned(const name player) { - vector data = get_row_by_account(casino_account, casino_account, N(banlist), player.value ); - return data.empty() ? false : true; - } }; const account_name casino_tester::casino_account = N(dao.casino); @@ -1864,26 +1859,6 @@ BOOST_FIXTURE_TEST_CASE(set_params_test, casino_tester) try { BOOST_REQUIRE_EQUAL(params_kek, expected_params_kek); } FC_LOG_AND_RETHROW() -BOOST_FIXTURE_TEST_CASE(ban_player_test, casino_tester) try { - const name player = N(player); - - BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); - - BOOST_REQUIRE_EQUAL( - success(), - push_action(casino_account, N(banplayer), casino_account, mvo()("player", player)) - ); - - BOOST_REQUIRE_EQUAL(true, is_player_banned(player)); - - BOOST_REQUIRE_EQUAL( - success(), - push_action(casino_account, N(unbanplayer), casino_account, mvo()("player", player)) - ); - - BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); -} FC_LOG_AND_RETHROW() - BOOST_AUTO_TEST_SUITE_END() } // namespace testing diff --git a/tests/platform_test.cpp b/tests/platform_test.cpp index 836bab8..958576c 100644 --- a/tests/platform_test.cpp +++ b/tests/platform_test.cpp @@ -47,6 +47,11 @@ class platform_tester : public basic_tester { vector data = get_row_by_account(platform_name, platform_name, N(token), pk); return data.empty() ? fc::variant() : abi_ser[platform_name].binary_to_variant("token_row", data, abi_serializer_max_time); } + + bool is_player_banned(const name player) { + vector data = get_row_by_account(platform_name, platform_name, N(banlist), player.value ); + return data.empty() ? false : true; + } }; const account_name platform_tester::platform_name = N(platform); @@ -503,5 +508,25 @@ BOOST_FIXTURE_TEST_CASE(add_del_token, platform_tester) try { } FC_LOG_AND_RETHROW() +BOOST_FIXTURE_TEST_CASE(ban_player_test, platform_tester) try { + const name player = N(player); + + BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); + + BOOST_REQUIRE_EQUAL( + success(), + push_action(platform_name, N(banplayer), platform_name, mvo()("player", player)) + ); + + BOOST_REQUIRE_EQUAL(true, is_player_banned(player)); + + BOOST_REQUIRE_EQUAL( + success(), + push_action(platform_name, N(unbanplayer), platform_name, mvo()("player", player)) + ); + + BOOST_REQUIRE_EQUAL(false, is_player_banned(player)); +} FC_LOG_AND_RETHROW() + BOOST_AUTO_TEST_SUITE_END() } // namespace testing