From 21b02fb44dd7fe26b0c57ac2c8875f288c0bf571 Mon Sep 17 00:00:00 2001 From: rosalina Date: Sat, 13 Jan 2024 01:59:04 -0500 Subject: [PATCH 1/4] event stream implementation (committing this right now because event streams seem broke) --- s3si.ts | 6 ++++-- src/app.ts | 30 +++++++++++++++++++++++++++++- src/constant.ts | 2 ++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/s3si.ts b/s3si.ts index cae0203..186c94e 100644 --- a/s3si.ts +++ b/s3si.ts @@ -4,7 +4,7 @@ import { flags } from "./deps.ts"; const parseArgs = (args: string[]) => { const parsed = flags.parse(args, { - string: ["profilePath", "exporter", "skipMode", "listMethod"], + string: ["profilePath", "exporter", "skipMode", "listMethod", "nxapiPresenceUrl"], boolean: ["help", "noProgress", "monitor", "withSummary"], alias: { "help": "h", @@ -15,6 +15,7 @@ const parseArgs = (args: string[]) => { "skipMode": ["s", "skip-mode"], "withSummary": "with-summary", "listMethod": "list-method", + "nxapiPresenceUrl": ["nxapi-presence-url", "nxapi-presence"] }, }); return parsed; @@ -39,7 +40,8 @@ Options: --skip-mode , -s Skip mode (default: null) ("vs", "coop") --with-summary Include summary in the output - --help Show this help message and exit`, + --help Show this help message and exit + --nxapi-presence-url Extends monitoring mode to use Nintendo Switch presence from nxapi`, ); Deno.exit(0); } diff --git a/src/app.ts b/src/app.ts index 4d5e9e6..94304cf 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,6 +9,7 @@ import { FileExporter } from "./exporters/file.ts"; import { delay, showError } from "./utils.ts"; import { GameFetcher } from "./GameFetcher.ts"; import { DEFAULT_ENV, Env } from "./env.ts"; +import { SPLATOON3_TITLE_ID } from "./constant.ts"; export type Opts = { profilePath: string; @@ -21,6 +22,7 @@ export type Opts = { cache?: Cache; stateBackend?: StateBackend; env: Env; + nxapiPresenceUrl?: string; }; export const DEFAULT_OPTS: Opts = { @@ -162,6 +164,7 @@ function progress({ total, currentUrl, done }: StepProgress): Progress { export class App { profile: Profile; env: Env; + isSplatoon3Active = false; constructor(public opts: Opts) { const stateBackend = opts.stateBackend ?? @@ -395,6 +398,29 @@ export class App { } } } + async monitorWithNxapi() { + this.env.logger.debug("Monitoring with nxapi presence"); + await this.exportOnce(); + this.env.logger.debug("Connecting to event stream"); + const nxapiEventStream = new EventSource(this.opts.nxapiPresenceUrl!); + nxapiEventStream.addEventListener("title", (event) => { + this.env.logger.debug("Received a new title event", event, event.data) + const eventData = JSON.parse(event.data) + const newStatus = eventData.id === SPLATOON3_TITLE_ID; + if (this.isSplatoon3Active !== newStatus) { + this.env.logger.log("Splatoon 3 presence changed, new status: ", newStatus ? "active" : "inactive"); + } + this.isSplatoon3Active = newStatus; + }) + + while (true) { + await this.countDown(this.profile.state.monitorInterval); + if (this.isSplatoon3Active) { + this.env.logger.log("Splatoon 3 is active, exporting data"); + await this.exportOnce(); + } + } + } async monitor() { while (true) { await this.exportOnce(); @@ -432,7 +458,9 @@ export class App { }); } - if (this.opts.monitor) { + if (this.opts.nxapiPresenceUrl) { + await this.monitorWithNxapi(); + } else if (this.opts.monitor) { await this.monitor(); } else { await this.exportOnce(); diff --git a/src/constant.ts b/src/constant.ts index ad0ec0a..ea945ec 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -116,3 +116,5 @@ export const SPLATNET3_STATINK_MAP: { 2: "high", }, }; + +export const SPLATOON3_TITLE_ID = "0100c2500fc20000"; From cbe7a5424ae3342b1dc20feb06515111bb4d279a Mon Sep 17 00:00:00 2001 From: rosalina Date: Sat, 13 Jan 2024 02:10:38 -0500 Subject: [PATCH 2/4] switch to polling every monitor interval --- src/app.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/app.ts b/src/app.ts index 94304cf..1ea2223 100644 --- a/src/app.ts +++ b/src/app.ts @@ -164,7 +164,7 @@ function progress({ total, currentUrl, done }: StepProgress): Progress { export class App { profile: Profile; env: Env; - isSplatoon3Active = false; + splatoon3PreviouslyActive = false; constructor(public opts: Opts) { const stateBackend = opts.stateBackend ?? @@ -401,24 +401,20 @@ export class App { async monitorWithNxapi() { this.env.logger.debug("Monitoring with nxapi presence"); await this.exportOnce(); - this.env.logger.debug("Connecting to event stream"); - const nxapiEventStream = new EventSource(this.opts.nxapiPresenceUrl!); - nxapiEventStream.addEventListener("title", (event) => { - this.env.logger.debug("Received a new title event", event, event.data) - const eventData = JSON.parse(event.data) - const newStatus = eventData.id === SPLATOON3_TITLE_ID; - if (this.isSplatoon3Active !== newStatus) { - this.env.logger.log("Splatoon 3 presence changed, new status: ", newStatus ? "active" : "inactive"); - } - this.isSplatoon3Active = newStatus; - }) while (true) { await this.countDown(this.profile.state.monitorInterval); - if (this.isSplatoon3Active) { + const nxapiResponse = await fetch(this.opts.nxapiPresenceUrl!); + const nxapiData = await nxapiResponse.json(); + const isSplatoon3Active = nxapiData.title?.id === SPLATOON3_TITLE_ID; + if (isSplatoon3Active || this.splatoon3PreviouslyActive) { this.env.logger.log("Splatoon 3 is active, exporting data"); await this.exportOnce(); } + if (isSplatoon3Active !== this.splatoon3PreviouslyActive) { + this.env.logger.debug("Splatoon 3 status has changed from", this.splatoon3PreviouslyActive, "to", isSplatoon3Active) + } + this.splatoon3PreviouslyActive = isSplatoon3Active } } async monitor() { From b8e53fc719b3730cc68f35353eee666eb068cee2 Mon Sep 17 00:00:00 2001 From: rosalina Date: Sat, 13 Jan 2024 02:34:43 -0500 Subject: [PATCH 3/4] simplify cli arguments to just nxapi-presence and add to readme --- README.md | 1 + s3si.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 89a324a..ba4d47a 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Options: ("vs", "coop") --with-summary Include summary in the output --help Show this help message and exit + --nxapi-presence Extends monitoring mode to use Nintendo Switch presence from nxapi ``` 3. If it's your first time running this, follow the instructions to login to diff --git a/s3si.ts b/s3si.ts index 186c94e..f4d3620 100644 --- a/s3si.ts +++ b/s3si.ts @@ -15,7 +15,7 @@ const parseArgs = (args: string[]) => { "skipMode": ["s", "skip-mode"], "withSummary": "with-summary", "listMethod": "list-method", - "nxapiPresenceUrl": ["nxapi-presence-url", "nxapi-presence"] + "nxapiPresenceUrl": ["nxapi-presence"] }, }); return parsed; @@ -41,7 +41,7 @@ Options: ("vs", "coop") --with-summary Include summary in the output --help Show this help message and exit - --nxapi-presence-url Extends monitoring mode to use Nintendo Switch presence from nxapi`, + --nxapi-presence Extends monitoring mode to use Nintendo Switch presence from nxapi`, ); Deno.exit(0); } From 055b1405df08b59a9fa59a059997922620d7aaf3 Mon Sep 17 00:00:00 2001 From: rosalina Date: Sun, 14 Jan 2024 02:08:23 -0500 Subject: [PATCH 4/4] run deno fmt --- s3si.ts | 10 ++++++++-- src/app.ts | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/s3si.ts b/s3si.ts index f4d3620..fa60559 100644 --- a/s3si.ts +++ b/s3si.ts @@ -4,7 +4,13 @@ import { flags } from "./deps.ts"; const parseArgs = (args: string[]) => { const parsed = flags.parse(args, { - string: ["profilePath", "exporter", "skipMode", "listMethod", "nxapiPresenceUrl"], + string: [ + "profilePath", + "exporter", + "skipMode", + "listMethod", + "nxapiPresenceUrl", + ], boolean: ["help", "noProgress", "monitor", "withSummary"], alias: { "help": "h", @@ -15,7 +21,7 @@ const parseArgs = (args: string[]) => { "skipMode": ["s", "skip-mode"], "withSummary": "with-summary", "listMethod": "list-method", - "nxapiPresenceUrl": ["nxapi-presence"] + "nxapiPresenceUrl": ["nxapi-presence"], }, }); return parsed; diff --git a/src/app.ts b/src/app.ts index 1ea2223..4651e57 100644 --- a/src/app.ts +++ b/src/app.ts @@ -412,9 +412,14 @@ export class App { await this.exportOnce(); } if (isSplatoon3Active !== this.splatoon3PreviouslyActive) { - this.env.logger.debug("Splatoon 3 status has changed from", this.splatoon3PreviouslyActive, "to", isSplatoon3Active) + this.env.logger.debug( + "Splatoon 3 status has changed from", + this.splatoon3PreviouslyActive, + "to", + isSplatoon3Active, + ); } - this.splatoon3PreviouslyActive = isSplatoon3Active + this.splatoon3PreviouslyActive = isSplatoon3Active; } } async monitor() {