File size: 4,158 Bytes
dec6d5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
pragma solidity ^0.4.24;
contract BREBuy {
struct ContractParam {
uint32 totalSize ;
uint256 singlePrice;
uint8 pumpRate;
bool hasChange;
}
address owner = 0x0;
uint32 gameIndex = 0;
uint256 totalPrice= 0;
ContractParam public setConfig;
ContractParam public curConfig;
address[] public addressArray = new address[](0);
event addPlayerEvent(uint32,address);
event GameOverEvent(uint32,uint32,uint256,uint8,address,uint );
constructor ( uint32 _totalSize,
uint256 _singlePrice
) public payable {
owner = msg.sender;
setConfig = ContractParam(_totalSize,_singlePrice * 1 finney ,5,false);
curConfig = ContractParam(_totalSize,_singlePrice * 1 finney ,5,false);
startNewGame();
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
if (newOwner != address(0)) {
owner = newOwner;
}
}
function changeConfig( uint32 _totalSize,uint256 _singlePrice,uint8 _pumpRate) onlyOwner public payable {
curConfig.hasChange = true;
if(setConfig.totalSize != _totalSize) {
setConfig.totalSize = _totalSize;
}
if(setConfig.pumpRate != _pumpRate){
setConfig.pumpRate = _pumpRate;
}
if(setConfig.singlePrice != _singlePrice * 1 finney){
setConfig.singlePrice = _singlePrice * 1 finney;
}
}
function startNewGame() private {
gameIndex++;
if(curConfig.hasChange) {
if(curConfig.totalSize != setConfig.totalSize) {
curConfig.totalSize = setConfig.totalSize;
}
if(curConfig.singlePrice != setConfig.singlePrice){
curConfig.singlePrice = setConfig.singlePrice;
}
if( curConfig.pumpRate != setConfig.pumpRate) {
curConfig.pumpRate = setConfig.pumpRate;
}
curConfig.hasChange = false;
}
addressArray.length=0;
}
function addPlayer() public payable {
require(msg.value == curConfig.singlePrice);
totalPrice = totalPrice + msg.value;
addressArray.push(msg.sender);
emit addPlayerEvent(gameIndex,msg.sender);
if(addressArray.length >= curConfig.totalSize) {
gameResult();
startNewGame();
}
}
function getGameInfo() public view returns (uint256,uint32,uint256,uint8,address[],uint256) {
return (gameIndex,
curConfig.totalSize,
curConfig.singlePrice,
curConfig.pumpRate,
addressArray,
totalPrice);
}
function getSelfCount() private view returns (uint32) {
uint32 count = 0;
for(uint i = 0; i < addressArray.length; i++) {
if(msg.sender == addressArray[i]) {
count++;
}
}
return count;
}
function gameResult() private {
uint index = getRamdon();
address lastAddress = addressArray[index];
uint totalBalace = address(this).balance;
uint giveToOwn = totalBalace * curConfig.pumpRate / 100;
uint giveToActor = totalBalace - giveToOwn;
owner.transfer(giveToOwn);
lastAddress.transfer(giveToActor);
emit GameOverEvent(
gameIndex,
curConfig.totalSize,
curConfig.singlePrice,
curConfig.pumpRate,
lastAddress,
now);
}
function getRamdon() private view returns (uint) {
bytes32 ramdon = keccak256(abi.encodePacked(ramdon,now,blockhash(block.number-1)));
for(uint i = 0; i < addressArray.length; i++) {
ramdon = keccak256(abi.encodePacked(ramdon,now, addressArray[i]));
}
uint index = uint(ramdon) % addressArray.length;
return index;
}
} |