Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

2.5. 以太坊競猜活動區塊鏈探索

合約實現了報名,退出,參加人數控制,競猜次數控制,公佈答案,獲獎名單等等功能

	
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;
	}

}