1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-20 03:29:26 +02:00

Game statistics and player awards for danger ores. (#1305)

This commit is contained in:
Jayefuu 2022-04-01 12:22:33 +01:00 committed by GitHub
parent 69c90069a1
commit 50c1bbdf47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 158 additions and 1 deletions

View File

@ -363,6 +363,7 @@ stds.factorio_control = {
"force_crc",
"get_active_entities_count",
"get_entity_by_tag",
"get_filtered_entity_prototypes",
"get_map_exchange_string",
"get_player",
"get_surface",

View File

@ -29,6 +29,9 @@ local player_total_kills_name = 'player-total-kills'
local player_turrets_killed_name = 'player-turrets-killed'
local player_entities_built_name = 'player_entities_built'
local player_fish_eaten_name = 'player_fish_eaten'
local player_resources_hand_mined_name = 'player_resources_hand_mined'
local resources_hand_mined_name = 'resources_hand_mined'
local resources_exhausted_name = 'resources_exhausted'
ScoreTracker.register(rocks_smashed_name, {'player_stats.rocks_smashed'}, '[img=entity.rock-huge]')
ScoreTracker.register(trees_cut_down_name, {'player_stats.trees_cut_down'}, '[img=entity.tree-02]')
@ -51,6 +54,9 @@ ScoreTracker.register(player_turrets_killed_name, {'player_stats.player_turrets_
ScoreTracker.register(player_total_kills_name, {'player_stats.player_total_kills'})
ScoreTracker.register(player_entities_built_name, {'player_stats.player_entities_built'})
ScoreTracker.register(player_fish_eaten_name, {'player_stats.player_fish_eaten'})
ScoreTracker.register(player_resources_hand_mined_name, {'player_stats.player_resources_hand_mined'})
ScoreTracker.register(resources_hand_mined_name, {'player_stats.resources_hand_mined'})
ScoreTracker.register(resources_exhausted_name, {'player_stats.resources_exhausted'})
local train_kill_causes = {
['locomotive'] = true,
@ -151,6 +157,10 @@ local function pre_player_mined_item(event)
change_for_global(rocks_smashed_name, 1)
return
end
if entity.type == 'resource' then
change_for_global(resources_hand_mined_name, 1)
change_for_player(event.player_index, player_resources_hand_mined_name, 1)
end
if entity.type == 'tree' then
change_for_global(trees_cut_down_name, 1)
return
@ -164,6 +174,13 @@ local function pre_player_mined_item(event)
end
end
local function resource_depleted(event)
local entity = event.entity
if entity.amount == 0 then
change_for_global(resources_exhausted_name, 1)
end
end
local function player_crafted_item(event)
local item_stack = event.item_stack
if not item_stack or not item_stack.valid_for_read then
@ -333,6 +350,7 @@ Event.add(defines.events.on_robot_built_entity, robot_built_entity)
Event.add(defines.events.on_entity_died, entity_died)
Event.add(defines.events.on_rocket_launched, rocket_launched)
Event.add(defines.events.on_player_used_capsule, capsule_used)
Event.add(defines.events.on_resource_depleted, resource_depleted)
Event.on_nth_tick(62, tick)
@ -357,7 +375,10 @@ local Public = {
player_total_kills_name = player_total_kills_name,
player_turrets_killed_name = player_turrets_killed_name,
player_entities_built_name = player_entities_built_name,
player_fish_eaten_name = player_fish_eaten_name
player_fish_eaten_name = player_fish_eaten_name,
player_resources_hand_mined_name = player_resources_hand_mined_name,
resources_hand_mined_name = resources_hand_mined_name,
resources_exhausted_name = resources_exhausted_name
}
-- Returns a dictionary of cause_name -> count

View File

@ -3,10 +3,16 @@ local Server = require 'features.server'
local Core = require 'utils.core'
local Restart = require 'features.restart_command'
local ShareGlobals = require 'map_gen.maps.danger_ores.modules.shared_globals'
local ScoreTracker = require 'utils.score_tracker'
local PlayerStats = require 'features.player_stats'
local format_number = require 'util'.format_number
return function(config)
local map_promotion_channel = Discord.channel_names.map_promotion
local danger_ore_role_mention = Discord.role_mentions.danger_ore
-- Use these settings for testing
--local map_promotion_channel = Discord.channel_names.bot_playground
--local danger_ore_role_mention = Discord.role_mentions.test
Restart.set_start_game_data({type = Restart.game_types.scenario, name = config.scenario_name or 'danger-ore-next'})
@ -29,6 +35,135 @@ return function(config)
local time_string = Core.format_time(game.ticks_played)
local end_epoch = Server.get_current_time()
if end_epoch == nil then
end_epoch = -1 -- end_epoch is nil if the restart command is used locally rather than on the server
end
local player_data = {}
for _, p in pairs(game.players) do
player_data[p.index] = {
name = p.name,
total_kills = ScoreTracker.get_for_player(p.index, PlayerStats.player_total_kills_name),
spawners_killed = ScoreTracker.get_for_player(p.index, PlayerStats.player_spawners_killed_name),
worms_killed = ScoreTracker.get_for_player(p.index, PlayerStats.player_worms_killed_name),
units_killed = ScoreTracker.get_for_player(p.index, PlayerStats.player_units_killed_name),
turrets_killed = ScoreTracker.get_for_player(p.index, PlayerStats.player_turrets_killed_name),
distance_walked = ScoreTracker.get_for_player(p.index, PlayerStats.player_distance_walked_name),
player_deaths = ScoreTracker.get_for_player(p.index, PlayerStats.player_deaths_name),
entities_built = ScoreTracker.get_for_player(p.index, PlayerStats.player_entities_built_name),
entities_crafted = ScoreTracker.get_for_player(p.index, PlayerStats.player_items_crafted_name),
resources_hand_mined = ScoreTracker.get_for_player(p.index, PlayerStats.player_resources_hand_mined_name),
time_played = p.online_time
}
end
local statistics = {
scenario = config.scenario_name,
start_epoch = Server.get_start_time(),
end_epoch = end_epoch, -- stored as key already, useful to have it as part of same structure
game_ticks = game.ticks_played,
biters_killed = ScoreTracker.get_for_global(PlayerStats.aliens_killed_name),
total_players = #game.players,
entities_built = ScoreTracker.get_for_global(PlayerStats.built_by_players_name),
resources_exhausted = ScoreTracker.get_for_global(PlayerStats.resources_exhausted_name),
resources_hand_mined = ScoreTracker.get_for_global(PlayerStats.resources_hand_mined_name),
player_data = player_data
}
local awards = {
['total_kills'] = {value = 0, player = ""},
['units_killed'] = {value = 0, player = ""},
['spawners_killed'] = {value = 0, player = ""},
['worms_killed'] = {value = 0, player = ""},
['player_deaths'] = {value = 0, player = ""},
['time_played'] = {value = 0, player = ""},
['entities_built'] = {value = 0, player = ""},
['entities_crafted'] = {value = 0, player = ""},
['distance_walked'] = {value = 0, player = ""},
['resources_hand_mined'] = {value = 0, player = ""}
}
for _, v in pairs(statistics.player_data) do
if v.total_kills > awards.total_kills.value then
awards.total_kills.value = v.total_kills
awards.total_kills.player = v.name
end
if v.units_killed > awards.units_killed.value then
awards.units_killed.value = v.units_killed
awards.units_killed.player = v.name
end
if v.spawners_killed > awards.spawners_killed.value then
awards.spawners_killed.value = v.spawners_killed
awards.spawners_killed.player = v.name
end
if v.worms_killed > awards.worms_killed.value then
awards.worms_killed.value = v.worms_killed
awards.worms_killed.player = v.name
end
if v.player_deaths > awards.player_deaths.value then
awards.player_deaths.value = v.player_deaths
awards.player_deaths.player = v.name
end
if v.time_played > awards.time_played.value then
awards.time_played.value = v.time_played
awards.time_played.player = v.name
end
if v.entities_built > awards.entities_built.value then
awards.entities_built.value = v.entities_built
awards.entities_built.player = v.name
end
if v.entities_crafted > awards.entities_crafted.value then
awards.entities_crafted.value = v.entities_crafted
awards.entities_crafted.player = v.name
end
if v.distance_walked > awards.distance_walked.value then
awards.distance_walked.value = v.distance_walked
awards.distance_walked.player = v.name
end
if v.resources_hand_mined > awards.resources_hand_mined.value then
awards.resources_hand_mined.value = v.resources_hand_mined
awards.resources_hand_mined.player = v.name
end
end
local resource_prototypes = game.get_filtered_entity_prototypes({{filter = "type", type = "resource"}})
local total_ore = 0
local ore_totals_message = '('
for ore_name,ore_prototype in pairs(resource_prototypes) do
if ore_prototype.resource_category == 'basic-solid' then
local count = game.forces["player"].item_production_statistics.get_input_count(ore_name)
total_ore = total_ore + count
ore_totals_message = ore_totals_message..ore_name:gsub( "-ore", "")..": "..format_number(count, true)..", "
end
end
ore_totals_message = ore_totals_message:sub(1, -3)..')' -- remove the last ", " and add a bracket
ore_totals_message = "Total ore mined: "..format_number(total_ore, true).. "\\n"..ore_totals_message
Server.to_discord_named_embed(map_promotion_channel, 'Danger ores map completed!\\n\\n'
.. 'Statistics:\\n'
.. 'Map time: '..time_string..'\\n'
.. 'Total entities built: '..statistics.entities_built..'\\n'
.. 'Total ore mined:'..ore_totals_message..'\\n'
.. 'Total ore resources exhausted: '..statistics.resources_exhausted..'\\n'
.. 'Total ore hand mined: '..statistics.resources_hand_mined..'\\n'
.. 'Players: '..statistics.total_players..'\\n'
.. 'Enemies killed: '..statistics.biters_killed..'\\n\\n'
.. 'Awards:\\n'
.. 'Most ore hand mined:'..awards.resources_hand_mined.player..' ('..awards.resources_hand_mined.value..')\\n'
.. 'Most items crafted: '..awards.entities_crafted.player..' ('..awards.entities_crafted.value..')\\n'
.. 'Most entities built: '..awards.entities_built.player..' ('..awards.entities_built.value..')\\n'
.. 'Most time played: '..awards.time_played.player..' ('..Core.format_time(awards.time_played.value)..')\\n'
.. 'Furthest walked: '..awards.distance_walked.player..' ('..math.floor(awards.distance_walked.value)..')\\n'
.. 'Most deaths: '..awards.player_deaths.player..' ('..awards.player_deaths.value..')\\n'
.. 'Most kills overall: '..awards.total_kills.player..' ('..awards.total_kills.value..')\\n'
.. 'Most biters/spitters killed: '..awards.units_killed.player..' ('..awards.units_killed.value..')\\n'
.. 'Most spawners killed: '..awards.spawners_killed.player..' ('..awards.spawners_killed.value..')\\n'
.. 'Most worms killed: '..awards.worms_killed.player..' ('..awards.worms_killed.value..')\\n'
)
Server.set_data('danger_ores_data', tostring(end_epoch), statistics)
local message = {
danger_ore_role_mention,
' **Danger Ore has just restarted! Previous map lasted: ',