-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnSystem.sol
More file actions
211 lines (187 loc) · 6.57 KB
/
SpawnSystem.sol
File metadata and controls
211 lines (187 loc) · 6.57 KB
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { BoardConfig, BoardConfigData } from "../codegen/tables/BoardConfig.sol";
import { MatchConfig, MatchConfigData } from "../codegen/tables/MatchConfig.sol";
import { MatchPlayer } from "../codegen/tables/MatchPlayer.sol";
import { MatchPlayers } from "../codegen/tables/MatchPlayers.sol";
import { SpawnStatus } from "../codegen/tables/SpawnStatus.sol";
import { Position, PositionData } from "../codegen/tables/Position.sol";
import { EntityAtPosition } from "../codegen/tables/EntityAtPosition.sol";
import { Commit } from "../codegen/tables/Commit.sol";
import { LibSpawnStatusType } from "../libraries/types/LibSpawnStatusType.sol";
import { LibSpawn } from "../libraries/LibSpawn.sol";
import { LibPosition } from "../libraries/LibPosition.sol";
import { LibCommit } from "../libraries/LibCommit.sol";
import { playerFromAddress } from "../libraries/LibUtils.sol";
import { LibSpawn } from "../libraries/LibSpawn.sol";
import { SpawnStatusTypes } from "../common/types.sol";
import { Errors } from "../common/Errors.sol";
import { BaseMatch } from "./base/BaseMatch.sol";
import { BaseUnit } from "./base/BaseUnit.sol";
import { ISpawnSystem } from "./interfaces/ISpawnSystem.sol";
contract SpawnSystem is System, BaseMatch, BaseUnit, ISpawnSystem {
using LibSpawnStatusType for SpawnStatusTypes;
using LibSpawnStatusType for uint8;
function commitSpawn(
bytes32 _commitHash,
bytes32 _matchEntity
)
public
override
onlyActiveMatch(_matchEntity)
onlyIfPreparationTimeIsNotOver(_matchEntity)
commitHashIsNotEmpty(_commitHash)
{
bytes32 matchPlayerEntity = playerFromAddress(
_matchEntity,
_msgSender()
);
_requireSpawnStatusToBeCommitSpawning(_matchEntity, matchPlayerEntity);
LibCommit.setCommit(_matchEntity, matchPlayerEntity, _commitHash);
LibSpawn.setSpawnStatus(
_matchEntity,
matchPlayerEntity,
SpawnStatusTypes.LockCommitSpawning
);
_checkSpawnStatusAndProceed(
_matchEntity,
SpawnStatusTypes.LockCommitSpawning
);
}
function revealSpawn(
bytes32 _matchEntity,
PositionData[] calldata _coordinates,
bytes32[] calldata _pieceEntities,
bytes32 _secret
)
public
override
onlyActiveMatch(_matchEntity)
onlyIfPreparationTimeIsNotOver(_matchEntity)
{
bytes32 matchPlayerEntity = playerFromAddress(
_matchEntity,
_msgSender()
);
_requireSecretIsValid(_matchEntity, matchPlayerEntity);
_requireEncodedHashIsEqualToCommitedHash(
_matchEntity,
matchPlayerEntity,
_coordinates,
_pieceEntities,
_secret
);
MatchConfigData memory matchConfig = MatchConfig.get(_matchEntity);
for (uint256 i = 0; i < _pieceEntities.length; i++) {
PositionData memory coordinate = _coordinates[i];
bytes32 pieceEntity = _pieceEntities[i];
_requireSpawnPositionToBeValidCoordinates(
matchConfig.boardEntity,
coordinate
);
_requireSpawnPositionToBeValidSpawnArea(
_matchEntity,
matchPlayerEntity,
matchConfig.boardEntity,
coordinate
);
_requireSpawnPositionIsEmptyCoordinate(_matchEntity, coordinate);
LibPosition.setPosition(_matchEntity, pieceEntity, coordinate);
}
LibSpawn.setSpawnStatus(
_matchEntity,
matchPlayerEntity,
SpawnStatusTypes.LockRevealSpawning
);
_checkSpawnStatusAndProceed(
_matchEntity,
SpawnStatusTypes.LockRevealSpawning
);
}
function _requireSpawnStatusToBeCommitSpawning(
bytes32 _matchEntity,
bytes32 matchPlayerEntity
) internal view {
if (
SpawnStatus
.get(_matchEntity, matchPlayerEntity)
.toSpawnStatusTypes()
.isNotCommitSpawning()
) {
revert Errors.IncorrectCommitStatus();
}
}
function _requireEncodedHashIsEqualToCommitedHash(
bytes32 _matchEntity,
bytes32 _matchPlayerEntity,
PositionData[] memory _coordinates,
bytes32[] memory _pieceEntities,
bytes32 _secret
) internal view {
bytes32 commitHash = Commit.get(_matchEntity, _matchPlayerEntity);
// Generate the combined hash from the piece types and secret
bytes32 encodedCommitHash = keccak256(
abi.encode(_pieceEntities, _coordinates, _secret)
);
if (encodedCommitHash != commitHash) {
revert Errors.InvalidReveal();
}
}
function _requireSecretIsValid(
bytes32 _matchEntity,
bytes32 _matchPlayerEntity
) internal view {
if (
SpawnStatus
.get(_matchEntity, _matchPlayerEntity)
.toSpawnStatusTypes()
.isNotRevealSpawning()
) {
revert Errors.IncorrectRevealStatus();
}
}
function _requireSpawnPositionToBeValidCoordinates(
bytes32 boardEntity,
PositionData memory coordinate
) internal view {
BoardConfigData memory boardConfigData = BoardConfig.get(boardEntity);
bool isValidCoordinates = LibSpawn.isWithinBoardCoordinates(
coordinate.x,
coordinate.y,
boardConfigData
);
if (!isValidCoordinates) {
revert Errors.CoordinateNotAllowed();
}
}
function _requireSpawnPositionToBeValidSpawnArea(
bytes32 matchEntity,
bytes32 matchPlayerEntity,
bytes32 boardEntity,
PositionData memory coordinate
) internal view {
bool isValidSpawnArea = LibSpawn.isSpawnableArea(
matchEntity,
matchPlayerEntity,
boardEntity,
coordinate
);
if (!isValidSpawnArea) {
revert Errors.NotInSpawnArea();
}
}
function _requireSpawnPositionIsEmptyCoordinate(
bytes32 matchEntity,
PositionData memory coordinate
) internal view {
bytes32 pieceEntity = EntityAtPosition.get(
matchEntity,
coordinate.x,
coordinate.y
);
if (pieceEntity != 0) {
revert Errors.PositionOccupied();
}
}
}