Skip to content

Commit

Permalink
Fixed relevant eslint errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinT committed Feb 21, 2020
1 parent d1b312b commit 26fc8b1
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 166 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"start": "webpack --env=dev && cd ./build && electron .",
"start:debug": "webpack --env=dev",
"build": "webpack --env=prod && node ./build-app",
"build:dev": "webpack --env=dev && node ./build-app",
"build": "yarn clean && webpack --env=prod && node ./build-app",
"build:dev": "yarn clean && webpack --env=dev && node ./build-app",
"build:proto": "pbjs -t static-module -w commonjs -p ./proto -o ./proto/ServerClientMessage.js ./proto/ServerClientMessage.proto && pbts -o ./proto/ServerClientMessage.d.ts ./proto/ServerClientMessage.js && pbjs -t static-module -w commonjs -p ./proto -o ./proto/ClientServerMessage.js ./proto/ClientServerMessage.proto && pbts -o ./proto/ClientServerMessage.d.ts ./proto/ClientServerMessage.js",
"prebuild:win": "electron-rebuild -v 2.0.17 -f -w winprocess,process-list",
"prebuild:linux": "electron-rebuild -v 2.0.17 -f -w process-list",
Expand Down
13 changes: 8 additions & 5 deletions src/main/Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export class Connector {
}

private readonly onUpdateEmulators = () => {
Emulator.updateEmulators()
Emulator.updateEmulators().catch((error) => {
console.error(error)
})
}

