This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
control.lua
273 lines (230 loc) · 7.41 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
local event = require("__flib__.event")
local dictionary = require("__flib__.dictionary")
local gui = require("__flib__.gui")
local migration = require("__flib__.migration")
local on_tick_n = require("__flib__.on-tick-n")
local global_data = require("scripts.global-data")
local ltn_data = require("scripts.ltn-data")
local migrations = require("scripts.migrations")
local player_data = require("scripts.player-data")
local main_gui = require("scripts.gui.index")
-- -----------------------------------------------------------------------------
-- COMMANDS
commands.add_command("LtnManager", { "ltnm-message.command-help" }, function(e)
if e.parameter == "refresh-player-data" then
local player = game.get_player(e.player_index)
local player_table = global.players[e.player_index]
player_data.refresh(player, player_table)
end
end)
-- -----------------------------------------------------------------------------
-- INTERFACES
remote.add_interface("LtnManager", {
--- Returns whether the LTN Manager GUI is open for the given player.
--- @param player_index number
--- @return boolean
is_gui_open = function(player_index)
if not player_index or type(player_index) ~= "number" then
error("Must provide a valid player_index")
end
if global.players then
local player_table = global.players[player_index]
if player_table then
local Gui = player_table.guis.main
if Gui then
return Gui.state.visible
end
end
end
return false
end,
--- Toggles the LTN Manager GUI for the given player, and returns its new state.
--- @param player_index number
--- @return boolean
toggle_gui = function(player_index)
if not player_index or type(player_index) ~= "number" then
error("Must provide a valid player_index")
end
if global.players then
local player_table = global.players[player_index]
if player_table then
local Gui = player_table.guis.main
if Gui and Gui.refs.window.valid then
Gui:toggle()
return Gui.state.visible
end
end
end
return false
end,
get_provided_inventory_for_surface = function(surface_index)
if not surface_index or type(surface_index) ~= "number" then
error("Must provide a valid surface_index")
end
if not global.data or not global.data.inventory or not global.data.inventory.provided then
return nil
end
return global.data.inventory.provided[surface_index]
end,
})
-- -----------------------------------------------------------------------------
-- EVENT HANDLERS
-- LTN data handlers are kept in `scripts.ltn-data`
-- all other handlers are kept here
-- BOOTSTRAP
event.on_init(function()
dictionary.init()
on_tick_n.init()
global_data.init()
global_data.build_dictionaries()
ltn_data.init()
ltn_data.connect()
for i, player in pairs(game.players) do
player_data.init(player, i)
player_data.refresh(player, global.players[i])
end
end)
event.on_load(function()
dictionary.load()
ltn_data.connect()
for _, player_table in pairs(global.players) do
if player_table.guis and player_table.guis.main then
main_gui.load(player_table.guis.main)
end
end
end)
event.on_configuration_changed(function(e)
if migration.on_config_changed(e, migrations) then
dictionary.init()
global_data.build_dictionaries()
ltn_data.init()
for i, player in pairs(game.players) do
player_data.refresh(player, global.players[i])
end
end
end)
-- GUI
local function handle_gui_event(msg, e)
if msg.gui == "main" then
local player_table = global.players[e.player_index]
if player_table.flags.can_open_gui then
local Gui = player_table.guis.main
if Gui and Gui.refs.window.valid then
Gui:dispatch(msg, e)
end
end
end
end
gui.hook_events(function(e)
local msg = gui.read_action(e)
if msg then
handle_gui_event(msg, e)
end
end)
event.register("ltnm-linked-focus-search", function(e)
local Gui = global.players[e.player_index].guis.main
if Gui and Gui.state.visible and not Gui.state.pinned then
handle_gui_event({ gui = "main", action = "focus_search" }, e)
end
end)
-- PLAYER
event.on_player_created(function(e)
local player = game.get_player(e.player_index)
player_data.init(player, e.player_index)
player_data.refresh(player, global.players[e.player_index])
end)
event.on_player_removed(function(e)
global.players[e.player_index] = nil
end)
event.on_player_joined_game(function(e)
local player = game.get_player(e.player_index)
if player.connected then
dictionary.translate(player)
end
end)
event.on_player_left_game(function(e)
dictionary.cancel_translation(e.player_index)
end)
-- SHORTCUT
event.register({ defines.events.on_lua_shortcut, "ltnm-toggle-gui" }, function(e)
if e.input_name or (e.prototype_name == "ltnm-toggle-gui") then
local player = game.get_player(e.player_index)
local player_table = global.players[e.player_index]
local flags = player_table.flags
local Gui = main_gui.get(e.player_index)
if Gui then
if flags.can_open_gui then
Gui:toggle()
else
if Gui.state.visible then
Gui:close()
end
if flags.translations_finished then
player.print({ "ltnm-message.ltn-no-data" })
else
player.print({ "ltnm-message.translations-not-finished" })
end
end
end
end
end)
-- TICK
event.on_tick(function(e)
dictionary.check_skipped()
local tasks = on_tick_n.retrieve(e.tick)
if tasks then
for _, task in pairs(tasks) do
if task.gui then
handle_gui_event(task, { player_index = task.player_index })
end
end
end
local flags = global.flags
if flags.iterating_ltn_data then
ltn_data.iterate()
end
if flags.updating_guis then
local player_index = global.next_update_index
local player = game.get_player(player_index)
local player_table = global.players[player_index]
local player_flags = player_table.flags
if player_flags.translations_finished then
if player_flags.can_open_gui then
local Gui = main_gui.get(player_index)
if Gui and Gui.state.visible and Gui.state.auto_refresh then
Gui.state.ltn_data = global.data
Gui:update()
end
else
main_gui.build(player, player_table)
end
end
local next_index = next(global.players, global.next_update_index)
if next_index then
global.next_update_index = next_index
else
global.next_update_index = nil
flags.updating_guis = false
end
end
end)
-- TRANSLATIONS
event.on_string_translated(function(e)
local language_data = dictionary.process_translation(e)
if language_data then
for _, player_index in pairs(language_data.players) do
local player_table = global.players[player_index]
-- If the player already has a language, replace it and rebuild the GUI
if player_table.dictionaries and (player_table.language or "") ~= language_data.language then
player_table.language = language_data.language
player_table.dictionaries = language_data.dictionaries
-- TODO: Rebuild GUI
elseif not player_table.flags.can_open_gui then
player_table.language = language_data.language
player_table.dictionaries = language_data.dictionaries
-- Enable opening the GUI on the next LTN update cycle
player_table.flags.translations_finished = true
end
end
end
end)