mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-01-08 00:39:30 +02:00
Merge pull request #60 from ComfyFactory/session_data_fix
fix broken session time
This commit is contained in:
commit
1639ac1dc7
@ -10,6 +10,7 @@ require 'modules.dangerous_goods'
|
||||
require 'modules.custom_death_messages'
|
||||
|
||||
local Unit_health_booster = require 'modules.biter_health_booster'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
local Difficulty = require 'modules.difficulty_vote'
|
||||
local Map = require 'modules.map_info'
|
||||
local Event = require 'utils.event'
|
||||
@ -63,6 +64,12 @@ function Public.reset_game()
|
||||
end
|
||||
end
|
||||
|
||||
local offline_players = game.players
|
||||
for i = 1, #offline_players do
|
||||
local player = offline_players[i]
|
||||
Session.clear_player(player)
|
||||
end
|
||||
|
||||
disable_tech()
|
||||
|
||||
local map_gen_settings = {}
|
||||
|
@ -15,6 +15,7 @@ require 'modules.launch_fish_to_win'
|
||||
local AntiGrief = require 'antigrief'
|
||||
local Terrain = require 'maps.fish_defender.terrain'
|
||||
local Unit_health_booster = require 'modules.biter_health_booster'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
local Difficulty = require 'modules.difficulty_vote'
|
||||
local Map = require 'modules.map_info'
|
||||
local Event = require 'utils.event'
|
||||
@ -1147,6 +1148,12 @@ local function on_init(reset)
|
||||
Diff.difficulty_poll_closing_timeout = this.wave_grace_period
|
||||
get_score.score_table = {}
|
||||
|
||||
local players = game.players
|
||||
for i = 1, #players do
|
||||
local player = players[i]
|
||||
Session.clear_player(player)
|
||||
end
|
||||
|
||||
game.remove_offline_players()
|
||||
|
||||
local map_gen_settings = {}
|
||||
|
@ -18,6 +18,7 @@ local Map = require 'modules.map_info'
|
||||
local Event = require 'utils.event'
|
||||
local Reset = require 'functions.soft_reset'
|
||||
local Server = require 'utils.server'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
local Poll = require 'comfy_panel.poll'
|
||||
local boss_biter = require 'maps.fish_defender_v2.boss_biters'
|
||||
local FDT = require 'maps.fish_defender_v2.table'
|
||||
@ -1321,6 +1322,12 @@ function Public.reset_game()
|
||||
end
|
||||
end
|
||||
|
||||
local offline_players = game.players
|
||||
for i = 1, #offline_players do
|
||||
local player = offline_players[i]
|
||||
Session.clear_player(player)
|
||||
end
|
||||
|
||||
local map_gen_settings = {}
|
||||
map_gen_settings.seed = math_random(10000, 99999)
|
||||
map_gen_settings.width = 4000
|
||||
|
@ -1,6 +1,7 @@
|
||||
--luacheck: ignore
|
||||
local Public = {}
|
||||
local Constants = require 'maps.journey.constants'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
|
||||
local function clear_world_selectors(journey)
|
||||
for k, world_selector in pairs(journey.world_selectors) do
|
||||
@ -91,6 +92,7 @@ local function remove_offline_players(maximum_age_in_hours)
|
||||
local players_to_remove = {}
|
||||
for _, player in pairs(game.players) do
|
||||
if player.last_online < t then
|
||||
Session.clear_player(player)
|
||||
table.insert(players_to_remove, player)
|
||||
end
|
||||
end
|
||||
|
@ -133,9 +133,15 @@ local function on_gui_opened(event)
|
||||
return
|
||||
end
|
||||
|
||||
local surface_index = car.surface
|
||||
local surface = game.surfaces[surface_index]
|
||||
if not surface or not surface.valid then
|
||||
return
|
||||
end
|
||||
|
||||
Minimap.minimap(
|
||||
game.players[event.player_index],
|
||||
car.surface,
|
||||
surface,
|
||||
{
|
||||
car.area.left_top.x + (car.area.right_bottom.x - car.area.left_top.x) * 0.5,
|
||||
car.area.left_top.y + (car.area.right_bottom.y - car.area.left_top.y) * 0.5
|
||||
|
@ -9,6 +9,7 @@ require 'modules.biters_yield_coins'
|
||||
require 'modules.custom_death_messages'
|
||||
|
||||
local Unit_health_booster = require 'modules.biter_health_booster_v2'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
local Difficulty = require 'modules.difficulty_vote'
|
||||
local Map = require 'modules.map_info'
|
||||
local Event = require 'utils.event'
|
||||
@ -1288,6 +1289,12 @@ function Public.reset_game()
|
||||
end
|
||||
end
|
||||
|
||||
local offline_players = game.players
|
||||
for i = 1, #offline_players do
|
||||
local player = offline_players[i]
|
||||
Session.clear_player(player)
|
||||
end
|
||||
|
||||
local map_gen_settings = {}
|
||||
map_gen_settings.seed = math_random(10000, 99999)
|
||||
map_gen_settings.width = 4000
|
||||
|
@ -2,6 +2,7 @@
|
||||
-- heavily modified by Gerkiz
|
||||
|
||||
local Global = require 'utils.global'
|
||||
local Session = require 'utils.datastore.session_data'
|
||||
local Event = require 'utils.event'
|
||||
local Server = require 'utils.server'
|
||||
local MapFuntions = require 'tools.map_functions'
|
||||
@ -1147,6 +1148,7 @@ local function remove_offline_players()
|
||||
if game.forces[player.name] then
|
||||
game.merge_forces(player.name, 'neutral')
|
||||
end
|
||||
Session.clear_player(player)
|
||||
game.remove_offline_players({player})
|
||||
offline_players.players[i] = nil
|
||||
end
|
||||
@ -1357,6 +1359,7 @@ local function on_player_died(e)
|
||||
if p.connected then
|
||||
return
|
||||
end
|
||||
Session.clear_player(p)
|
||||
game.remove_offline_players({p})
|
||||
end
|
||||
|
||||
|
@ -14,6 +14,7 @@ local trusted = {}
|
||||
local settings = {
|
||||
-- local trusted_value = 2592000 -- 12h
|
||||
trusted_value = 5184000, -- 24h
|
||||
required_only_time_to_save_time = 36000, -- nearest prime to 10 minutes in ticks
|
||||
nth_tick = 18000 -- nearest prime to 5 minutes in ticks
|
||||
}
|
||||
local set_data = Server.set_data
|
||||
@ -35,7 +36,11 @@ Global.register(
|
||||
end
|
||||
)
|
||||
|
||||
local Public = {}
|
||||
local Public = {
|
||||
events = {
|
||||
on_player_removed = Event.generate_event_name('on_player_removed')
|
||||
}
|
||||
}
|
||||
|
||||
local try_download_data =
|
||||
Token.register(
|
||||
@ -48,9 +53,13 @@ local try_download_data =
|
||||
trusted[key] = true
|
||||
end
|
||||
else
|
||||
local player = game.get_player(key)
|
||||
session[key] = 0
|
||||
trusted[key] = false
|
||||
set_data(session_data_set, key, session[key])
|
||||
-- we don't want to clutter the database with players less than 10 minutes played.
|
||||
if player.online_time >= settings.required_only_time_to_save_time then
|
||||
set_data(session_data_set, key, session[key])
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
@ -62,12 +71,24 @@ local try_upload_data =
|
||||
local value = data.value
|
||||
local player = game.get_player(key)
|
||||
if value then
|
||||
-- we don't want to clutter the database with players less than 10 minutes played.
|
||||
if player.online_time <= settings.required_only_time_to_save_time then
|
||||
return
|
||||
end
|
||||
|
||||
local old_time_ingame = value
|
||||
|
||||
if not online_track[key] then
|
||||
online_track[key] = 0
|
||||
end
|
||||
|
||||
if online_track[key] > player.online_time then
|
||||
-- instance has been reset but scenario owner did not clear the player.
|
||||
-- so we clear it here and return.
|
||||
online_track[key] = 0
|
||||
return
|
||||
end
|
||||
|
||||
local new_time = old_time_ingame + player.online_time - online_track[key]
|
||||
if new_time <= 0 then
|
||||
new_time = old_time_ingame + player.online_time
|
||||
@ -78,6 +99,13 @@ local try_upload_data =
|
||||
set_data(session_data_set, key, new_time)
|
||||
session[key] = new_time
|
||||
online_track[key] = player.online_time
|
||||
else
|
||||
if player.online_time >= settings.required_only_time_to_save_time then
|
||||
if not session[key] then
|
||||
session[key] = 0
|
||||
end
|
||||
set_data(session_data_set, key, session[key])
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
@ -85,7 +113,8 @@ local try_upload_data =
|
||||
local nth_tick_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
local player = data.player
|
||||
local index = data.index
|
||||
local player = game.get_player(index)
|
||||
if player and player.valid then
|
||||
Public.try_ul_data(player.name)
|
||||
end
|
||||
@ -97,10 +126,9 @@ local function upload_data()
|
||||
local players = game.connected_players
|
||||
local count = 0
|
||||
for i = 1, #players do
|
||||
count = count + 1
|
||||
count = count + 10
|
||||
local player = players[i]
|
||||
local random_timing = count * 5
|
||||
set_timeout_in_ticks(random_timing, nth_tick_token, {player = player})
|
||||
set_timeout_in_ticks(count, nth_tick_token, {index = player.index})
|
||||
end
|
||||
end
|
||||
|
||||
@ -191,18 +219,45 @@ end
|
||||
--- Clears a given player from the session tables.
|
||||
-- @param LuaPlayer
|
||||
function Public.clear_player(player)
|
||||
local name = player.name
|
||||
if session[name] then
|
||||
session[name] = nil
|
||||
end
|
||||
if online_track[name] then
|
||||
online_track[name] = nil
|
||||
end
|
||||
if trusted[name] then
|
||||
trusted[name] = nil
|
||||
if player and player.valid then
|
||||
local name = player.name
|
||||
local connected = player.connected
|
||||
|
||||
if not connected then
|
||||
if session[name] then
|
||||
session[name] = nil
|
||||
end
|
||||
if online_track[name] then
|
||||
online_track[name] = nil
|
||||
end
|
||||
if trusted[name] then
|
||||
trusted[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Resets a given player from the online_track table.
|
||||
-- @param LuaPlayer
|
||||
function Public.reset_online_track(player)
|
||||
local name = player.name
|
||||
if online_track[name] then
|
||||
online_track[name] = 0
|
||||
end
|
||||
end
|
||||
|
||||
--- It's vital that we reset the online_track so we
|
||||
--- don't calculate the values wrong.
|
||||
Event.add(
|
||||
Public.events.on_player_removed,
|
||||
function()
|
||||
for name, _ in pairs(online_track) do
|
||||
local player = game.get_player(name)
|
||||
Public.clear_player(player)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
Event.add(
|
||||
defines.events.on_player_joined_game,
|
||||
function(event)
|
||||
@ -232,15 +287,20 @@ Event.on_nth_tick(settings.nth_tick, upload_data)
|
||||
Server.on_data_set_changed(
|
||||
session_data_set,
|
||||
function(data)
|
||||
session[data.key] = data.value
|
||||
if data.value > settings.trusted_value then
|
||||
trusted[data.key] = true
|
||||
else
|
||||
if trusted[data.key] then
|
||||
trusted[data.key] = false
|
||||
local player = game.get_player(data.key)
|
||||
if player and player.valid then
|
||||
session[data.key] = data.value
|
||||
if data.value > settings.trusted_value then
|
||||
trusted[data.key] = true
|
||||
else
|
||||
if trusted[data.key] then
|
||||
trusted[data.key] = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
Public.upload_data = upload_data
|
||||
|
||||
return Public
|
||||
|
@ -123,7 +123,7 @@ Gui.on_click(
|
||||
function(event)
|
||||
local element = event.element
|
||||
local data = Gui.get_data(element)
|
||||
if not data or not data.valid then
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user