Skip to content

Commit

Permalink
do workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 27, 2024
1 parent 3b119e9 commit 910ea19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
39 changes: 38 additions & 1 deletion smart-turret/packages/client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
import { useCallback, useState } from "react";
import { stash } from "./mud/stash";
import { useStash } from "./mud/useStash";
import { worldContract } from "./mud/worldContract";

export const App = () => {
return <>Hello, World</>;
const [characterId, setCharacterId] = useState(0n);

const guestList = useStash(stash, (state) =>
Object.values(state.records.test.GuestList)
);

const addToGuestList = useCallback(() => {
worldContract.write.test__addToGuestList([characterId]);
}, [characterId]);

return (
<div>
<div>
<input
id="characterId"
placeholder="Character ID"
value={String(characterId)}
onChange={(e) => setCharacterId(BigInt(e.target.value))}
></input>
<button onClick={addToGuestList}>Add to guest list</button>
</div>
<div>
Guest list:
<ul>
{guestList.map((item) => (
<li key={"guestList-" + item.characterId}>
{String(item.characterId)}: {String(item.hasAccess)}
</li>
))}
</ul>
</div>
</div>
);
};
10 changes: 9 additions & 1 deletion smart-turret/packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@ import { defineWorld } from "@latticexyz/world";

export default defineWorld({
namespace: "test",
tables: {},
tables: {
GuestList: {
schema: {
characterId: "uint256",
hasAccess: "bool",
},
key: ["characterId"],
},
},
});
11 changes: 11 additions & 0 deletions smart-turret/packages/contracts/src/systems/GuestListSystem.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { System } from "@latticexyz/world/src/System.sol";
import { GuestList } from "../codegen/tables/GuestList.sol";

contract GuestListSystem is System {
function addToGuestList(uint256 characterId) public {
GuestList.setHasAccess(characterId, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ contract SmartTurretSystem is System {
}
}

//TODO: Implement the logic
function _shouldShoot(SmartTurretTarget memory target) internal returns (bool) {
return true;
return !GuestList.getHasAccess(target.characterId);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions smart-turret/packages/contracts/test/SmartTurretTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ contract SmartTurretTest is MudTest {
}

function testInProximity() public {
world.test__addToGuestList(newTurretTarget.characterId);

TargetPriority[] memory returnTargetQueue = smartTurret.inProximity(
smartTurretId,
characterId,
Expand All @@ -104,8 +106,7 @@ contract SmartTurretTest is MudTest {
newTurretTarget
);

assertEq(returnTargetQueue.length, 2);
assertEq(returnTargetQueue.length, 1);
assertEq(returnTargetQueue[0].target.characterId, previousTurretTarget.characterId);
assertEq(returnTargetQueue[1].target.characterId, newTurretTarget.characterId);
}
}

0 comments on commit 910ea19

Please sign in to comment.