竞猜活动区块链方案探索
本文节选自电子书《Netkiller Blockchain 手札》
Netkiller Blockchain 手札
本文作者最近在找工作,有意向致电 13113668890
Mr. Neo Chan, 陈景峯(BG7NYT)
中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>
文档始创于2018-02-10
版权 © 2018 Netkiller(Neo Chan). All rights reserved.
版权声明
转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。
微信订阅号 netkiller-ebook (微信扫描二维码) |
---|
QQ:13721218 请注明“读者” |
QQ群:128659835 请注明“读者” |
网站:http://www.netkiller.cn |
合约实现了报名,退出,参加人数控制,竞猜次数控制,公布答案,获奖名单等等功能
pragma solidity ^0.4.20; // Author netkiller<netkiller@msn.com> // Home http://www.netkiller.cn contract Guess { address public owner; string name; //活动名称 bool start = false; //合约锁 uint number; //参赛人数统计 uint public quota; //名额限定 mapping (address => string) public registrantsPaid; //参加活动 uint maxCounter = 3; //最大竞猜次数 mapping (address => uint) counter; //竞猜次数统计 string public question; //竞猜问题 mapping (uint => string) public options; //选项 // 答案结构 struct Answer { address player; uint answer; } mapping (uint => Answer) public answer; //答案 uint answerIndex = 0; //公布最终答案 uint public expose; //获奖名单 mapping (address => uint) winner; function Guess(string _name, uint _quota) public { name = _name; quota = _quota; number = 0; } modifier onlyOwner { require(msg.sender == owner); _; } // 获取活动名称 function getName() public view returns(string){ return name; } function setStatus(bool _start) onlyOwner public { start = _start; } function setQuestion(string _question) public { question = _question; } // 增加人物属性,例如肤色,眼睛,头发等等 function putOptions(uint _key, string _value) onlyOwner public{ if(start == false){ options[_key] = _value; } } function join(string _password) onlyOwner public returns (bool success) { require(start == ture); if (number >= quota) { return false; } registrantsPaid[msg.sender] = _password; number++; return true; } function changeQuota(uint _quota) onlyOwner public { quota = _quota; } function quit() onlyOwner public { require(start == false); //require (registrantsPaid[msg.sender] == _password); delete registrantsPaid[msg.sender]; number--; } function setGuess(uint _answer) public{ require(start == ture); if(maxCounter > counter[msg.sender]){ counter[msg.sender]++; answer[answerIndex] = Answer(msg.sender, _answer); answerIndex++; } } //揭秘答案 function setExpose(uint _expose) onlyOwner public { require(start == ture); expose = _expose; for(uint i=0;i<answerIndex;i++) { Answer memory ans = answer[i]; if(ans.answer == expose){ winner[ans.player] = ans.answer; } } } // 数量 function getCount() public view returns(uint){ return number; } }