1. 程式人生 > >創建自己的區塊鏈遊戲SLOT——以太坊代幣(三)

創建自己的區塊鏈遊戲SLOT——以太坊代幣(三)

rdm con there ppi multipl als div play 數組

一個以太坊合約版本的輪盤遊戲,向合約轉賬ETH,有幾率獲得3,5,10,100倍獎勵

合約地址:0x53DA598E70a1505Ad95cBF17fc5DCA0d2c51174b

捐贈ETH地址:0xdc834D429b3098f0568Af873c2d73b08790BF677

github地址:https://github.com/lxr1907/slot-on-ethereum

pragma solidity ^0.4.18;
contract LxrContract{
    //18 decimals 1ETH=10^18 wei
    uint8 constant decimals = 18;
    //合約擁有者
address owner; //所有者獎勵 uint256 ownerFee; //所有者獎勵比例千分之10 uint256 ownerFeeRate=10; //活動獎勵 uint256 extBonus=0; //0.001個ETH最小下註額 uint256 minBet=(10**uint256(decimals))/1000; //0.1個ETH最大下註額 uint256 maxBet=(10**uint256(decimals))/10; struct player{ //獎池 uint256 bonus;
//玩的次數 uint256 times; //上次的日期 uint256 lastDate; } //創建所有賬戶余額數組 mapping (address => player) players; address[] playersArray; /** * 初始化合約 */ function LxrContract( ) public { //初始化合約所有人 owner=msg.sender; } /// 使用以太坊下註
function () payable public { uint amount = msg.value; require(amount>=minBet); require(amount<=maxBet); addToArray(msg.sender); players[msg.sender].times+=1; uint lastDate=players[msg.sender].lastDate; players[msg.sender].lastDate=now; uint8 rewardMultiple=0; uint salt=block.coinbase.balance+this.balance+players[msg.sender].times*1313+lastDate; uint key1=salt%985; uint key2=salt%95; uint key3=salt%85; uint key4=salt%81; uint chance=15; if(players[msg.sender].bonus>minBet*105){ chance=35; } if(key1<now%1000&&now%1000<=key1+chance){ rewardMultiple=100; } if(key2<now%100&&now%100<=key2+5){ rewardMultiple=10; } if(key3<now%100&&now%100<=key3+15){ rewardMultiple=5; } if(key4<now%100&&now%100<=key4+19){ rewardMultiple=3; } //手續費 uint ownerFeePlus=amount/1000*ownerFeeRate; ownerFee=ownerFee+ownerFeePlus; players[msg.sender].bonus+=amount-ownerFeePlus; if(rewardMultiple>0){ if(players[msg.sender].bonus>rewardMultiple*amount){ players[msg.sender].bonus-=rewardMultiple*amount; if(this.balance-rewardMultiple*amount>ownerFee) msg.sender.transfer(rewardMultiple*amount); }else if(rewardMultiple>=5&&rewardMultiple<=10){ //獎勵不足倍數,清空並發放所有獎勵 uint bonus=players[msg.sender].bonus; players[msg.sender].bonus=0; if(this.balance-bonus>ownerFee) msg.sender.transfer(bonus); }else if(rewardMultiple==100){ //100倍獎池不足,則使用活動獎池金額的一半 if(extBonus>minBet){ extBonus=extBonus/2; msg.sender.transfer(extBonus); } } } } //將該地址加入數組 function addToArray(address _player) internal{ //如果不存在,將該地址加入數組,用於以後遍歷訪問 if(players[msg.sender].times==0){ playersArray.push(_player); } } /** * 提取合約所有人手續費 */ function getAll()public{ require(owner==msg.sender); require(this.balance>=ownerFee); uint _ownerFee=ownerFee; ownerFee=0; owner.transfer(_ownerFee); } /** * 設置手續費千分率 */ function setRate(uint rate)public{ require(owner==msg.sender); ownerFeeRate=rate; } /** * 設置最大下註為最小的多少倍 */ function setMax(uint count)public{ require(owner==msg.sender); maxBet=minBet*count; } /// 增加活動獎池的獎勵 function addExtBonus() payable public { uint amount = msg.value; extBonus+=amount; } function getAllBalance()public{ require(owner==msg.sender); owner.transfer(this.balance); } }

創建自己的區塊鏈遊戲SLOT——以太坊代幣(三)