1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00

Merge pull request #60 from ComfyFactory/session_data_fix

fix broken session time
This commit is contained in:
Gerkiz 2021-05-18 21:14:14 +02:00 committed by GitHub
commit 1639ac1dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 23 deletions

View File

@ -10,6 +10,7 @@ require 'modules.dangerous_goods'
require 'modules.custom_death_messages' require 'modules.custom_death_messages'
local Unit_health_booster = require 'modules.biter_health_booster' local Unit_health_booster = require 'modules.biter_health_booster'
local Session = require 'utils.datastore.session_data'
local Difficulty = require 'modules.difficulty_vote' local Difficulty = require 'modules.difficulty_vote'
local Map = require 'modules.map_info' local Map = require 'modules.map_info'
local Event = require 'utils.event' local Event = require 'utils.event'
@ -63,6 +64,12 @@ function Public.reset_game()
end end
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() disable_tech()
local map_gen_settings = {} local map_gen_settings = {}

View File

@ -15,6 +15,7 @@ require 'modules.launch_fish_to_win'
local AntiGrief = require 'antigrief' local AntiGrief = require 'antigrief'
local Terrain = require 'maps.fish_defender.terrain' local Terrain = require 'maps.fish_defender.terrain'
local Unit_health_booster = require 'modules.biter_health_booster' local Unit_health_booster = require 'modules.biter_health_booster'
local Session = require 'utils.datastore.session_data'
local Difficulty = require 'modules.difficulty_vote' local Difficulty = require 'modules.difficulty_vote'
local Map = require 'modules.map_info' local Map = require 'modules.map_info'
local Event = require 'utils.event' local Event = require 'utils.event'
@ -1147,6 +1148,12 @@ local function on_init(reset)
Diff.difficulty_poll_closing_timeout = this.wave_grace_period Diff.difficulty_poll_closing_timeout = this.wave_grace_period
get_score.score_table = {} 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() game.remove_offline_players()
local map_gen_settings = {} local map_gen_settings = {}

View File

@ -18,6 +18,7 @@ local Map = require 'modules.map_info'
local Event = require 'utils.event' local Event = require 'utils.event'
local Reset = require 'functions.soft_reset' local Reset = require 'functions.soft_reset'
local Server = require 'utils.server' local Server = require 'utils.server'
local Session = require 'utils.datastore.session_data'
local Poll = require 'comfy_panel.poll' local Poll = require 'comfy_panel.poll'
local boss_biter = require 'maps.fish_defender_v2.boss_biters' local boss_biter = require 'maps.fish_defender_v2.boss_biters'
local FDT = require 'maps.fish_defender_v2.table' local FDT = require 'maps.fish_defender_v2.table'
@ -1321,6 +1322,12 @@ function Public.reset_game()
end end
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 = {} local map_gen_settings = {}
map_gen_settings.seed = math_random(10000, 99999) map_gen_settings.seed = math_random(10000, 99999)
map_gen_settings.width = 4000 map_gen_settings.width = 4000

View File

@ -1,6 +1,7 @@
--luacheck: ignore --luacheck: ignore
local Public = {} local Public = {}
local Constants = require 'maps.journey.constants' local Constants = require 'maps.journey.constants'
local Session = require 'utils.datastore.session_data'
local function clear_world_selectors(journey) local function clear_world_selectors(journey)
for k, world_selector in pairs(journey.world_selectors) do 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 = {} local players_to_remove = {}
for _, player in pairs(game.players) do for _, player in pairs(game.players) do
if player.last_online < t then if player.last_online < t then
Session.clear_player(player)
table.insert(players_to_remove, player) table.insert(players_to_remove, player)
end end
end end

View File

