72 lines
2.0 KiB
TypeScript
Executable File
72 lines
2.0 KiB
TypeScript
Executable File
import {
|
|
OutcomeType,
|
|
PlayerGameResults,
|
|
PlayerStats,
|
|
PlayerStatsUpdater
|
|
} from "./stats";
|
|
import {
|
|
getMongoObjectCollection,
|
|
MongoStoredObject, MongoStoredObjectCollection, StoredObject, StoredObjectCollection, tryQuery,
|
|
} from "./utils";
|
|
import {CellValue} from "../controllers/statsController";
|
|
import mongo from "mongodb";
|
|
import {Ruleset} from "../rulesets";
|
|
|
|
export interface CellDetails {
|
|
id: string;
|
|
value: CellValue;
|
|
}
|
|
|
|
export interface StoredPlayerData {
|
|
_id: string;
|
|
nick: string;
|
|
stats: PlayerStats;
|
|
}
|
|
|
|
interface StoredPlayerCollection extends StoredObjectCollection<StoredPlayer> {
|
|
}
|
|
|
|
class MongoStoredPlayerCollection
|
|
extends MongoStoredObjectCollection<StoredPlayerData, StoredPlayer>
|
|
implements StoredPlayerCollection {
|
|
|
|
private updater: PlayerStatsUpdater;
|
|
constructor(collectionClient: mongo.Collection) {
|
|
super(collectionClient, MongoStoredPlayer);
|
|
this.updater = new PlayerStatsUpdater();
|
|
}
|
|
|
|
newPlayer(nick: string): Promise<StoredPlayer> {
|
|
return tryQuery(async () => {
|
|
return this.create({nick});
|
|
});
|
|
}
|
|
}
|
|
|
|
export interface StoredPlayer extends StoredObject {
|
|
nick(): string;
|
|
setNick(newNick: string): Promise<void>;
|
|
updateStats(results: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset): Promise<void>;
|
|
}
|
|
|
|
export class MongoStoredPlayer extends MongoStoredObject<StoredPlayerData> implements StoredPlayer {
|
|
constructor(data: StoredPlayerData) {
|
|
super(data);
|
|
}
|
|
|
|
nick(): string {
|
|
return this.data.nick;
|
|
}
|
|
|
|
async setNick(newNick: string): Promise<void> {
|
|
this.data.nick = newNick;
|
|
}
|
|
|
|
async updateStats(playerGameResults: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset) {
|
|
const statsUpdater = new PlayerStatsUpdater(this.data.stats);
|
|
await statsUpdater.updateStats(playerGameResults, ruleset);
|
|
}
|
|
}
|
|
|
|
const StoredPlayers = new MongoStoredPlayerCollection(getMongoObjectCollection("players"));
|
|
export default StoredPlayers; |