event stream implementation
(committing this right now because event streams seem broke)nxapi-presence
parent
cc348653d6
commit
81a228cfb8
6
s3si.ts
6
s3si.ts
|
|
@ -4,7 +4,7 @@ import { flags } from "./deps.ts";
|
||||||
|
|
||||||
const parseArgs = (args: string[]) => {
|
const parseArgs = (args: string[]) => {
|
||||||
const parsed = flags.parse(args, {
|
const parsed = flags.parse(args, {
|
||||||
string: ["profilePath", "exporter", "skipMode", "listMethod"],
|
string: ["profilePath", "exporter", "skipMode", "listMethod", "nxapiPresenceUrl"],
|
||||||
boolean: ["help", "noProgress", "monitor", "withSummary"],
|
boolean: ["help", "noProgress", "monitor", "withSummary"],
|
||||||
alias: {
|
alias: {
|
||||||
"help": "h",
|
"help": "h",
|
||||||
|
|
@ -15,6 +15,7 @@ const parseArgs = (args: string[]) => {
|
||||||
"skipMode": ["s", "skip-mode"],
|
"skipMode": ["s", "skip-mode"],
|
||||||
"withSummary": "with-summary",
|
"withSummary": "with-summary",
|
||||||
"listMethod": "list-method",
|
"listMethod": "list-method",
|
||||||
|
"nxapiPresenceUrl": ["nxapi-presence-url", "nxapi-presence"]
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return parsed;
|
return parsed;
|
||||||
|
|
@ -39,7 +40,8 @@ Options:
|
||||||
--skip-mode <mode>, -s Skip mode (default: null)
|
--skip-mode <mode>, -s Skip mode (default: null)
|
||||||
("vs", "coop")
|
("vs", "coop")
|
||||||
--with-summary Include summary in the output
|
--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);
|
Deno.exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
30
src/app.ts
30
src/app.ts
|
|
@ -9,6 +9,7 @@ import { FileExporter } from "./exporters/file.ts";
|
||||||
import { delay, showError } from "./utils.ts";
|
import { delay, showError } from "./utils.ts";
|
||||||
import { GameFetcher } from "./GameFetcher.ts";
|
import { GameFetcher } from "./GameFetcher.ts";
|
||||||
import { DEFAULT_ENV, Env } from "./env.ts";
|
import { DEFAULT_ENV, Env } from "./env.ts";
|
||||||
|
import { SPLATOON3_TITLE_ID } from "./constant.ts";
|
||||||
|
|
||||||
export type Opts = {
|
export type Opts = {
|
||||||
profilePath: string;
|
profilePath: string;
|
||||||
|
|
@ -21,6 +22,7 @@ export type Opts = {
|
||||||
cache?: Cache;
|
cache?: Cache;
|
||||||
stateBackend?: StateBackend;
|
stateBackend?: StateBackend;
|
||||||
env: Env;
|
env: Env;
|
||||||
|
nxapiPresenceUrl?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DEFAULT_OPTS: Opts = {
|
export const DEFAULT_OPTS: Opts = {
|
||||||
|
|
@ -162,6 +164,7 @@ function progress({ total, currentUrl, done }: StepProgress): Progress {
|
||||||
export class App {
|
export class App {
|
||||||
profile: Profile;
|
profile: Profile;
|
||||||
env: Env;
|
env: Env;
|
||||||
|
isSplatoon3Active = false;
|
||||||
|
|
||||||
constructor(public opts: Opts) {
|
constructor(public opts: Opts) {
|
||||||
const stateBackend = opts.stateBackend ??
|
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() {
|
async monitor() {
|
||||||
while (true) {
|
while (true) {
|
||||||
await this.exportOnce();
|
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();
|
await this.monitor();
|
||||||
} else {
|
} else {
|
||||||
await this.exportOnce();
|
await this.exportOnce();
|
||||||
|
|
|
||||||
|
|
@ -116,3 +116,5 @@ export const SPLATNET3_STATINK_MAP: {
|
||||||
2: "high",
|
2: "high",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SPLATOON3_TITLE_ID = "0100c2500fc20000";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue