refactor: move round table helper to utils and clean GroupMember
- extract getRoundTable into src/utils.ts as a reusable generic helper - update GroupMember to import getRoundTable from utils - remove round pairing debug log from GroupMember
This commit is contained in:
parent
a9b7113173
commit
a4fc0d0031
@ -1,6 +1,7 @@
|
|||||||
|
import { useMemo, useState } from "react";
|
||||||
import { Button, Card, Divider, Drawer, Flex, Space, Table } from "antd";
|
import { Button, Card, Divider, Drawer, Flex, Space, Table } from "antd";
|
||||||
import type { BasePlayer } from "../types";
|
import type { BasePlayer } from "../types";
|
||||||
import { useMemo, useState } from "react";
|
import { getRoundTable } from "../utils";
|
||||||
import User from "./User";
|
import User from "./User";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -65,7 +66,6 @@ function BattleTable({ nameList }: {
|
|||||||
<Flex vertical gap={12} justify="center" align="center">
|
<Flex vertical gap={12} justify="center" align="center">
|
||||||
{new Array(list.length - 1).fill(0).map((_, i) => {
|
{new Array(list.length - 1).fill(0).map((_, i) => {
|
||||||
const [left, right] = getRoundTable(list, i);
|
const [left, right] = getRoundTable(list, i);
|
||||||
console.debug({ left, right });
|
|
||||||
return (
|
return (
|
||||||
<Card size="small" key={`round-${i}`} title={`Round: ${i + 1}`}>
|
<Card size="small" key={`round-${i}`} title={`Round: ${i + 1}`}>
|
||||||
{left?.map((e, r) => {
|
{left?.map((e, r) => {
|
||||||
@ -86,23 +86,3 @@ function BattleTable({ nameList }: {
|
|||||||
</Flex>
|
</Flex>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRoundTable(nameList: string[], round: number) {
|
|
||||||
const list = [...nameList];
|
|
||||||
const half = list.length / 2;
|
|
||||||
if (round > list.length - 1) {
|
|
||||||
const left = [...list].slice(0, half);
|
|
||||||
const right = [...list].slice(half);
|
|
||||||
return [left, right];
|
|
||||||
}
|
|
||||||
const sliceStart = (list.length) - round;
|
|
||||||
const slice = list.slice(sliceStart);
|
|
||||||
// console.debug(JSON.stringify({ list }));
|
|
||||||
list.splice(sliceStart);
|
|
||||||
const [first, ...others] = list;
|
|
||||||
const newList = [first, ...slice, ...others].filter(Boolean);
|
|
||||||
// console.debug(JSON.stringify({ sliceStart, len: list.length, first, others, slice, newList }));
|
|
||||||
const left = [...newList].slice(0, half);
|
|
||||||
const right = [...newList].slice(half).reverse();
|
|
||||||
return [left, right];
|
|
||||||
}
|
|
||||||
20
src/utils.ts
20
src/utils.ts
@ -120,3 +120,23 @@ export enum SEX {
|
|||||||
'男' = 1,
|
'男' = 1,
|
||||||
'女' = 2,
|
'女' = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRoundTable<T>(nameList: T[], round: number) {
|
||||||
|
const list = [...nameList];
|
||||||
|
const half = list.length / 2;
|
||||||
|
if (round > list.length - 1) {
|
||||||
|
const left = [...list].slice(0, half);
|
||||||
|
const right = [...list].slice(half);
|
||||||
|
return [left, right];
|
||||||
|
}
|
||||||
|
const sliceStart = (list.length) - round;
|
||||||
|
const slice = list.slice(sliceStart);
|
||||||
|
// console.debug(JSON.stringify({ list }));
|
||||||
|
list.splice(sliceStart);
|
||||||
|
const [first, ...others] = list;
|
||||||
|
const newList = [first, ...slice, ...others].filter(Boolean);
|
||||||
|
// console.debug(JSON.stringify({ sliceStart, len: list.length, first, others, slice, newList }));
|
||||||
|
const left = [...newList].slice(0, half);
|
||||||
|
const right = [...newList].slice(half).reverse();
|
||||||
|
return [left, right];
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user