2023-06-13 02:43:52 +02:00
|
|
|
import type { PJ } from '@lastres/pj'
|
|
|
|
import type { Location } from '@lastres/location'
|
|
|
|
import type InputPacket from '@lastres/input-packet'
|
|
|
|
import type { LogLine } from '@lastres/log-line'
|
2023-06-30 19:59:18 +02:00
|
|
|
import type { ActionHash } from '@lastres/action'
|
2023-07-09 18:41:21 +02:00
|
|
|
import type { TalkNPCs } from '@lastres/talk-npc'
|
2023-07-13 10:28:24 +02:00
|
|
|
import type { Words } from '@lastres/word'
|
|
|
|
|
2023-06-13 02:43:52 +02:00
|
|
|
import InputPacketInfo from '@lastres/input-packet/info'
|
|
|
|
import InputPacketPong from '@lastres/input-packet/pong'
|
2023-06-18 20:29:42 +02:00
|
|
|
|
2023-06-13 02:43:52 +02:00
|
|
|
type SetTeamPJs = (set: PJ[] | null) => void
|
2023-06-27 07:10:56 +02:00
|
|
|
type SetIsBattling = (set: boolean | null) => void
|
|
|
|
type SetEnemyTeamPJs = (set: PJ[] | null) => void
|
2023-06-13 02:43:52 +02:00
|
|
|
type SetCurrentLocation = (set: Location | null) => void
|
|
|
|
type SetConnectedLocations = (set: Location[] | null) => void
|
|
|
|
type DispatchHash = Record<string, any>
|
2023-06-18 20:29:42 +02:00
|
|
|
type SetLogLinesCallback = (set: LogLine[] | null) => LogLine[]
|
|
|
|
type SetLogLines = (set: LogLine[] | SetLogLinesCallback | null) => void
|
|
|
|
type SetError = (set: string | null) => void
|
2023-06-19 17:15:00 +02:00
|
|
|
type SetScrollLogCallback = (set: number | null) => number | null
|
|
|
|
type SetScrollLog = (set: number | null | SetScrollLogCallback) => void
|
|
|
|
type LogPresentationRef = React.RefObject<HTMLDivElement>
|
2023-06-23 01:39:47 +02:00
|
|
|
type SetMovingTo = (set: Location | null) => void
|
2023-06-23 02:31:11 +02:00
|
|
|
type SetRemainingFrames = (set: number | null) => void
|
2023-06-30 19:59:18 +02:00
|
|
|
type SetActionHash = (set: ActionHash | null) => void
|
2023-07-09 18:41:21 +02:00
|
|
|
type SetTalkNPCs = (set: TalkNPCs | null) => void
|
2023-07-13 10:28:24 +02:00
|
|
|
type SetWords = (set: Words | null) => void
|
2023-06-18 20:29:42 +02:00
|
|
|
|
2023-06-13 02:43:52 +02:00
|
|
|
interface Packet {
|
|
|
|
command: string
|
|
|
|
data: any
|
|
|
|
}
|
2023-06-18 20:29:42 +02:00
|
|
|
type LogHash = Record<string, LogLine>
|
|
|
|
|
2023-06-13 02:43:52 +02:00
|
|
|
export default class InputPackets {
|
|
|
|
setTeamPJs: SetTeamPJs
|
2023-06-27 07:10:56 +02:00
|
|
|
setEnemyTeamPJs: SetEnemyTeamPJs
|
|
|
|
setIsBattling: SetIsBattling
|
2023-06-13 02:43:52 +02:00
|
|
|
setCurrentLocation: SetCurrentLocation
|
|
|
|
setConnectedLocations: SetConnectedLocations
|
|
|
|
setLogLines: SetLogLines
|
2023-06-18 20:29:42 +02:00
|
|
|
logLines: LogLine[] | null
|
2023-06-13 02:43:52 +02:00
|
|
|
cachedHash: DispatchHash | null = null
|
|
|
|
cachedArray: InputPacket[] | null = null
|
2023-06-18 20:29:42 +02:00
|
|
|
setError: SetError
|
2023-06-19 17:15:00 +02:00
|
|
|
setScrollLog: SetScrollLog
|
|
|
|
logPresentationRef: LogPresentationRef
|
2023-06-23 01:39:47 +02:00
|
|
|
setMovingTo: SetMovingTo
|
2023-06-23 02:31:11 +02:00
|
|
|
setRemainingFrames: SetRemainingFrames
|
2023-06-30 19:59:18 +02:00
|
|
|
setActionHash: SetActionHash
|
2023-07-09 18:41:21 +02:00
|
|
|
setTalkNPCs: SetTalkNPCs
|
2023-07-13 10:28:24 +02:00
|
|
|
setWords: SetWords
|
2023-06-13 02:43:52 +02:00
|
|
|
constructor (setTeamPJs: SetTeamPJs,
|
2023-06-27 07:10:56 +02:00
|
|
|
setEnemyTeamPJs: SetEnemyTeamPJs,
|
|
|
|
setIsBattling: SetIsBattling,
|
2023-06-13 02:43:52 +02:00
|
|
|
setCurrentLocation: SetCurrentLocation,
|
|
|
|
setConnectedLocations: SetConnectedLocations,
|
2023-06-18 20:29:42 +02:00
|
|
|
logLines: LogLine[] | null,
|
|
|
|
setLogLines: SetLogLines,
|
2023-06-19 17:15:00 +02:00
|
|
|
setError: SetError,
|
|
|
|
setScrollLog: SetScrollLog,
|
2023-06-23 01:39:47 +02:00
|
|
|
logPresentationRef: LogPresentationRef,
|
2023-06-23 02:31:11 +02:00
|
|
|
setMovingTo: SetMovingTo,
|
2023-06-30 19:59:18 +02:00
|
|
|
setRemainingFrames: SetRemainingFrames,
|
2023-07-09 18:41:21 +02:00
|
|
|
setActionHash: SetActionHash,
|
2023-07-13 10:28:24 +02:00
|
|
|
setTalkNPCs: SetTalkNPCs,
|
|
|
|
setWords: SetWords) {
|
2023-06-13 02:43:52 +02:00
|
|
|
this.setTeamPJs = setTeamPJs
|
2023-06-27 07:10:56 +02:00
|
|
|
this.setEnemyTeamPJs = setEnemyTeamPJs
|
2023-06-13 02:43:52 +02:00
|
|
|
this.setCurrentLocation = setCurrentLocation
|
|
|
|
this.setConnectedLocations = setConnectedLocations
|
2023-06-18 20:29:42 +02:00
|
|
|
this.logLines = logLines
|
2023-06-13 02:43:52 +02:00
|
|
|
this.setLogLines = setLogLines
|
2023-06-18 20:29:42 +02:00
|
|
|
this.setError = setError
|
2023-06-19 17:15:00 +02:00
|
|
|
this.setScrollLog = setScrollLog
|
|
|
|
this.logPresentationRef = logPresentationRef
|
2023-06-23 01:39:47 +02:00
|
|
|
this.setMovingTo = setMovingTo
|
2023-06-23 02:31:11 +02:00
|
|
|
this.setRemainingFrames = setRemainingFrames
|
2023-06-27 07:10:56 +02:00
|
|
|
this.setIsBattling = setIsBattling
|
2023-06-30 19:59:18 +02:00
|
|
|
this.setActionHash = setActionHash
|
2023-07-09 18:41:21 +02:00
|
|
|
this.setTalkNPCs = setTalkNPCs
|
2023-07-13 10:28:24 +02:00
|
|
|
this.setWords = setWords
|
2023-06-13 02:43:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handle (packet: Packet): void {
|
|
|
|
const hash = this.hashAvailablePackets()
|
|
|
|
const identifier = packet.command
|
|
|
|
const inputPacket = hash[identifier]
|
|
|
|
inputPacket.recv(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
listAvailablePackets (): InputPacket[] {
|
2023-06-19 17:15:00 +02:00
|
|
|
let firstTime = true
|
2023-06-13 02:43:52 +02:00
|
|
|
if (this.cachedArray === null) {
|
|
|
|
const infoPacket = new InputPacketInfo()
|
|
|
|
const pongPacket = new InputPacketPong()
|
|
|
|
infoPacket.onReceive((data) => {
|
2023-06-19 17:15:00 +02:00
|
|
|
const logPresentationRef = this.logPresentationRef
|
|
|
|
let scrollData: number[] = []
|
|
|
|
function saveScroll (): void {
|
|
|
|
if (logPresentationRef.current === null) {
|
|
|
|
return
|
|
|
|
}
|
2023-07-08 00:09:04 +02:00
|
|
|
scrollData = [
|
|
|
|
logPresentationRef.current.scrollHeight,
|
|
|
|
logPresentationRef.current.scrollTop,
|
|
|
|
logPresentationRef.current.offsetHeight
|
|
|
|
]
|
2023-06-19 17:15:00 +02:00
|
|
|
}
|
|
|
|
function applyScroll (): void {
|
|
|
|
if (scrollData.length < 3) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (logPresentationRef.current === null) {
|
2023-06-20 01:48:55 +02:00
|
|
|
console.log('Not defined')
|
2023-06-19 17:15:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const logPresentation = logPresentationRef.current
|
|
|
|
const [scrollHeight, scrollTop, offsetHeight] = scrollData
|
|
|
|
if (firstTime) {
|
|
|
|
firstTime = false
|
2023-06-19 17:22:15 +02:00
|
|
|
logPresentation.scrollTo(0, logPresentation.scrollHeight)
|
2023-06-19 17:15:00 +02:00
|
|
|
return
|
|
|
|
}
|
2023-06-26 21:26:31 +02:00
|
|
|
if (scrollHeight <= scrollTop + offsetHeight * (3 / 2)) {
|
2023-06-19 17:15:00 +02:00
|
|
|
logPresentation.scrollTo(0, logPresentation.scrollHeight)
|
|
|
|
}
|
|
|
|
}
|
2023-06-18 20:29:42 +02:00
|
|
|
if (data.error !== undefined) {
|
|
|
|
this.setError(data.error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (data.team_pjs !== undefined) {
|
|
|
|
this.setTeamPJs(data.team_pjs)
|
|
|
|
}
|
2023-06-27 07:10:56 +02:00
|
|
|
if (data.enemy_team_pjs !== undefined) {
|
|
|
|
this.setEnemyTeamPJs(data.enemy_team_pjs)
|
|
|
|
}
|
|
|
|
if (data.is_battling !== undefined) {
|
|
|
|
this.setIsBattling(data.is_battling)
|
|
|
|
}
|
2023-06-23 01:39:47 +02:00
|
|
|
if (data.location_data !== undefined) {
|
|
|
|
console.log(data.location_data)
|
|
|
|
if (data.location_data.connected_places !== undefined) {
|
|
|
|
this.setConnectedLocations(data.location_data.connected_places)
|
|
|
|
}
|
|
|
|
if (data.location_data.current !== undefined) {
|
|
|
|
this.setCurrentLocation(data.location_data.current)
|
|
|
|
}
|
|
|
|
if (data.location_data.moving_to !== undefined) {
|
|
|
|
this.setMovingTo(data.location_data.moving_to)
|
|
|
|
} else {
|
|
|
|
this.setMovingTo(null)
|
|
|
|
}
|
2023-06-18 20:29:42 +02:00
|
|
|
}
|
2023-07-09 18:41:21 +02:00
|
|
|
if (data.npcs !== undefined) {
|
|
|
|
this.setTalkNPCs(data.npcs)
|
|
|
|
}
|
2023-06-23 02:31:11 +02:00
|
|
|
if (data.remaining_frames !== undefined) {
|
|
|
|
this.setRemainingFrames(data.remaining_frames)
|
|
|
|
}
|
2023-06-18 20:29:42 +02:00
|
|
|
if (data.set_log !== undefined) {
|
2023-06-19 17:15:00 +02:00
|
|
|
saveScroll()
|
2023-06-18 20:29:42 +02:00
|
|
|
this.setLogLines(data.set_log)
|
2023-06-19 17:15:00 +02:00
|
|
|
window.setTimeout(() => {
|
|
|
|
applyScroll()
|
|
|
|
}, 10)
|
2023-06-18 20:29:42 +02:00
|
|
|
}
|
2023-07-13 10:28:24 +02:00
|
|
|
if (data.known_words !== undefined) {
|
2023-07-13 19:02:29 +02:00
|
|
|
console.log(data.known_words);
|
2023-07-13 10:28:24 +02:00
|
|
|
this.setWords(data.known_words)
|
|
|
|
}
|
2023-06-18 20:29:42 +02:00
|
|
|
if (data.append_log !== undefined) {
|
|
|
|
const logHash: LogHash = {}
|
2023-06-19 17:15:00 +02:00
|
|
|
saveScroll()
|
2023-06-18 20:29:42 +02:00
|
|
|
this.setLogLines((logLines: LogLine[] | null): LogLine[] => {
|
|
|
|
if (logLines !== null) {
|
|
|
|
for (const item of logLines) {
|
|
|
|
logHash[item.uuid] = item
|
|
|
|
}
|
|
|
|
logHash[data.append_log.uuid] = data.append_log
|
|
|
|
const outputLog: LogLine[] = Object.keys(logHash).map((item, i): LogLine => {
|
|
|
|
return logHash[item]
|
|
|
|
})
|
|
|
|
return outputLog
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
})
|
2023-06-19 17:15:00 +02:00
|
|
|
window.setTimeout(() => {
|
|
|
|
applyScroll()
|
|
|
|
}, 10)
|
2023-06-18 20:29:42 +02:00
|
|
|
}
|
2023-06-30 19:59:18 +02:00
|
|
|
if (data.available_actions !== undefined) {
|
|
|
|
this.setActionHash(data.available_actions)
|
|
|
|
}
|
2023-06-13 02:43:52 +02:00
|
|
|
})
|
|
|
|
this.cachedArray = [infoPacket, pongPacket]
|
|
|
|
}
|
|
|
|
return this.cachedArray
|
|
|
|
}
|
|
|
|
|
|
|
|
hashAvailablePackets (): DispatchHash {
|
|
|
|
if (this.cachedHash === null) {
|
|
|
|
this.cachedHash = {}
|
|
|
|
for (const inputPacket of this.listAvailablePackets()) {
|
|
|
|
this.cachedHash[inputPacket.identifier()] = inputPacket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.cachedHash
|
|
|
|
}
|
|
|
|
}
|