mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-01 15:00:36 +01:00
feat: shuffle button
This commit is contained in:
1 parent
ff4a87e5b8
commit
263f292a59
6 files changed
+33
-26
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
export function fisherYates<Type extends unknown[]>(array: Type) {
|
||||||
|
for (let i = array.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[array[i], array[j]] = [array[j], array[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
export * from './axis';
|
export * from "./axis";
|
||||||
export * from './formatCompact';
|
export * from "./fisherYates";
|
||||||
export * from './isNumber';
|
export * from "./formatCompact";
|
||||||
export * from './NaNFallback';
|
export * from "./isNumber";
|
||||||
export * from './normalizeAngleRad';
|
export * from "./NaNFallback";
|
||||||
export * from './normalToEuler';
|
export * from "./normalizeAngleRad";
|
||||||
export * from './numberToRomanNumeral';
|
export * from "./normalToEuler";
|
||||||
|
export * from "./numberToRomanNumeral";
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { fisherYates } from "@blitzkit/core";
|
||||||
import { CaretDownIcon } from "@radix-ui/react-icons";
|
import { CaretDownIcon } from "@radix-ui/react-icons";
|
||||||
import { Button, Flex } from "@radix-ui/themes";
|
import { Button, Flex } from "@radix-ui/themes";
|
||||||
import { useLocale } from "../hooks/useLocale";
|
import { useLocale } from "../hooks/useLocale";
|
||||||
@@ -20,7 +21,14 @@ export function PlaylistActions() {
|
|||||||
{strings.website.tools.playlist.reset}
|
{strings.website.tools.playlist.reset}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<Button variant="outline">
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => {
|
||||||
|
Playlist.mutate((draft) => {
|
||||||
|
if (draft.list) fisherYates(draft.list);
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
{strings.website.tools.playlist.shuffle}
|
{strings.website.tools.playlist.shuffle}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { tankIcon } from "@blitzkit/core";
|
import { fisherYates, tankIcon } from "@blitzkit/core";
|
||||||
import { literals } from "@blitzkit/i18n";
|
import { literals } from "@blitzkit/i18n";
|
||||||
import { ExclamationTriangleIcon } from "@radix-ui/react-icons";
|
import { ExclamationTriangleIcon } from "@radix-ui/react-icons";
|
||||||
import { Box, Button, Flex, Grid, Text } from "@radix-ui/themes";
|
import { Box, Button, Flex, Grid, Text } from "@radix-ui/themes";
|
||||||
@@ -21,16 +21,10 @@ export function PlaylistGenerator() {
|
|||||||
const { strings, locale } = useLocale();
|
const { strings, locale } = useLocale();
|
||||||
const wargaming = App.use((state) => state.logins.wargaming);
|
const wargaming = App.use((state) => state.logins.wargaming);
|
||||||
const filters = TankFilters.use();
|
const filters = TankFilters.use();
|
||||||
const randomTankList = useMemo(() => {
|
const randomTankList = useMemo(
|
||||||
const list = Object.values(tankDefinitions.tanks);
|
() => fisherYates(Object.values(tankDefinitions.tanks)),
|
||||||
|
[filters]
|
||||||
for (let i = list.length - 1; i > 0; i--) {
|
);
|
||||||
const j = Math.floor(Math.random() * (i + 1));
|
|
||||||
[list[i], list[j]] = [list[j], list[i]];
|
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
|
||||||
}, [filters]);
|
|
||||||
const tanks = usePromise(
|
const tanks = usePromise(
|
||||||
() => filterTanks(filters, randomTankList, wargaming?.id),
|
() => filterTanks(filters, randomTankList, wargaming?.id),
|
||||||
// react-promise-suspense has awful type annotations
|
// react-promise-suspense has awful type annotations
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export function PlaylistOrder() {
|
|||||||
maxHeight="100%"
|
maxHeight="100%"
|
||||||
>
|
>
|
||||||
<CurrentlyPlaying />
|
<CurrentlyPlaying />
|
||||||
|
|
||||||
<PlaylistActions />
|
<PlaylistActions />
|
||||||
|
|
||||||
<Box flexGrow="1" width="100%" position="relative">
|
<Box flexGrow="1" width="100%" position="relative">
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
fisherYates,
|
||||||
getTankStats,
|
getTankStats,
|
||||||
idToRegion,
|
idToRegion,
|
||||||
type IndividualTankStats,
|
type IndividualTankStats,
|
||||||
@@ -24,14 +25,10 @@ export async function generatePlaylist() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = filtered.length - 1; i > 0; i--) {
|
fisherYates(filtered);
|
||||||
const j = Math.floor(Math.random() * (i + 1));
|
|
||||||
[filtered[i], filtered[j]] = [filtered[j], filtered[i]];
|
|
||||||
}
|
|
||||||
|
|
||||||
Playlist.mutate((draft) => {
|
Playlist.mutate((draft) => {
|
||||||
draft.list = filtered.map((tank, index) => ({
|
draft.list = filtered.map((tank) => ({
|
||||||
index,
|
|
||||||
tank,
|
tank,
|
||||||
battles: stats[tank.id]?.all.battles,
|
battles: stats[tank.id]?.all.battles,
|
||||||
wins: stats[tank.id]?.all.wins,
|
wins: stats[tank.id]?.all.wins,
|
||||||
|
|||||||
Reference in new issue
Block a user