sendou.ink/app/modules/brackets-manager/manager.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

143 lines
4.3 KiB
TypeScript

import type {
CrudInterface,
Database,
DataTypes,
Storage,
Table,
} from "./types";
import type { InputStage, Stage } from "brackets-model";
import { create } from "./create";
import { Get } from "./get";
import { Update } from "./update";
import { Delete } from "./delete";
import { Find } from "./find";
import { Reset } from "./reset";
import * as helpers from "./helpers";
/**
* A class to handle tournament management at those levels: `stage`, `group`, `round`, `match` and `match_game`.
*/
export class BracketsManager {
public storage: Storage;
public get: Get;
public update: Update;
public delete: Delete;
public find: Find;
public reset: Reset;
/**
* Creates an instance of BracketsManager, which will handle all the stuff from the library.
*
* @param storageInterface An implementation of CrudInterface.
*/
constructor(storageInterface: CrudInterface) {
const storage = storageInterface as Storage;
storage.selectFirst = <T extends Table>(
table: T,
filter: Partial<DataTypes[T]>
): DataTypes[T] | null => {
const results = this.storage.select<T>(table, filter);
if (!results || results.length === 0) return null;
return results[0];
};
storage.selectLast = <T extends Table>(
table: T,
filter: Partial<DataTypes[T]>
): DataTypes[T] | null => {
const results = this.storage.select<T>(table, filter);
if (!results || results.length === 0) return null;
return results[results.length - 1];
};
this.storage = storage;
this.get = new Get(this.storage);
this.update = new Update(this.storage);
this.delete = new Delete(this.storage);
this.find = new Find(this.storage);
this.reset = new Reset(this.storage);
}
/**
* Creates a stage for an existing tournament. The tournament won't be created.
*
* @param stage A stage to create.
*/
public create(stage: InputStage): Stage {
return create.call(this, stage);
}
/**
* Imports data in the database.
*
* @param data Data to import.
* @param normalizeIds Enable ID normalization: all IDs (and references to them) are remapped to consecutive IDs starting from 0.
*/
public import(data: Database, normalizeIds = false): void {
if (normalizeIds) data = helpers.normalizeIds(data);
if (!this.storage.delete("participant"))
throw Error("Could not empty the participant table.");
if (!this.storage.insert("participant", data.participant))
throw Error("Could not import participants.");
if (!this.storage.delete("stage"))
throw Error("Could not empty the stage table.");
if (!this.storage.insert("stage", data.stage))
throw Error("Could not import stages.");
if (!this.storage.delete("group"))
throw Error("Could not empty the group table.");
if (!this.storage.insert("group", data.group))
throw Error("Could not import groups.");
if (!this.storage.delete("round"))
throw Error("Could not empty the round table.");
if (!this.storage.insert("round", data.round))
throw Error("Could not import rounds.");
if (!this.storage.delete("match"))
throw Error("Could not empty the match table.");
if (!this.storage.insert("match", data.match))
throw Error("Could not import matches.");
if (!this.storage.delete("match_game"))
throw Error("Could not empty the match_game table.");
if (!this.storage.insert("match_game", data.match_game))
throw Error("Could not import match games.");
}
/**
* Exports data from the database.
*/
public export(): Database {
const participants = this.storage.select("participant");
if (!participants) throw Error("Error getting participants.");
const stages = this.storage.select("stage");
if (!stages) throw Error("Error getting stages.");
const groups = this.storage.select("group");
if (!groups) throw Error("Error getting groups.");
const rounds = this.storage.select("round");
if (!rounds) throw Error("Error getting rounds.");
const matches = this.storage.select("match");
if (!matches) throw Error("Error getting matches.");
const matchGames = this.get.matchGames(matches);
return {
participant: participants,
stage: stages,
group: groups,
round: rounds,
match: matches,
match_game: matchGames,
};
}
}