2023-02-27 23:52:40 -05:00
|
|
|
import { MongoDB } from "../../deps.ts";
|
2023-02-28 11:34:07 -05:00
|
|
|
import { AGENT_VERSION, NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts";
|
2023-03-01 09:06:48 -05:00
|
|
|
import { CoopHistoryDetail, ExportResult, Game, GameExporter, Summary, VsHistoryDetail } from "../types.ts";
|
2023-02-27 23:52:40 -05:00
|
|
|
import { parseHistoryDetailId } from "../utils.ts";
|
2023-02-27 23:06:04 -05:00
|
|
|
|
2023-02-27 23:52:40 -05:00
|
|
|
export class MongoDBExporter implements GameExporter {
|
|
|
|
|
name = "mongodb";
|
|
|
|
|
mongoDbClient: MongoDB.MongoClient;
|
|
|
|
|
mongoDb: MongoDB.Db;
|
|
|
|
|
battlesCollection: MongoDB.Collection;
|
|
|
|
|
jobsCollection: MongoDB.Collection;
|
2023-03-01 09:06:48 -05:00
|
|
|
summariesCollection: MongoDB.Collection;
|
2023-02-27 23:52:40 -05:00
|
|
|
constructor(private mongoDbUri: string) {
|
|
|
|
|
this.mongoDbClient = new MongoDB.MongoClient(mongoDbUri);
|
|
|
|
|
this.mongoDb = this.mongoDbClient.db("splashcat");
|
|
|
|
|
this.battlesCollection = this.mongoDb.collection("battles");
|
|
|
|
|
this.jobsCollection = this.mongoDb.collection("jobs");
|
2023-03-01 09:06:48 -05:00
|
|
|
this.summariesCollection = this.mongoDb.collection("summaries");
|
2023-02-27 23:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 09:06:48 -05:00
|
|
|
static getGameId(id: string) { // very similar to the file exporter
|
2023-02-27 23:52:40 -05:00
|
|
|
const { uid, timestamp } = parseHistoryDetailId(id);
|
|
|
|
|
|
|
|
|
|
return `${uid}_${timestamp}Z`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async notExported({ type, list }: { type: Game["type"], list: string[] }): Promise<string[]> {
|
|
|
|
|
const out: string[] = [];
|
|
|
|
|
|
|
|
|
|
const collection = type === "CoopInfo" ? this.jobsCollection : this.battlesCollection;
|
|
|
|
|
|
|
|
|
|
for (const id of list) {
|
2023-03-01 09:06:48 -05:00
|
|
|
const uniqueId = MongoDBExporter.getGameId(id);
|
2023-02-27 23:52:40 -05:00
|
|
|
const countNewStorage = await collection.countDocuments({
|
|
|
|
|
gameId: uniqueId,
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-02 11:39:10 -05:00
|
|
|
if (countNewStorage === 0) {
|
2023-02-27 23:52:40 -05:00
|
|
|
out.push(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
2023-02-28 11:34:07 -05:00
|
|
|
|
|
|
|
|
async exportGame(game: Game): Promise<ExportResult> {
|
2023-03-01 09:06:48 -05:00
|
|
|
const uniqueId = MongoDBExporter.getGameId(game.detail.id);
|
2023-02-28 11:34:07 -05:00
|
|
|
|
|
|
|
|
const common = {
|
|
|
|
|
// this seems like useful data to store...
|
|
|
|
|
// loosely modeled after FileExporterTypeCommon
|
|
|
|
|
nsoVersion: NSOAPP_VERSION,
|
|
|
|
|
agentVersion: AGENT_VERSION,
|
|
|
|
|
s3siVersion: S3SI_VERSION,
|
2023-03-01 09:06:48 -05:00
|
|
|
exportDate: new Date(),
|
2023-02-28 11:34:07 -05:00
|
|
|
};
|
2023-03-04 19:09:06 -05:00
|
|
|
|
|
|
|
|
const splatNetData = {
|
|
|
|
|
...game.detail,
|
|
|
|
|
playedTime: new Date(game.detail.playedTime),
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-28 11:34:07 -05:00
|
|
|
const body:
|
|
|
|
|
{
|
|
|
|
|
data: Game,
|
2023-03-04 19:17:19 -05:00
|
|
|
splatNetData: Omit<(VsHistoryDetail | CoopHistoryDetail), "playedTime"> & { playedTime: Date },
|
2023-02-28 11:34:07 -05:00
|
|
|
gameId: string,
|
|
|
|
|
} & typeof common = {
|
|
|
|
|
...common,
|
|
|
|
|
data: game,
|
2023-03-04 19:09:06 -05:00
|
|
|
splatNetData,
|
2023-02-28 11:34:07 -05:00
|
|
|
gameId: uniqueId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isJob = game.type === "CoopInfo";
|
|
|
|
|
|
|
|
|
|
const collection = isJob ? this.jobsCollection : this.battlesCollection;
|
|
|
|
|
|
|
|
|
|
const result = await collection.insertOne(body);
|
|
|
|
|
|
|
|
|
|
const objectId = result.insertedId;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: "success",
|
|
|
|
|
url: `https://new.splatoon.catgirlin.space/battle/${objectId.toString()}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-03-01 09:06:48 -05:00
|
|
|
|
|
|
|
|
async exportSummary(summary: Summary): Promise<ExportResult> {
|
|
|
|
|
const id = summary.uid;
|
|
|
|
|
|
|
|
|
|
await this.summariesCollection.insertOne({
|
|
|
|
|
summaryId: id,
|
|
|
|
|
...summary,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: "success",
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-02-27 23:52:40 -05:00
|
|
|
}
|