From 89223f5dbd6ea5fcfc185df94ddc4e5ccbe66821 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Tue, 18 May 2021 21:11:28 +0200 Subject: [PATCH 1/2] fix broken session time --- maps/crab_defender/main.lua | 7 ++ maps/fish_defender/main.lua | 7 ++ maps/fish_defender_v2/main.lua | 7 ++ maps/journey/functions.lua | 2 + maps/mountain_fortress_v3/ic/main.lua | 8 +- maps/pidgeotto/main.lua | 7 ++ maps/planet_prison.lua | 3 + utils/datastore/session_data.lua | 102 ++++++++++++++++++++------ utils/debug/public_global_view.lua | 2 +- 9 files changed, 122 insertions(+), 23 deletions(-) diff --git a/maps/crab_defender/main.lua b/maps/crab_defender/main.lua index 18e9891e..7d31a9ef 100644 --- a/maps/crab_defender/main.lua +++ b/maps/crab_defender/main.lua @@ -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 = {} diff --git a/maps/fish_defender/main.lua b/maps/fish_defender/main.lua index 98461b90..04ac0bc0 100644 --- a/maps/fish_defender/main.lua +++ b/maps/fish_defender/main.lua @@ -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 = {} diff --git a/maps/fish_defender_v2/main.lua b/maps/fish_defender_v2/main.lua index ffa48468..8938477a 100644 --- a/maps/fish_defender_v2/main.lua +++ b/maps/fish_defender_v2/main.lua @@ -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 diff --git a/maps/journey/functions.lua b/maps/journey/functions.lua index 32bae50a..834a492e 100644 --- a/maps/journey/functions.lua +++ b/maps/journey/functions.lua @@ -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 diff --git a/maps/mountain_fortress_v3/ic/main.lua b/maps/mountain_fortress_v3/ic/main.lua index b547200b..7fe06baa 100644 --- a/maps/mountain_fortress_v3/ic/main.lua +++ b/maps/mountain_fortress_v3/ic/main.lua @@ -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 diff --git a/maps/pidgeotto/main.lua b/maps/pidgeotto/main.lua index 6e7abeea..2266f5bc 100644 --- a/maps/pidgeotto/main.lua +++ b/maps/pidgeotto/main.lua @@ -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 players = game.players + for i = 1, #players do + local player = players[i] + Session.clear_player(player) + end + local map_gen_settings = {} map_gen_settings.seed = math_random(10000, 99999) map_gen_settings.width = 4000 diff --git a/maps/planet_prison.lua b/maps/planet_prison.lua index d4b02b5f..ae282020 100644 --- a/maps/planet_prison.lua +++ b/maps/planet_prison.lua @@ -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 diff --git a/utils/datastore/session_data.lua b/utils/datastore/session_data.lua index e00d8c1d..d90ab2ed 100644 --- a/utils/datastore/session_data.lua +++ b/utils/datastore/session_data.lua @@ -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 diff --git a/utils/debug/public_global_view.lua b/utils/debug/public_global_view.lua index 4c9970b9..c6a560cb 100644 --- a/utils/debug/public_global_view.lua +++ b/utils/debug/public_global_view.lua @@ -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 From 376c101484791da214a3be3e132f1b9628788605 Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Tue, 18 May 2021 21:13:08 +0200 Subject: [PATCH 2/2] fix local variable shadowing --- maps/pidgeotto/main.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maps/pidgeotto/main.lua b/maps/pidgeotto/main.lua index 2266f5bc..800cfec1 100644 --- a/maps/pidgeotto/main.lua +++ b/maps/pidgeotto/main.lua @@ -1289,9 +1289,9 @@ function Public.reset_game() end end - local players = game.players - for i = 1, #players do - local player = players[i] + local offline_players = game.players + for i = 1, #offline_players do + local player = offline_players[i] Session.clear_player(player) end