2019-05-26 17:39:54 +02:00
|
|
|
local Global = require 'utils.global'
|
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Token = require 'utils.token'
|
|
|
|
local Schedule = require 'utils.task'
|
|
|
|
local Server = require 'features.server'
|
|
|
|
local Settings = require 'utils.redmew_settings'
|
|
|
|
local set_timeout_in_ticks = Schedule.set_timeout_in_ticks
|
2019-05-26 18:54:45 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local raise_event = script.raise_event
|
2019-05-26 17:39:54 +02:00
|
|
|
|
|
|
|
local Public = {}
|
|
|
|
|
2019-05-26 18:54:45 +02:00
|
|
|
Public.events = {
|
2019-05-26 20:15:05 +02:00
|
|
|
--- Triggered when the settings are synced back to the server
|
2019-05-26 18:54:45 +02:00
|
|
|
-- Event {
|
|
|
|
-- player = player
|
|
|
|
-- }
|
|
|
|
on_synced_to_server = Event.generate_event_name('on_synced_to_server'),
|
|
|
|
|
|
|
|
--- Triggered when the settings are synced back from the server
|
|
|
|
--- keeps track of whether or not it's cancelled
|
|
|
|
-- Event {
|
|
|
|
-- player = player
|
|
|
|
-- cancelled = cancelled
|
|
|
|
-- }
|
|
|
|
on_synced_from_server = Event.generate_event_name('on_synced_from_server'),
|
|
|
|
}
|
|
|
|
|
2019-05-26 17:39:54 +02:00
|
|
|
local memory = {
|
2019-05-26 18:54:45 +02:00
|
|
|
-- when already scheduled, no new schedules have to be added
|
2019-05-27 18:57:06 +02:00
|
|
|
sync_scheduled = {},
|
2019-05-26 18:54:45 +02:00
|
|
|
|
|
|
|
-- when locked it won't schedule anything to prevent recursion syncing back to server
|
|
|
|
locked = false,
|
2019-05-26 17:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Global.register(memory, function (tbl) memory = tbl end)
|
|
|
|
|
2019-05-30 21:57:43 +02:00
|
|
|
local do_sync_settings_to_server = Token.register(function(params)
|
2019-05-26 17:39:54 +02:00
|
|
|
local player_index = params.player_index;
|
|
|
|
local player = game.get_player(player_index)
|
|
|
|
if not player or not player.valid then
|
2019-05-27 18:57:06 +02:00
|
|
|
-- The player doesn't exist or got removed, ensure it's reset so it doesn't
|
|
|
|
-- stay on scheduled
|
|
|
|
memory.sync_scheduled[player_index] = nil
|
2019-05-26 17:39:54 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- do update
|
|
|
|
Server.set_data('player_settings', player.name, Settings.all(player_index))
|
|
|
|
|
|
|
|
-- mark it as updated
|
2019-05-27 18:57:06 +02:00
|
|
|
memory.sync_scheduled[player_index] = nil
|
2019-05-26 18:54:45 +02:00
|
|
|
|
|
|
|
raise_event(Public.events.on_synced_to_server, {
|
|
|
|
player = player
|
|
|
|
})
|
2019-05-26 17:39:54 +02:00
|
|
|
end)
|
|
|
|
|
2019-05-30 21:57:43 +02:00
|
|
|
local function schedule_sync_to_server(player_index)
|
|
|
|
set_timeout_in_ticks(1, do_sync_settings_to_server, {
|
|
|
|
player_index = player_index
|
|
|
|
})
|
|
|
|
memory.sync_scheduled[player_index] = true
|
|
|
|
end
|
|
|
|
|
2019-05-26 17:39:54 +02:00
|
|
|
local function setting_set(event)
|
2019-05-27 18:57:06 +02:00
|
|
|
local player_index = event.player_index
|
|
|
|
if memory.locked or memory.sync_scheduled[player_index] then
|
2019-05-26 17:39:54 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-05-27 16:01:48 +02:00
|
|
|
if not event.value_changed then
|
2019-05-26 17:39:54 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-05-27 18:57:06 +02:00
|
|
|
schedule_sync_to_server(player_index)
|
2019-05-26 17:39:54 +02:00
|
|
|
end
|
|
|
|
|
2019-05-26 18:54:45 +02:00
|
|
|
local on_player_settings_get = Token.register(function (data)
|
|
|
|
local player = game.get_player(data.key)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.cancelled then
|
|
|
|
raise_event(Public.events.on_synced_from_server, {
|
|
|
|
player = player,
|
|
|
|
cancelled = true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local settings = data.value
|
|
|
|
|
|
|
|
if settings ~= nil then
|
|
|
|
-- temporarily lock the sync so it won't sync from server to client to server
|
|
|
|
-- as this would cause recursion
|
|
|
|
memory.locked = true
|
|
|
|
local player_index = player.index
|
|
|
|
for key, value in pairs(settings) do
|
|
|
|
Settings.set(player_index, key, value)
|
|
|
|
end
|
|
|
|
memory.locked = false
|
|
|
|
end
|
|
|
|
|
|
|
|
raise_event(Public.events.on_synced_from_server, {
|
|
|
|
player = player,
|
|
|
|
cancelled = false
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
local function player_joined(event)
|
|
|
|
local player = game.get_player(event.player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Server.try_get_data_timeout('player_settings', player.name, on_player_settings_get, 30)
|
|
|
|
end
|
|
|
|
|
2019-05-26 17:39:54 +02:00
|
|
|
Event.add(Settings.events.on_setting_set, setting_set);
|
2019-05-26 18:54:45 +02:00
|
|
|
Event.add(defines.events.on_player_joined_game, player_joined)
|
2019-05-26 17:39:54 +02:00
|
|
|
|
|
|
|
return Public
|