2018-06-27 15:48:04 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Global = require 'utils.global'
|
2018-09-23 00:25:13 +02:00
|
|
|
local Game = require 'utils.game'
|
2018-12-15 22:28:53 +02:00
|
|
|
require 'utils.table'
|
2018-12-10 22:06:39 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local sqrt = math.sqrt
|
2018-06-27 15:48:04 +02:00
|
|
|
|
2018-06-28 15:49:22 +02:00
|
|
|
local player_last_position = {}
|
2018-06-27 15:48:04 +02:00
|
|
|
local player_walk_distances = {}
|
2018-08-01 17:35:48 +02:00
|
|
|
local player_coin_earned = {}
|
|
|
|
local player_coin_spent = {}
|
2018-12-15 22:28:53 +02:00
|
|
|
local player_crafted_items = {}
|
|
|
|
local player_console_chats = {}
|
2018-06-27 15:48:04 +02:00
|
|
|
local player_deaths = {}
|
2018-06-29 00:50:38 +02:00
|
|
|
local total_players = {0}
|
2018-12-15 22:28:53 +02:00
|
|
|
local total_train_kills = {0}
|
|
|
|
local total_player_trees_mined = {0}
|
|
|
|
local total_player_rocks_mined = {0}
|
|
|
|
local total_robot_built_entities = {0}
|
|
|
|
local total_player_built_entities = {0}
|
|
|
|
local total_biter_kills = {0}
|
2019-01-21 22:36:32 +02:00
|
|
|
local total_coins_spent = {0}
|
2018-12-15 22:28:53 +02:00
|
|
|
|
|
|
|
local train_kill_causes = {
|
2019-01-01 23:26:57 +02:00
|
|
|
['locomotive'] = true,
|
|
|
|
['cargo-wagon'] = true,
|
|
|
|
['fluid-wagon'] = true,
|
|
|
|
['artillery-wagon'] = true
|
2018-12-15 22:28:53 +02:00
|
|
|
}
|
2018-06-27 15:48:04 +02:00
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
2018-06-28 15:49:22 +02:00
|
|
|
player_last_position = player_last_position,
|
2018-06-27 15:48:04 +02:00
|
|
|
player_walk_distances = player_walk_distances,
|
2018-08-01 17:35:48 +02:00
|
|
|
player_coin_earned = player_coin_earned,
|
|
|
|
player_coin_spent = player_coin_spent,
|
2018-06-29 00:50:38 +02:00
|
|
|
player_deaths = player_deaths,
|
2018-12-15 22:28:53 +02:00
|
|
|
total_players = total_players,
|
|
|
|
total_train_kills = total_train_kills,
|
|
|
|
total_player_trees_mined = total_player_trees_mined,
|
|
|
|
total_player_rocks_mined = total_player_rocks_mined,
|
|
|
|
player_crafted_items = player_crafted_items,
|
|
|
|
player_console_chats = player_console_chats,
|
|
|
|
total_robot_built_entities = total_robot_built_entities,
|
|
|
|
total_player_built_entities = total_player_built_entities,
|
2019-01-21 22:36:32 +02:00
|
|
|
total_biter_kills = total_biter_kills,
|
|
|
|
total_coins_spent = total_coins_spent
|
2018-06-27 15:48:04 +02:00
|
|
|
},
|
|
|
|
function(tbl)
|
2018-06-28 15:49:22 +02:00
|
|
|
player_last_position = tbl.player_last_position
|
2018-06-27 15:48:04 +02:00
|
|
|
player_walk_distances = tbl.player_walk_distances
|
2018-08-01 17:35:48 +02:00
|
|
|
player_coin_earned = tbl.player_coin_earned
|
|
|
|
player_coin_spent = tbl.player_coin_spent
|
2018-06-27 15:48:04 +02:00
|
|
|
player_deaths = tbl.player_deaths
|
2018-06-29 00:50:38 +02:00
|
|
|
total_players = tbl.total_players
|
2018-12-15 22:28:53 +02:00
|
|
|
total_train_kills = tbl.total_train_kills
|
|
|
|
total_player_trees_mined = tbl.total_player_trees_mined
|
|
|
|
total_player_rocks_mined = tbl.total_player_rocks_mined
|
|
|
|
player_crafted_items = tbl.player_crafted_items
|
|
|
|
player_console_chats = tbl.player_console_chats
|
|
|
|
total_robot_built_entities = tbl.total_robot_built_entities
|
|
|
|
total_player_built_entities = tbl.total_player_built_entities
|
|
|
|
total_biter_kills = tbl.total_biter_kills
|
2019-01-21 22:36:32 +02:00
|
|
|
total_coins_spent = tbl.total_coins_spent
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-12-15 22:28:53 +02:00
|
|
|
--- When the player first logs on, initialize their stats and pull their former playtime
|
2018-06-27 15:48:04 +02:00
|
|
|
local function player_created(event)
|
|
|
|
local index = event.player_index
|
|
|
|
|
2018-09-23 12:46:58 +02:00
|
|
|
player_last_position[index] = Game.get_player_by_index(index).position
|
2018-06-27 15:48:04 +02:00
|
|
|
player_walk_distances[index] = 0
|
2018-08-01 17:35:48 +02:00
|
|
|
player_coin_earned[index] = 0
|
|
|
|
player_coin_spent[index] = 0
|
2018-12-15 22:28:53 +02:00
|
|
|
player_crafted_items[index] = 0
|
2018-12-17 19:04:46 +02:00
|
|
|
player_console_chats[index] = 0
|
2018-06-27 15:48:04 +02:00
|
|
|
player_deaths[index] = {causes = {}, count = 0}
|
2018-06-29 00:50:38 +02:00
|
|
|
total_players[1] = total_players[1] + 1
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_cause_name(cause)
|
|
|
|
if cause then
|
|
|
|
local name = cause.name
|
|
|
|
if name == 'player' then
|
2018-06-28 15:49:22 +02:00
|
|
|
local player = cause.player
|
2018-06-27 15:48:04 +02:00
|
|
|
if player and player.valid then
|
|
|
|
return player.name
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 'No cause'
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_died(event)
|
|
|
|
local player_index = event.player_index
|
|
|
|
local cause = get_cause_name(event.cause)
|
|
|
|
|
|
|
|
local data = player_deaths[player_index]
|
|
|
|
data.count = data.count + 1
|
|
|
|
|
|
|
|
local causes = data.causes
|
|
|
|
local cause_count = causes[cause] or 0
|
|
|
|
causes[cause] = cause_count + 1
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-01 23:26:57 +02:00
|
|
|
if train_kill_causes[cause] then
|
|
|
|
total_train_kills[1] = total_train_kills[1] + 1
|
2018-12-15 22:28:53 +02:00
|
|
|
end
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-06-29 01:44:20 +02:00
|
|
|
local function picked_up_item(event)
|
|
|
|
local stack = event.item_stack
|
2018-08-01 17:35:48 +02:00
|
|
|
if stack.name == 'coin' then
|
2018-06-29 01:44:20 +02:00
|
|
|
local player_index = event.player_index
|
2018-08-01 17:35:48 +02:00
|
|
|
player_coin_earned[player_index] = player_coin_earned[player_index] + stack.count
|
2018-06-29 01:44:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-15 22:28:53 +02:00
|
|
|
local function player_mined_item(event)
|
|
|
|
if event.entity.type == 'simple-entity' then -- Cheap check for rock, may have other side effects
|
|
|
|
total_player_rocks_mined[1] = total_player_rocks_mined[1] + 1
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if event.entity.type == 'tree' then
|
|
|
|
total_player_trees_mined[1] = total_player_trees_mined[1] + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_crafted_item(event)
|
|
|
|
local stack = event.item_stack
|
|
|
|
local player_index = event.player_index
|
|
|
|
player_crafted_items[player_index] = player_crafted_items[player_index] + stack.count
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_console_chat(event)
|
|
|
|
local player_index = event.player_index
|
|
|
|
if player_index then
|
|
|
|
player_console_chats[player_index] = player_console_chats[player_index] + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function player_built_entity()
|
|
|
|
total_player_built_entities[1] = total_player_built_entities[1] + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local function robot_built_entity()
|
|
|
|
total_robot_built_entities[1] = total_robot_built_entities[1] + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local function biter_kill_counter(event)
|
|
|
|
if event.entity.force.name == 'enemy' then
|
|
|
|
total_biter_kills[1] = total_biter_kills[1] + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-27 15:48:04 +02:00
|
|
|
local function tick()
|
2018-12-10 22:06:39 +02:00
|
|
|
for _, p in pairs(game.connected_players) do
|
2018-06-27 15:48:04 +02:00
|
|
|
if (p.afk_time < 30 or p.walking_state.walking) and p.vehicle == nil then
|
|
|
|
local index = p.index
|
2018-06-28 15:49:22 +02:00
|
|
|
local last_pos = player_last_position[index]
|
2018-06-27 15:48:04 +02:00
|
|
|
local pos = p.position
|
|
|
|
|
|
|
|
local d_x = last_pos.x - pos.x
|
|
|
|
local d_y = last_pos.y - pos.y
|
|
|
|
|
2018-12-10 22:06:39 +02:00
|
|
|
player_walk_distances[index] = player_walk_distances[index] + sqrt(d_x * d_x + d_y * d_y)
|
2018-06-28 15:49:22 +02:00
|
|
|
player_last_position[index] = pos
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(defines.events.on_player_created, player_created)
|
|
|
|
Event.add(defines.events.on_player_died, player_died)
|
2018-12-10 22:06:39 +02:00
|
|
|
Event.add(defines.events.on_player_mined_item, picked_up_item)
|
2018-06-29 01:44:20 +02:00
|
|
|
Event.add(defines.events.on_picked_up_item, picked_up_item)
|
2018-12-15 22:28:53 +02:00
|
|
|
Event.add(defines.events.on_pre_player_mined_item, player_mined_item)
|
|
|
|
Event.add(defines.events.on_player_crafted_item, player_crafted_item)
|
|
|
|
Event.add(defines.events.on_console_chat, player_console_chat)
|
|
|
|
Event.add(defines.events.on_built_entity, player_built_entity)
|
|
|
|
Event.add(defines.events.on_robot_built_entity, robot_built_entity)
|
|
|
|
Event.add(defines.events.on_entity_died, biter_kill_counter)
|
|
|
|
|
2018-06-27 15:48:04 +02:00
|
|
|
Event.on_nth_tick(62, tick)
|
|
|
|
|
|
|
|
local Public = {}
|
|
|
|
|
|
|
|
function Public.get_walk_distance(player_index)
|
|
|
|
return player_walk_distances[player_index]
|
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.get_coin_earned(player_index)
|
|
|
|
return player_coin_earned[player_index]
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.set_coin_earned(player_index, value)
|
|
|
|
player_coin_earned[player_index] = value
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.change_coin_earned(player_index, amount)
|
|
|
|
player_coin_earned[player_index] = player_coin_earned[player_index] + amount
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.get_coin_spent(player_index)
|
|
|
|
return player_coin_spent[player_index]
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.set_coin_spent(player_index, value)
|
2019-01-21 22:36:32 +02:00
|
|
|
local old_value = player_coin_spent[player_index]
|
2018-08-01 17:35:48 +02:00
|
|
|
player_coin_spent[player_index] = value
|
2019-01-21 22:36:32 +02:00
|
|
|
|
|
|
|
local diff = value - old_value
|
|
|
|
total_coins_spent[1] = total_coins_spent[1] + diff
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
2018-08-01 17:35:48 +02:00
|
|
|
function Public.change_coin_spent(player_index, amount)
|
|
|
|
player_coin_spent[player_index] = player_coin_spent[player_index] + amount
|
2019-01-21 22:36:32 +02:00
|
|
|
total_coins_spent[1] = total_coins_spent[1] + amount
|
2018-06-27 15:48:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_death_count(player_index)
|
|
|
|
return player_deaths[player_index].count
|
|
|
|
end
|
|
|
|
|
2018-12-06 13:18:52 +02:00
|
|
|
-- Returns a dictionary of cause_name -> count
|
|
|
|
function Public.get_all_death_counts_by_cause(player_index)
|
2018-06-27 15:48:04 +02:00
|
|
|
return player_deaths[player_index].causes or {}
|
|
|
|
end
|
|
|
|
|
2018-06-29 00:50:38 +02:00
|
|
|
function Public.get_total_player_count()
|
|
|
|
return total_players[1]
|
|
|
|
end
|
|
|
|
|
2018-12-15 22:28:53 +02:00
|
|
|
function Public.get_total_train_kills()
|
|
|
|
return total_train_kills[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_total_player_trees_mined()
|
|
|
|
return total_player_trees_mined[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_total_player_rocks_mined()
|
|
|
|
return total_player_rocks_mined[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_player_crafted_item(player_index)
|
|
|
|
return player_crafted_items[player_index]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_player_console_chat(player_index)
|
|
|
|
return player_console_chats[player_index]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_total_robot_built_entities()
|
|
|
|
return total_robot_built_entities[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_total_player_built_entities()
|
|
|
|
return total_player_built_entities[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_total_biter_kills()
|
|
|
|
return total_biter_kills[1]
|
|
|
|
end
|
|
|
|
|
2019-01-21 22:36:32 +02:00
|
|
|
function Public.get_total_coins_spent()
|
|
|
|
return total_coins_spent[1]
|
|
|
|
end
|
|
|
|
|
2018-06-27 15:48:04 +02:00
|
|
|
return Public
|