Cleaned up naming scheme and finalised StoredPlayer, stats updaters, and generic templates for StoredObjectCollection and StoredObject
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
import mongoose from "mongoose";
|
|
||||||
import {
|
import {
|
||||||
IPlayerStats,
|
OutcomeType,
|
||||||
IPlayerStatsDoc, OutcomeType,
|
|
||||||
PlayerGameResults,
|
PlayerGameResults,
|
||||||
PlayerStats,
|
PlayerStats,
|
||||||
PlayerStatsMongoObjectInterface,
|
PlayerStatsUpdater
|
||||||
PlayerStatsSchema, PlayerStatsUpdater
|
|
||||||
} from "./stats";
|
} from "./stats";
|
||||||
import {
|
import {
|
||||||
|
getMongoObjectCollection,
|
||||||
MongoStoredObject, MongoStoredObjectCollection,
|
MongoStoredObject, MongoStoredObjectCollection,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import {CellValue} from "../controllers/statsController";
|
import {CellValue} from "../controllers/statsController";
|
||||||
@@ -65,9 +63,10 @@ class MongoStoredPlayer extends MongoStoredObject implements StoredPlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateStats(playerGameResults: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset) {
|
async updateStats(playerGameResults: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset) {
|
||||||
const statsInterface = new PlayerStatsMongoObjectInterface(this.data.stats);
|
const statsUpdater = new PlayerStatsUpdater(this.data.stats);
|
||||||
await statsInterface.updateStats(playerGameResults, ruleset);
|
await statsUpdater.updateStats(playerGameResults, ruleset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PlayerCollection;
|
const StoredPlayers = new MongoStoredPlayerCollection(getMongoObjectCollection("players"));
|
||||||
|
export default StoredPlayers;
|
||||||
@@ -137,8 +137,8 @@ class BaseStatsUpdater {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RulesetMongoObjectInterface {
|
class MongoStoredRuleset {
|
||||||
private data: Ruleset;
|
private readonly data: Ruleset;
|
||||||
constructor(data: Ruleset) {
|
constructor(data: Ruleset) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import mongo, {MongoClient, Db} from "mongodb";
|
import mongo, {MongoClient, Db} from "mongodb";
|
||||||
import Settings from "../server-config.json";
|
import Settings from "../server-config.json";
|
||||||
import {PlayerData, StoredPlayer} from "./player";
|
|
||||||
|
|
||||||
let SessionDbClient: Db;
|
let SessionDbClient: Db;
|
||||||
export async function initMongoSessionClient() {
|
export async function initMongoSessionClient() {
|
||||||
@@ -10,7 +9,7 @@ export async function initMongoSessionClient() {
|
|||||||
}
|
}
|
||||||
return SessionDbClient;
|
return SessionDbClient;
|
||||||
}
|
}
|
||||||
export async function getMongoObjectCollection(collectionName: string) {
|
export function getMongoObjectCollection(collectionName: string) {
|
||||||
if (SessionDbClient === undefined) {
|
if (SessionDbClient === undefined) {
|
||||||
throw new MongoError("Cannot retrieve a collection before the session client has been initialised!");
|
throw new MongoError("Cannot retrieve a collection before the session client has been initialised!");
|
||||||
}
|
}
|
||||||
@@ -19,7 +18,7 @@ export async function getMongoObjectCollection(collectionName: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class MongoStoredObjectCollection<T extends MongoStoredObject> {
|
export abstract class MongoStoredObjectCollection<T extends MongoStoredObject<any>> {
|
||||||
protected mongoDbClientCollection: mongo.Collection;
|
protected mongoDbClientCollection: mongo.Collection;
|
||||||
protected constructor(collectionClient: mongo.Collection) {
|
protected constructor(collectionClient: mongo.Collection) {
|
||||||
this.mongoDbClientCollection = collectionClient;
|
this.mongoDbClientCollection = collectionClient;
|
||||||
@@ -36,14 +35,14 @@ export abstract class MongoStoredObjectCollection<T extends MongoStoredObject> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class MongoStoredObject {
|
export abstract class MongoStoredObject<T> {
|
||||||
protected constructor(protected data: {_id: string} & any) {}
|
protected constructor(protected data: {_id: string} & T) {}
|
||||||
|
|
||||||
id(): string {
|
id(): string {
|
||||||
return this.data._id;
|
return this.data._id;
|
||||||
}
|
}
|
||||||
|
|
||||||
rawData(): PlayerData {
|
rawData(): T {
|
||||||
return this.data;
|
return this.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user