-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOGames.sol
86 lines (70 loc) · 2.83 KB
/
XOGames.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
pragma solidity ^0.4.17;
import "./XOGameBoard.sol";
contract XOGames{
address owner; // владелец контракта
uint256 commission = 5; // комиссия за использование платформы 5% ;-)
uint256 public numberOpenGames;
// статусы игры
enum StatusGame {
GAME_EXPECTED, // игра готова к старту, ожидается второй игрок
GAME_START // игра стартовала
}
// структура игры
struct Game{
address player1; // игрок 1, он инициирует игру
uint256 rate;
uint256 numGame;
StatusGame statusGame; // статус игры
}
struct OpenGame{
address addressGameBoard;
uint256 rateInGame;
}
mapping(uint256 => OpenGame) public openGames;
mapping(address => Game) public games; // игры
// модификатор прав владельуа контракта
modifier isOwner {
require(owner == msg.sender);
_;
}
event GameWait(address _address, uint256 _rate);
function XOGames() public {
owner = msg.sender;
numberOpenGames = 0;
}
function setCommission(uint256 _commission) public isOwner returns(uint256){
commission = _commission;
return commission;
}
function createGame() public payable returns(XOGameBoard gameBoard){
require(msg.value != 0);
numberOpenGames++;
gameBoard = (new XOGameBoard).value((msg.value - (msg.value * commission / 100)))(msg.sender);
games[gameBoard].player1 = msg.sender;
games[gameBoard].statusGame = StatusGame.GAME_EXPECTED;
games[gameBoard].rate = msg.value;
games[gameBoard].numGame = numberOpenGames;
openGames[numberOpenGames].addressGameBoard = gameBoard;
openGames[numberOpenGames].rateInGame = msg.value;
emit GameWait(gameBoard, msg.value);
return gameBoard;
}
function joinToGame(XOGameBoard gameBoard) public payable returns(bool){
require(msg.sender != games[gameBoard].player1);
require(games[gameBoard].statusGame != StatusGame.GAME_START);
require(msg.value != 0);
require(games[gameBoard].rate == msg.value);
games[gameBoard].statusGame = StatusGame.GAME_START;
gameBoard.setRate.value((msg.value - (msg.value * commission / 100)))(msg.sender);
delete openGames[games[gameBoard].numGame];
delete games[gameBoard];
return true;
}
function sendBalance(address _address) public payable isOwner returns(bool){
_address.transfer(address(this).balance);
}
// уничтожение контракта
function kill() public isOwner {
selfdestruct(owner);
}
}