sendou.ink/app/modules/brackets-memory-db/index.ts
Kalle ef78d3a2c2
Tournament full (#1373)
* Got something going

* Style overwrites

* width != height

* More playing with lines

* Migrations

* Start bracket initial

* Unhardcode stage generation params

* Link to match page

* Matches page initial

* Support directly adding seed to map list generator

* Add docs

* Maps in matches page

* Add invariant about tie breaker map pool

* Fix PICNIC lacking tie breaker maps

* Only link in bracket when tournament has started

* Styled tournament roster inputs

* Prefer IGN in tournament match page

* ModeProgressIndicator

* Some conditional rendering

* Match action initial + better error display

* Persist bestOf in DB

* Resolve best of ahead of time

* Move brackets-manager to core

* Score reporting works

* Clear winner on score report

* ModeProgressIndicator: highlight winners

* Fix inconsistent input

* Better text when submitting match

* mapCountPlayedInSetWithCertainty that works

* UNDO_REPORT_SCORE implemented

* Permission check when starting tournament

* Remove IGN from upsert

* View match results page

* Source in DB

* Match page waiting for teams

* Move tournament bracket to feature folder

* REOPEN_MATCH initial

* Handle proper resetting of match

* Inline bracket-manager

* Syncify

* Transactions

* Handle match is locked gracefully

* Match page auto refresh

* Fix match refresh called "globally"

* Bracket autoupdate

* Move fillWithNullTillPowerOfTwo to utils with testing

* Fix map lists not visible after tournament started

* Optimize match events

* Show UI while in progress to members

* Fix start tournament alert not being responsive

* Teams can check in

* Fix map list 400

* xxx -> TODO

* Seeds page

* Remove map icons for team page

* Don't display link to seeds after tournament has started

* Admin actions initial

* Change captain admin action

* Make all hooks ts

* Admin actions functioning

* Fix validate error not displaying in CatchBoundary

* Adjust validate args order

* Remove admin loader

* Make delete team button menancing

* Only include checked in teams to bracket

* Optimize to.id route loads

* Working show map list generator toggle

* Update full tournaments flow

* Make full tournaments work with many start times

* Handle undefined in crud

* Dynamic stage banner

* Handle default strat if map list generation fails

* Fix crash on brackets if less than 2 teams

* Add commented out test for reference

* Add TODO

* Add players from team during register

* TrustRelationship

* Prefers not to host feature

* Last before merge

* Rename some vars

* More renames
2023-05-15 22:37:43 +03:00

251 lines
5.6 KiB
TypeScript

import clone from "just-clone";
import type {
CrudInterface,
OmitId,
Table,
Database,
} from "~/modules/brackets-manager";
export class InMemoryDatabase implements CrudInterface {
protected data: Database = {
participant: [],
stage: [],
group: [],
round: [],
match: [],
match_game: [],
};
/**
* @param data "import" data from external
*/
setData(data: Database): void {
this.data = data;
}
/**
* @param partial Filter
*/
makeFilter(partial: any): (entry: any) => boolean {
return (entry: any): boolean => {
let result = true;
for (const key of Object.keys(partial))
result = result && entry[key] === partial[key];
return result;
};
}
/**
* Clearing all of the data
*/
reset(): void {
this.data = {
participant: [],
stage: [],
group: [],
round: [],
match: [],
match_game: [],
};
}
insert<T>(table: Table, value: OmitId<T>): number;
/**
* Inserts multiple values in the database.
*
* @param table Where to insert.
* @param values What to insert.
*/
insert<T>(table: Table, values: OmitId<T>[]): boolean;
/**
* Implementation of insert
*
* @param table Where to insert.
* @param values What to insert.
*/
insert<T>(table: Table, values: OmitId<T> | OmitId<T>[]): number | boolean {
let id =
this.data[table].length > 0
? Math.max(...this.data[table].map((d) => d.id)) + 1
: 0;
if (!Array.isArray(values)) {
try {
// @ts-expect-error imported
this.data[table].push({ id, ...values });
} catch (error) {
return -1;
}
return id;
}
try {
values.forEach((object) => {
// @ts-expect-error imported
this.data[table].push({ id: id++, ...object });
});
} catch (error) {
return false;
}
return true;
}
/**
* Gets all data from a table in the database.
*
* @param table Where to get from.
*/
select<T>(table: Table): T[] | null;
/**
* Gets specific data from a table in the database.
*
* @param table Where to get from.
* @param id What to get.
*/
select<T>(table: Table, id: number): T | null;
/**
* Gets data from a table in the database with a filter.
*
* @param table Where to get from.
* @param filter An object to filter data.
*/
select<T>(table: Table, filter: Partial<T>): T[] | null;
/**
* @param table Where to get from.
* @param arg Arg.
*/
select<T>(table: Table, arg?: number | Partial<T>): T[] | null {
try {
if (arg === undefined) {
// @ts-expect-error imported
return this.data[table].map(clone);
}
if (typeof arg === "number") {
// @ts-expect-error imported
return clone(this.data[table].find((d) => d.id === arg));
}
// @ts-expect-error imported
return this.data[table].filter(this.makeFilter(arg)).map(clone);
} catch (error) {
return null;
}
}
/**
* Updates data in a table.
*
* @param table Where to update.
* @param id What to update.
* @param value How to update.
*/
update<T>(table: Table, id: number, value: T): boolean;
/**
* Updates data in a table.
*
* @param table Where to update.
* @param filter An object to filter data.
* @param value How to update.
*/
update<T>(table: Table, filter: Partial<T>, value: Partial<T>): boolean;
/**
* Updates data in a table.
*
* @param table Where to update.
* @param arg
* @param value How to update.
*/
update<T>(
table: Table,
arg: number | Partial<T>,
value?: Partial<T>
): boolean {
if (typeof arg === "number") {
try {
// @ts-expect-error imported
this.data[table][arg] = value;
return true;
} catch (error) {
return false;
}
}
// @ts-expect-error imported
const values = this.data[table].filter(this.makeFilter(arg));
if (!values) {
return false;
}
values.forEach((v: { id: any }) => {
const existing = this.data[table][v.id];
for (const key in value) {
if (
// @ts-expect-error imported
existing[key] &&
// @ts-expect-error imported
typeof existing[key] === "object" &&
typeof value[key] === "object"
) {
// @ts-expect-error imported
Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
} else {
// @ts-expect-error imported
existing[key] = value[key]; // Otherwise, do a simple value assignment.
}
}
this.data[table][v.id] = existing;
});
return true;
}
/**
* Empties a table completely.
*
* @param table Where to delete everything.
*/
delete(table: Table): boolean;
/**
* Delete data in a table, based on a filter.
*
* @param table Where to delete in.
* @param filter An object to filter data.
*/
delete<T>(table: Table, filter: Partial<T>): boolean;
/**
* Delete data in a table, based on a filter.
*
* @param table Where to delete in.
* @param filter An object to filter data.
*/
delete<T>(table: Table, filter?: Partial<T>): boolean {
const values = this.data[table];
if (!values) {
return false;
}
if (!filter) {
this.data[table] = [];
return true;
}
const predicate = this.makeFilter(filter);
const negativeFilter = (value: any): boolean => !predicate(value);
// @ts-expect-error imported
this.data[table] = values.filter(negativeFilter);
return true;
}
}