Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
921e1f0
Delete everything
adzialocha Aug 23, 2022
00c4176
Set up Rollup project
adzialocha Aug 23, 2022
93f0626
Correct .nvmrc format
adzialocha Aug 23, 2022
cfb55ab
Add a README.md
adzialocha Aug 23, 2022
aef56a0
Update dependencies
adzialocha Aug 23, 2022
411c619
Rename too ZooAdventures component
adzialocha Aug 23, 2022
30e9aff
Add nodemon to watch for changes
adzialocha Aug 23, 2022
5346e77
Install graphql-request dependencies
adzialocha Aug 23, 2022
891f522
Add CommonJS to build library
adzialocha Aug 23, 2022
41f9806
Initial script to build schema and first board
adzialocha Aug 23, 2022
a1dc4f5
Initialise key pair and pass over configuration
adzialocha Aug 23, 2022
0913969
Display game board
adzialocha Aug 23, 2022
6fc83c5
Update field when clicking
adzialocha Aug 23, 2022
f9b3531
Add linter for react hooks
adzialocha Aug 23, 2022
79f3509
Define animal emoji based on public key
adzialocha Aug 23, 2022
400a53e
A little bit of styling
adzialocha Aug 23, 2022
50b4568
Do not show hover when already set
adzialocha Aug 23, 2022
0050b46
Do not send request when field is already set
adzialocha Aug 23, 2022
5e1f28e
Frequently update game board
adzialocha Aug 23, 2022
ca7dd33
Block player if they just made a move
adzialocha Aug 23, 2022
363e576
Split up in multiple files
adzialocha Aug 23, 2022
b0395d9
Move React components into separate files
adzialocha Aug 23, 2022
003ac79
Add some comments, improve logic for blocking player
adzialocha Aug 23, 2022
e426504
Add some doc strings
adzialocha Aug 23, 2022
09c3d39
WIP detect winner
adzialocha Aug 23, 2022
97cac6a
Add some doc strings
adzialocha Aug 24, 2022
9af8c7d
Calculate win combinations once, represent them as strings
adzialocha Aug 24, 2022
da8a28b
Get current winners of board
adzialocha Aug 24, 2022
605fe60
Show winner on board
adzialocha Aug 24, 2022
dc4260f
Fix checking winning positions
adzialocha Aug 24, 2022
ddd8961
Update comment
adzialocha Aug 24, 2022
e86ae85
Show messages to the user
adzialocha Aug 24, 2022
caf50bd
Style Message component, move animal info to the bottom
adzialocha Aug 24, 2022
67d593b
Remove emojis which are not animals
adzialocha Aug 24, 2022
92caf0e
Change message on the bottom
adzialocha Aug 24, 2022
cee7e0b
Change winSize to 3, check for sane configs
adzialocha Aug 24, 2022
925924a
Upload screenshot
adzialocha Aug 24, 2022
93be9f9
Update README.md
adzialocha Aug 24, 2022
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
Prev Previous commit
Next Next commit
Add some comments, improve logic for blocking player
  • Loading branch information
adzialocha committed Aug 23, 2022
commit 003ac79ce77f08da44e0de3abd87fb214cf6aa99
86 changes: 52 additions & 34 deletions src/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,64 @@ export const Game: React.FC<Props> = ({ config }) => {
return loadKeyPair();
}, []);

const client = useMemo(() => {
return new GraphQLClient(config.endpoint);
}, [config.endpoint]);

const animal = useMemo(() => {
return publicKeyToAnimal(keyPair.publicKey());
}, [keyPair]);

const client = useMemo(() => {
return new GraphQLClient(config.endpoint);
}, [config.endpoint]);

// Latest document view id of the game board
const [viewId, setViewId] = useState<string>();

// State of the game board
const [fields, setFields] = useState<string[]>();

// Remember document view id of our last move, this helps us to detect if we
// are the last player who updated the board
const [lastMove, setLastMove] = useState<string | null>(() => {
return loadLastMove();
});

// Remember what the last update was, this helps us to detect if the node
// could send us something new
const [lastUpdate, setLastUpdate] = useState<string>();

// Block the user interface when we just made a move and we're waiting for
// updates from the node
const [ready, setReady] = useState(true);

const updateBoard = useCallback(async () => {
const board = await fetchBoard(
client,
config.schemaId,
config.documentId,
config.boardSize,
);

// Make sure to only affect the board state when we really have something
// new for the client. This prevents overriding temporarily set local-only
// state.
if (lastUpdate !== board.viewId) {
setViewId(board.viewId);
setFields(board.fields);
setLastUpdate(board.viewId);
setReady(true);
}
}, [
client,
config.boardSize,
config.documentId,
config.schemaId,
lastUpdate,
]);

const onSetField = useCallback(
async (fieldIndex: number) => {
if (!viewId) {
// Do not do allow making a move when we're waiting for the latest state
// from the node
if (!viewId || !ready) {
return;
}

Expand All @@ -42,7 +83,9 @@ export const Game: React.FC<Props> = ({ config }) => {
return;
}

// Apply update locally first
setReady(false);

// Apply update locally first to see the changes directly
setFields((value) => {
if (!value) {
return;
Expand All @@ -66,33 +109,14 @@ export const Game: React.FC<Props> = ({ config }) => {
animal,
);

// Set and persist last move
// Set and persist last move so we remember it when we come back later
setLastMove(latestViewId);
storeLastMove(latestViewId);

// Temporarily guess that this might be the latest viewId from the
// perspective of the node as well. The next update will proof us right
// or wrong ..
//
// At least it helps us to block the player until the next update!
setViewId(latestViewId);
},
[viewId, client, lastMove, keyPair, config, animal],
[viewId, client, lastMove, keyPair, config, animal, ready],
);

useEffect(() => {
const updateBoard = async () => {
const board = await fetchBoard(
client,
config.schemaId,
config.documentId,
config.boardSize,
);

setViewId(board.viewId);
setFields(board.fields);
};

const interval = window.setInterval(() => {
updateBoard();
}, config.updateIntervalMs);
Expand All @@ -102,13 +126,7 @@ export const Game: React.FC<Props> = ({ config }) => {
return () => {
window.clearInterval(interval);
};
}, [
client,
config.boardSize,
config.documentId,
config.schemaId,
config.updateIntervalMs,
]);
}, [client, updateBoard, config.updateIntervalMs]);

return (
<>
Expand Down