- update data model: simplify key/data (by removing data)
This commit is contained in:
parent
b82e3a1bbf
commit
8e7208ba08
8 changed files with 221 additions and 282 deletions
|
|
@ -6,11 +6,8 @@ interface FactorioEventUpdateKey {
|
|||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdateData {
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdate extends FactorioEventUpdateKey, FactorioEventUpdateData {
|
||||
interface FactorioEventUpdate extends FactorioEventUpdateKey {
|
||||
events: string[] | null;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
|
|
@ -25,31 +22,7 @@ interface PlayerUpdateKey extends FactorioEventUpdateKey {
|
|||
index: uint;
|
||||
}
|
||||
|
||||
interface PlayerUpdateData extends FactorioEventUpdateData {
|
||||
afkTime: uint | null;
|
||||
bannedReason: string | null;
|
||||
characterUnitNumber: uint | null;
|
||||
chatColour: Colour | null;
|
||||
colour: Colour | null;
|
||||
diedCause: EntityIdentifiers | null;
|
||||
disconnectReason: string | null;
|
||||
forceIndex: uint | null;
|
||||
isAdmin: boolean | null;
|
||||
isConnected: boolean | null;
|
||||
isRemoved: boolean | null;
|
||||
isShowOnMap: boolean | null;
|
||||
isSpectator: boolean | null;
|
||||
kickedReason: string | null;
|
||||
lastOnline: uint | null;
|
||||
name: string | null;
|
||||
onlineTime: uint | null;
|
||||
position: MapEntityPosition | null;
|
||||
surfaceIndex: uint | null;
|
||||
tag: string | null;
|
||||
ticksToRespawn: uint | null;
|
||||
}
|
||||
|
||||
interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey, PlayerUpdateData {
|
||||
interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey {
|
||||
afkTime: uint | null;
|
||||
bannedReason: string | null;
|
||||
characterUnitNumber: uint | null;
|
||||
|
|
@ -57,6 +30,7 @@ interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey, PlayerUpdat
|
|||
colour: Colour | null;
|
||||
diedCause: EntityIdentifiers | null;
|
||||
disconnectReason: string | null;
|
||||
events: string[] | null;
|
||||
forceIndex: uint | null;
|
||||
index: uint;
|
||||
isAdmin: boolean | null;
|
||||
|
|
@ -81,20 +55,9 @@ interface EntityUpdateKey extends EntityIdentifiers, FactorioEventUpdateKey {
|
|||
unitNumber: uint;
|
||||
}
|
||||
|
||||
interface EntityUpdateData extends FactorioEventUpdateData {
|
||||
chunkPosition: MapChunkPosition | null;
|
||||
graphicsVariation: number | null;
|
||||
health: float | null;
|
||||
isActive: boolean | null;
|
||||
isRotatable: boolean | null;
|
||||
lastUser: int | null;
|
||||
localisedDescription: string | null;
|
||||
localisedName: string | null;
|
||||
prototype: string | null;
|
||||
}
|
||||
|
||||
interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey, EntityUpdateData {
|
||||
interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey {
|
||||
chunkPosition: MapChunkPosition | null;
|
||||
events: string[] | null;
|
||||
graphicsVariation: number | null;
|
||||
health: float | null;
|
||||
isActive: boolean | null;
|
||||
|
|
@ -114,16 +77,9 @@ interface MapChunkUpdateKey extends FactorioEventUpdateKey {
|
|||
surfaceIndex: uint;
|
||||
}
|
||||
|
||||
interface MapChunkUpdateData extends FactorioEventUpdateData {
|
||||
force: uint | null;
|
||||
isDeleted: boolean | null;
|
||||
player: uint | null;
|
||||
robot: EntityIdentifiers | null;
|
||||
tiles: MapTile[] | null;
|
||||
}
|
||||
|
||||
interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey, MapChunkUpdateData {
|
||||
interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey {
|
||||
chunkPosition: MapChunkPosition;
|
||||
events: string[] | null;
|
||||
force: uint | null;
|
||||
isDeleted: boolean | null;
|
||||
player: uint | null;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import {NonNullableKeys} from "../types";
|
||||
|
||||
export namespace EventDataCache {
|
||||
|
||||
|
||||
|
|
@ -88,8 +86,7 @@ export namespace EventDataCache {
|
|||
const expiredData: Array<FactorioEventUpdate> = []
|
||||
for (let [key, entry] of global.cache) {
|
||||
if (isExpired(entry)) {
|
||||
const update: FactorioEventUpdate = {...key, ...entry.data}
|
||||
expiredData.push(update)
|
||||
expiredData.push(entry.data)
|
||||
global.cache.delete(key)
|
||||
}
|
||||
}
|
||||
|
|
@ -104,7 +101,7 @@ export namespace EventDataCache {
|
|||
return undefined
|
||||
}
|
||||
const value: CacheEntry<any> = global.cache.get(key)
|
||||
if (isType(value, key.updateType)) {
|
||||
if (isEntryInstanceOf(value, key.updateType)) {
|
||||
return value
|
||||
} else {
|
||||
// Type mismatch. This shouldn't happen...
|
||||
|
|
@ -117,8 +114,7 @@ export namespace EventDataCache {
|
|||
key: CacheKey<TYPE>,
|
||||
mutate: CacheDataMutator<TYPE>,
|
||||
): CacheEntry<TYPE> {
|
||||
// @ts-ignore // TODO resolve ts-ignore for "not assignable to type"
|
||||
const data: CacheData<TYPE> = {updateType: key.updateType}
|
||||
const data: CacheData<TYPE> = <CacheData<TYPE>>{updateType: key.updateType}
|
||||
mutate(data)
|
||||
let entry: CacheEntry<TYPE> = new CacheEntry<TYPE>(data)
|
||||
global.cache.set(key, entry)
|
||||
|
|
@ -127,34 +123,35 @@ export namespace EventDataCache {
|
|||
|
||||
|
||||
// /** Map a {@link FactorioEventUpdateType} to a {@link FactorioEventUpdate} DTO */
|
||||
// type ConvertUpdateType<TYPE extends FactorioEventUpdateType> =
|
||||
// TYPE extends "PLAYER" ? PlayerUpdate :
|
||||
// TYPE extends "MAP_CHUNK" ? MapChunkUpdate :
|
||||
// TYPE extends "ENTITY" ? EntityUpdate :
|
||||
// never
|
||||
type ConvertToUpdate<TYPE extends FactorioEventUpdateType> =
|
||||
TYPE extends "PLAYER" ? PlayerUpdate :
|
||||
TYPE extends "MAP_CHUNK" ? MapChunkUpdate :
|
||||
TYPE extends "ENTITY" ? EntityUpdate :
|
||||
never
|
||||
|
||||
type ConvertKeyType<TYPE extends FactorioEventUpdateType> =
|
||||
type ConvertToUpdateKey<TYPE extends FactorioEventUpdateType> =
|
||||
TYPE extends "PLAYER" ? PlayerUpdateKey :
|
||||
TYPE extends "MAP_CHUNK" ? MapChunkUpdateKey :
|
||||
TYPE extends "ENTITY" ? EntityUpdateKey :
|
||||
never
|
||||
type ConvertKeyData<TYPE extends FactorioEventUpdateType> =
|
||||
TYPE extends "PLAYER" ? PlayerUpdateData :
|
||||
TYPE extends "MAP_CHUNK" ? MapChunkUpdateData :
|
||||
TYPE extends "ENTITY" ? EntityUpdateData :
|
||||
never
|
||||
// type ConvertToUpdateData<TYPE extends FactorioEventUpdateType> =
|
||||
// TYPE extends "PLAYER" ? PlayerUpdateData :
|
||||
// TYPE extends "MAP_CHUNK" ? MapChunkUpdateData :
|
||||
// TYPE extends "ENTITY" ? EntityUpdateData :
|
||||
// never
|
||||
|
||||
|
||||
/** Only include the non-null properties, and make `updateType` specific */
|
||||
/** Make `updateType` specific */
|
||||
export type CacheKey<TYPE extends FactorioEventUpdateType> =
|
||||
CacheTyped<TYPE>
|
||||
& Omit<ConvertKeyType<TYPE>, "updateType">
|
||||
& Omit<ConvertToUpdateKey<TYPE>, "updateType">
|
||||
|
||||
|
||||
/** Exclude {@link CacheKey} fields, and make `updateType` specific */
|
||||
/** Make nullable-fields optional, and `updateType` specific */
|
||||
export type CacheData<TYPE extends FactorioEventUpdateType> =
|
||||
CacheTyped<TYPE>
|
||||
& NonNullableKeys<Partial<Omit<ConvertKeyData<TYPE>, "updateType">>>
|
||||
& Omit<ConvertToUpdate<TYPE>, "updateType">
|
||||
// & NullablePartial<Omit<ConvertToUpdate<TYPE>, "updateType">>
|
||||
|
||||
|
||||
export type CacheTyped<TYPE extends FactorioEventUpdateType> = {
|
||||
|
|
@ -189,54 +186,19 @@ export namespace EventDataCache {
|
|||
}
|
||||
|
||||
|
||||
function isType<TYPE extends FactorioEventUpdateType>(
|
||||
data: CacheEntry<TYPE>,
|
||||
function isEntryInstanceOf<TYPE extends FactorioEventUpdateType>(
|
||||
data: CacheEntry<any>,
|
||||
updateType: TYPE,
|
||||
): data is CacheEntry<TYPE> {
|
||||
return data.data.updateType == updateType
|
||||
return isDataInstanceOf<TYPE>(data.data, updateType)
|
||||
}
|
||||
|
||||
|
||||
function isDataInstanceOf<TYPE extends FactorioEventUpdateType>(
|
||||
data: CacheData<any>,
|
||||
updateType: TYPE,
|
||||
): data is CacheData<TYPE> {
|
||||
return data.updateType == updateType
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// interface FactorioEventUpdate {
|
||||
// type: string
|
||||
// }
|
||||
//
|
||||
// interface Update<TYPE extends FactorioEventUpdateTypeType> {
|
||||
// type: T
|
||||
// }
|
||||
//
|
||||
// export interface PlayerUpdate extends Update<"PLAYER"> {
|
||||
// index: uint
|
||||
//
|
||||
// unhandledEvents?: string[]
|
||||
//
|
||||
// characterUnitNumber?: uint
|
||||
// chatColour?: Colour
|
||||
// colour?: Colour
|
||||
// disconnectReason?: string
|
||||
// forceIndex?: uint
|
||||
// isAdmin?: boolean
|
||||
// isConnected?: boolean
|
||||
// isShowOnMap?: boolean
|
||||
// isSpectator?: boolean
|
||||
// lastOnline?: uint
|
||||
// afkTime?: uint
|
||||
// name?: string
|
||||
// onlineTime?: uint
|
||||
// position?: MapEntityPosition
|
||||
// surfaceIndex?: uint
|
||||
// ticksToRespawn?: uint
|
||||
// tag?: string
|
||||
// }
|
||||
|
||||
// export type CacheKey<Type> = MapChunkCacheKey | EntityCacheKey | PlayerCacheKey & {
|
||||
// type: Type
|
||||
// }
|
||||
//
|
||||
// export function isCacheType<CK extends CacheKey>(
|
||||
// cacheKey: CacheKey,
|
||||
// type: CacheType
|
||||
// ): cacheKey is CK {
|
||||
// return cacheKey.type == type
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import {EventName} from "../types";
|
||||
|
||||
export namespace Converters {
|
||||
|
||||
// export function playerToTable(player: LuaPlayer): PlayerData {
|
||||
|
|
@ -15,6 +17,18 @@ export namespace Converters {
|
|||
// }
|
||||
// }
|
||||
|
||||
|
||||
const mapEventIdToName = new LuaTable<defines.Events, EventName>()
|
||||
for (const [eventName, eventId] of pairs(defines.events)) {
|
||||
mapEventIdToName.set(eventId, eventName)
|
||||
}
|
||||
|
||||
|
||||
export function eventNameString(event: defines.Events): EventName {
|
||||
return mapEventIdToName.get(event)
|
||||
}
|
||||
|
||||
|
||||
export function entityToTable(entity: LuaEntity): EntityData {
|
||||
|
||||
let player: LuaPlayer | null = entity.is_player() ? entity.player!! : null
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import {EventDataCache} from "../cache/EventDataCache";
|
||||
import {isEventType} from "./eventTypeCheck";
|
||||
import {Converters} from "./converters";
|
||||
import CacheKey = EventDataCache.CacheKey;
|
||||
import CacheData = EventDataCache.CacheData;
|
||||
|
||||
|
|
@ -11,8 +12,10 @@ function mapTilesUpdateDebounce(
|
|||
surface: LuaSurface | undefined,
|
||||
chunkPosition: ChunkPositionTable,
|
||||
tiles: TileRead[],
|
||||
updater?: MapChunkUpdater
|
||||
eventName: string,
|
||||
updater?: MapChunkUpdater,
|
||||
) {
|
||||
|
||||
if (surface == undefined) {
|
||||
return
|
||||
}
|
||||
|
|
@ -38,6 +41,13 @@ function mapTilesUpdateDebounce(
|
|||
)
|
||||
}
|
||||
|
||||
if (data.events == null) {
|
||||
data.events = []
|
||||
}
|
||||
if (eventName ! in data.events) {
|
||||
data.events.push(eventName)
|
||||
}
|
||||
|
||||
if (updater != undefined) {
|
||||
updater(data)
|
||||
}
|
||||
|
|
@ -67,7 +77,7 @@ script.on_event(
|
|||
log(`on_chunk_generated ${e.tick}`)
|
||||
|
||||
let tiles = getTiles(e.surface, e.area)
|
||||
mapTilesUpdateDebounce(e.surface, e.position, tiles)
|
||||
mapTilesUpdateDebounce(e.surface, e.position, tiles, Converters.eventNameString(e.name))
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -76,15 +86,15 @@ script.on_event(
|
|||
(e: OnChunkChartedEvent) => {
|
||||
log(`on_chunk_charted ${e.tick}`)
|
||||
|
||||
const surface = getSurface(e.surface_index)
|
||||
if (surface == undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
let tiles = getTiles(surface, e.area)
|
||||
mapTilesUpdateDebounce(surface, e.position, tiles, (data => {
|
||||
data.force = e.force.index
|
||||
}))
|
||||
// const surface = getSurface(e.surface_index)
|
||||
// if (surface == undefined) {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// let tiles = getTiles(surface, e.area)
|
||||
// mapTilesUpdateDebounce(surface, e.position, tiles, (data => {
|
||||
// data.force = e.force.index
|
||||
// }))
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -101,7 +111,7 @@ script.on_event(
|
|||
const groupedTiles = groupTiles(e.tiles)
|
||||
|
||||
for (const [chunkPos, tiles] of groupedTiles) {
|
||||
mapTilesUpdateDebounce(surface, chunkPos, tiles)
|
||||
mapTilesUpdateDebounce(surface, chunkPos, tiles, Converters.eventNameString(e.name))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
@ -146,12 +156,14 @@ function onBuildTileEvent(event: OnPlayerBuiltTileEvent | OnRobotBuiltTileEvent)
|
|||
|
||||
const tiles: TileRead[] = convertOldPosition(event.tiles, event.tile)
|
||||
const groupedTiles = groupTiles(tiles)
|
||||
const eventName = Converters.eventNameString(event.name)
|
||||
|
||||
for (const [chunkPos, tiles] of groupedTiles) {
|
||||
mapTilesUpdateDebounce(
|
||||
surface,
|
||||
chunkPos,
|
||||
tiles,
|
||||
eventName,
|
||||
(data => {
|
||||
if (isEventType(event, defines.events.on_player_built_tile)) {
|
||||
data.player = event.player_index
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ type PlayerUpdater = (player: LuaPlayer, data: CacheData<"PLAYER">) => void
|
|||
|
||||
function playerUpdateThrottle(
|
||||
playerIndex: uint,
|
||||
eventName: string,
|
||||
mutate: PlayerUpdater,
|
||||
) {
|
||||
EventDataCache.throttle<"PLAYER">(
|
||||
|
|
@ -15,6 +16,13 @@ function playerUpdateThrottle(
|
|||
if (player != undefined) {
|
||||
mutate(player, data)
|
||||
}
|
||||
|
||||
if (data.events == null) {
|
||||
data.events = []
|
||||
}
|
||||
if (eventName ! in data.events) {
|
||||
data.events.push(eventName)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
@ -41,9 +49,10 @@ script.on_event(
|
|||
(e: OnPlayerJoinedGameEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.isAdmin = player.admin
|
||||
data.characterUnitNumber = player.character?.unit_number
|
||||
data.characterUnitNumber = player.character?.unit_number ?? null
|
||||
data.chatColour = Converters.mapColour(player.chat_color)
|
||||
data.colour = Converters.mapColour(player.color)
|
||||
data.forceIndex = player.force.index
|
||||
|
|
@ -62,6 +71,7 @@ script.on_event(
|
|||
(e: OnPlayerChangedPositionEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.position = player.position
|
||||
}
|
||||
|
|
@ -74,6 +84,7 @@ script.on_event(
|
|||
(e: OnPlayerChangedSurfaceEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.position = player.position
|
||||
data.surfaceIndex = player.surface.index
|
||||
|
|
@ -87,8 +98,9 @@ script.on_event(
|
|||
(e: OnPlayerDiedEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.ticksToRespawn = player.ticks_to_respawn
|
||||
data.ticksToRespawn = player.ticks_to_respawn ?? null
|
||||
playerOnlineInfo(player, data)
|
||||
if (e.cause != undefined) {
|
||||
data.diedCause = {
|
||||
|
|
@ -102,43 +114,31 @@ script.on_event(
|
|||
}
|
||||
)
|
||||
|
||||
script.on_event(
|
||||
defines.events.on_player_banned,
|
||||
(e: OnPlayerBannedEvent) => {
|
||||
if (e.player_index != undefined) {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
(player, data) => {
|
||||
data.bannedReason = e.reason
|
||||
playerOnlineInfo(player, data)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
script.on_event(defines.events.on_player_banned, handleBannedEvent)
|
||||
script.on_event(defines.events.on_player_unbanned, handleBannedEvent)
|
||||
|
||||
script.on_event(
|
||||
defines.events.on_player_unbanned,
|
||||
(e: OnPlayerUnbannedEvent) => {
|
||||
if (e.player_index != undefined) {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
(player, data) => {
|
||||
data.bannedReason = e.reason
|
||||
playerOnlineInfo(player, data)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
function handleBannedEvent(e: OnPlayerBannedEvent | OnPlayerUnbannedEvent) {
|
||||
|
||||
if (e.player_index != undefined) {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.bannedReason = e.reason ?? null
|
||||
playerOnlineInfo(player, data)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
script.on_event(
|
||||
defines.events.on_player_kicked,
|
||||
(e: OnPlayerKickedEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.kickedReason = e.reason
|
||||
data.kickedReason = e.reason ?? null
|
||||
playerOnlineInfo(player, data)
|
||||
}
|
||||
)
|
||||
|
|
@ -156,6 +156,7 @@ script.on_event(
|
|||
(e: OnPrePlayerLeftGameEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.disconnectReason = disconnectReasons.get(e.reason)
|
||||
playerOnlineInfo(player, data)
|
||||
|
|
@ -169,6 +170,7 @@ script.on_event(
|
|||
(e: OnPlayerRemovedEvent) => {
|
||||
playerUpdateThrottle(
|
||||
e.player_index,
|
||||
Converters.eventNameString(e.name),
|
||||
(player, data) => {
|
||||
data.isRemoved = true
|
||||
playerOnlineInfo(player, data)
|
||||
|
|
|
|||
|
|
@ -3,23 +3,16 @@ import {
|
|||
handleConsoleChat,
|
||||
handleEntityUpdate,
|
||||
handleSurfaceUpdate,
|
||||
// handleTilesUpdate
|
||||
} from "./handlers";
|
||||
import {Queue} from "../queue/queue";
|
||||
import {EventName} from "../types";
|
||||
import {isEventType} from "./eventTypeCheck";
|
||||
|
||||
|
||||
const mapEventIdToName = new LuaTable<defines.Events, EventName>()
|
||||
for (const [eventName, eventId] of pairs(defines.events)) {
|
||||
mapEventIdToName.set(eventId, eventName)
|
||||
}
|
||||
import {Converters} from "./converters";
|
||||
|
||||
|
||||
script.on_event(
|
||||
defines.events.on_player_mined_entity,
|
||||
(e: OnPlayerMinedEntityEvent) => {
|
||||
let eventName = mapEventIdToName.get(e.name)
|
||||
let eventName = Converters.eventNameString(e.name)
|
||||
// handlePlayerUpdate(e.tick, eventName, e.player_index)
|
||||
handleEntityUpdate(e.tick, eventName, e.entity)
|
||||
}
|
||||
|
|
@ -44,7 +37,7 @@ script.on_event(
|
|||
(e: OnTickEvent) => {
|
||||
if (e.tick % 1000 == 0) {
|
||||
for (const [, surface] of game.surfaces) {
|
||||
handleSurfaceUpdate(e.tick, mapEventIdToName.get(e.name), surface)
|
||||
handleSurfaceUpdate(e.tick, Converters.eventNameString(e.name), surface)
|
||||
}
|
||||
|
||||
// let packets = new KafkatorioPacketQueue().dequeueValues(1)
|
||||
|
|
@ -61,7 +54,7 @@ script.on_event(
|
|||
|
||||
for (const event of events) {
|
||||
if (isEventType(event, defines.events.on_chunk_generated)) {
|
||||
let eName = mapEventIdToName.get(event.name)
|
||||
let eName = Converters.eventNameString(event.name)
|
||||
|
||||
log(`[${e.tick}] dequed event ${eName}`)
|
||||
handleChunkUpdate(e.tick, eName, event.surface.index, event.position, event.area)
|
||||
|
|
@ -75,29 +68,15 @@ script.on_event(
|
|||
script.on_event(
|
||||
defines.events.on_console_chat,
|
||||
(e: OnConsoleChatEvent) => {
|
||||
handleConsoleChat(e.tick, mapEventIdToName.get(e.name), e.player_index, e.message)
|
||||
handleConsoleChat(e.tick, Converters.eventNameString(e.name), e.player_index, e.message)
|
||||
}
|
||||
)
|
||||
|
||||
// script.on_event(
|
||||
// defines.events.on_chunk_generated,
|
||||
// (e: OnChunkGeneratedEvent) => {
|
||||
// handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area)
|
||||
// }
|
||||
// )
|
||||
//
|
||||
// script.on_event(
|
||||
// [
|
||||
// defines.events.on_player_built_tile,
|
||||
// defines.events.on_robot_built_tile,
|
||||
// ],
|
||||
// (builtTilesEvent) => {
|
||||
// handleTilesUpdate(
|
||||
// builtTilesEvent.tick,
|
||||
// mapEventIdToName.get(builtTilesEvent.name),
|
||||
// builtTilesEvent.surface_index,
|
||||
// builtTilesEvent.tile,
|
||||
// builtTilesEvent.tiles,
|
||||
// )
|
||||
// }
|
||||
// )
|
||||
// handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position,
|
||||
// e.area) } ) script.on_event( [ defines.events.on_player_built_tile,
|
||||
// defines.events.on_robot_built_tile, ], (builtTilesEvent) => { handleTilesUpdate(
|
||||
// builtTilesEvent.tick, mapEventIdToName.get(builtTilesEvent.name), builtTilesEvent.surface_index,
|
||||
// builtTilesEvent.tile, builtTilesEvent.tiles, ) } )
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package dev.adamko.kafkatorio.events.schema
|
||||
|
||||
import dev.adamko.kafkatorio.events.schema.FactorioEventUpdate.FactorioEventUpdateType
|
||||
import kotlinx.serialization.Contextual
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.EncodeDefault
|
||||
import kotlinx.serialization.Serializable
|
||||
|
|
@ -31,16 +32,19 @@ sealed interface FactorioEventUpdateKey {
|
|||
}
|
||||
|
||||
|
||||
sealed interface FactorioEventUpdateData {
|
||||
val updateType: FactorioEventUpdateType
|
||||
}
|
||||
//sealed interface FactorioEventUpdateData {
|
||||
// val updateType: FactorioEventUpdateType
|
||||
// val event: String?
|
||||
//}
|
||||
|
||||
|
||||
@Serializable(with = FactorioEventUpdate.Companion.JsonSerializer::class)
|
||||
sealed class FactorioEventUpdate : FactorioEventUpdateKey, FactorioEventUpdateData {
|
||||
sealed class FactorioEventUpdate : FactorioEventUpdateKey {
|
||||
|
||||
@EncodeDefault
|
||||
abstract override val updateType: FactorioEventUpdateType
|
||||
@Contextual
|
||||
abstract val events: List<String>?
|
||||
|
||||
@Serializable
|
||||
enum class FactorioEventUpdateType {
|
||||
|
|
@ -93,63 +97,66 @@ sealed interface PlayerUpdateKey : FactorioEventUpdateKey {
|
|||
}
|
||||
|
||||
|
||||
sealed interface PlayerUpdateData : FactorioEventUpdateData {
|
||||
val characterUnitNumber: UnitNumber?
|
||||
val chatColour: Colour?
|
||||
val colour: Colour?
|
||||
val name: String?
|
||||
|
||||
val afkTime: Tick?
|
||||
val ticksToRespawn: Tick?
|
||||
val forceIndex: ForceIndex?
|
||||
val isAdmin: Boolean?
|
||||
val isConnected: Boolean?
|
||||
val isShowOnMap: Boolean?
|
||||
val isSpectator: Boolean?
|
||||
val lastOnline: Tick?
|
||||
val onlineTime: Tick?
|
||||
val position: MapEntityPosition?
|
||||
val surfaceIndex: SurfaceIndex?
|
||||
val tag: String?
|
||||
val diedCause: EntityIdentifiers?
|
||||
|
||||
val bannedReason: String?
|
||||
val kickedReason: String?
|
||||
val disconnectReason: String?
|
||||
/** `true` when a player is removed (deleted) from the game */
|
||||
val isRemoved: Boolean?
|
||||
}
|
||||
//sealed interface PlayerUpdateData : FactorioEventUpdateData {
|
||||
// val characterUnitNumber: UnitNumber?
|
||||
// val chatColour: Colour?
|
||||
// val colour: Colour?
|
||||
// val name: String?
|
||||
//
|
||||
// val afkTime: Tick?
|
||||
// val ticksToRespawn: Tick?
|
||||
// val forceIndex: ForceIndex?
|
||||
// val isAdmin: Boolean?
|
||||
// val isConnected: Boolean?
|
||||
// val isShowOnMap: Boolean?
|
||||
// val isSpectator: Boolean?
|
||||
// val lastOnline: Tick?
|
||||
// val onlineTime: Tick?
|
||||
// val position: MapEntityPosition?
|
||||
// val surfaceIndex: SurfaceIndex?
|
||||
// val tag: String?
|
||||
// val diedCause: EntityIdentifiers?
|
||||
//
|
||||
// val bannedReason: String?
|
||||
// val kickedReason: String?
|
||||
// val disconnectReason: String?
|
||||
// /** `true` when a player is removed (deleted) from the game */
|
||||
// val isRemoved: Boolean?
|
||||
//}
|
||||
|
||||
|
||||
@Serializable
|
||||
data class PlayerUpdate(
|
||||
override val index: PlayerIndex,
|
||||
|
||||
override val characterUnitNumber: UnitNumber? = null,
|
||||
override val chatColour: Colour? = null,
|
||||
override val colour: Colour? = null,
|
||||
override val name: String? = null,
|
||||
@Contextual
|
||||
override val events: List<String>? = null,
|
||||
|
||||
override val afkTime: Tick? = null,
|
||||
override val ticksToRespawn: Tick? = null,
|
||||
override val forceIndex: ForceIndex? = null,
|
||||
override val isAdmin: Boolean? = null,
|
||||
override val isConnected: Boolean? = null,
|
||||
override val isShowOnMap: Boolean? = null,
|
||||
override val isSpectator: Boolean? = null,
|
||||
override val lastOnline: Tick? = null,
|
||||
override val onlineTime: Tick? = null,
|
||||
override val position: MapEntityPosition? = null,
|
||||
override val surfaceIndex: SurfaceIndex? = null,
|
||||
override val tag: String? = null,
|
||||
override val diedCause: EntityIdentifiers? = null,
|
||||
val characterUnitNumber: UnitNumber? = null,
|
||||
val chatColour: Colour? = null,
|
||||
val colour: Colour? = null,
|
||||
val name: String? = null,
|
||||
|
||||
override val bannedReason: String? = null,
|
||||
override val kickedReason: String? = null,
|
||||
override val disconnectReason: String? = null,
|
||||
val afkTime: Tick? = null,
|
||||
val ticksToRespawn: Tick? = null,
|
||||
val forceIndex: ForceIndex? = null,
|
||||
val isAdmin: Boolean? = null,
|
||||
val isConnected: Boolean? = null,
|
||||
val isShowOnMap: Boolean? = null,
|
||||
val isSpectator: Boolean? = null,
|
||||
val lastOnline: Tick? = null,
|
||||
val onlineTime: Tick? = null,
|
||||
val position: MapEntityPosition? = null,
|
||||
val surfaceIndex: SurfaceIndex? = null,
|
||||
val tag: String? = null,
|
||||
val diedCause: EntityIdentifiers? = null,
|
||||
|
||||
val bannedReason: String? = null,
|
||||
val kickedReason: String? = null,
|
||||
val disconnectReason: String? = null,
|
||||
/** `true` when a player is removed (deleted) from the game */
|
||||
override val isRemoved: Boolean? = null,
|
||||
) : FactorioEventUpdate(), PlayerUpdateKey, PlayerUpdateData {
|
||||
val isRemoved: Boolean? = null,
|
||||
) : FactorioEventUpdate(), PlayerUpdateKey {
|
||||
@EncodeDefault
|
||||
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.PLAYER
|
||||
}
|
||||
|
|
@ -165,17 +172,17 @@ sealed interface EntityUpdateKey : EntityIdentifiers, FactorioEventUpdateKey {
|
|||
override val type: String
|
||||
}
|
||||
|
||||
sealed interface EntityUpdateData : FactorioEventUpdateData {
|
||||
val chunkPosition: MapChunkPosition?
|
||||
val graphicsVariation: UShort?
|
||||
val health: Float?
|
||||
val isActive: Boolean?
|
||||
val isRotatable: Boolean?
|
||||
val lastUser: UInt?
|
||||
val localisedDescription: String?
|
||||
val localisedName: String?
|
||||
val prototype: PrototypeName?
|
||||
}
|
||||
//sealed interface EntityUpdateData : FactorioEventUpdateData {
|
||||
// val chunkPosition: MapChunkPosition?
|
||||
// val graphicsVariation: UShort?
|
||||
// val health: Float?
|
||||
// val isActive: Boolean?
|
||||
// val isRotatable: Boolean?
|
||||
// val lastUser: UInt?
|
||||
// val localisedDescription: String?
|
||||
// val localisedName: String?
|
||||
// val prototype: PrototypeName?
|
||||
//}
|
||||
|
||||
@Serializable
|
||||
data class EntityUpdate(
|
||||
|
|
@ -183,16 +190,19 @@ data class EntityUpdate(
|
|||
override val name: String,
|
||||
override val type: String,
|
||||
|
||||
override val chunkPosition: MapChunkPosition? = null,
|
||||
override val graphicsVariation: UShort? = null,
|
||||
override val health: Float? = null,
|
||||
override val isActive: Boolean? = null,
|
||||
override val isRotatable: Boolean? = null,
|
||||
override val lastUser: UInt? = null,
|
||||
override val localisedDescription: String? = null,
|
||||
override val localisedName: String? = null,
|
||||
override val prototype: PrototypeName? = null,
|
||||
) : FactorioEventUpdate(), EntityIdentifiers, EntityUpdateKey, EntityUpdateData {
|
||||
@Contextual
|
||||
override val events: List<String>? = null,
|
||||
|
||||
val chunkPosition: MapChunkPosition? = null,
|
||||
val graphicsVariation: UShort? = null,
|
||||
val health: Float? = null,
|
||||
val isActive: Boolean? = null,
|
||||
val isRotatable: Boolean? = null,
|
||||
val lastUser: UInt? = null,
|
||||
val localisedDescription: String? = null,
|
||||
val localisedName: String? = null,
|
||||
val prototype: PrototypeName? = null,
|
||||
) : FactorioEventUpdate(), EntityIdentifiers, EntityUpdateKey {
|
||||
@EncodeDefault
|
||||
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.ENTITY
|
||||
}
|
||||
|
|
@ -207,14 +217,13 @@ sealed interface MapChunkUpdateKey : FactorioEventUpdateKey {
|
|||
}
|
||||
|
||||
|
||||
sealed interface MapChunkUpdateData : FactorioEventUpdateData {
|
||||
val player: PlayerIndex?
|
||||
val robot: EntityIdentifiers?
|
||||
val force: ForceIndex?
|
||||
/** updated tiles - might be partial */
|
||||
val tiles: List<MapTile>?
|
||||
val isDeleted: Boolean?
|
||||
}
|
||||
//sealed interface MapChunkUpdateData : FactorioEventUpdateData {
|
||||
// val player: PlayerIndex?
|
||||
// val robot: EntityIdentifiers?
|
||||
// val force: ForceIndex?
|
||||
// val tiles: List<MapTile>?
|
||||
// val isDeleted: Boolean?
|
||||
//}
|
||||
|
||||
|
||||
@Serializable
|
||||
|
|
@ -222,14 +231,19 @@ data class MapChunkUpdate(
|
|||
override val chunkPosition: MapChunkPosition,
|
||||
override val surfaceIndex: SurfaceIndex,
|
||||
|
||||
override val player: PlayerIndex? = null,
|
||||
override val robot: EntityIdentifiers? = null,
|
||||
override val force: ForceIndex? = null,
|
||||
@Contextual
|
||||
override val events: List<String>? = null,
|
||||
|
||||
val player: PlayerIndex? = null,
|
||||
val robot: EntityIdentifiers? = null,
|
||||
val force: ForceIndex? = null,
|
||||
/** updated tiles - might be partial */
|
||||
// note: this must be a List, not a Map<Location, ProtoName>
|
||||
// because JSON can't have non-string keys.
|
||||
override val tiles: List<MapTile>? = null,
|
||||
override val isDeleted: Boolean? = null,
|
||||
) : FactorioEventUpdate(), MapChunkUpdateKey, MapChunkUpdateData {
|
||||
@Contextual
|
||||
val tiles: List<MapTile>? = null,
|
||||
val isDeleted: Boolean? = null,
|
||||
) : FactorioEventUpdate(), MapChunkUpdateKey {
|
||||
@EncodeDefault
|
||||
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.MAP_CHUNK
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ fun main(args: Array<String>) {
|
|||
add(FactorioEventUpdatePacket::class)
|
||||
addAll(FactorioEventUpdate::class.sealedSubclasses)
|
||||
addAll(FactorioEventUpdateKey::class.sealedSubclasses)
|
||||
addAll(FactorioEventUpdateData::class.sealedSubclasses)
|
||||
// addAll(FactorioEventUpdateData::class.sealedSubclasses)
|
||||
|
||||
},
|
||||
mappings = mapOf(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue