diff --git a/config/statistics.lua b/config/statistics.lua index 856af59d..8693e486 100644 --- a/config/statistics.lua +++ b/config/statistics.lua @@ -3,6 +3,7 @@ local e = defines.events -- order as per lua api as it was easier just to go down the list return { + MapsPlayed = true, --- @setting MapsPlayed If the number of maps which a player has played should be tracked Playtime = true, --- @setting Playtime If playtime is tracked for a player, play time measured in minutes AfkTime = true, --- @setting AfkTime If afk time is tracked for a player, play time measured in minutes, afk is once a player does nothing for 5 minutes DistanceTravelled = true, --- @setting DistanceTravelled If distance Travelled is checked, only counts if not afk @@ -21,7 +22,6 @@ return { ItemsPickedUp = e.on_picked_up_item, TilesBuilt = e.on_player_built_tile, ItemsCrafted = e.on_player_crafted_item, - MapsPlayed = e.on_player_created, DeconstructionPlannerUsed = e.on_player_deconstructed_area, Deaths = e.on_player_died, JoinCount = e.on_player_joined_game, diff --git a/modules/data/statistics.lua b/modules/data/statistics.lua index 709610fb..f3549add 100644 --- a/modules/data/statistics.lua +++ b/modules/data/statistics.lua @@ -1,10 +1,17 @@ local Event = require 'utils.event' ---@dep utils.event +local Global = require 'utils.global' ---@dep utils.global local config = require 'config.statistics' ---@dep config.statistics local format_time = _C.format_time local floor = math.floor local afk_required = 5*3600 -- 5 minutes +--- Stores players who have been created, required to avoid loss of data +local new_players, MapsPlayed = {}, nil +Global.register(new_players, function(tbl) + new_players = tbl +end) + --- Stores the statistics on a player local PlayerData = require 'expcore.player_data' --- @dep expcore.player_data local AllPlayerData = PlayerData.All @@ -18,6 +25,7 @@ Statistics:on_load(function(player_name, player_statistics) local existing_data = AllPlayerData:get(player_name) if existing_data and existing_data.valid then return end local counters = config.counters + -- Merge all data from before you data loaded for key, value in pairs(Statistics:get(player_name, {})) do if config[key] or counters[key] then if not player_statistics[key] then @@ -27,6 +35,11 @@ Statistics:on_load(function(player_name, player_statistics) end end end + -- Increment your maps played if this is your first time on this map + if new_players[player_name] then + new_players[player_name] = nil + MapsPlayed:increment(player_name) + end return player_statistics end) @@ -39,6 +52,16 @@ local function format_minutes(value) }) end +--- Add MapsPlayed if it is enabled +if config.MapsPlayed then + MapsPlayed = Statistics:combine('MapsPlayed') + MapsPlayed:set_metadata{unit=' maps'} + Event.add(defines.events.on_player_joined_game, function(event) + local player = game.players[event.player_index] + new_players[player.name] = true + end) +end + --- Add Playtime and AfkTime if it is enabled if config.Playtime or config.AfkTime then local playtime, afk_time