Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final state for MUD workshop #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}