-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathCryptoHerosGame.sol
97 lines (72 loc) · 2.97 KB
/
CryptoHerosGame.sol
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
pragma solidity ^0.4.17;
import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import './CryptoHerosToken.sol';
contract CryptoHerosGame is Ownable {
uint constant gameFee = 0.005 ether;
uint constant minPrice = 0.01 ether;
uint constant minHerosToken = 5 ether;
//address public cryptoHerosGame = 0x0;
uint256 public maxSingleGameId = 0;
uint nonce = 0;
CryptoHerosToken cryptoHerosToken;
struct SingleGame {
address player;
uint256 userResult;
uint256 contractResult;
uint256 playerBet;
uint8 game; // 0: smaller. 1: greater
uint8 result; // 0 user win, 1 contract win, 2 draw
}
SingleGame[] public singleGames;
mapping(address => uint256[]) public usersSingleGames;
constructor(CryptoHerosToken _cryptoHerosToken) public {
cryptoHerosToken = _cryptoHerosToken;
}
function () payable public {
}
function createSingleGame(uint _tokenId) payable public returns (uint256) {
require(msg.value >= minPrice);
require(address(this).balance >= minHerosToken);
require(cryptoHerosToken.ownerOf(_tokenId) == msg.sender);
uint userTokenNumber;
uint contractTokenNumber;
(userTokenNumber, , ,) = cryptoHerosToken.getTokenProperty(_tokenId);
(contractTokenNumber, , ,) = cryptoHerosToken.getTokenProperty(rand(0, cryptoHerosToken.getHerosLength()));
int result;
uint8 game = uint8(rand(0, 2));
if (game > 0) {
result = int(userTokenNumber - contractTokenNumber);
} else {
result = int(contractTokenNumber - userTokenNumber);
}
SingleGame memory _singleGame;
if (result == 0) {
_singleGame = SingleGame({player: msg.sender, userResult: userTokenNumber, contractResult: contractTokenNumber, playerBet: msg.value, game: game, result: 2});
require(msg.sender.send(msg.value * 1 - gameFee));
} else if (result > 0) {
_singleGame = SingleGame({player: msg.sender, userResult: userTokenNumber, contractResult: contractTokenNumber, playerBet: msg.value, game: game, result: 0});
require(msg.sender.send(msg.value * 150 / 100));
} else {
_singleGame = SingleGame({player: msg.sender, userResult: userTokenNumber, contractResult: contractTokenNumber, playerBet: msg.value, game: game, result: 1});
}
maxSingleGameId = singleGames.push(_singleGame) - 1;
uint256[] userSingleGames = usersSingleGames[msg.sender];
userSingleGames.push(maxSingleGameId);
return maxSingleGameId;
}
// function readUserGamesCount(address _address, uint _idx) public returns (uint){
// return usersSingleGames[_address][_idx].length;
// }
function getUserSingleGames(address _address) external view returns (uint256[]) {
return usersSingleGames[_address];
}
function rand(uint min, uint max) private returns (uint){
nonce++;
return uint(sha3(nonce))%(min+max)-min;
}
function withdraw(uint amount) public payable onlyOwner returns(bool) {
require(amount <= address(this).balance);
owner.transfer(amount);
return true;
}
}