I think it's done?
This commit is contained in:
54
src/ObjectCollections/SavedGameCollection.ts
Normal file
54
src/ObjectCollections/SavedGameCollection.ts
Normal 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;
|
||||
Reference in New Issue
Block a user