1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/features/redmew_commands.lua

433 lines
12 KiB
Lua
Raw Normal View History

local Game = require 'utils.game'
local Timestamp = require 'utils.timestamp'
local Command = require 'utils.command'
local Settings = require 'utils.settings'
2019-02-03 01:10:40 +02:00
local Utils = require 'utils.core'
local Report = require 'features.report'
local Server = require 'features.server'
local UserGroups = require 'features.user_groups'
local Walkabout = require 'features.walkabout'
local PlayerStats = require 'features.player_stats'
local format = string.format
local ceil = math.ceil
2019-01-22 22:03:34 +02:00
local concat = table.concat
local pcall = pcall
local tostring = tostring
local tonumber = tonumber
local pairs = pairs
--- Kill a player with fish as the cause of death.
local function do_fish_kill(player, suicide)
local c = player.character
if not c then
return false
end
local e = player.surface.create_entity {name = 'fish', position = player.position}
c.die(player.force, e)
-- Don't want people killing themselves for free fish.
if suicide then
e.destroy()
end
return true
end
--- Kill a player: admins and the server can kill others, non-admins can only kill themselves
local function kill(args, player)
local target_name = args.player
local target
if target_name then
target = game.players[target_name]
if not target then
Game.player_print(format('Player %s was not found.', target_name))
return
end
end
if (player and Walkabout.is_on_walkabout(player.index)) or (target and Walkabout.is_on_walkabout(target.index)) then
Game.player_print("A player on walkabout cannot be killed by a mere fish, don't waste your efforts.")
return
end
if not target and player then
if not do_fish_kill(player, true) then
Game.player_print("Sorry, you don't have a character to kill.")
end
elseif player then
if target == player then
if not do_fish_kill(player, true) then
Game.player_print("Sorry, you don't have a character to kill.")
end
elseif target and player.admin then
if not do_fish_kill(target) then
Game.player_print(table.concat {"'Sorry, '", target.name, "' doesn't have a character to kill."})
end
else
Game.player_print("Sorry you don't have permission to use the kill command on other players.")
end
elseif target then
if not do_fish_kill(target) then
Game.player_print(table.concat {"'Sorry, '", target.name, "' doesn't have a character to kill."})
end
end
end
--- Check players' afk times
local function afk()
local count = 0
for _, v in pairs(game.players) do
if v.afk_time > 300 then
count = count + 1
local time = ' '
if v.afk_time > 21600 then
time = time .. math.floor(v.afk_time / 216000) .. ' hours '
end
if v.afk_time > 3600 then
time = time .. math.floor(v.afk_time / 3600) % 60 .. ' minutes and '
end
time = time .. math.floor(v.afk_time / 60) % 60 .. ' seconds.'
Game.player_print(v.name .. ' has been afk for' .. time)
end
end
if count == 0 then
Game.player_print('No players afk.')
end
end
--- Lets a player set their zoom level
local function zoom(args, player)
if tonumber(args.zoom) then
player.zoom = tonumber(args.zoom)
else
Game.player_print('You must give zoom a number.')
end
end
--- Creates an alert for the player at the location of their target
local function find_player(args, player)
local name = args.player
local target = game.players[name]
if not target then
Game.player_print('player ' .. name .. ' not found')
return
end
target = target.character
if not target or not target.valid then
Game.player_print('player ' .. name .. ' does not have a character')
return
end
player.add_custom_alert(target, {type = 'virtual', name = 'signal-F'}, name, true)
end
--- Turns on rail block visualization for player
local function show_rail_block(_, player)
local vs = player.game_view_settings
local show = not vs.show_rail_block_visualisation
vs.show_rail_block_visualisation = show
Game.player_print('show_rail_block_visualisation set to ' .. tostring(show))
end
--- Provides the time on the server
local function server_time(_, player)
local p
if not player then
p = print
elseif player.valid then
p = player.print
else
return
end
local secs = Server.get_current_time()
if secs == nil then
p('Server time is not available, is this game running on a Redmew server?')
else
p(Timestamp.to_string(secs))
end
end
2019-01-08 22:58:12 +02:00
local function search_command(arguments)
local keyword = arguments.keyword
2019-01-03 19:16:50 +02:00
local p = Game.player_print
if #keyword < 2 then
p('Keyword should be 2 characters or more')
return
end
local per_page = 7
local matches = Command.search(keyword)
local count = #matches
if count == 0 then
p('---- 0 Search Results ----')
p(format('No commands found matching "%s"', keyword))
p('-------------------------')
return
end
local page = tonumber(arguments.page)
local pages = ceil(count / per_page)
if nil == page then
p('Page should be a valid number')
return
end
-- just show the last page
if page > pages then
page = pages
end
if page < 1 then
page = 1
end
local page_start = per_page * (page - 1) + 1
local page_end = per_page * page
page_end = page_end <= count and page_end or count
p(format('---- %d Search %s -----', count, count == 1 and 'Result' or 'Results'))
p(format('Searching for: "%s"', keyword))
for i = page_start, page_end do
p(format('[%d] /%s', i, matches[i]))
end
p(format('-------- Page %d / %d --------', page, pages))
end
2019-01-22 22:03:34 +02:00
local function list_seeds()
local seeds = {}
local count_of_seeds = 0
for _, surface in pairs(game.surfaces) do
seeds[count_of_seeds + 1] = surface.name
seeds[count_of_seeds + 2] = ': '
seeds[count_of_seeds + 3] = tostring(surface.map_gen_settings.seed)
count_of_seeds = count_of_seeds + 4
seeds[count_of_seeds] = ', '
end
seeds[#seeds] = nil
seeds = concat(seeds)
Game.player_print(seeds)
end
local function print_version()
local version_str
if global.redmew_version then
version_str = global.redmew_version
else
version_str = 'This map was created from source code, only releases (zips with names) and server saves have versions'
end
Game.player_print(version_str)
end
--- Prints information about the target player
local function print_player_info(args, player)
local name = args.player
local target = game.players[name]
if not target then
Game.player_print('Target not found')
2019-02-03 01:14:48 +02:00
return
end
2019-02-03 01:14:48 +02:00
2019-01-31 02:33:11 +02:00
local index = target.index
local info_t = {
'Name: ' .. name,
2019-02-03 01:09:01 +02:00
target.connected and 'Online: yes' or 'Online: no',
2019-01-31 02:33:11 +02:00
'Index: ' .. target.index,
'Rank: ' .. UserGroups.get_rank(target),
2019-02-03 01:09:01 +02:00
UserGroups.is_donator(target.name) and 'Donator: yes' or 'Donator: no',
2019-01-31 02:33:11 +02:00
'Time played: ' .. Utils.format_time(target.online_time),
2019-02-03 01:19:59 +02:00
'AFK time: ' .. Utils.format_time(target.afk_time or 0),
2019-01-31 02:33:11 +02:00
'Force: ' .. target.force.name,
'Surface: ' .. target.surface.name,
'Tag: ' .. target.tag,
'Distance walked: ' .. PlayerStats.get_walk_distance(index),
'Coin earned: ' .. PlayerStats.get_coin_earned(index),
'Coin spent: ' .. PlayerStats.get_coin_spent(index),
'Deaths: ' .. PlayerStats.get_death_count(index),
'Crafted items: ' .. PlayerStats.get_crafted_item(index),
'Chat messages: ' .. PlayerStats.get_console_chat(index)
}
2019-01-31 02:33:11 +02:00
Game.player_print(concat(info_t, '\n- '))
2019-01-31 02:33:11 +02:00
if (not player or player.admin) and args.inventory then
local m_inventory = target.get_inventory(defines.inventory.player_main)
m_inventory = m_inventory.get_contents()
local q_inventory = target.get_inventory(defines.inventory.player_quickbar)
q_inventory = q_inventory.get_contents()
Game.player_print('Main and hotbar inventories: ')
Game.player_print(serpent.line(m_inventory))
Game.player_print(serpent.line(q_inventory))
end
end
2019-01-08 22:58:12 +02:00
-- Command registrations
Command.add(
'kill',
{
description = 'Will kill you.',
arguments = {'player'},
default_values = {player = false},
allowed_by_server = true
},
kill
)
Command.add(
'afk',
{
description = 'Shows how long players have been afk.',
allowed_by_server = true
},
afk
)
Command.add(
'zoom',
{
description = 'Sets your zoom.',
arguments = {'zoom'}
},
zoom
)
Command.add(
'find',
{
description = 'shows an alert on the map where the player is located',
arguments = {'player'}
},
find_player
)
Command.add(
'show-rail-block',
{
description = 'Toggles rail block visualisation.'
},
show_rail_block
)
Command.add(
'server-time',
{
description = "Prints the server's time.",
allowed_by_server = true
},
server_time
)
Command.add(
'search-command',
{
description = 'Search for commands matching the keyword in name or description',
arguments = {'keyword', 'page'},
2019-01-03 19:09:26 +02:00
default_values = {page = 1},
allowed_by_server = true
},
search_command
)
2019-01-22 22:03:34 +02:00
Command.add(
'seeds',
{
description = 'List the seeds of all surfaces',
allowed_by_server = true
2019-01-22 22:03:34 +02:00
},
list_seeds
)
Command.add(
'redmew-version',
{
description = 'Prints the version of the RedMew scenario',
allowed_by_server = true
},
print_version
)
Command.add(
'whois',
{
2019-01-31 02:33:11 +02:00
description = 'provides information about a given player, admins can see the inventory of a player by adding "yes" as a second argument',
arguments = {'player', 'inventory'},
default_values = {inventory = false},
allowed_by_server = true
},
print_player_info
)
-- Commands with no functions, only calls to other modules
Command.add(
'report',
{
description = 'Reports a user to admins',
arguments = {'player', 'message'},
capture_excess_arguments = true
},
Report.report_command
)
Command.add(
'regulars',
{
description = 'Prints a list of game regulars.',
allowed_by_server = true
},
UserGroups.print_regulars
)
Command.add('setting-set', {
description = 'Set a setting for yourself',
arguments = {'setting_name', 'new_value'},
capture_excess_arguments = true,
}, function (arguments, player)
local value
local setting_name = arguments.setting_name
local success, message = pcall(function()
value = Settings.set(player.index, setting_name, arguments.new_value)
end)
if not success then
player.print(message)
return
end
player.print(format('Changed "%s" to: "%s"', setting_name, value))
end)
Command.add('setting-get', {
description = 'Display a setting value for yourself',
arguments = {'setting_name'},
}, function (arguments, player)
local value
local setting_name = arguments.setting_name
local success, message = pcall(function()
value = Settings.get(player.index, setting_name)
end)
if not success then
player.print(message)
return
end
player.print(format('Setting "%s" has a value of: "%s"', setting_name, value))
end)
Command.add('setting-all', {
description = 'Display all settings for yourself',
}, function (_, player)
for name, value in pairs(Settings.all(player.index)) do
player.print(format('%s=%s', name, value))
end
end)