- make each event update have a key/data interface
- separate .d.ts file for event updates - fix initialisation of global items
This commit is contained in:
parent
494913d3b9
commit
5208d9affd
14 changed files with 451 additions and 266 deletions
|
|
@ -1,16 +1,17 @@
|
|||
import {emitConfigurationUpdate} from "./main/config-update/handleConfigurationUpdate";
|
||||
import {emitPrototypes} from "./main/config-update/prototypes";
|
||||
import {Queue} from "./main/queue/queue";
|
||||
import {EventDataCache} from "./main/cache/EventDataCache";
|
||||
import {initGlobal} from "./main/global-init";
|
||||
|
||||
require("./main/events/registerHandlers")
|
||||
require("./main/events/playerEvents")
|
||||
require("./main/events/mapEvents")
|
||||
require("./main/commands/kafkatorioCommands")
|
||||
require("./main/cache/cacheChecker")
|
||||
|
||||
script.on_configuration_changed((data: ConfigurationChangedData) => {
|
||||
// runs whenever the version of the base game, or a mod, changes
|
||||
|
||||
reInitGlobal()
|
||||
initGlobal(true)
|
||||
emitConfigurationUpdate(data)
|
||||
emitPrototypes()
|
||||
})
|
||||
|
|
@ -18,27 +19,5 @@ script.on_configuration_changed((data: ConfigurationChangedData) => {
|
|||
|
||||
script.on_init(() => {
|
||||
// runs only once (when the mod is first added to the save)
|
||||
initGlobal()
|
||||
initGlobal(true)
|
||||
})
|
||||
|
||||
|
||||
function initGlobal() {
|
||||
log("Initialising Kafkatorio Global variables...")
|
||||
|
||||
Queue.init()
|
||||
EventDataCache.init()
|
||||
|
||||
log("Finished initialising Kafkatorio Global variables")
|
||||
}
|
||||
|
||||
function reInitGlobal() {
|
||||
log("Re-initialising Kafkatorio Global variables...")
|
||||
|
||||
global.MOD_VERSION = script.active_mods[script.mod_name]
|
||||
global.FACTORIO_VERSION = script.active_mods["base"]
|
||||
|
||||
Queue.reset()
|
||||
EventDataCache.reset()
|
||||
|
||||
log("Finished re-initialising Kafkatorio Global variables")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,33 @@ interface FactorioObjectData {
|
|||
objectName: ObjectName;
|
||||
}
|
||||
|
||||
interface PlayerData extends FactorioObjectData, FactorioLuaControl {
|
||||
associatedCharactersUnitNumbers: uint[];
|
||||
characterUnitNumber: uint | null;
|
||||
chatColour: Colour;
|
||||
colour: Colour;
|
||||
force: uint;
|
||||
lastOnline: uint;
|
||||
name: string;
|
||||
objectName: ObjectName;
|
||||
position: MapEntityPosition;
|
||||
surface: uint;
|
||||
}
|
||||
|
||||
interface EntityData extends FactorioObjectData, FactorioLuaControl {
|
||||
active: boolean;
|
||||
force: uint;
|
||||
health: double | null;
|
||||
healthRatio: double;
|
||||
name: string;
|
||||
objectName: ObjectName;
|
||||
playerIndex: uint | null;
|
||||
position: MapEntityPosition;
|
||||
surface: uint;
|
||||
type: string;
|
||||
unitNumber: uint | null;
|
||||
}
|
||||
|
||||
interface SurfaceData extends FactorioObjectData {
|
||||
daytime: double;
|
||||
index: uint;
|
||||
|
|
|
|||
|
|
@ -7,19 +7,6 @@ interface KafkatorioPacket {
|
|||
packetType: PacketType;
|
||||
}
|
||||
|
||||
type FactorioEventUpdateType = "PLAYER" | "MAP_CHUNK" | "ENTITY";
|
||||
|
||||
interface FactorioEventUpdate {
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdatePacket extends KafkatorioPacket {
|
||||
modVersion: string;
|
||||
packetType: PacketType;
|
||||
tick: uint;
|
||||
update: FactorioEventUpdate;
|
||||
}
|
||||
|
||||
interface FactorioEvent extends KafkatorioPacket {
|
||||
data: FactorioObjectData;
|
||||
eventType: string;
|
||||
|
|
@ -46,33 +33,6 @@ interface Colour {
|
|||
red: float;
|
||||
}
|
||||
|
||||
interface PlayerData extends FactorioObjectData, FactorioLuaControl {
|
||||
associatedCharactersUnitNumbers: uint[];
|
||||
characterUnitNumber: uint | null;
|
||||
chatColour: Colour;
|
||||
colour: Colour;
|
||||
force: uint;
|
||||
lastOnline: uint;
|
||||
name: string;
|
||||
objectName: ObjectName;
|
||||
position: MapEntityPosition;
|
||||
surface: uint;
|
||||
}
|
||||
|
||||
interface EntityData extends FactorioObjectData, FactorioLuaControl {
|
||||
active: boolean;
|
||||
force: uint;
|
||||
health: double | null;
|
||||
healthRatio: double;
|
||||
name: string;
|
||||
objectName: ObjectName;
|
||||
playerIndex: uint | null;
|
||||
position: MapEntityPosition;
|
||||
surface: uint;
|
||||
type: string;
|
||||
unitNumber: uint | null;
|
||||
}
|
||||
|
||||
interface MapTilePosition {
|
||||
x: int;
|
||||
y: int;
|
||||
|
|
@ -99,56 +59,3 @@ interface EntityIdentifiers {
|
|||
type: string;
|
||||
unitNumber: uint | null;
|
||||
}
|
||||
|
||||
interface PlayerUpdate extends FactorioEventUpdate {
|
||||
afkTime: uint | null;
|
||||
bannedReason: string | null;
|
||||
characterUnitNumber: uint | null;
|
||||
chatColour: Colour | null;
|
||||
colour: Colour | null;
|
||||
diedCause: EntityIdentifiers | null;
|
||||
disconnectReason: string | null;
|
||||
forceIndex: uint | null;
|
||||
index: uint;
|
||||
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;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers {
|
||||
chunkPosition: MapChunkPosition | null;
|
||||
graphicsVariation: number | null;
|
||||
health: float | null;
|
||||
isActive: boolean | null;
|
||||
isRotatable: boolean | null;
|
||||
lastUser: int | null;
|
||||
localisedDescription: string | null;
|
||||
localisedName: string | null;
|
||||
name: string;
|
||||
prototype: string | null;
|
||||
type: string;
|
||||
unitNumber: uint;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface MapChunkUpdate extends FactorioEventUpdate {
|
||||
chunkPosition: MapChunkPosition;
|
||||
force: uint | null;
|
||||
isDeleted: boolean | null;
|
||||
player: uint | null;
|
||||
robot: EntityIdentifiers | null;
|
||||
surfaceIndex: uint;
|
||||
tiles: Map<MapTilePosition, string> | null;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
|
|
|||
134
modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts
vendored
Normal file
134
modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts
vendored
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
// Generated by TypeScriptGenerator - do not edit this file manually
|
||||
|
||||
type FactorioEventUpdateType = "PLAYER" | "MAP_CHUNK" | "ENTITY";
|
||||
|
||||
interface FactorioEventUpdateKey {
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdateData {
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdate extends FactorioEventUpdateKey, FactorioEventUpdateData {
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface FactorioEventUpdatePacket extends KafkatorioPacket {
|
||||
modVersion: string;
|
||||
packetType: PacketType;
|
||||
tick: uint;
|
||||
update: FactorioEventUpdate;
|
||||
}
|
||||
|
||||
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 {
|
||||
afkTime: uint | null;
|
||||
bannedReason: string | null;
|
||||
characterUnitNumber: uint | null;
|
||||
chatColour: Colour | null;
|
||||
colour: Colour | null;
|
||||
diedCause: EntityIdentifiers | null;
|
||||
disconnectReason: string | null;
|
||||
forceIndex: uint | null;
|
||||
index: uint;
|
||||
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;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface EntityUpdateKey extends EntityIdentifiers, FactorioEventUpdateKey {
|
||||
name: string;
|
||||
type: string;
|
||||
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 {
|
||||
chunkPosition: MapChunkPosition | null;
|
||||
graphicsVariation: number | null;
|
||||
health: float | null;
|
||||
isActive: boolean | null;
|
||||
isRotatable: boolean | null;
|
||||
lastUser: int | null;
|
||||
localisedDescription: string | null;
|
||||
localisedName: string | null;
|
||||
name: string;
|
||||
prototype: string | null;
|
||||
type: string;
|
||||
unitNumber: uint;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
||||
interface MapChunkUpdateKey extends FactorioEventUpdateKey {
|
||||
chunkPosition: MapChunkPosition;
|
||||
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 {
|
||||
chunkPosition: MapChunkPosition;
|
||||
force: uint | null;
|
||||
isDeleted: boolean | null;
|
||||
player: uint | null;
|
||||
robot: EntityIdentifiers | null;
|
||||
surfaceIndex: uint;
|
||||
tiles: MapTile[] | null;
|
||||
updateType: FactorioEventUpdateType;
|
||||
}
|
||||
|
|
@ -1,32 +1,34 @@
|
|||
import {ExcludeNullKeys, NonNullableKeys} from "../types";
|
||||
import {NonNullableKeys} from "../types";
|
||||
|
||||
export namespace EventDataCache {
|
||||
|
||||
|
||||
declare const global: {
|
||||
cache: LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>,
|
||||
DEFAULT_CACHE_DURATION_TICKS2: Record<FactorioEventUpdateType | "default", uint>,
|
||||
defaultCacheDurationTicks: Record<FactorioEventUpdateType | "default", uint>,
|
||||
}
|
||||
|
||||
|
||||
export function reset() {
|
||||
log("Resetting EventDataCache")
|
||||
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
|
||||
}
|
||||
export function init(force?: boolean) {
|
||||
|
||||
const isAnythingUndefined = global.cache == undefined ||
|
||||
global.defaultCacheDurationTicks == undefined
|
||||
|
||||
export function init() {
|
||||
if (typeof global.cache == undefined) {
|
||||
log("Initialising EventDataCache")
|
||||
reset()
|
||||
log(`Initialising EventDataCache (force=${force}, isAnythingUndefined=${isAnythingUndefined})...`)
|
||||
|
||||
global.DEFAULT_CACHE_DURATION_TICKS2 = {
|
||||
"PLAYER": 30, // 0.5 seconds
|
||||
"MAP_CHUNK": 60 * 15, // ~ 15 seconds
|
||||
"ENTITY": 60 * 2, // ~2 seconds
|
||||
"default": 60
|
||||
if (force == true || isAnythingUndefined) {
|
||||
log("Initialising global.cache")
|
||||
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
|
||||
|
||||
log("Initialising global.DEFAULT_CACHE_DURATION_TICKS")
|
||||
global.defaultCacheDurationTicks = {
|
||||
"PLAYER": 60, // 1 second
|
||||
"MAP_CHUNK": 60 * 30, // 30 seconds
|
||||
"ENTITY": 60 * 2, // 2 seconds
|
||||
"default": 60 * 5, // 5 seconds
|
||||
}
|
||||
}
|
||||
log(`Finished initialising EventDataCache`)
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -73,7 +75,7 @@ export namespace EventDataCache {
|
|||
*/
|
||||
export function setExpiration(
|
||||
key: CacheKey<FactorioEventUpdateType>,
|
||||
expirationDurationTicks?: uint,
|
||||
expirationDurationTicks: uint,
|
||||
) {
|
||||
const entry = getCacheEntry(key)
|
||||
if (entry != undefined) {
|
||||
|
|
@ -82,11 +84,12 @@ export namespace EventDataCache {
|
|||
}
|
||||
|
||||
|
||||
export function extractExpired(): Array<CacheData<FactorioEventUpdateType>> {
|
||||
const expiredData: Array<CacheData<FactorioEventUpdateType>> = []
|
||||
export function extractExpired(): Array<FactorioEventUpdate> {
|
||||
const expiredData: Array<FactorioEventUpdate> = []
|
||||
for (let [key, entry] of global.cache) {
|
||||
if (entry.isExpired()) {
|
||||
expiredData.push(entry.data)
|
||||
if (isExpired(entry)) {
|
||||
const update: FactorioEventUpdate = {...key, ...entry.data}
|
||||
expiredData.push(update)
|
||||
global.cache.delete(key)
|
||||
}
|
||||
}
|
||||
|
|
@ -114,7 +117,7 @@ export namespace EventDataCache {
|
|||
key: CacheKey<TYPE>,
|
||||
mutate: CacheDataMutator<TYPE>,
|
||||
): CacheEntry<TYPE> {
|
||||
// @ts-ignore // TODO remove ts-ignore for "not assignable to type"
|
||||
// @ts-ignore // TODO resolve ts-ignore for "not assignable to type"
|
||||
const data: CacheData<TYPE> = {updateType: key.updateType}
|
||||
mutate(data)
|
||||
let entry: CacheEntry<TYPE> = new CacheEntry<TYPE>(data)
|
||||
|
|
@ -123,24 +126,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 :
|
||||
// /** 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 ConvertKeyType<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
|
||||
|
||||
|
||||
/** Only include the non-null properties, and make `updateType` specific */
|
||||
export type CacheKey<TYPE extends FactorioEventUpdateType> =
|
||||
CacheTyped<TYPE>
|
||||
& Omit<ExcludeNullKeys<ConvertUpdateType<TYPE>>, "updateType">
|
||||
& Omit<ConvertKeyType<TYPE>, "updateType">
|
||||
|
||||
|
||||
/** Exclude {@link CacheKey} fields, and make `updateType` specific */
|
||||
export type CacheData<TYPE extends FactorioEventUpdateType> =
|
||||
CacheTyped<TYPE>
|
||||
& NonNullableKeys<Partial<Omit<ConvertUpdateType<TYPE>, keyof CacheKey<TYPE> | "updateType">>>
|
||||
& NonNullableKeys<Partial<Omit<ConvertKeyData<TYPE>, "updateType">>>
|
||||
|
||||
|
||||
export type CacheTyped<TYPE extends FactorioEventUpdateType> = {
|
||||
|
|
@ -163,17 +177,15 @@ export namespace EventDataCache {
|
|||
expirationDurationTicks?: uint,
|
||||
) {
|
||||
this.data = data
|
||||
if (expirationDurationTicks != undefined) {
|
||||
this.expirationDurationTicks = expirationDurationTicks
|
||||
}
|
||||
this.expirationDurationTicks = expirationDurationTicks
|
||||
}
|
||||
}
|
||||
|
||||
isExpired(): boolean {
|
||||
const cacheDuration =
|
||||
this.expirationDurationTicks ?? global.DEFAULT_CACHE_DURATION_TICKS2[this.data.updateType]
|
||||
|
||||
return game.tick - this.lastUpdatedTick > cacheDuration
|
||||
}
|
||||
function isExpired<TYPE extends FactorioEventUpdateType>(entry: CacheEntry<TYPE>): boolean {
|
||||
const expiryDuration =
|
||||
entry.expirationDurationTicks ?? global.defaultCacheDurationTicks[entry.data.updateType]
|
||||
return game.tick - entry.lastUpdatedTick > expiryDuration
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import {emitPacket} from "../emitKafkatorioPacket";
|
|||
/** Every 0.1 seconds */
|
||||
script.on_nth_tick(6, (nthTick: NthTickEventData) => {
|
||||
|
||||
const cachedEvents: Array<EventDataCache.CacheData<any>> = EventDataCache.extractExpired()
|
||||
const cachedEvents: Array<FactorioEventUpdate> = EventDataCache.extractExpired()
|
||||
|
||||
if (cachedEvents.length > 0) {
|
||||
log(`nth tick ${nthTick.tick} has ${cachedEvents.length} events`)
|
||||
|
|
@ -22,34 +22,3 @@ script.on_nth_tick(6, (nthTick: NthTickEventData) => {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
//
|
||||
// function createEntityUpdate(cacheKey: EntityCacheKey, events: EventName[]): EntityUpdate | null {
|
||||
//
|
||||
//
|
||||
// return null
|
||||
// }
|
||||
//
|
||||
// interface EntityUpdate {
|
||||
// name: string
|
||||
//
|
||||
// unit_number: uint
|
||||
//
|
||||
// unhandledEvents?: string[]
|
||||
//
|
||||
// chunkPosition: MapChunkPosition
|
||||
// graphics_variation?: uint8
|
||||
// health?: float
|
||||
// isActive?: boolean
|
||||
// isRotatable?: boolean
|
||||
// last_user?: uint
|
||||
// localised_description?: string
|
||||
// localised_name?: string
|
||||
// prototype?: string
|
||||
//
|
||||
// // ghost_localised_description?: string
|
||||
// // ghost_localised_name?: string
|
||||
// // ghost_name?: string
|
||||
// // ghost_prototype?: string
|
||||
// // ghost_unit_number?: uint
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import {emitPrototypes} from "../config-update/prototypes";
|
||||
import {Queue} from "../queue/queue";
|
||||
import {initGlobal} from "../global-init";
|
||||
import floor = math.floor;
|
||||
|
||||
|
||||
|
|
@ -8,18 +9,20 @@ commands.add_command(
|
|||
"kafkatorio innit bruv",
|
||||
(e: CustomCommandData) => {
|
||||
|
||||
let player = (e.player_index != null) ? game.get_player(e.player_index) : null
|
||||
const player = (e.player_index != null) ? game.get_player(e.player_index) : null
|
||||
|
||||
if (e.parameter == undefined) {
|
||||
const paramUppercase = e.parameter?.toUpperCase()
|
||||
|
||||
if (paramUppercase == undefined) {
|
||||
if (player != null) {
|
||||
player.print("no parameter")
|
||||
}
|
||||
// do nothing
|
||||
} else if ("PROTOTYPES" == e.parameter.toUpperCase()) {
|
||||
} else if ("PROTOTYPES" == paramUppercase) {
|
||||
emitPrototypes()
|
||||
} else if (e.parameter.toUpperCase().startsWith("CHUNKS")) {
|
||||
} else if (paramUppercase.startsWith("CHUNKS")) {
|
||||
|
||||
let size = e.parameter.split(" ")?.[1] ?? null
|
||||
let size = paramUppercase.split(" ")?.[1] ?? null
|
||||
let radius = (size != null) ? parseInt(size) : 1
|
||||
|
||||
if (e.player_index != undefined) {
|
||||
|
|
@ -60,10 +63,8 @@ commands.add_command(
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if ("QUEUE_RESET" == e.parameter.toUpperCase()) {
|
||||
Queue.reset()
|
||||
} else if ("QUEUE_INIT" == e.parameter.toUpperCase()) {
|
||||
Queue.init()
|
||||
} else if (paramUppercase.startsWith("INIT_GLOBAL")) {
|
||||
initGlobal(paramUppercase.startsWith("INIT_GLOBAL FORCE"))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
function isEventType<ED extends EventData | CustomInputEvent>(
|
||||
export function isEventType<ED extends EventData | CustomInputEvent>(
|
||||
eventData: EventData,
|
||||
type: ED extends CustomInputEvent ? string : EventId<ED>
|
||||
): eventData is ED {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import {EventDataCache} from "../cache/EventDataCache";
|
||||
import {isEventType} from "./eventTypeCheck";
|
||||
import CacheKey = EventDataCache.CacheKey;
|
||||
import CacheData = EventDataCache.CacheData;
|
||||
|
||||
|
|
@ -24,12 +25,17 @@ function mapTilesUpdateDebounce(
|
|||
},
|
||||
data => {
|
||||
if (data.tiles == undefined) {
|
||||
data.tiles = new Map<MapTilePosition, string>()
|
||||
data.tiles = []
|
||||
}
|
||||
|
||||
for (const tile of tiles) {
|
||||
const position: MapTilePosition = {x: tile.position.x, y: tile.position.y}
|
||||
data.tiles.set(position, tile.name)
|
||||
data.tiles.push(
|
||||
{
|
||||
x: tile.position.x,
|
||||
y: tile.position.y,
|
||||
proto: tile.name,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (updater != undefined) {
|
||||
|
|
@ -58,6 +64,8 @@ function getSurface(surfaceIndex: uint): LuaSurface | undefined {
|
|||
script.on_event(
|
||||
defines.events.on_chunk_generated,
|
||||
(e: OnChunkGeneratedEvent) => {
|
||||
log(`on_chunk_generated ${e.tick}`)
|
||||
|
||||
let tiles = getTiles(e.surface, e.area)
|
||||
mapTilesUpdateDebounce(e.surface, e.position, tiles)
|
||||
}
|
||||
|
|
@ -66,6 +74,7 @@ script.on_event(
|
|||
script.on_event(
|
||||
defines.events.on_chunk_charted,
|
||||
(e: OnChunkChartedEvent) => {
|
||||
log(`on_chunk_charted ${e.tick}`)
|
||||
|
||||
const surface = getSurface(e.surface_index)
|
||||
if (surface == undefined) {
|
||||
|
|
@ -82,6 +91,7 @@ script.on_event(
|
|||
script.on_event(
|
||||
defines.events.script_raised_set_tiles,
|
||||
(e: ScriptRaisedSetTilesEvent) => {
|
||||
log(`script_raised_set_tiles ${e.tick}`)
|
||||
|
||||
const surface = getSurface(e.surface_index)
|
||||
if (surface == undefined) {
|
||||
|
|
@ -164,8 +174,14 @@ function onBuildTileEvent(event: OnPlayerBuiltTileEvent | OnRobotBuiltTileEvent)
|
|||
}
|
||||
}
|
||||
|
||||
script.on_event(defines.events.on_player_built_tile, (e: OnPlayerBuiltTileEvent) => onBuildTileEvent(e))
|
||||
script.on_event(defines.events.on_robot_built_tile, (e: OnRobotBuiltTileEvent) => onBuildTileEvent(e))
|
||||
script.on_event(defines.events.on_player_built_tile, (e: OnPlayerBuiltTileEvent) => {
|
||||
log(`on_player_built_tile ${e.tick}`)
|
||||
onBuildTileEvent(e)
|
||||
})
|
||||
script.on_event(defines.events.on_robot_built_tile, (e: OnRobotBuiltTileEvent) => {
|
||||
log(`on_robot_built_tile ${e.tick}`)
|
||||
onBuildTileEvent(e)
|
||||
})
|
||||
|
||||
// script.on_event(
|
||||
// defines.events.on_player_mined_tile,
|
||||
|
|
@ -187,6 +203,7 @@ script.on_event(defines.events.on_robot_built_tile, (e: OnRobotBuiltTileEvent) =
|
|||
script.on_event(
|
||||
defines.events.on_pre_chunk_deleted,
|
||||
(e: OnPreChunkDeletedEvent) => {
|
||||
log(`on_pre_chunk_deleted ${e.tick}`)
|
||||
|
||||
const surface = getSurface(e.surface_index)
|
||||
if (surface == undefined) {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
} from "./handlers";
|
||||
import {Queue} from "../queue/queue";
|
||||
import {EventName} from "../types";
|
||||
import {isEventType} from "./eventTypeCheck";
|
||||
|
||||
|
||||
const mapEventIdToName = new LuaTable<defines.Events, EventName>()
|
||||
|
|
|
|||
15
modules/events-mod/src/main/typescript/main/global-init.ts
Normal file
15
modules/events-mod/src/main/typescript/main/global-init.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import {Queue} from "./queue/queue";
|
||||
import {EventDataCache} from "./cache/EventDataCache";
|
||||
|
||||
|
||||
export function initGlobal(force: boolean = false) {
|
||||
log(`Initialising Kafkatorio Global variables (force=${force})...`)
|
||||
|
||||
global.MOD_VERSION = script.active_mods[script.mod_name]
|
||||
global.FACTORIO_VERSION = script.active_mods["base"]
|
||||
|
||||
Queue.init(true)
|
||||
EventDataCache.init(true)
|
||||
|
||||
log("Finished initialising Kafkatorio Global variables")
|
||||
}
|
||||
|
|
@ -10,24 +10,27 @@ export namespace Queue {
|
|||
}
|
||||
|
||||
|
||||
export function init() {
|
||||
if (global.store == undefined) {
|
||||
log("initialising EventQueue")
|
||||
reset()
|
||||
export function init(force?: boolean) {
|
||||
|
||||
const isAnythingUndefined = global.store == undefined ||
|
||||
global.size == undefined ||
|
||||
global.head == undefined ||
|
||||
global.tail == undefined
|
||||
|
||||
log(`Initialising EventQueue globals (force=${force}, isAnythingUndefined=${isAnythingUndefined})`)
|
||||
|
||||
if (force == true || isAnythingUndefined) {
|
||||
global.store = {}
|
||||
global.head = null
|
||||
global.tail = null
|
||||
global.size = 0
|
||||
}
|
||||
|
||||
log(`Finished initialising EventQueue`)
|
||||
}
|
||||
|
||||
|
||||
export function reset() {
|
||||
log("Resetting EventQueue")
|
||||
global.store = {}
|
||||
global.head = null
|
||||
global.tail = null
|
||||
global.size = 0
|
||||
}
|
||||
|
||||
|
||||
export function size(): uint {
|
||||
export function size(): uint | undefined {
|
||||
return global.size
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue