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

370 lines
12 KiB
Lua
Raw Normal View History

2018-10-03 21:17:12 +02:00
--scoreboard by mewmew
2018-09-19 06:51:25 +02:00
2018-10-03 21:17:12 +02:00
local event = require 'utils.event'
2019-10-28 18:38:36 +02:00
local Tabs = require 'comfy_panel.main'
2018-10-03 21:17:12 +02:00
local sorting_symbol = {ascending = "", descending = ""}
2019-10-20 12:52:19 +02:00
local building_and_mining_blacklist = {
["tile-ghost"] = true,
["entity-ghost"] = true,
["item-entity"] = true,
}
2018-09-19 06:51:25 +02:00
2019-06-11 01:52:49 +02:00
local function get_score_list(force)
local score = global.score[force]
local score_list = {}
for _, p in pairs(game.connected_players) do
if score.players[p.name] then
local score = score.players[p.name]
table.insert(score_list, {
name = p.name,
killscore = score.killscore or 0,
deaths = score.deaths or 0,
built_entities = score.built_entities or 0,
mined_entities = score.mined_entities or 0,
})
2018-10-03 21:17:12 +02:00
end
end
return score_list
2018-09-19 06:51:25 +02:00
end
2019-06-11 01:52:49 +02:00
local function get_sorted_list(method, column_name, score_list)
local comparators = {
["ascending"] = function(a,b) return a[column_name] < b[column_name] end,
["descending"] = function(a,b) return a[column_name] > b[column_name] end
}
table.sort(score_list, comparators[method])
return score_list
end
2018-10-25 13:00:38 +02:00
local biters = {"small-biter", "medium-biter", "big-biter", "behemoth-biter", "small-spitter", "medium-spitter", "big-spitter", "behemoth-spitter"}
local function get_total_biter_killcount(force)
local count = 0
for _, biter in pairs(biters) do
count = count + force.kill_count_statistics.get_input_count(biter)
end
return count
end
2019-06-11 01:52:49 +02:00
local function add_global_stats(frame, player)
2018-10-03 21:17:12 +02:00
local score = global.score[player.force.name]
local t = frame.add { type = "table", column_count = 5}
2019-06-11 01:52:49 +02:00
local l = t.add { type = "label", caption = "Rockets launched: "}
l.style.font = "default-game"
2018-10-03 21:17:12 +02:00
l.style.font_color = {r = 175, g = 75, b = 255}
l.style.minimal_width = 140
2019-06-11 01:52:49 +02:00
local l = t.add { type = "label", caption = player.force.rockets_launched}
2019-02-26 21:05:31 +02:00
l.style.font = "default-listbox"
2018-10-03 21:17:12 +02:00
l.style.font_color = { r=0.9, g=0.9, b=0.9}
l.style.minimal_width = 123
2018-09-19 06:51:25 +02:00
2019-10-20 12:52:19 +02:00
local l = t.add { type = "label", caption = "Dead bugs: "}
2019-06-11 01:52:49 +02:00
l.style.font = "default-game"
2018-10-03 21:17:12 +02:00
l.style.font_color = { r=0.90, g=0.3, b=0.3}
l.style.minimal_width = 100
2019-06-11 01:52:49 +02:00
2018-10-25 13:00:38 +02:00
local l = t.add { type = "label", caption = tostring(get_total_biter_killcount(player.force))}
2019-02-26 21:05:31 +02:00
l.style.font = "default-listbox"
2018-10-03 21:17:12 +02:00
l.style.font_color = { r=0.9, g=0.9, b=0.9}
l.style.minimal_width = 145
2019-06-11 01:52:49 +02:00
local l = t.add { type = "checkbox", caption = "Show floating numbers", state = global.show_floating_killscore[player.name], name = "show_floating_killscore_texts" }
2019-06-11 01:52:49 +02:00
l.style.font_color = { r=0.8, g=0.8, b=0.8}
end
2019-10-28 18:38:36 +02:00
local show_score = (function (player, frame)
2019-10-20 12:52:19 +02:00
frame.clear()
2019-06-11 01:52:49 +02:00
-- Global stats : rockets, biters kills
add_global_stats(frame, player)
-- Separator
2019-10-20 12:52:19 +02:00
local line = frame.add { type = "line"}
line.style.top_margin = 8
line.style.bottom_margin = 8
2019-06-11 01:52:49 +02:00
-- Score per player
local t = frame.add { type = "table", column_count = 5 }
-- Score headers
local headers = {
{ name = "score_player", caption = "Player" },
{ column = "killscore", name = "score_killscore", caption = "Killscore" },
{ column = "deaths", name = "score_deaths", caption = "Deaths" },
{ column = "built_entities", name = "score_built_entities", caption = "Built structures" },
{ column = "mined_entities", name = "score_mined_entities", caption = "Mined entities" }
}
local sorting_pref = global.score_sort_by[player.name]
for _, header in ipairs(headers) do
local cap = header.caption
-- Add sorting symbol if any
if header.column and sorting_pref.column == header.column then
local symbol = sorting_symbol[sorting_pref.method]
cap = symbol .. cap
end
2019-06-11 01:52:49 +02:00
-- Header
local label = t.add {
type = "label",
caption = cap,
name = header.name
}
label.style.font = "default-listbox"
label.style.font_color = { r=0.98, g=0.66, b=0.22 } -- yellow
2019-10-20 12:52:19 +02:00
label.style.minimal_width = 150
label.style.horizontal_align = "right"
2018-10-03 21:17:12 +02:00
end
2019-06-11 01:52:49 +02:00
-- Score list
local score_list = get_score_list(player.force.name)
2018-10-03 21:17:12 +02:00
if #game.connected_players > 1 then
score_list = get_sorted_list(sorting_pref.method, sorting_pref.column, score_list)
2019-06-11 01:52:49 +02:00
end
-- New pane for scores (while keeping headers at same position)
2018-10-06 11:29:53 +02:00
local scroll_pane = frame.add({ type = "scroll-pane", name = "score_scroll_pane", direction = "vertical", horizontal_scroll_policy = "never", vertical_scroll_policy = "auto"})
2019-06-11 01:52:49 +02:00
scroll_pane.style.maximal_height = 400
local t = scroll_pane.add { type = "table", column_count = 5}
2019-06-11 01:52:49 +02:00
-- Score entries
for _, entry in pairs(score_list) do
2018-10-03 21:17:12 +02:00
local p = game.players[entry.name]
2019-06-11 01:52:49 +02:00
local special_color = {
r = p.color.r * 0.6 + 0.4,
g = p.color.g * 0.6 + 0.4,
b = p.color.b * 0.6 + 0.4,
a = 1
}
local line = {
{ caption = entry.name, color = special_color },
{ caption = tostring(entry.killscore) },
{ caption = tostring(entry.deaths) },
{ caption = tostring(entry.built_entities) },
{ caption = tostring(entry.mined_entities) }
}
local default_color = { r=0.9, g=0.9, b=0.9 }
for _, column in ipairs(line) do
local label = t.add {
type = "label",
caption = column.caption,
color = column.color or default_color
}
label.style.font = "default"
2019-10-20 12:52:19 +02:00
label.style.minimal_width = 150
label.style.maximal_width = 150
label.style.horizontal_align = "right"
2019-06-11 01:52:49 +02:00
end -- foreach column
end -- foreach entry
end -- show_score
2019-10-28 18:38:36 +02:00
)
2018-10-03 21:17:12 +02:00
2018-10-06 11:29:53 +02:00
local function refresh_score_full()
for _, player in pairs(game.connected_players) do
2019-10-28 18:38:36 +02:00
local frame = Tabs.comfy_panel_get_active_frame(player)
2019-10-20 12:52:19 +02:00
if frame then
if frame.name == "Scoreboard" then
show_score(player, frame)
2019-06-11 01:52:49 +02:00
end
2018-10-03 21:17:12 +02:00
end
end
2018-09-19 06:51:25 +02:00
end
2018-10-25 14:13:15 +02:00
local function init_player_table(player)
2018-11-26 18:50:18 +02:00
if not player then return end
2018-10-25 14:13:15 +02:00
if not global.score[player.force.name] then global.score[player.force.name] = {} end
if not global.score[player.force.name].players then global.score[player.force.name].players = {} end
2019-10-22 05:28:45 +02:00
if not global.score[player.force.name].players[player.name] then
global.score[player.force.name].players[player.name] = {
built_entities = 0,
deaths = 0,
killscore = 0,
mined_entities = 0,
2019-10-28 18:38:36 +02:00
}
2019-10-22 05:28:45 +02:00
end
2018-10-25 14:13:15 +02:00
end
2018-10-03 21:17:12 +02:00
local function on_player_joined_game(event)
local player = game.players[event.player_index]
if not global.score then global.score = {} end
2019-06-11 01:52:49 +02:00
init_player_table(player)
2018-10-03 21:17:12 +02:00
if not global.score_sort_by then global.score_sort_by = {} end
if not global.score_sort_by[player.name] then
global.score_sort_by[player.name] = {method = "descending", column = "killscore"}
end
if not global.show_floating_killscore then global.show_floating_killscore = {} end
2019-06-11 01:52:49 +02:00
if not global.show_floating_killscore[player.name] then global.show_floating_killscore[player.name] = false end
2018-10-03 21:17:12 +02:00
end
2018-09-19 06:51:25 +02:00
local function on_gui_click(event)
if not event then return end
if not event.element then return end
if not event.element.valid then return end
2019-06-11 01:52:49 +02:00
2018-09-19 06:51:25 +02:00
local player = game.players[event.element.player_index]
2019-10-28 18:38:36 +02:00
local frame = Tabs.comfy_panel_get_active_frame(player)
2019-10-20 12:52:19 +02:00
if not frame then return end
if frame.name ~= "Scoreboard" then return end
2019-06-11 01:52:49 +02:00
local name = event.element.name
-- Handles click on the checkbox, for floating score
if name == "show_floating_killscore_texts" then
2019-06-11 01:52:49 +02:00
global.show_floating_killscore[player.name] = event.element.state
return
end
-- Handles click on a score header
local element_to_column = {
["score_killscore"] = "killscore",
["score_deaths"] = "deaths",
["score_built_entities"] = "built_entities",
["score_mined_entities"] = "mined_entities"
}
local column = element_to_column[name]
if column then
local sorting_pref = global.score_sort_by[player.name]
if sorting_pref.column == column and sorting_pref.method == "descending" then
sorting_pref.method = "ascending"
else
2019-06-11 01:52:49 +02:00
sorting_pref.method = "descending"
sorting_pref.column = column
end
2019-10-20 12:52:19 +02:00
show_score(player, frame)
2018-10-06 11:29:53 +02:00
return
end
2019-06-11 01:52:49 +02:00
-- No more to handle
2018-10-03 21:17:12 +02:00
end
local function on_rocket_launched(event)
refresh_score_full()
2018-10-03 21:17:12 +02:00
end
2019-10-22 05:28:45 +02:00
local entity_score_values = {
2018-10-03 21:17:12 +02:00
["behemoth-biter"] = 100,
["behemoth-spitter"] = 100,
2019-10-22 05:28:45 +02:00
["behemoth-worm-turret"] = 300,
["big-biter"] = 30,
["big-spitter"] = 30,
["big-worm-turret"] = 300,
2018-10-03 21:17:12 +02:00
["biter-spawner"] = 200,
2019-10-22 05:28:45 +02:00
["medium-biter"] = 15,
["medium-spitter"] = 15,
2018-10-03 21:17:12 +02:00
["medium-worm-turret"] = 150,
2019-10-22 05:28:45 +02:00
["character"] = 1000,
["small-biter"] = 5,
["small-spitter"] = 5,
["small-worm-turret"] = 50,
["spitter-spawner"] = 200,
["gun-turret"] = 50,
["laser-turret"] = 150,
["flamethrower-turret"] = 300,
2018-10-03 21:17:12 +02:00
}
2019-10-22 05:28:45 +02:00
local function train_type_cause(event)
local players = {}
if event.cause.train.passengers then
for _, player in pairs(event.cause.train.passengers) do
players[#players + 1] = player
2018-10-03 21:17:12 +02:00
end
2019-10-22 05:28:45 +02:00
end
return players
end
2019-06-11 01:52:49 +02:00
2019-10-22 05:28:45 +02:00
local kill_causes = {
["character"] =
function(event)
if not event.cause.player then return end
return {event.cause.player}
end,
["combat-robot"] =
function(event)
if not event.cause.last_user then return end
if not game.players[event.cause.last_user.index] then return end
return {game.players[event.cause.last_user.index]}
end,
2019-10-22 05:28:45 +02:00
["car"] =
function(event)
local players = {}
local driver = event.cause.get_driver()
if driver then
if driver.player then players[#players + 1] = driver.player end
2019-06-11 01:52:49 +02:00
end
2019-10-22 05:28:45 +02:00
local passenger = event.cause.get_passenger()
if passenger then
if passenger.player then players[#players + 1] = passenger.player end
2019-10-12 08:30:38 +02:00
end
2019-10-22 05:28:45 +02:00
return players
end,
["locomotive"] = train_type_cause,
["cargo-wagon"] = train_type_cause,
["artillery-wagon"] = train_type_cause,
["fluid-wagon"] = train_type_cause,
}
2019-06-11 01:52:49 +02:00
2019-10-22 05:28:45 +02:00
local function on_entity_died(event)
if not event.entity.valid then return end
if not event.cause then return end
if not event.cause.valid then return end
if event.entity.force.index == event.cause.force.index then return end
if not entity_score_values[event.entity.name] then return end
if not kill_causes[event.cause.type] then return end
local players_to_reward = kill_causes[event.cause.type](event)
if not players_to_reward then return end
2019-10-23 06:01:19 +02:00
if #players_to_reward == 0 then return end
2019-10-22 05:28:45 +02:00
for _, player in pairs(players_to_reward) do
init_player_table(player)
2019-10-23 20:57:10 +02:00
local score = global.score[player.force.name].players[player.name]
2019-10-22 05:28:45 +02:00
score.killscore = score.killscore + entity_score_values[event.entity.name]
if global.show_floating_killscore[player.name] then
event.entity.surface.create_entity({name = "flying-text", position = event.entity.position, text = tostring(entity_score_values[event.entity.name]), color = player.chat_color})
end
end
2019-10-22 05:28:45 +02:00
end
2018-10-03 21:17:12 +02:00
local function on_player_died(event)
local player = game.players[event.player_index]
2018-10-25 14:13:15 +02:00
init_player_table(player)
2019-06-11 01:52:49 +02:00
local score = global.score[player.force.name].players[player.name]
score.deaths = 1 + (score.deaths or 0)
2018-09-19 06:51:25 +02:00
end
2018-10-03 21:17:12 +02:00
local function on_player_mined_entity(event)
2019-10-20 12:52:19 +02:00
if not event.entity.valid then return end
if building_and_mining_blacklist[event.entity.type] then return end
local player = game.players[event.player_index]
2018-10-25 14:13:15 +02:00
init_player_table(player)
2019-06-11 01:52:49 +02:00
local score = global.score[player.force.name].players[player.name]
score.mined_entities = 1 + (score.mined_entities or 0)
2018-10-03 21:17:12 +02:00
end
local function on_built_entity(event)
2019-01-04 10:06:26 +02:00
if not event.created_entity.valid then return end
2019-10-20 12:52:19 +02:00
if building_and_mining_blacklist[event.created_entity.type] then return end
2018-10-03 21:17:12 +02:00
local player = game.players[event.player_index]
2018-10-25 14:13:15 +02:00
init_player_table(player)
2019-06-11 01:52:49 +02:00
local score = global.score[player.force.name].players[player.name]
score.built_entities = 1 + (score.built_entities or 0)
2018-10-06 23:49:29 +02:00
end
--[[
local function tick(event)
refresh_score_full()
2018-09-19 06:51:25 +02:00
end
]]
2019-10-20 12:52:19 +02:00
comfy_panel_tabs["Scoreboard"] = show_score
--event.on_nth_tick(300, tick)
2018-10-03 21:17:12 +02:00
event.add(defines.events.on_player_mined_entity, on_player_mined_entity)
event.add(defines.events.on_player_died, on_player_died)
event.add(defines.events.on_built_entity, on_built_entity)
event.add(defines.events.on_entity_died, on_entity_died)
event.add(defines.events.on_gui_click, on_gui_click)
event.add(defines.events.on_player_joined_game, on_player_joined_game)
2019-10-20 12:52:19 +02:00
event.add(defines.events.on_rocket_launched, on_rocket_launched)