pragma solidity ^0.6.2;
contract Fomo3D{
mapping(address => uint256) public keyMap; // 每个用户持有key的数量
mapping(address => bool) public turnOutMap; // 记录用户是否领取奖励
uint256 public totalKey; // key的总数量
uint256 public totalAbonus; // 总分红
uint256 public endAbonus; // 游戏结束给最后一个用户的奖励
uint256 public intervalTime = 1 days;
uint256 public endTime; // 游戏结束时间
uint256 public keyPrice = 1 ether; // 单个key的价格
address payable public winner; // 胜利者
// 购买key
function buyKey() public payable{
require(now < endTime,"game over"); // 游戏还未结束
uint256 num = getNum(msg.value); // 购买key的数量
keyMap[msg.sender] += num;
totalKey += num;
totalAbonus += msg.value / 2; // 50%资产用来给参与用户分红
endAbonus += msg.value / 2; // 50%资产用来奖励给最后一个参与者
endTime = now + intervalTime; // 只要有人参与就延长游戏结束时间
winner = msg.sender; // 胜利者一定是最后一个参与者
keyPrice += 1 finney;
}
// 游戏获胜者领取奖励
function turnOutBywinner() public{
require(now >= endTime,"game is not over");
winner.transfer(endAbonus);
winner = address(0x0);
}
// 游戏结束,用户根据key领取分红
function turnOutByUser() public{
require(turnOutMap[msg.sender] == false,"Your have already received the reward");
uint256 value = keyMap[msg.sender] * totalAbonus / totalKey; // 计算用户获取奖励
msg.sender.transfer(value); // 发放奖励
turnOutMap[msg.sender] = true; // 记录该用户已经领取奖励
}
// 根据转入ETH金额计算可以购买key的数量
function getNum(uint256 _value) public view returns(uint256){
if(_value == keyPrice){
return 1;
}else if(_value == 2 * keyPrice){
return 2;
}else if(_value == 5 * keyPrice){
return 5;
}else if(_value == 10 * keyPrice){
return 10;
}else{
revert("eth amount is error");
}
}
}
网友评论