Skip to content

Commit 07ebc75

Browse files
committed
tbg time in unix instead of b.number
1 parent fa23fa2 commit 07ebc75

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

deploy/03_deployGame.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { DeployFunction } from 'hardhat-deploy/types';
33
import {
44
BESTOF_CONTRACT_NAME,
55
BESTOF_CONTRACT_VERSION,
6-
BOG_BLOCKS_PER_TURN,
6+
BOG_TIME_PER_TURN,
77
BOG_MAX_PLAYERS,
88
BOG_MIN_PLAYERS,
99
BOG_MAX_TURNS,
10-
BOG_BLOCKS_TO_JOIN,
10+
BOG_TIME_TO_JOIN,
1111
BOG_GAME_PRICE,
1212
BOG_JOIN_GAME_PRICE,
1313
BOG_NUM_WINNERS,
@@ -50,12 +50,12 @@ const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
5050
const settings: BestOfInit.ContractInitializerStruct =
5151
process.env.NODE_ENV === 'TEST'
5252
? {
53-
blocksPerTurn: BOG_BLOCKS_PER_TURN,
53+
timePerTurn: BOG_TIME_PER_TURN,
5454
maxTurns: BOG_MAX_TURNS,
5555
maxPlayersSize: BOG_MAX_PLAYERS,
5656
minPlayersSize: BOG_MIN_PLAYERS,
5757
rankTokenAddress: rankToken.address,
58-
blocksToJoin: BOG_BLOCKS_TO_JOIN,
58+
timeToJoin: BOG_TIME_TO_JOIN,
5959
gamePrice: BOG_GAME_PRICE,
6060
joinGamePrice: BOG_JOIN_GAME_PRICE,
6161
numWinners: BOG_NUM_WINNERS,
@@ -64,12 +64,12 @@ const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
6464
agendaToken: agendaToken.address,
6565
}
6666
: {
67-
blocksPerTurn: getProcessEnv(false, 'BLOCKS_PER_TURN'),
67+
timePerTurn: getProcessEnv(false, 'TIME_PER_TURN'),
6868
maxTurns: getProcessEnv(false, 'MAX_TURNS'),
6969
maxPlayersSize: getProcessEnv(false, 'MAX_PLAYERS'),
7070
minPlayersSize: getProcessEnv(false, 'MIN_PLAYERS'),
7171
rankTokenAddress: rankToken.address,
72-
blocksToJoin: getProcessEnv(false, 'BLOCKS_TO_JOIN'),
72+
timeToJoin: getProcessEnv(false, 'BLOCKS_TO_JOIN'),
7373
gamePrice: ethers.utils.parseEther(getProcessEnv(false, 'GAME_PRICE_ETH')),
7474
joinGamePrice: ethers.utils.parseEther(getProcessEnv(false, 'JOIN_GAME_PRICE_ETH')),
7575
numWinners: getProcessEnv(false, 'NUM_WINNERS'),

src/facets/GameOwnersFacet.sol

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ contract GameOwnersFacet {
5050
_BOG.rankTokenAddress = newRankToken;
5151
}
5252

53-
function setBlocksPerTurn(uint256 newBlocksPerTurn) external {
53+
function setTimePerTurn(uint256 newTimePerTurn) external {
5454
LibDiamond.enforceIsContractOwner();
55-
if (newBlocksPerTurn == 0) {
55+
if (newTimePerTurn == 0) {
5656
revert ZeroValue();
5757
}
5858
LibTBG.TBGStorageStruct storage tbg = LibTBG.TBGStorage();
59-
tbg.settings.blocksPerTurn = newBlocksPerTurn;
59+
tbg.settings.timePerTurn = newTimePerTurn;
6060
}
6161

6262
function setMaxPlayersSize(uint256 newMaxPlayersSize) external {
@@ -77,13 +77,13 @@ contract GameOwnersFacet {
7777
tbg.settings.minPlayersSize = newMinPlayersSize;
7878
}
7979

80-
function setBlocksToJoin(uint256 newBlocksToJoin) external {
80+
function setBlocksToJoin(uint256 newTimeToJoin) external {
8181
LibDiamond.enforceIsContractOwner();
82-
if (newBlocksToJoin == 0) {
82+
if (newTimeToJoin == 0) {
8383
revert ZeroValue();
8484
}
8585
LibTBG.TBGStorageStruct storage tbg = LibTBG.TBGStorage();
86-
tbg.settings.blocksToJoin = newBlocksToJoin;
86+
tbg.settings.timeToJoin = newTimeToJoin;
8787
}
8888

8989
function setMaxTurns(uint256 newMaxTurns) external {

src/initializers/BestOfInit.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ contract BestOfInit {
4242
}
4343

4444
struct contractInitializer {
45-
uint256 blocksPerTurn;
45+
uint256 timePerTurn;
4646
uint256 maxPlayersSize;
4747
uint256 minPlayersSize;
4848
address rankTokenAddress;
49-
uint256 blocksToJoin;
49+
uint256 timeToJoin;
5050
uint256 gamePrice;
5151
uint256 joinGamePrice;
5252
uint256 maxTurns;
@@ -93,10 +93,10 @@ contract BestOfInit {
9393
_BOG.contractInitialized = true;
9494

9595
LibTBG.GameSettings memory settings;
96-
settings.blocksPerTurn = initializer.blocksPerTurn;
96+
settings.timePerTurn = initializer.timePerTurn;
9797
settings.maxPlayersSize = initializer.maxPlayersSize;
9898
settings.minPlayersSize = initializer.minPlayersSize;
99-
settings.blocksToJoin = initializer.blocksToJoin;
99+
settings.timeToJoin = initializer.timeToJoin;
100100
settings.maxTurns = initializer.maxTurns;
101101
settings.numWinners = initializer.numWinners;
102102
settings.subject = initializer.subject;

src/libraries/LibTurnBasedGame.sol

+20-18
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
1111
import {LibArray} from "../libraries/LibArray.sol";
1212
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
1313

14+
error invalidConfiguration(string paramter);
1415
library LibTBG {
1516
// using EnumerableMap for EnumerableMap.AddressToUintMap;
1617
// using EnumerableMap for EnumerableMap.UintToAddressMap;
1718
using EnumerableSet for EnumerableSet.AddressSet;
1819

1920
struct GameSettings {
20-
uint256 blocksPerTurn;
21+
uint256 timePerTurn;
2122
uint256 maxPlayersSize;
2223
uint256 minPlayersSize;
23-
uint256 blocksToJoin;
24+
uint256 timeToJoin;
2425
uint256 maxTurns;
2526
uint256 numWinners;
2627
uint256 voteCredits;
@@ -68,15 +69,16 @@ library LibTBG {
6869

6970
function init(GameSettings memory settings) internal {
7071
TBGStorageStruct storage tbg = TBGStorage();
71-
require(settings.blocksPerTurn != 0, "init->blocksPerTurn");
72-
require(settings.maxPlayersSize != 0, "init->maxPartySize");
73-
require(settings.minPlayersSize > 2, "init->minPartySize");
74-
require(settings.maxTurns != 0, "init->maxTurns");
75-
require((settings.numWinners != 0) && (settings.numWinners < settings.minPlayersSize), "init->numWinners");
76-
require(settings.blocksToJoin != 0, "init->blocksToJoin");
77-
require(settings.maxPlayersSize >= settings.minPlayersSize, "init->maxPlayersSize");
78-
require(settings.voteCredits > 0, "init->voteCredits");
79-
require(bytes(settings.subject).length != 0, "init->subject");
72+
if(settings.timePerTurn == 0) revert invalidConfiguration('timePerTurn');
73+
if(settings.maxPlayersSize == 0) revert invalidConfiguration('maxPlayersSize');
74+
if(settings.minPlayersSize < 2) revert invalidConfiguration('minPlayersSize');
75+
if(settings.maxTurns == 0) revert invalidConfiguration('maxTurns');
76+
if(settings.numWinners == 0 || settings.numWinners >= settings.minPlayersSize) revert invalidConfiguration('numWinners');
77+
if(settings.timeToJoin == 0) revert invalidConfiguration('timeToJoin');
78+
if(settings.maxPlayersSize < settings.minPlayersSize) revert invalidConfiguration('maxPlayersSize');
79+
if(settings.voteCredits < 1) revert invalidConfiguration('voteCredits');
80+
if(bytes(settings.subject).length == 0) revert invalidConfiguration('subject length');
81+
8082

8183
tbg.settings = settings;
8284
tbg.maxQuadraticVote = Math.sqrt(settings.voteCredits);
@@ -159,7 +161,7 @@ library LibTBG {
159161
GameInstance storage _game = _getGame(gameId);
160162
assert(gameId != 0);
161163
assert(_game.hasStarted == true);
162-
if (block.number <= tbg.settings.blocksPerTurn + _game.turnStartedAt) return false;
164+
if (block.timestamp <= tbg.settings.timePerTurn + _game.turnStartedAt) return false;
163165
return true;
164166
}
165167

@@ -232,7 +234,7 @@ library LibTBG {
232234
function openRegistration(uint256 gameId) internal {
233235
require(gameExists(gameId), "game not found");
234236
GameInstance storage _game = _getGame(gameId);
235-
_game.registrationOpenAt = block.number;
237+
_game.registrationOpenAt = block.timestamp;
236238
}
237239

238240
function isRegistrationOpen(uint256 gameId) internal view returns (bool) {
@@ -241,7 +243,7 @@ library LibTBG {
241243
if (_game.registrationOpenAt == 0) {
242244
return false;
243245
} else {
244-
return _game.registrationOpenAt < block.number + tbg.settings.blocksToJoin ? true : false;
246+
return _game.registrationOpenAt < block.timestamp + tbg.settings.timeToJoin ? true : false;
245247
}
246248
}
247249

@@ -251,7 +253,7 @@ library LibTBG {
251253
bool retval = true;
252254
if (_game.hasStarted != false) retval = false;
253255
if (_game.registrationOpenAt == 0) retval = false;
254-
if (block.number <= _game.registrationOpenAt + tbg.settings.blocksToJoin) retval = false;
256+
if (block.timestamp <= _game.registrationOpenAt + tbg.settings.timeToJoin) retval = false;
255257
if (gameId == 0) retval = false;
256258
if (_game.players.length() < tbg.settings.minPlayersSize) retval = false;
257259
return retval;
@@ -262,13 +264,13 @@ library LibTBG {
262264
TBGStorageStruct storage tbg = TBGStorage();
263265
require(_game.hasStarted == false, "startGame->already started");
264266
require(_game.registrationOpenAt != 0, "startGame->Game registration was not yet open");
265-
require(block.number > _game.registrationOpenAt + tbg.settings.blocksToJoin, "startGame->Still Can Join");
267+
require(block.timestamp > _game.registrationOpenAt + tbg.settings.timeToJoin, "startGame->Still Can Join");
266268
require(gameId != 0, "startGame->Game not found");
267269
require(_game.players.length() >= tbg.settings.minPlayersSize, "startGame->Not enough players");
268270
_game.hasStarted = true;
269271
_game.hasEnded = false;
270272
_game.currentTurn = 1;
271-
_game.turnStartedAt = block.number;
273+
_game.turnStartedAt = block.timestamp;
272274
_resetPlayerStates(_game);
273275
}
274276

@@ -327,7 +329,7 @@ library LibTBG {
327329
enforceIsNotOver(gameId);
328330
_clearCurrentMoves(_game);
329331
_game.currentTurn += 1;
330-
_game.turnStartedAt = block.number;
332+
_game.turnStartedAt = block.timestamp;
331333
bool _isLastTurn = isLastTurn(gameId);
332334
bool _isOvertime = _game.isOvertime;
333335
address[] memory sortedLeaders = new address[](getPlayers(gameId).length);

test/BestOfGame.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const startGame = async (
218218
gameId: BigNumberish,
219219
// players: [SignerIdentity, SignerIdentity, ...SignerIdentity[]]
220220
) => {
221-
await mineBlocks(BOGSettings.BOG_BLOCKS_TO_JOIN + 1);
221+
await mineBlocks(BOGSettings.BOG_TIME_TO_JOIN + 1);
222222
await env.bestOfGame.connect(adr.gameMaster1.wallet).startGame(gameId);
223223
// proposalsStruct = await mockProposals({
224224
// players: players,
@@ -267,7 +267,7 @@ const fillParty = async (
267267
await env.rankToken.connect(players[i].wallet).setApprovalForAll(env.bestOfGame.address, true);
268268
await gameContract.connect(players[i].wallet).joinGame(gameId, { value: ethers.utils.parseEther('0.4') });
269269
}
270-
if (mineJoinBlocks) await mineBlocks(BOGSettings.BOG_BLOCKS_TO_JOIN + 1);
270+
if (mineJoinBlocks) await mineBlocks(BOGSettings.BOG_TIME_TO_JOIN + 1);
271271
if (startGame && gameMaster) {
272272
await env.bestOfGame.connect(gameMaster.wallet).startGame(gameId);
273273
}
@@ -337,9 +337,9 @@ describe(scriptName, () => {
337337
expect(state.BestOfState.numGames).to.be.equal(0);
338338
expect(state.BestOfState.rankTokenAddress).to.be.equal(env.rankToken.address);
339339
expect(state.TBGSEttings.maxTurns).to.be.equal(BOGSettings.BOG_MAX_TURNS);
340-
expect(state.TBGSEttings.blocksPerTurn).to.be.equal(BOGSettings.BOG_BLOCKS_PER_TURN);
340+
expect(state.TBGSEttings.timePerTurn).to.be.equal(BOGSettings.BOG_TIME_PER_TURN);
341341
expect(state.TBGSEttings.minPlayersSize).to.be.equal(BOGSettings.BOG_MIN_PLAYERS);
342-
expect(state.TBGSEttings.blocksToJoin).to.be.equal(BOGSettings.BOG_BLOCKS_TO_JOIN);
342+
expect(state.TBGSEttings.timeToJoin).to.be.equal(BOGSettings.BOG_TIME_TO_JOIN);
343343
expect(state.TBGSEttings.maxTurns).to.be.equal(BOGSettings.BOG_MAX_TURNS);
344344
});
345345
it('Transfer ownership can be done only by contract owner', async () => {
@@ -570,7 +570,7 @@ describe(scriptName, () => {
570570
).to.be.revertedWith('Game has not yet started');
571571
});
572572
it('Cannot be started if not enough players', async () => {
573-
await mineBlocks(BOGSettings.BOG_BLOCKS_TO_JOIN + 1);
573+
await mineBlocks(BOGSettings.BOG_TIME_TO_JOIN + 1);
574574
await expect(env.bestOfGame.connect(adr.gameMaster1.wallet).startGame(1)).to.be.revertedWith(
575575
'startGame->Not enough players',
576576
);
@@ -583,7 +583,7 @@ describe(scriptName, () => {
583583
await expect(env.bestOfGame.connect(adr.gameMaster1.wallet).startGame(1)).to.be.revertedWith(
584584
'startGame->Still Can Join',
585585
);
586-
await mineBlocks(BOGSettings.BOG_BLOCKS_TO_JOIN + 1);
586+
await mineBlocks(BOGSettings.BOG_TIME_TO_JOIN + 1);
587587
await expect(env.bestOfGame.connect(adr.gameMaster1.wallet).startGame(1)).to.be.emit(
588588
env.bestOfGame,
589589
'GameStarted',
@@ -667,7 +667,7 @@ describe(scriptName, () => {
667667
).to.be.revertedWith('Only game master');
668668
});
669669
it('Can end turn if timeout reached with zero scores', async () => {
670-
await mineBlocks(BOGSettings.BOG_BLOCKS_PER_TURN + 1);
670+
await mineBlocks(BOGSettings.BOG_TIME_PER_TURN + 1);
671671
await expect(env.bestOfGame.connect(adr.gameMaster1.wallet).endTurn(1, [], [], []))
672672
.to.be.emit(env.bestOfGame, 'TurnEnded')
673673
.withArgs(
@@ -752,7 +752,7 @@ describe(scriptName, () => {
752752
// );
753753
// }
754754

755-
// await mineBlocks(BOGSettings.BOG_BLOCKS_PER_TURN + 1);
755+
// await mineBlocks(BOGSettings.BOG_TIME_PER_TURN + 1);
756756
// await expect(
757757
// env.bestOfGame.connect(adr.gameMaster1.wallet).endTurn(
758758
// 1,
@@ -784,7 +784,7 @@ describe(scriptName, () => {
784784
).to.be.revertedWith('Some players still have time to propose');
785785
});
786786
it('Can end turn if timeout reached', async () => {
787-
await mineBlocks(BOGSettings.BOG_BLOCKS_PER_TURN + 1);
787+
await mineBlocks(BOGSettings.BOG_TIME_PER_TURN + 1);
788788
expect(await env.bestOfGame.getTurn(1)).to.be.equal(2);
789789
const players = getPlayers(adr, BOGSettings.BOG_MIN_PLAYERS);
790790
const expectedScores: number[] = players.map(v => 0);

test/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,22 @@ export const setupAddresses = async (
257257
const baseFee = 1 * 10 ** 18;
258258
export const BESTOF_CONTRACT_NAME = 'BESTOFNAME';
259259
export const BESTOF_CONTRACT_VERSION = '0.0.1';
260-
export const BOG_BLOCKS_PER_TURN = '25';
260+
export const BOG_TIME_PER_TURN = '25';
261261
export const BOG_MAX_PLAYERS = 5;
262262
export const BOG_MIN_PLAYERS = 4;
263263
export const BOG_MAX_TURNS = 3;
264-
export const BOG_BLOCKS_TO_JOIN = '200';
264+
export const BOG_TIME_TO_JOIN = '200';
265265
export const BOG_GAME_PRICE = ethers.utils.parseEther('0.001');
266266
export const BOG_JOIN_GAME_PRICE = ethers.utils.parseEther('0.001');
267267
export const BOG_NUM_WINNERS = 3;
268268
export const BOG_VOTE_CREDITS = 14;
269269
export const BOG_SUBJECT = 'Best Music on youtube';
270270
export const BOGSettings = {
271-
BOG_BLOCKS_PER_TURN,
271+
BOG_TIME_PER_TURN,
272272
BOG_MAX_PLAYERS,
273273
BOG_MIN_PLAYERS,
274274
BOG_MAX_TURNS,
275-
BOG_BLOCKS_TO_JOIN,
275+
BOG_TIME_TO_JOIN,
276276
BOG_GAME_PRICE,
277277
BOG_JOIN_GAME_PRICE,
278278
BOG_NUM_WINNERS,

0 commit comments

Comments
 (0)