UNPKG

@krp-races/krp-sharedmemory-client

Version:

A node.js wrapper for shared memory plugin in kart racing pro.

118 lines (115 loc) 3.74 kB
import EventEmitter from 'events'; import { addon } from './binding.mjs'; import { EGameState } from './enums/GameState.mjs'; export { EChallengeSessionType } from './enums/ChallengeSessionType.mjs'; export { ECommunicationOffence } from './enums/CommunicationOffence.mjs'; export { ECommunicationPenaltyType } from './enums/CommunicationPenaltyType.mjs'; export { ECommunicationReason } from './enums/CommunicationReason.mjs'; export { ECommunicationType } from './enums/CommunicationType.mjs'; export { EDriveType } from './enums/DriveType.mjs'; export { EEngineCooling } from './enums/EngineCooling.mjs'; export { EEntryState } from './enums/EntryState.mjs'; export { EEventType } from './enums/EventType.mjs'; export { EPracticeSessionState } from './enums/PracticeSessionState.mjs'; export { ERaceSessionState } from './enums/RaceSessionState.mjs'; export { ERaceSessionType } from './enums/RaceSessionType.mjs'; export { ETrackSegmentType } from './enums/TrackSegmentType.mjs'; export { EWeatherCondition } from './enums/WeatherCondition.mjs'; import 'module'; class KRPSharedMemoryClient extends EventEmitter { constructor(retryInterval = 5000, updateInterval = 100) { super(); this.activated = false; this.connected = false; this.sequenceNumber = -1; this.retryInterval = retryInterval; this.updateInterval = updateInterval; } activate() { this.activated = true; this.emit('activated'); this.connect(); } deactivate() { this.activated = false; this.emit('deactivated'); this.disconnect(); } isActivated() { return this.activated; } isConnected() { return this.connected; } connect() { if (!this.activated || this.connected) return; try { addon.connect(); if (!addon.isConnected()) { setTimeout(this.connect.bind(this), this.retryInterval); return; } this.connected = true; this.emit('connected'); this.update(); } catch (e) { if (e instanceof Error) this.emit('error', e.message); setTimeout(this.connect.bind(this), this.retryInterval); } } disconnect() { if (!this.connected) return; try { addon.disconnect(); } catch (e) { if (e instanceof Error) this.emit('error', e.message); } this.sequenceNumber = -1; this.connected = false; this.emit('disconnected'); } update() { if (!this.connected) return; let data; try { data = addon.read(); } catch (e) { if (e instanceof Error) this.emit('error', e.message); } if (!data) { this.disconnect(); this.connect(); return; } if (data.gameState === EGameState.Closed) { this.disconnect(); this.connect(); return; } if (data.sequenceNumber % 2 == 0 && this.sequenceNumber != data.sequenceNumber) { this.sequenceNumber = data.sequenceNumber; this.emit('update', data); } setTimeout(this.update.bind(this), this.updateInterval); } requestSpectateVehicle(id) { if (!this.connected) return; addon.requestSpectateVehicle(id); } requestCamera(id) { if (!this.connected) return; addon.requestCamera(id); } } export { EGameState, KRPSharedMemoryClient, KRPSharedMemoryClient as default };