Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: in-game options, flags overhaul #506

Merged
merged 14 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions components/commandService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { SyncBailHook } from "./hooksImpl"
import { log, LogLevel } from "./loggingInterop"
import { nilUuid } from "./utils"

const peacockCommandPrefix = "peacock:"

// LennardF1989: Do we want to consider string[] because of the use of URLSearchParams?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, no, maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either, your call

type CommandArgs = {
[key: string]: string /* | string[] */
}

type CommandResponseCache = {
[key: string]: unknown
}

type CommandStatus = {
CacheBuster: string
Commands: CommandResponseCache
IsConfirmed: boolean
LinkedEmail: string
IOIAccountId: string
IOIAccountBaseUrl: string
}

export type CommandFunction = (
lastResponse: unknown | undefined,
args: unknown,
) => unknown

/* export */ class CommandService {
public handleCommand: SyncBailHook<
[
/** lastResponse */ unknown,
/** command */ string,
/** args */ unknown,
],
unknown
>

private commandResponseCache: CommandResponseCache

constructor() {
this.handleCommand = new SyncBailHook()
this.commandResponseCache = {}
}

public getCommandStatus(): CommandStatus {
return {
CacheBuster: Date.now().toString(),
Commands: this.commandResponseCache,
IsConfirmed: true,
LinkedEmail: "mail@example.com",
IOIAccountId: nilUuid,
IOIAccountBaseUrl: "https://account.ioi.dk",
}
}

public submitCommands(email: string): CommandStatus {
if (!email.startsWith(peacockCommandPrefix)) {
return this.getCommandStatus()
}

try {
const commands = email
.substring(peacockCommandPrefix.length)
.split("|")

commands.forEach((c) => {
Copy link
Member Author

@LennardF1989 LennardF1989 Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a const-of, I somehow missed this or accidentally reverted it...

const commandIndex = c.indexOf("?")

let command = undefined
let args: CommandArgs

if (commandIndex < 0) {
command = c
args = {}
} else {
command = c.substring(0, commandIndex)
args = Object.fromEntries(
new URLSearchParams(c.substring(commandIndex + 1)),
)
}

if (command === "clear") {
const commands = (args.commands as string)?.split(",")

for (const c of commands) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c clashes with outer variable, why is the linter fine with this?

this.commandResponseCache[c] = undefined
}
} else {
this.commandResponseCache[command] =
this.handleCommand.call(
this.commandResponseCache[command],
command,
args,
)
}

// logDebug(command, args, this.commandResponseCache)
})
} catch (e) {
log(LogLevel.ERROR, `Failed to handle Peacock command: ${e}`)
}

return this.getCommandStatus()
}

public handleCommandMap(
name: string,
commandMap: Map<string, CommandFunction>,
) {
this.handleCommand.tap(
name,
(
lastResponse: unknown,
command: string,
args: unknown,
): unknown => {
if (commandMap.has(command)) {
return commandMap.get(command)!(lastResponse, args)
}

return undefined
},
)
}
}

export const commandService = new CommandService()
Loading