Big update
This commit is contained in:
@@ -1,54 +1,63 @@
|
||||
import MongoStoredObjectCollection from "./MongoStoredObjectCollection";
|
||||
import mongo from "mongodb";
|
||||
import {ActiveRecordId} from "../Objects/ActiveRecord";
|
||||
import PlayerCollection from "./PlayerCollection";
|
||||
import SavedGame from "../Objects/SavedGame";
|
||||
import {getMongoObjectCollection} from "../database";
|
||||
import {PlayerGameResults} from "../Objects/DefaultStatsMongoData";
|
||||
import RulesetCollection from "./RulesetCollection";
|
||||
import {GameSubmission} from "../controllers/statsController";
|
||||
import {ProcessedGameSubmission, ScoredResultsWithOutcome} from "../Controllers/statsController";
|
||||
|
||||
export interface SavedGameMongoData {
|
||||
id: string;
|
||||
rulesetUsed: ActiveRecordId;
|
||||
ruleset: ActiveRecordId;
|
||||
players: ActiveRecordId[];
|
||||
results: PlayerGameResults[];
|
||||
results: ScoredResultsWithOutcome[];
|
||||
}
|
||||
|
||||
class SavedGameCollection extends MongoStoredObjectCollection<SavedGameMongoData> {
|
||||
constructor(collectionClient: mongo.Collection) {
|
||||
super(collectionClient);
|
||||
private static instance?: SavedGameCollection;
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
static getInstance(): SavedGameCollection {
|
||||
if (SavedGameCollection.instance === undefined) {
|
||||
SavedGameCollection.instance = new SavedGameCollection();
|
||||
}
|
||||
return SavedGameCollection.instance;
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.mongoDbClientCollection = getMongoObjectCollection("savedGames");
|
||||
}
|
||||
|
||||
private async savedGameFrom(data: SavedGameMongoData): Promise<SavedGame> {
|
||||
const playerList: {name: string, id: ActiveRecordId}[] = [];
|
||||
for (const playerId of data.players) {
|
||||
const player = await PlayerCollection.read(playerId);
|
||||
const player = await PlayerCollection().read(playerId);
|
||||
playerList.push({name: player.getNick(), id: playerId})
|
||||
}
|
||||
const rulesetUsed = await RulesetCollection.read(data.rulesetUsed);
|
||||
const rulesetUsed = await RulesetCollection().read(data.ruleset);
|
||||
return new SavedGame(
|
||||
data.id,
|
||||
{name: rulesetUsed.getName(), id: data.rulesetUsed},
|
||||
{name: rulesetUsed.getName(), id: data.ruleset},
|
||||
playerList,
|
||||
data.results);
|
||||
}
|
||||
|
||||
async read(id: string): Promise<SavedGame> {
|
||||
async read(id: ActiveRecordId): Promise<SavedGame> {
|
||||
const foundGame = await this.mongoRead(id);
|
||||
return this.savedGameFrom(foundGame);
|
||||
}
|
||||
|
||||
async create(gameSubmission: GameSubmission): Promise<SavedGame> {
|
||||
const pids = gameSubmission.players.map(playerIdAndNick => playerIdAndNick.id);
|
||||
async create(submission: ProcessedGameSubmission): Promise<SavedGame> {
|
||||
const pids = submission.players.map(playerIdAndNick => playerIdAndNick.id);
|
||||
return this.savedGameFrom(
|
||||
await this.mongoCreate({
|
||||
rulesetUsed: gameSubmission.rulesetId,
|
||||
ruleset: submission.ruleset,
|
||||
players: pids,
|
||||
results: gameSubmission.results})
|
||||
results: submission.results})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const SavedGameCollectionSingleton = new SavedGameCollection(getMongoObjectCollection("users"));
|
||||
export default SavedGameCollectionSingleton;
|
||||
export default SavedGameCollection.getInstance;
|
||||
Reference in New Issue
Block a user