-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWegaGameController.sol
268 lines (240 loc) · 8.87 KB
/
WegaGameController.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "hardhat/console.sol";
import "./escrow/IWegaERC20Escrow.sol";
import "./escrow/IEscrow.sol";
import "./roles/WegaProtocolAdminRole.sol";
import "./events/IWegaGameControllerEvents.sol";
import "./events/IERC20EscrowEvents.sol";
import "./IWegaGameController.sol";
import "./IWegaRandomizerController.sol";
import "./utils/Arrays.sol";
import './errors/WegaGameControllerErrors.sol';
import "./utils/Strings.sol";
import "./games/IWega.sol";
/**
* @title GameController (MVP)
* @author @RasenGUY @Daosourced.
* The Game controller contract controls game play and winner declaration, it is the only contract
* with access controll that can interact with the Two-way chance game contract
* the Two-Way chance game that power the randomization logic for chance games
* that require two people for play
* @dev note this is draft contract not meant to be used in production
*/
contract WegaGameController is
IWegaGameController,
IWegaGameControllerEvents,
WegaGameControllerErrors,
WegaProtocolAdminRole,
UUPSUpgradeable
{
using EnumerableMap for EnumerableMap.UintToUintMap;
using EnumerableSet for EnumerableSet.UintSet;
using Strings for string;
using Arrays for uint256[];
IWegaERC20Escrow public erc20Escrow;
IWegaRandomizerController public randomizerController;
mapping(bytes32 => IWega.Wega) private _games;
mapping(uint256 => address) _registeredGames;
mapping(uint256 => GameSettings) _gameSettings;
EnumerableSet.UintSet _gameNameHashes;
function initialize(
address erc20EscrowAddress,
address randomizerController_,
GameSettings[] memory gameSettings
) initializer public {
__WegaController_init(erc20EscrowAddress, gameSettings);
__WegaProtocolAdminRole_init();
__UUPSUpgradeable_init();
randomizerController = IWegaRandomizerController(randomizerController_);
}
function __WegaController_init(
address erc20EscrowAddress,
GameSettings[] memory gameSettings
) internal onlyInitializing {
__WegaController_init_unchained(erc20EscrowAddress, gameSettings);
}
function __WegaController_init_unchained(
address erc20EscrowAddress,
GameSettings[] memory gameSettings
) internal onlyInitializing {
erc20Escrow = IWegaERC20Escrow(erc20EscrowAddress);
for(uint256 i = 0; i < gameSettings.length; i++) {
_registeredGames[gameSettings[i].name.keyHash()] = gameSettings[i].proxy;
_gameNameHashes.add(gameSettings[i].name.keyHash());
_setGameConfiguration(gameSettings[i]);
}
}
function createGame(
string memory name,
address tokenAddress,
uint256 wagerAmount,
uint256[] memory randomNumbers
) public override {
require(_gameNameHashes.contains(name.keyHash()), INVALID_WEGA_GAME);
GameSettings memory settings = getGameSettings(name);
_seedRandomizerBeforeCreateOrDeposit(randomNumbers);
bytes32 escrowHash = erc20Escrow.createWagerRequest(
tokenAddress,
_msgSender(),
settings.requiredPlayers,
wagerAmount
);
IWega.Wega memory game;
game.name = name;
game.state = IWega.WegaState.WAITING;
address[] memory players_ = new address[](settings.requiredPlayers);
game.currentPlayers = players_;
game.currentPlayers[0] = _msgSender();
game.deposited += 1;
_games[escrowHash] = game;
emit GameCreation(escrowHash, erc20Escrow.getWagerRequest(escrowHash).nonce, _msgSender(), name);
}
function depositOrPlay(bytes32 escrowHash, uint256[] memory randomNumbers) public override {
IWega.Wega memory game =_games[escrowHash];
GameSettings memory settings = getGameSettings(game.name);
_depositWagerTo(escrowHash);
_seedRandomizerBeforeCreateOrDeposit(randomNumbers);
if(_games[escrowHash].deposited == settings.requiredPlayers) {
_playDice(settings.proxy, escrowHash, _games[escrowHash].currentPlayers, settings.denominator, settings.minRounds);
}
}
function depositOrPlay(
bytes32 escrowHash,
uint256[] memory playerChoices,
uint256[] memory randomNumbers
) public override {
IWega.Wega memory game =_games[escrowHash];
GameSettings memory settings = getGameSettings(game.name);
_depositWagerTo(escrowHash);
_seedRandomizerBeforeCreateOrDeposit(randomNumbers);
if(_games[escrowHash].deposited == settings.requiredPlayers) {
_playCoinFlip(
settings.proxy,
escrowHash,
_games[escrowHash].currentPlayers,
playerChoices,
settings.denominator,
settings.minRounds
);
}
}
function _playDice(
address gameAddress,
bytes32 escrowHash,
address[] memory currentPlayers,
uint256 rounds,
uint256 denominator
) internal {
address[] memory gameWinners = IWega(gameAddress).play(
escrowHash,
currentPlayers,
rounds,
denominator
);
erc20Escrow.setWithdrawers(escrowHash, gameWinners);
_games[escrowHash].state = IWega.WegaState.PLAYED;
emit WinnerDeclaration(escrowHash, gameWinners);
}
function _playCoinFlip(
address gameAddress,
bytes32 escrowHash,
address[] memory currentPlayers,
uint256[] memory playerChoices,
uint256 rounds,
uint256 denominator
) internal {
address[] memory gameWinners = IWega(gameAddress).play(
escrowHash,
currentPlayers,
playerChoices,
rounds,
denominator
);
erc20Escrow.setWithdrawers(escrowHash, gameWinners);
_games[escrowHash].state = IWega.WegaState.PLAYED;
emit WinnerDeclaration(escrowHash, gameWinners);
}
function _depositWagerTo(bytes32 escrowHash) internal {
IWega.Wega memory game =_games[escrowHash];
GameSettings memory settings = getGameSettings(game.name);
IEscrow.ERC20WagerRequest memory wagerRequest = erc20Escrow.getWagerRequest(escrowHash);
require(game.state != IWega.WegaState.PLAYED, INVALID_GAME_STATE);
if(game.deposited != settings.requiredPlayers) {
erc20Escrow.deposit(escrowHash, _msgSender(), wagerRequest.wagerAmount);
_games[escrowHash].currentPlayers[game.deposited] = _msgSender();
_games[escrowHash].deposited +=1;
}
}
function winners(
string memory game,
bytes32 escrowHash
) external view override returns(address[] memory) {
GameSettings memory settings = getGameSettings(game);
return IWega(settings.proxy).winners(escrowHash);
}
function players(
bytes32 escrowHash
) external view override returns(address[] memory) {
return _games[escrowHash].currentPlayers;
}
function getGame(bytes32 escrowHash) external view override returns(IWega.Wega memory) {
return _games[escrowHash];
}
function gameResults(
string memory game,
bytes32 escrowHash,
address[] memory players_
) external view override returns(uint256[][] memory) {
return IWega(getGameSettings(game).proxy).multiplePlayersResults(escrowHash, players_);
}
function playerScore(
string memory game,
bytes32 escrowHash,
address player
) external view override returns(uint256) {
return IWega(getGameSettings(game).proxy).playerScore(escrowHash, player);
}
function setGameConfiguration(GameSettings memory config) external onlyWegaProtocolAdmin {
require(existsGame(config.name), INVALID_WEGA_GAME);
_setGameConfiguration(config);
}
function registerGame(GameSettings memory config) external override onlyWegaProtocolAdmin {
require(!existsGame(config.name), INVALID_WEGA_GAME);
_registeredGames[config.name.keyHash()] = config.proxy;
_gameNameHashes.add(config.name.keyHash());
_setGameConfiguration(config);
emit GameRegistration(config.name, config.proxy);
}
function _setGameConfiguration(GameSettings memory config) internal {
_gameSettings[config.name.keyHash()] = config;
emit SetGame(
config.name,
config.denominator,
config.minRounds,
config.requiredPlayers,
config.proxy,
config.randomNumberController
);
}
function getGameSettings(
string memory game
) public view returns (GameSettings memory settings) {
settings = _gameSettings[game.keyHash()];
}
function removeGame(string memory game) external override {
require(existsGame(game), INVALID_WEGA_GAME);
delete _gameSettings[game.keyHash()];
delete _registeredGames[game.keyHash()];
}
function existsGame(string memory game) public view override returns (bool exists) {
exists = _registeredGames[game.keyHash()] != address(0);
}
function _authorizeUpgrade(address newImplementation) internal onlyOwner override {}
function _seedRandomizerBeforeCreateOrDeposit(uint256[] memory randomNumbers) internal {
randomizerController.seedRandomizer(randomNumbers);
}
}