private readonly onCreateEmulatorConnection = (
Expand All @@ -69,19 +71,20 @@ export class Connector {
connection.sendPlayerUpdate({ username, characterId })
}

public sendPlayerUpdate(
public sendPlayerUpdate (
{ username, characterId }:
{ username: string, characterId: number }
) {
this.onPlayerUpdate({} as any, { username, characterId }) // This relies on a mock unconsumed event to pass through the same workflow
// This relies on a mock unconsumed event to pass through the same workflow so we cast as any
this.onPlayerUpdate({} as any, { username, characterId })
}

private onHotkeysChanged = (
private readonly onHotkeysChanged = (
_: Electron.Event,
{ hotkeyBindings, globalHotkeysEnabled, username }:
{ hotkeyBindings: { [shortcut: string]: string | undefined }, globalHotkeysEnabled: boolean, username: string }
) => {
if (!!username) this.hotkeyManager.username = username
if (username) { this.hotkeyManager.username = username }
this.hotkeyManager.setHotkeys(hotkeyBindings, globalHotkeysEnabled, this, this.window)
}

Expand Down
52 changes: 35 additions & 17 deletions src/main/HotkeyManager.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { globalShortcut, BrowserWindow } from 'electron'
import { Connector } from './Connector'
import { ButtonState } from '../renderer/GamepadManager'
const electronLocalshortcut = require('electron-localshortcut') // @types typings require a conflicting version of electron

// @types typings require a conflicting version of electron
// eslint-disable-next-line @typescript-eslint/no-var-requires
const electronLocalshortcut = require('electron-localshortcut')

export class HotkeyManager {
private _username = ''
public set username(value: string) { this._username = value }
public get username(): string { return this._username }
public set username (value: string) { this._username = value }
public get username (): string { return this._username }

private _hotkeysEnabled = true
public set hotkeysEnabled(value: boolean) { this._hotkeysEnabled = value }
public get hotkeysEnabled(): boolean { return this._hotkeysEnabled }
public set hotkeysEnabled (value: boolean) { this._hotkeysEnabled = value }
public get hotkeysEnabled (): boolean { return this._hotkeysEnabled }

private _characterCyclingOrder: Array<{characterId: number, on: boolean}> = []
private _characterCyclingIndex: number = 0;
private _characterCyclingIndex = 0;

private _hotkeyBindings: { [shortcut: string]: string | undefined } = {}

public validKeyboardHotkeys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

public setHotkeys (hotkeyBindings: { [shortcut: string]: string | undefined }, globalHotkeysEnabled: boolean, connector: Connector, window: BrowserWindow) {
public setHotkeys (
hotkeyBindings: { [shortcut: string]: string | undefined },
globalHotkeysEnabled: boolean,
connector: Connector,
window: BrowserWindow
) {
this._hotkeyBindings = hotkeyBindings
electronLocalshortcut.unregisterAll(window)
globalShortcut.unregisterAll()
Object.entries(hotkeyBindings).forEach(([characterIdString, hotkey]) => {
const username = this.username
if (!!hotkey) {
if (hotkey) {
if (this.isNumeric(characterIdString)) {
const callback = () => {
const characterId = parseInt(characterIdString, 10)
Expand Down Expand Up @@ -76,7 +84,7 @@ export class HotkeyManager {
} else {
electronLocalshortcut.register(window, hotkey.toLocaleUpperCase(), callback)
}
}
}
}
}
})
Expand All @@ -93,8 +101,9 @@ export class HotkeyManager {
Object.entries(this._hotkeyBindings)
.filter(([_, hotkey]) => !!hotkey && hotkey.includes('button'))
.forEach(([characterIdString, hotkey]) => {
if (!!hotkey) {
if (buttonState.some((button) => hotkey === `button${button.key}` && button.pressed)) { // Check if button was pressed
if (hotkey) {
// Check if button was pressed
if (buttonState.some((button) => hotkey === `button${button.key}` && button.pressed)) {
if (this.isNumeric(characterIdString)) { // Character hotkey
const characterId = parseInt(characterIdString, 10)
this.changeCharacter({ username, characterId, connector })
Expand All @@ -115,19 +124,25 @@ export class HotkeyManager {
}
}
}
});
})
}
}

private getNextCharacterId (characterCyclingOrder: Array<{characterId: number, on: boolean}>, characterCyclingIndex: number): number | undefined {
private getNextCharacterId (
characterCyclingOrder: Array<{characterId: number, on: boolean}>,
characterCyclingIndex: number
): number | undefined {
let nextIndex = characterCyclingOrder.findIndex((value, index) => value.on && index > characterCyclingIndex)
if (nextIndex === -1) {
nextIndex = characterCyclingOrder.findIndex((value) => value.on)
}
return nextIndex === -1 ? undefined : nextIndex
}

private getPreviousCharacterId (characterCyclingOrder: Array<{characterId: number, on: boolean}>, characterCyclingIndex: number): number | undefined {
private getPreviousCharacterId (
characterCyclingOrder: Array<{characterId: number, on: boolean}>,
characterCyclingIndex: number
): number | undefined {
let prevIndex
for (let i = characterCyclingIndex - 1; i >= 0; i--) {
const value = characterCyclingOrder[i]
Expand All @@ -149,21 +164,24 @@ export class HotkeyManager {
}

private isNumeric (value: string): boolean {
// Self comparison is needed to check complex string values
// eslint-disable-next-line no-self-compare
return +value === +value
}

/**
* @function changeCharacter - Boilerplate logic for changing a character
* @param {Object} parameters
* @param {Object} parameters - Parameters
* @property {string} parameters.username - Player username
* @property {number} parameters.characterId - Id of the character to change to
* @property {Connector} parameters.connector - Connector to emit character change data to
*/
private changeCharacter ({ username, characterId, connector }: { username: string, characterId: number, connector: Connector }) {
private changeCharacter ({ username, characterId, connector }:
{ username: string, characterId: number, connector: Connector }
) {
if (this._hotkeysEnabled) {
connector.setCharacter(characterId)
connector.sendPlayerUpdate({ username, characterId })
}
}

}
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, globalShortcut, ipcMain } from 'electron'
import { app, BrowserWindow } from 'electron'

import * as path from 'path'
import * as fs from 'fs'
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ is incompatible with your client API version (${process.env.MAJOR}.${process.env
console.info(...messages)
}

private onSetCharacter = (_: Electron.Event, characterId: number) => {
private readonly onSetCharacter = (_: Electron.Event, characterId: number) => {
store.dispatch(setCharacter(characterId))
}

Expand Down Expand Up @@ -243,5 +243,4 @@ is incompatible with your client API version (${process.env.MAJOR}.${process.env
): void {
ipcRenderer.send(RendererMessage.GAMEPAD_BUTTON_STATE_CHANGED, { buttonState })
}

}
45 changes: 26 additions & 19 deletions src/renderer/GamepadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ export type ButtonState = Array<{
}>

export class GamepadManager {
private window: Window
private connector: Connector
private readonly window: Window
private readonly connector: Connector
private _buttonState: ButtonState | undefined = undefined
private _buttonStateListeners: ((buttonState: ButtonState) => void)[] = []
private readonly _buttonStateListeners: Array<(buttonState: ButtonState) => void> = []

private _selectedGamepad: Gamepad | undefined = undefined
public set selectedGamepad(value: Gamepad | undefined) {
public set selectedGamepad (value: Gamepad | undefined) {
this._selectedGamepad = value
}
public get selectedGamepad(): Gamepad | undefined {

public get selectedGamepad (): Gamepad | undefined {
return this._selectedGamepad
}

constructor(window: Window, connector: Connector, defaultGamepadId?: string) {
constructor (window: Window, connector: Connector, defaultGamepadId?: string) {
this.window = window
this.connector = connector
this.window.addEventListener('gamepadconnected', (event) => {
Expand All @@ -41,19 +42,24 @@ export class GamepadManager {
requestAnimationFrame(this.updateState)
}

updateState() {
if (!!this.selectedGamepad) {
const gamepad = this.getConnectedGamepads().find((gamepad) => (!!gamepad ? gamepad.id : undefined) === this.selectedGamepad!.id)
if (!!gamepad) {
updateState () {
const selectedGamepad = this.selectedGamepad
if (selectedGamepad) {
const gamepad = this.getConnectedGamepads().find((gamepad) =>
(gamepad ? gamepad.id : undefined) === selectedGamepad.id
)
if (gamepad) {
// If this is the initializer, skip change detection
if (this._buttonState === undefined) {
this._buttonState = gamepad.buttons.map((gamepadButton, index) => ({ key: index, pressed: gamepadButton.pressed, value: gamepadButton.value }))
this._buttonState = gamepad.buttons.map((gamepadButton, index) =>
({ key: index, pressed: gamepadButton.pressed, value: gamepadButton.value })
)
requestAnimationFrame(this.updateState)
return;
return
}

// Detect changes in button state since last poll
let changes: ButtonState = []
const changes: ButtonState = []
for (let index = 0; index < gamepad.buttons.length; index++) {
const button = gamepad.buttons[index]
const oldButton = this._buttonState[index]
Expand All @@ -62,7 +68,9 @@ export class GamepadManager {
}
}
if (changes.length > 0) {
this._buttonState = gamepad.buttons.map((gamepadButton, index) => ({ key: index, pressed: gamepadButton.pressed, value: gamepadButton.value }))
this._buttonState = gamepad.buttons.map((gamepadButton, index) =>
({ key: index, pressed: gamepadButton.pressed, value: gamepadButton.value })
)
// Emit an event to renderer and main with the changes
this.emitButtonState(changes)
}
Expand All @@ -71,24 +79,23 @@ export class GamepadManager {
requestAnimationFrame(this.updateState)
}

private emitButtonState(buttonState: ButtonState) {
private emitButtonState (buttonState: ButtonState) {
this.connector.emitButtonState({ buttonState })
this._buttonStateListeners.forEach((callback) => callback(buttonState))
}

public addButtonStateListener(callback: ((buttonState: ButtonState) => void)) {
public addButtonStateListener (callback: ((buttonState: ButtonState) => void)) {
this._buttonStateListeners.push(callback)
}

public removeButtonStateListener(callback: ((buttonState: ButtonState) => void)) {
public removeButtonStateListener (callback: ((buttonState: ButtonState) => void)) {
const index = this._buttonStateListeners.findIndex((value) => value === callback)
if (index !== -1) {
this._buttonStateListeners.splice(index, 1)
}
}

public getConnectedGamepads() {
public getConnectedGamepads () {
return Array.from(this.window.navigator.getGamepads())
}

}
Loading

0 comments on commit 26fc8b1

Please sign in to comment.