@ -133,9 +133,15 @@ local function on_gui_opened(event)
return return
end end
local surface_index = car.surface
local surface = game.surfaces[surface_index]
if not surface or not surface.valid then
return
end
Minimap.minimap( Minimap.minimap(
game.players[event.player_index], 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.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 car.area.left_top.y + (car.area.right_bottom.y - car.area.left_top.y) * 0.5

View File

@ -9,6 +9,7 @@ require 'modules.biters_yield_coins'
require 'modules.custom_death_messages' require 'modules.custom_death_messages'
local Unit_health_booster = require 'modules.biter_health_booster_v2' local Unit_health_booster = require 'modules.biter_health_booster_v2'
local Session = require 'utils.datastore.session_data'
local Difficulty = require 'modules.difficulty_vote' local Difficulty = require 'modules.difficulty_vote'
local Map = require 'modules.map_info' local Map = require 'modules.map_info'
local Event = require 'utils.event' local Event = require 'utils.event'
@ -1288,6 +1289,12 @@ function Public.reset_game()
end end
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 = {} local map_gen_settings = {}
map_gen_settings.seed = math_random(10000, 99999) map_gen_settings.seed = math_random(10000, 99999)
map_gen_settings.width = 4000 map_gen_settings.width = 4000

View File

@ -2,6 +2,7 @@
-- heavily modified by Gerkiz -- heavily modified by Gerkiz
local Global = require 'utils.global' local Global = require 'utils.global'
local Session = require 'utils.datastore.session_data'
local Event = require 'utils.event' local Event = require 'utils.event'
local Server = require 'utils.server' local Server = require 'utils.server'
local MapFuntions = require 'tools.map_functions' local MapFuntions = require 'tools.map_functions'
@ -1147,6 +1148,7 @@ local function remove_offline_players()
if game.forces[player.name] then if game.forces[player.name] then
game.merge_forces(player.name, 'neutral') game.merge_forces(player.name, 'neutral')
end end
Session.clear_player(player)
game.remove_offline_players({player}) game.remove_offline_players({player})
offline_players.players[i] = nil offline_players.players[i] = nil
end end
@ -1357,6 +1359,7 @@ local function on_player_died(e)
if p.connected then if p.connected then
return return
end end
Session.clear_player(p)
game.remove_offline_players({p}) game.remove_offline_players({p})
end end

View File

@ -14,6 +14,7 @@ local trusted = {}
local settings = { local settings = {
-- local trusted_value = 2592000 -- 12h -- local trusted_value = 2592000 -- 12h
trusted_value = 5184000, -- 24h 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 nth_tick = 18000 -- nearest prime to 5 minutes in ticks
} }
local set_data = Server.set_data local set_data = Server.set_data
@ -35,7 +36,11 @@ Global.register(
end end
) )
local Public = {} local Public = {
events = {
on_player_removed = Event.generate_event_name('on_player_removed')
}
}
local try_download_data = local try_download_data =
Token.register( Token.register(
@ -48,9 +53,13 @@ local try_download_data =
trusted[key] = true trusted[key] = true
end end
else else
local player = game.get_player(key)
session[key] = 0 session[key] = 0
trusted[key] = false 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
end end
) )
@ -62,12 +71,24 @@ local try_upload_data =
local value = data.value local value = data.value
local player = game.get_player(key) local player = game.get_player(key)
if value then 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 local old_time_ingame = value
if not online_track[key] then if not online_track[key] then
online_track[key] = 0 online_track[key] = 0
end 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] local new_time = old_time_ingame + player.online_time - online_track[key]
if new_time <= 0 then if new_time <= 0 then
new_time = old_time_ingame + player.online_time new_time = old_time_ingame + player.online_time
@ -78,6 +99,13 @@ local try_upload_data =
set_data(session_data_set, key, new_time) set_data(session_data_set, key, new_time)
session[key] = new_time session[key] = new_time
online_track[key] = player.online_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
end end
) )
@ -85,7 +113,8 @@ local try_upload_data =
local nth_tick_token = local nth_tick_token =
Token.register( Token.register(
function(data) function(data)
local player = data.player local index = data.index
local player = game.get_player(index)
if player and player.valid then if player and player.valid then
Public.try_ul_data(player.name) Public.try_ul_data(player.name)
end end
@ -97,10 +126,9 @@ local function upload_data()
local players = game.connected_players local players = game.connected_players
local count = 0 local count = 0
for i = 1, #players do for i = 1, #players do
count = count + 1 count = count + 10
local player = players[i] local player = players[i]
local random_timing = count * 5 set_timeout_in_ticks(count, nth_tick_token, {index = player.index})
set_timeout_in_ticks(random_timing, nth_tick_token, {player = player})
end end
end end
@ -191,18 +219,45 @@ end
--- Clears a given player from the session tables. --- Clears a given player from the session tables.
-- @param LuaPlayer -- @param LuaPlayer
function Public.clear_player(player) function Public.clear_player(player)
local name = player.name if player and player.valid then
if session[name] then local name = player.name
session[name] = nil local connected = player.connected
end
if online_track[name] then if not connected then
online_track[name] = nil if session[name] then
end session[name] = nil
if trusted[name] then end
trusted[name] = nil if online_track[name] then
online_track[name] = nil
end
if trusted[name] then
trusted[name] = nil
end
end
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( Event.add(
defines.events.on_player_joined_game, defines.events.on_player_joined_game,
function(event) function(event)
@ -232,15 +287,20 @@ Event.on_nth_tick(settings.nth_tick, upload_data)
Server.on_data_set_changed( Server.on_data_set_changed(
session_data_set, session_data_set,
function(data) function(data)
session[data.key] = data.value local player = game.get_player(data.key)
if data.value > settings.trusted_value then if player and player.valid then
trusted[data.key] = true session[data.key] = data.value
else if data.value > settings.trusted_value then
if trusted[data.key] then trusted[data.key] = true
trusted[data.key] = false else
if trusted[data.key] then
trusted[data.key] = false
end
end end
end end
end end
) )
Public.upload_data = upload_data
return Public return Public

View File

@ -123,7 +123,7 @@ Gui.on_click(
function(event) function(event)
local element = event.element local element = event.element
local data = Gui.get_data(element) local data = Gui.get_data(element)
if not data or not data.valid then if not data then
return return
end end