2019-05-15

#include <eosio/eosio.hpp>

#include <eosio/asset.hpp>

#include <eosio/transaction.hpp>

#include <eosio/crypto.hpp>

#include <eosio/time.hpp>

#include <eosio/system.hpp>

#include <eosio/print.hpp>

using namespace eosio;

using namespace std;

CONTRACT pvpbetwallet : public contract {

? public:

? ? ? using contract::contract;

? ? ? pvpbetwallet( name receiver, name code, datastream<const char*> ds )

? ? ? ? : contract(receiver, code, ds), boardtable(receiver, receiver.value), bettable(receiver, receiver.value) {}

? ? ? ACTION transfer(name from, name to, asset quantity, string memo);

? ? ? ACTION reveal(const uint64_t &table_id, const checksum256 &seed);

? ? ? ACTION send(const int &index, const name &winner, const uint64_t &table_id);? ? ?

? ? ? ? // EOSIO network

? ? const string symbol_name = "EOS";

? ? // Contract network

? ? const symbol network_symbol = symbol(symbol_name, 4);

? ? const name & lord? = name("suwzhaoihuan");

? ? const name & wallet = name("pvpbetwallet");


? ? const int values[16][3] = {

? ? ? ? {1000, 1960, 40},

? ? ? ? {1960, 3840, 80},

? ? ? ? {3840, 7520, 160},

? ? ? ? {7520, 14720, 320},

? ? ? ? {14720, 28800, 640},

? ? ? ? {28800, 56320, 1280},

? ? ? ? {56320, 110080, 2560},

? ? ? ? {110080, 215040, 5120},

? ? ? ? {215040, 419840, 10240},

? ? ? ? {419840, 819200, 20480},

? ? ? ? {819200, 1597440, 40960},

? ? ? ? {1597440, 3112960, 81920},

? ? ? ? {3112960, 6062080, 163840},

? ? ? ? {6062080, 11796480, 327680},

? ? ? ? {11796480, 22937600, 655360},

? ? ? ? {22937600, 44564480, 1310720}

? ? };

? ? time_point next(){

? ? ? ? return current_time_point() + seconds(360);

? ? }


? ? int findIndex(asset quantity){

? ? ? ? for (int i = 0; i < 16; i++){

? ? ? ? ? ? if (quantity.amount == values[i][0]){

? ? ? ? ? ? ? ? return i;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? check(false, "quantity index not found");

? ? }

? ? void split(const std::string &s, vector<std::string> &v, const std::string &c){

? ? ? ? std::string::size_type pos1, pos2;

? ? ? ? pos2 = s.find(c);

? ? ? ? pos1 = 0;

? ? ? ? while (std::string::npos != pos2){

? ? ? ? ? ? v.push_back(s.substr(pos1, pos2 - pos1));

? ? ? ? ? ? pos1 = pos2 + c.size();

? ? ? ? ? ? pos2 = s.find(c, pos1);

? ? ? ? }

? ? ? ? if (pos1 != s.length()){

? ? ? ? ? ? v.push_back(s.substr(pos1));

? ? ? ? }

? ? }

TABLE board_table {

? ? ? ? ? ? // 僅有一條記錄

? ? ? ? ? ? uint64_t id;

? ? ? ? ? ? // 登頂時間

? ? ? ? ? ? time_point top_time;

? ? ? ? ? ? // 目前積累的獎賞

? ? ? ? ? ? asset pool;

? ? ? ? ? ? // 當(dāng)前頭部用戶

? ? ? ? ? ? name player_name;

? ? ? ? ? ? // 頭部用戶投注額分紅后進行減半操作

? ? ? ? ? ? asset quantity;

? ? ? ? ? ? // 進行下次分紅的時間诈皿。

? ? ? ? ? ? time_point next_time;


? ? ? ? ? ? uint64_t primary_key() const { return id; }

? ? ? ? ? };

? ? ? ? ? // typedef eosio::multi_index<N(表名),表的對象類型> 實例化變量名

? ? ? ? ? typedef eosio::multi_index<"boardtablea"_n, board_table> board_tables;

? ? ? ? TABLE bet_table {

? ? ? ? ? ? // 桌子的標(biāo)識

? ? ? ? ? ? uint64_t id;

? ? ? ? ? ? // 有效時間

? ? ? ? ? ? time_point valid_time;

? ? ? ? ? ? // 投注額

? ? ? ? ? ? asset quantity;

? ? ? ? ? ? // 桌子的哈希

? ? ? ? ? ? checksum256 table_hash;

? ? ? ? ? ? // 玩家1名稱

? ? ? ? ? ? name p1_name;

? ? ? ? ? ? // 玩家2名稱

? ? ? ? ? ? name p2_name;

? ? ? ? ? ? // 玩家1哈希

? ? ? ? ? ? checksum256 p1_hash;

? ? ? ? ? ? // 玩家2哈希

? ? ? ? ? ? checksum256 p2_hash;

? ? ? ? ? ? // 桌子的種子

? ? ? ? ? ? checksum256 table_seed;

? ? ? ? ? ? // 玩家1種子

? ? ? ? ? ? checksum256 p1_seed;

? ? ? ? ? ? // 玩家2種子

? ? ? ? ? ? checksum256 p2_seed;


? ? ? ? ? ? uint64_t primary_key() const { return id; }

? ? ? ? ? };

? ? ? ? ? typedef eosio::multi_index<"bettablea"_n, bet_table> bet_tables;


? ? ? using transfer_action = action_wrapper<"transfer"_n, &pvpbetwallet::transfer>;

? ? ? using reveal_action = action_wrapper<"reveal"_n, &pvpbetwallet::reveal>;

? ? ? using send_action = action_wrapper<"send"_n, &pvpbetwallet::send>;


? ? ? board_tables boardtable;

? bet_tables bettable;

};

ACTION pvpbetwallet::transfer(name from, name to, asset quantity, std::string memo){

? if (from == lord){

? ? // 補充能量

? ? return;

? }


? require_auth(_self);

? eosio::check(from != to, "cannot transfer to self");

? eosio::check(_self == to, "must transfer to this contract");

? eosio::check(memo.size() <= 256, "memo must smaller than 256");

? eosio::check(quantity.symbol == eosio::symbol("EOS", 4), "only accepts EOS");

? eosio::check(quantity.is_valid(), "invalid token transfer");

? int index = pvpbetwallet::findIndex(quantity);

? eosio::check(-1 != index, "quantity not found");

? // composed by user_id-table_hash-user_hash-user_seed-player_hash

? std::vector<std::string> memos;

? pvpbetwallet::split(memo, memos, "-");

? std::string user_id = memos[0];

? std::string table_hash = memos[1];

? std::string user_hash = memos[2];

? std::string user_seed = memos[3];

? std::string player_hash = memos[4];

? // 調(diào)試

? eosio::print("user_id:" + memos[0]);

? eosio::print("table_hash:" + memos[1]);

? eosio::print("user_hash:" + memos[2]);

? eosio::print("user_seed:" + memos[3]);

? eosio::print("player_hash:" + memos[4]);

// 驗證給定數(shù)據(jù)和其sha256哈希值是否匹配

//void assert_sha256( const char* data, uint32_t length, const eosio::checksum256& hash );

? // 選手需要檢查user_hash為user_seed生成芦疏。

? //const char* seed_sha256 = const_cast<char*>(user_seed.c_str());

? eosio::assert_sha256(user_seed.c_str, std::strlen(user_seed), user_hash);

? auto player_id = atoi(user_id.c_str);

? auto table_id = player_id / 2;

? // bet_tables bets(_code, _code.value);

? auto bet = bets.find(table_id);

? if (bet == bets.end()){

? ? bets.emplace(_self, [&](auto &bet) {

? ? ? ? bet.id = table_id;

? ? ? ? bet.valid_time = eosio::current_time_point() + 180000;

? ? ? ? bet.quantity = quantity;

? ? ? ? bet.table_hash = table_hash;

? ? ? ? bet.p1_name = from;

? ? ? ? bet.p1_hash = user_hash;

? ? ? ? bet.p2_hash = player_hash;

? ? ? ? bet.p1_seed = user_seed;

? ? });

? }else{

? ? // 選手需要檢查user_hash為user_seed生成儡蔓,

? ? // 同時檢查p1_hash與player_hash相等,同時檢查p2_hash與user_hash相等蛉抓,

? ? // 同時檢查table_hash與數(shù)據(jù)庫的table_hash相等,同時檢查數(shù)量相等。

? ? eosio::check(bet->valid_time >= eosio::current_time_point(), "time expired");

? ? eosio::check(quantity.amount == bet->quantity.amount, "must equal quantity");

? ? eosio::check(player_hash == bet->p1_hash, "must match player hash");

? ? eosio::check(user_hash == bet->p2_hash, "must match user hash");

? ? eosio::check(table_hash == bet->table_hash, "must match table hash");

? ? bets.modify(bet, _self, [&](auto &bet) {

? ? ? ? bet.p2_name = from;

? ? ? ? bet.p2_seed = user_seed;

? ? });

? }

}

ACTION pvpbetwallet::reveal(const uint64_t &table_id, const eosio::checksum256 &seed){

}

ACTION pvpbetwallet::send(const int &index, const name &winner, const uint64_t &table_id){

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市彬犯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌哩至,老刑警劉巖躏嚎,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異菩貌,居然都是意外死亡卢佣,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門箭阶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來虚茶,“玉大人,你說我怎么就攤上這事仇参∴诮校” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵诈乒,是天一觀的道長罩扇。 經(jīng)常有香客問我,道長怕磨,這世上最難降的妖魔是什么喂饥? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮肠鲫,結(jié)果婚禮上员帮,老公的妹妹穿的比我還像新娘。我一直安慰自己导饲,他們只是感情好捞高,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著渣锦,像睡著了一般硝岗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上袋毙,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天辈讶,我揣著相機與錄音,去河邊找鬼娄猫。 笑死贱除,一個胖子當(dāng)著我的面吹牛生闲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播月幌,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼碍讯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了扯躺?” 一聲冷哼從身側(cè)響起捉兴,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎录语,沒想到半個月后倍啥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡澎埠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年虽缕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒲稳。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡氮趋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出江耀,到底是詐尸還是另有隱情剩胁,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布祥国,位于F島的核電站昵观,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏舌稀。R本人自食惡果不足惜啊犬,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望扩借。 院中可真熱鬧椒惨,春花似錦缤至、人聲如沸潮罪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嫉到。三九已至,卻和暖如春月洛,著一層夾襖步出監(jiān)牢的瞬間何恶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工嚼黔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留细层,地道東北人惜辑。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像疫赎,于是被迫代替她去往敵國和親盛撑。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容

  • yun'zudict: python的dict是字典dictionary捧搞,其他語言中為map抵卫,有鍵和值。 還要注意...
    每日派森閱讀 248評論 0 0
  • 學(xué)習(xí)區(qū)塊鏈,最刺激的莫過于發(fā)幣晚树,第一篇文章里介紹了如何搭建EOS開發(fā)環(huán)境姻采,第二篇文章我們已經(jīng)介紹了如何部署調(diào)用合約...
    P叔閱讀 4,738評論 5 6
  • 灞河是發(fā)源于秦嶺的一條河,是“八水繞長安”中的八水之一题涨,灞河古稱滋水偎谁,春秋時秦穆公不斷向外擴張,稱霸西戎后改名霸水...
    長安行閱讀 1,246評論 0 1
  • 三個八小時纲堵,上班時間可以把工作外接硬盤整理記錄下來巡雨,隨時清空更新記錄;下班時間很多時候被瑣事和惰性占據(jù)席函,上下班路上...
    曲同寧閱讀 203評論 4 0