diff --git a/src/models/player.ts b/src/models/player.ts index 99c8bd8..f7c69bc 100755 --- a/src/models/player.ts +++ b/src/models/player.ts @@ -1,13 +1,11 @@ -import mongoose from "mongoose"; import { - IPlayerStats, - IPlayerStatsDoc, OutcomeType, + OutcomeType, PlayerGameResults, PlayerStats, - PlayerStatsMongoObjectInterface, - PlayerStatsSchema, PlayerStatsUpdater + PlayerStatsUpdater } from "./stats"; import { + getMongoObjectCollection, MongoStoredObject, MongoStoredObjectCollection, } from "./utils"; import {CellValue} from "../controllers/statsController"; @@ -65,9 +63,10 @@ class MongoStoredPlayer extends MongoStoredObject implements StoredPlayer { } async updateStats(playerGameResults: PlayerGameResults & {outcome: OutcomeType}, ruleset: Ruleset) { - const statsInterface = new PlayerStatsMongoObjectInterface(this.data.stats); - await statsInterface.updateStats(playerGameResults, ruleset); + const statsUpdater = new PlayerStatsUpdater(this.data.stats); + await statsUpdater.updateStats(playerGameResults, ruleset); } } -export default PlayerCollection; \ No newline at end of file +const StoredPlayers = new MongoStoredPlayerCollection(getMongoObjectCollection("players")); +export default StoredPlayers; \ No newline at end of file diff --git a/src/models/stats.ts b/src/models/stats.ts index 66eb168..298bc22 100755 --- a/src/models/stats.ts +++ b/src/models/stats.ts @@ -137,8 +137,8 @@ class BaseStatsUpdater { } } -class RulesetMongoObjectInterface { - private data: Ruleset; +class MongoStoredRuleset { + private readonly data: Ruleset; constructor(data: Ruleset) { this.data = data; } diff --git a/src/models/utils.ts b/src/models/utils.ts index bfb1a6d..0345bc0 100644 --- a/src/models/utils.ts +++ b/src/models/utils.ts @@ -1,6 +1,5 @@ import mongo, {MongoClient, Db} from "mongodb"; import Settings from "../server-config.json"; -import {PlayerData, StoredPlayer} from "./player"; let SessionDbClient: Db; export async function initMongoSessionClient() { @@ -10,7 +9,7 @@ export async function initMongoSessionClient() { } return SessionDbClient; } -export async function getMongoObjectCollection(collectionName: string) { +export function getMongoObjectCollection(collectionName: string) { if (SessionDbClient === undefined) { 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 { +export abstract class MongoStoredObjectCollection> { protected mongoDbClientCollection: mongo.Collection; protected constructor(collectionClient: mongo.Collection) { this.mongoDbClientCollection = collectionClient; @@ -36,14 +35,14 @@ export abstract class MongoStoredObjectCollection { } } -export abstract class MongoStoredObject { - protected constructor(protected data: {_id: string} & any) {} +export abstract class MongoStoredObject { + protected constructor(protected data: {_id: string} & T) {} id(): string { return this.data._id; } - rawData(): PlayerData { + rawData(): T { return this.data; } }