From 81f35e9a3203e7bf24ac23bd944842b41bb6a35b Mon Sep 17 00:00:00 2001 From: Matt Isenhower Date: Sat, 25 Nov 2017 14:19:58 -0800 Subject: [PATCH] Track the time when a stage is first seen and first available This will be used for tweets. --- src/updater/splatnet.js | 4 ++ src/updater/update.js | 11 +---- src/updater/updaters/SchedulesUpdater.js | 61 ++++++++++++++++++++++++ src/updater/updaters/Updater.js | 2 +- 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 src/updater/updaters/SchedulesUpdater.js diff --git a/src/updater/splatnet.js b/src/updater/splatnet.js index a6f85ea..769d76c 100644 --- a/src/updater/splatnet.js +++ b/src/updater/splatnet.js @@ -45,6 +45,10 @@ class SplatNet { return this.getResponse('coop_schedules'); } + getStages() { + return this.getResponse('data/stages'); + } + getTimeline() { return this.getResponse('timeline'); } diff --git a/src/updater/update.js b/src/updater/update.js index a2e6892..8a9f748 100644 --- a/src/updater/update.js +++ b/src/updater/update.js @@ -1,6 +1,7 @@ require('./bootstrap'); const Updater = require('./updaters/Updater'); +const SchedulesUpdater = require('./updaters/SchedulesUpdater'); const OriginalGearImageUpdater = require('./updaters/OriginalGearImageUpdater'); const FestivalsUpdater = require('./updaters/FestivalsUpdater'); const MerchandisesUpdater = require('./updaters/MerchandisesUpdater'); @@ -10,15 +11,7 @@ const updaters = [ new OriginalGearImageUpdater(), // Schedules - new Updater({ - name: 'Schedules', - filename: 'schedules.json', - request: (splatnet) => splatnet.getSchedules(), - imagePaths: [ - '$..stage_a.image', - '$..stage_b.image', - ], - }), + new SchedulesUpdater, // Co-op Schedules new Updater({ diff --git a/src/updater/updaters/SchedulesUpdater.js b/src/updater/updaters/SchedulesUpdater.js new file mode 100644 index 0000000..a66e100 --- /dev/null +++ b/src/updater/updaters/SchedulesUpdater.js @@ -0,0 +1,61 @@ +const Updater = require('./Updater'); +const SplatNet = require('../splatnet'); +const path = require('path'); +const fs = require('fs'); +const { getTopOfCurrentHour, readJson } = require('../utilities'); + +const stagesPath = path.resolve('storage/stages.json'); + +class SchedulesUpdater extends Updater { + constructor() { + super({ + name: 'Schedules', + filename: 'schedules.json', + request: (splatnet) => splatnet.getSchedules(), + imagePaths: [ + '$..stage_a.image', + '$..stage_b.image', + ], + }); + } + + async processData(data) { + // We need to track whether there are any new stages included with this schedule update + + // If we don't have a "known stages" file, create it from the current list of stages + if (!fs.existsSync(stagesPath)) { + let splatnet = new SplatNet; + let stageData = await splatnet.getStages(); + let stages = stageData.stages.map(s => Object.assign(s, { first_seen: -1, first_available: -1 })); + this.writeFile(stagesPath, JSON.stringify(stages)); + } + + // Load known stages + let stages = readJson(stagesPath); + + // Look for new stages (in Regular Battle) + let sortedSchedules = data.regular.sort((a, b) => a.start_time - b.start_time); + for (let schedule of sortedSchedules) { + for (let stage of [schedule.stage_a, schedule.stage_b]) { + // Have wee seen this stage before? + let knownStage = stages.find(s => s.id == stage.id); + + if (!knownStage) { + // If we haven't seen the stage before, add it to the list with the current time + // and the time the stage will be first available. + stages.push(Object.assign({}, stage, { + first_seen: getTopOfCurrentHour(), + first_available: schedule.start_time, + })); + } + } + } + + // Update the stages file + this.writeFile(stagesPath, JSON.stringify(stages)); + + return data; + } +} + +module.exports = SchedulesUpdater; diff --git a/src/updater/updaters/Updater.js b/src/updater/updaters/Updater.js index 523a5db..1d94d02 100644 --- a/src/updater/updaters/Updater.js +++ b/src/updater/updaters/Updater.js @@ -23,7 +23,7 @@ class Updater { data = this.filterRootKeys(data); // Apply any other processing - data = this.processData(data); + data = await this.processData(data); // Convert the data to a JSON string let dataString = JSON.stringify(data);