Skip to content

Commit a80de09

Browse files
committed
Merge branch 'develop'
2 parents 59cd9a7 + 2040feb commit a80de09

File tree

3 files changed

+76
-84
lines changed

3 files changed

+76
-84
lines changed

src/components/ListGames.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useEffect, useMemo } from "react";
22
import { Link, useParams } from "react-router-dom";
33
import { useTranslation } from "react-i18next";
4-
import { gameinfo, GameFactory } from "@abstractplay/gameslib";
4+
import { gameinfo } from "@abstractplay/gameslib";
55
import { API_ENDPOINT_OPEN } from "../config";
66
import {
77
getCoreRowModel,
@@ -14,6 +14,7 @@ import {
1414
} from "@tanstack/react-table";
1515
import { useStorageState } from "react-use-storage-state";
1616
import { Helmet } from "react-helmet-async";
17+
import { useExpandVariants } from "../hooks/useExpandVariants";
1718

1819
const allSize = Number.MAX_SAFE_INTEGER;
1920

@@ -24,6 +25,7 @@ function ListGames({ fixedState }) {
2425
const [, maxPlayersSetter] = useState(2);
2526
const [showState, showStateSetter] = useStorageState("listgames-show", 20);
2627
const [sorting, setSorting] = useState([]);
28+
const { expandVariants } = useExpandVariants(metaGame);
2729

2830
useEffect(() => {
2931
if (gameState === "completed") {
@@ -57,23 +59,6 @@ function ListGames({ fixedState }) {
5759
}, [gameState, metaGame, fixedState]);
5860

5961
const metaGameName = gameinfo.get(metaGame).name;
60-
const variantMap = useMemo(() => {
61-
const info = gameinfo.get(metaGame);
62-
let gameEngine;
63-
if (info.playercounts.length > 1) {
64-
gameEngine = GameFactory(info.uid, 2);
65-
} else {
66-
gameEngine = GameFactory(info.uid);
67-
}
68-
const all = gameEngine.allvariants();
69-
if (all !== undefined) {
70-
return new Map(
71-
gameEngine.allvariants().map((rec) => [rec.uid, rec.name])
72-
);
73-
} else {
74-
return new Map();
75-
}
76-
}, [metaGame]);
7762

7863
const data = useMemo(
7964
() =>
@@ -101,14 +86,12 @@ function ListGames({ fixedState }) {
10186
: null,
10287
variants:
10388
"variants" in rec && rec.variants !== null
104-
? rec.variants.map((id) =>
105-
variantMap.has(id) ? variantMap.get(id) : id
106-
)
89+
? expandVariants(rec.variants)
10790
: null,
10891
cbit: fixedState === "completed" || gameState === "completed" ? 1 : 0,
10992
};
11093
}),
111-
[games, gameState, fixedState, variantMap]
94+
[games, gameState, fixedState, expandVariants]
11295
);
11396

11497
const columnHelper = createColumnHelper();

src/components/StandingChallenges.js

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useState, useEffect, useContext, useMemo, useCallback } from "react";
1+
import React, { useState, useEffect, useContext, useMemo } from "react";
22
import { useTranslation } from "react-i18next";
33
import { useParams, Link } from "react-router-dom";
4-
import { gameinfo, GameFactory } from "@abstractplay/gameslib";
4+
import { gameinfo } from "@abstractplay/gameslib";
55
import { Auth } from "aws-amplify";
66
import { API_ENDPOINT_OPEN } from "../config";
77
import {
@@ -20,6 +20,7 @@ import ActivityMarker from "./ActivityMarker";
2020
import NewChallengeModal from "./NewChallengeModal";
2121
import { useStorageState } from "react-use-storage-state";
2222
import { Helmet } from "react-helmet-async";
23+
import { useExpandVariants } from "../hooks/useExpandVariants";
2324

2425
const allSize = Number.MAX_SAFE_INTEGER;
2526

@@ -38,6 +39,7 @@ function StandingChallenges(props) {
3839
const [sorting, setSorting] = useState([]);
3940
const [showAccepted, showAcceptedSetter] = useState(false);
4041
const [showModal, showModalSetter] = useState(false);
42+
const { expandVariants } = useExpandVariants(metaGame);
4143

4244
async function reportError(error) {
4345
try {
@@ -229,64 +231,6 @@ function StandingChallenges(props) {
229231
console.log(metaGame);
230232
const showRespond = loggedin && challenges;
231233

232-
const [variantMap, setVariantMap] = useState(new Map());
233-
const [variantGroups, setVariantGroups] = useState(new Set());
234-
const [varId2Group, setVarId2Group] = useState(new Map());
235-
236-
useEffect(() => {
237-
const info = gameinfo.get(metaGame);
238-
let gameEngine;
239-
if (info.playercounts.length > 1) {
240-
gameEngine = GameFactory(info.uid, 2);
241-
} else {
242-
gameEngine = GameFactory(info.uid);
243-
}
244-
const all = gameEngine.allvariants();
245-
if (all !== undefined) {
246-
setVariantMap(new Map(
247-
all.map((rec) => [rec.uid, rec.name])
248-
));
249-
setVariantGroups(new Set(
250-
all.map((rec) => rec.group).filter(Boolean)
251-
));
252-
setVarId2Group(new Map(
253-
all.map((rec) => [rec.uid, rec.group])
254-
));
255-
} else {
256-
setVariantMap(new Map());
257-
setVariantGroups(new Set());
258-
setVarId2Group(new Map());
259-
}
260-
}, [metaGame]);
261-
262-
263-
const vars2string = useCallback((vars) => {
264-
// if the string is empty, return all the group defaults
265-
if (vars.length === 0) {
266-
return [...variantMap.entries()]
267-
.filter(([k]) => k.startsWith("#"))
268-
.map(([, v]) => v)
269-
.filter(Boolean);
270-
}
271-
// otherwise add any missing defaults and just look up the rest
272-
else {
273-
const groups = new Set([...variantGroups]);
274-
for (const v of vars) {
275-
const g = varId2Group.get(v);
276-
if (g !== undefined) {
277-
groups.delete(g);
278-
}
279-
}
280-
console.log(`Some groups not defined: ${[...groups]}`);
281-
// if any groups are not defined, add the defaults to vars
282-
for (const g of groups) {
283-
vars.push(`#${g}`);
284-
}
285-
console.log(`About to return: ${vars}`);
286-
return vars.map((v) => variantMap.get(v)).filter(Boolean);
287-
}
288-
}, [variantMap, variantGroups, varId2Group]);
289-
290234
const data = useMemo(
291235
() =>
292236
challenges.map((rec) => {
@@ -311,11 +255,11 @@ function StandingChallenges(props) {
311255
players: rec.players.filter((p) => p.id !== rec.challenger?.id),
312256
rated: rec.rated,
313257
seating: rec.seating,
314-
variants: vars2string(rec.variants),
258+
variants: expandVariants(rec.variants),
315259
comment: rec.comment,
316260
};
317261
}),
318-
[challenges, allUsers, vars2string]
262+
[challenges, allUsers, expandVariants]
319263
);
320264

321265
const columnHelper = createColumnHelper();

src/hooks/useExpandVariants.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
import { useState, useEffect, useCallback } from 'react';
3+
import { GameFactory, gameinfo } from '@abstractplay/gameslib';
4+
5+
export function useExpandVariants(metaGame) {
6+
const [variantMap, setVariantMap] = useState(new Map());
7+
const [variantGroups, setVariantGroups] = useState(new Set());
8+
const [varId2Group, setVarId2Group] = useState(new Map());
9+
10+
useEffect(() => {
11+
const info = gameinfo.get(metaGame);
12+
let gameEngine;
13+
if (info.playercounts.length > 1) {
14+
gameEngine = GameFactory(info.uid, 2);
15+
} else {
16+
gameEngine = GameFactory(info.uid);
17+
}
18+
const all = gameEngine.allvariants();
19+
if (all !== undefined) {
20+
setVariantMap(new Map(
21+
all.map((rec) => [rec.uid, rec.name])
22+
));
23+
setVariantGroups(new Set(
24+
all.map((rec) => rec.group).filter(Boolean)
25+
));
26+
setVarId2Group(new Map(
27+
all.map((rec) => [rec.uid, rec.group])
28+
));
29+
} else {
30+
setVariantMap(new Map());
31+
setVariantGroups(new Set());
32+
setVarId2Group(new Map());
33+
}
34+
}, [metaGame]);
35+
36+
const expandVariants = useCallback((vars) => {
37+
// if the string is empty, return all the group defaults
38+
if (vars.length === 0) {
39+
return [...variantMap.entries()]
40+
.filter(([k]) => k.startsWith("#"))
41+
.map(([, v]) => v)
42+
.filter(Boolean);
43+
}
44+
// otherwise add any missing defaults and just look up the rest
45+
else {
46+
const groups = new Set([...variantGroups]);
47+
for (const v of vars) {
48+
const g = varId2Group.get(v);
49+
if (g !== undefined) {
50+
groups.delete(g);
51+
}
52+
}
53+
console.log(`Some groups not defined: ${[...groups]}`);
54+
// if any groups are not defined, add the defaults to vars
55+
for (const g of groups) {
56+
vars.push(`#${g}`);
57+
}
58+
console.log(`About to return: ${vars}`);
59+
return vars.map((v) => variantMap.get(v)).filter(Boolean);
60+
}
61+
}, [variantMap, variantGroups, varId2Group]);
62+
63+
return { expandVariants };
64+
}
65+

0 commit comments

Comments
 (0)