I think it's done?

This commit is contained in:
Daniel Ledda
2020-07-17 22:23:05 +02:00
parent 4542036b77
commit 42b7560d6d
21 changed files with 443 additions and 272 deletions

View File

@@ -0,0 +1,54 @@
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";
export interface SavedGameMongoData {
id: string;
rulesetUsed: ActiveRecordId;
players: ActiveRecordId[];
results: PlayerGameResults[];
}
class SavedGameCollection extends MongoStoredObjectCollection<SavedGameMongoData> {
constructor(collectionClient: mongo.Collection) {
super(collectionClient);
}
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);
playerList.push({name: player.getNick(), id: playerId})
}
const rulesetUsed = await RulesetCollection.read(data.rulesetUsed);
return new SavedGame(
data.id,
{name: rulesetUsed.getName(), id: data.rulesetUsed},
playerList,
data.results);
}
async read(id: string): 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);
return this.savedGameFrom(
await this.mongoCreate({
rulesetUsed: gameSubmission.rulesetId,
players: pids,
results: gameSubmission.results})
);
}
}
const SavedGameCollectionSingleton = new SavedGameCollection(getMongoObjectCollection("users"));
export default SavedGameCollectionSingleton;