Skip to content

Commit

Permalink
Merge branch 'refactor-collect-4'
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Sep 1, 2023
2 parents 7c297a8 + 412fcb1 commit e131623
Show file tree
Hide file tree
Showing 18 changed files with 606 additions and 771 deletions.
9 changes: 8 additions & 1 deletion common/src/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export function fromContractFullCellToCell(

const accountIndex = accounts.indexOf(cell.owner);

return {
const processedCell = {
x,
y,
lastEpochUpdate: cell.lastEpochUpdate,
Expand All @@ -323,6 +323,13 @@ export function fromContractFullCellToCell(
? -1
: undefined,
};

console.log(`--------------`);
console.log(cell);
console.log(`------------- processed cell from contract`);
console.log(processedCell);
console.log(`--------------`);
return processedCell;
};
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"compile:watch": "as-soon -w src pnpm compile",
"deploy": "ldenv hardhat --network @@MODE deploy @@",
"deploy:watch": "as-soon -w generated -w deploy pnpm run deploy",
"test": "vitest",
"test": "vitest --bail 1",
"execute": "ldenv -n HARDHAT_NETWORK -m localhost tsx @@",
"coverage:compile": "hardhat compile-for-coverage",
"coverage:watch:compile": "as-soon -w src pnpm coverage:compile",
Expand Down
117 changes: 0 additions & 117 deletions contracts/src/GreetingsRegistry.sol

This file was deleted.

11 changes: 7 additions & 4 deletions contracts/src/game/debug/IStratagemsWithDebug.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import '../interface/UsingStratagemsTypes.sol';
import 'solidity-kit/solc_0.8/debug/time/interfaces/ITime.sol';

interface IStratagemsDebug is UsingStratagemsTypes, ITime, ITimeSetter {
error InvalidCellOverwrite();
error InvalidLifeConfiguration(uint256 life, int32 x, int32 y);

event ForceCells(DebugCell[] cells);
event ForceSimpleCells(uint32 epoch, SimpleCell[] cells);
event ForceSimpleCells(uint24 epoch, SimpleCell[] cells);

function forceMoves(address player, Move[] memory moves) external;

struct DebugCell {
uint64 position;
address owner;
uint32 lastEpochUpdate;
uint32 epochWhenTokenIsAdded;
uint24 lastEpochUpdate;
uint24 epochWhenTokenIsAdded;
Color color;
uint8 life;
int8 delta;
uint8 enemymask;
uint8 enemyMap;
}

function forceCells(DebugCell[] memory cells) external;
Expand Down
81 changes: 51 additions & 30 deletions contracts/src/game/debug/StratagemsDebug.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import 'solidity-kit/solc_0.8/utils/GenericErrors.sol';
import 'solidity-kit/solc_0.8/debug/time/implementations/UsingControlledTime.sol';
import '../internal/UsingStratagemsSetters.sol';
import './IStratagemsWithDebug.sol';
Expand All @@ -20,8 +21,10 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
}

function forceMoves(address player, Move[] memory moves) external {
require(msg.sender == _getOwner(), 'NOT_AUTHORIZED');
(uint32 epoch, bool commiting) = _epoch();
if (msg.sender != _getOwner()) {
revert NotAuthorized();
}
(uint24 epoch, bool commiting) = _epoch();
if (commiting) {
epoch--;
}
Expand All @@ -33,7 +36,9 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
}

function forceCells(DebugCell[] memory cells) external {
require(msg.sender == _getOwner(), 'NOT_AUTHORIZED');
if (msg.sender != _getOwner()) {
revert NotAuthorized();
}
for (uint256 i = 0; i < cells.length; i++) {
DebugCell memory debugCell = cells[i];
_cells[debugCell.position] = Cell({
Expand All @@ -42,32 +47,37 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
color: debugCell.color,
life: debugCell.life,
delta: debugCell.delta,
enemymask: debugCell.enemymask
enemyMap: debugCell.enemyMap,
distributionMap: 0 // TODO let debug distributionMap ?
});
_owners[debugCell.position] = uint256(uint160(debugCell.owner));
}
emit ForceCells(cells);
}

function forceSimpleCells(SimpleCell[] memory cells) external {
require(msg.sender == _getOwner(), 'NOT_AUTHORIZED');
(uint32 epoch, ) = _epoch();
if (msg.sender != _getOwner()) {
revert NotAuthorized();
}
(uint24 epoch, ) = _epoch();

for (uint256 i = 0; i < cells.length; i++) {
SimpleCell memory simpleCell = cells[i];
require(_cells[simpleCell.position].lastEpochUpdate == 0, 'NO_OVERWRITE');
// require(simpleCell.life > 0, 'ZERO_LIFE');
if (_cells[simpleCell.position].lastEpochUpdate != 0) {
revert InvalidCellOverwrite();
}
TOKENS.transferFrom(msg.sender, address(this), NUM_TOKENS_PER_GEMS);

(int8 delta, uint8 enemymask) = _updateNeighbosrDelta(simpleCell.position, simpleCell.color, epoch);
(int8 delta, uint8 enemyMap) = _updateNeighbosrDelta(simpleCell.position, simpleCell.color, epoch);

_cells[simpleCell.position] = Cell({
lastEpochUpdate: epoch,
epochWhenTokenIsAdded: epoch,
color: simpleCell.color,
life: simpleCell.life,
delta: delta,
enemymask: enemymask
enemyMap: enemyMap,
distributionMap: 0
});
_owners[simpleCell.position] = uint256(uint160(simpleCell.owner));
}
Expand All @@ -78,30 +88,41 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata

// we act as if the token were added in previous epochs
// this is so it does not affect the resolution phase
int256 potentialLife = int256(uint256(cell.life)) - cell.delta;
int8 effectiveDelta = cell.delta != 0 ? cell.delta : -1;
if (effectiveDelta < 0 && cell.enemyMap == 0) {
effectiveDelta = 0;
}
int256 potentialLife = int256(uint256(cell.life)) - effectiveDelta;
if (potentialLife < 0) {
potentialLife = 0;
}
if (uint256(potentialLife) > MAX_LIFE) {
unchecked {
int256 x = int256(int32(int256(uint256(position) & 0xFFFFFFFF)));
int256 y = int256(int32(int256(uint256(position) >> 32)));
require(
uint256(potentialLife) > MAX_LIFE,
string.concat('INVALID_LIFE_CONFIGURATION: ', Strings.toString(x), ',', Strings.toString(y))
);
int32 x = int32(int256(uint256(position) & 0xFFFFFFFF));
int32 y = int32(int256(uint256(position) >> 32));
revert InvalidLifeConfiguration(uint256(potentialLife), x, y);
}
}
cell.life = uint8(uint256(potentialLife));

_cells[position] = Cell({
Cell memory updatedCell = Cell({
lastEpochUpdate: epoch - 1,
epochWhenTokenIsAdded: epoch - 1,
color: cell.color,
life: cell.life,
delta: cell.delta,
enemymask: cell.enemymask
enemyMap: cell.enemyMap,
distributionMap: 0 // TODO let debug distributionMap ?
});
_cells[position] = updatedCell;

logger.logCell(
0,
string.concat('forceSimpleCells at epoch ', Strings.toString(epoch)),
uint64(position),
updatedCell,
address(uint160(_owners[position]))
);
}

emit ForceSimpleCells(epoch, cells);
Expand All @@ -114,23 +135,23 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
return 0;
}

function _computeDelta(uint64 position, Color color) internal view returns (int8 delta, uint8 enemymask) {
function _computeDelta(uint64 position, Color color) internal view returns (int8 delta, uint8 enemyMap) {
unchecked {
int256 x = int256(int32(int256(uint256(position) & 0xFFFFFFFF)));
int256 y = int256(int32(int256(uint256(position) >> 32)));
{
uint64 upPosition = uint64((uint256(y - 1) << 32) + uint256(x));
int8 enemyOrFriend = isEnemyOrFriend(color, _cells[upPosition].color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 1;
enemyMap = enemyMap | 1;
}
delta += enemyOrFriend;
}
{
uint64 leftPosition = uint64((uint256(y) << 32) + uint256(x - 1));
int8 enemyOrFriend = isEnemyOrFriend(color, _cells[leftPosition].color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 1;
enemyMap = enemyMap | 1;
}
delta += enemyOrFriend;
}
Expand All @@ -139,15 +160,15 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
uint64 downPosition = uint64((uint256(y + 1) << 32) + uint256(x));
int8 enemyOrFriend = isEnemyOrFriend(color, _cells[downPosition].color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 1;
enemyMap = enemyMap | 1;
}
delta += enemyOrFriend;
}
{
uint64 rightPosition = uint64((uint256(y) << 32) + uint256(x + 1));
int8 enemyOrFriend = isEnemyOrFriend(color, _cells[rightPosition].color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 1;
enemyMap = enemyMap | 1;
}
delta += enemyOrFriend;
}
Expand All @@ -157,8 +178,8 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
function _updateNeighbosrDelta(
uint64 center,
Color color,
uint32 epoch
) internal returns (int8 delta, uint8 enemymask) {
uint24 epoch
) internal returns (int8 delta, uint8 enemyMap) {
unchecked {
int256 x = int256(int32(int256(uint256(center) & 0xFFFFFFFF)));
int256 y = int256(int32(int256(uint256(center) >> 32)));
Expand All @@ -168,7 +189,7 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
if (cell.color != Color.None) {
int8 enemyOrFriend = isEnemyOrFriend(color, cell.color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 1;
enemyMap = enemyMap | 1;
}
delta += enemyOrFriend;
_updateCellFromNeighbor(upPosition, cell, cell.life, epoch, 2, Color.None, color);
Expand All @@ -180,7 +201,7 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
if (cell.color != Color.None) {
int8 enemyOrFriend = isEnemyOrFriend(color, cell.color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 2;
enemyMap = enemyMap | 2;
}
delta += enemyOrFriend;
_updateCellFromNeighbor(leftPosition, cell, cell.life, epoch, 3, Color.None, color);
Expand All @@ -193,7 +214,7 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
if (cell.color != Color.None) {
int8 enemyOrFriend = isEnemyOrFriend(color, cell.color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 4;
enemyMap = enemyMap | 4;
}
delta += enemyOrFriend;
_updateCellFromNeighbor(downPosition, cell, cell.life, epoch, 0, Color.None, color);
Expand All @@ -205,7 +226,7 @@ contract StratagemsDebug is UsingStratagemsSetters, UsingControlledTime, IStrata
if (cell.color != Color.None) {
int8 enemyOrFriend = isEnemyOrFriend(color, cell.color);
if (enemyOrFriend < 0) {
enemymask = enemymask | 8;
enemyMap = enemyMap | 8;
}
delta += enemyOrFriend;
_updateCellFromNeighbor(rightPosition, cell, cell.life, epoch, 1, Color.None, color);
Expand Down
Loading

0 comments on commit e131623

Please sign in to comment.