1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Fixed crashes when command were run from console #43 and implemented console chat #47

This commit is contained in:
Valansch 2017-09-02 14:31:01 +02:00
parent 9d2dfe81ea
commit 6e2e9230cb
4 changed files with 108 additions and 69 deletions

View File

@ -29,33 +29,42 @@ function format_time(ticks)
end
function log_chat_message(event, message)
log(message)
game.write_file("chatlog.txt", "[" .. format_time(event.tick) .. "] " .. message .. "\n", true)
end
function player_send_command(event)
local silent = event.command == "silent-command"
if not silent then
if event.player_index then
local player = game.players[event.player_index]
log_chat_message(event, player.name .. " used command: " .. event.command .. " " .. event.parameters)
else
log_chat_message(event, "<Server> used command: " .. event.command .. " " .. event.parameters)
end
end
end
function player_send(event)
if event.player_index then
local player = game.players[event.player_index]
log_chat_message(event, player.name .. ": " .. event.message)
log_chat_message(event, player.name .. ": " .. event.message)
else
log_chat_message(event, "<Server>: " .. event.message)
end
end
function player_joined(event)
update_player_list()
local player = game.players[event.player_index]
log_chat_message(event, "### " .. player.name .. " joined the game. ###")
update_player_list()
local player = game.players[event.player_index]
log_chat_message(event, "### " .. player.name .. " joined the game. ###")
end
function player_left(event)
update_player_list()
local player = game.players[event.player_index]
log_chat_message(event, "### " .. player.name .. " left the game. ###")
update_player_list()
local player = game.players[event.player_index]
log_chat_message(event, "### " .. player.name .. " left the game. ###")
end
function set_time(d, month, h, m)
@ -65,7 +74,8 @@ function set_time(d, month, h, m)
global.scenario.variables.days_passed = 0
global.scenario.variables.current_h = h
global.scenario.variables.current_m = m
game.print(game.player.name .. " set the clock to " .. format_time(game.tick) .. ". Type /clock to check the time.")
local actor = get_actor()
game.print(actor .. " set the clock to " .. format_time(game.tick) .. ". Type /clock to check the time.")
end
function update_player_list()

View File

@ -1,18 +1,26 @@
local Thread = require "locale.utils.Thread"
require "locale/utils/event"
function player_print(str)
if game.player then
game.player.print(str)
else
log(str)
end
end
function cant_run(name)
game.player.print("Can't run command (" .. name .. ") - insufficient permission.")
player_print("Can't run command (" .. name .. ") - insufficient permission.")
end
function invoke(cmd)
if not (game.player.admin or is_mod(game.player.name)) then
if not game.player or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
local target = cmd["parameter"]
if target == nil or game.players[target] == nil then
game.player.print("Unknown player.")
player_print("Unknown player.")
return
end
local pos = game.surfaces[1].find_non_colliding_position("player", game.player.position, 0, 1)
@ -21,13 +29,13 @@ function invoke(cmd)
end
function teleport_player(cmd)
if not (game.player.admin or is_mod(game.player.name)) then
if not game.player or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
local target = cmd["parameter"]
if target == nil or game.players[target] == nil then
game.player.print("Unknown player.")
player_print("Unknown player.")
return
end
local pos = game.surfaces[1].find_non_colliding_position("player", game.players[target].position, 0, 1)
@ -36,12 +44,12 @@ function teleport_player(cmd)
end
function teleport_location(cmd)
if not game.player.admin then
if not game.player or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
if game.player.selected == nil then
game.player.print("Nothing selected.")
player_print("Nothing selected.")
return
end
local pos = game.surfaces[1].find_non_colliding_position("player", game.player.selected.position, 0, 1)
@ -49,25 +57,28 @@ function teleport_location(cmd)
end
local function detrain(param)
if not (game.player.admin or is_mod(game.player.name)) then
if game.player and not (game.player.admin or is_mod(game.player.name)) then
cant_run(param.name)
return
end
local player_name = param["parameter"]
if player_name == nil or game.players[player_name] == nil then game.player.print("Unknown player.") return end
if game.players[player_name].vehicle == nil then game.player.print("Player not in vehicle.") return end
if player_name == nil or game.players[player_name] == nil then player_print("Unknown player.") return end
if game.players[player_name].vehicle == nil then player_print("Player not in vehicle.") return end
game.players[player_name].vehicle.passenger = game.player
game.print(string.format("%s kicked %s off the train. God damn!", game.player.name, player_name))
local player = game.player or {name = "<server>"}
game.print(string.format("%s kicked %s off the train. God damn!", player.name, player_name))
end
function kill()
game.player.character.die()
if game.player then
game.player.character.die()
end
end
global.walking = {}
function walkabout(cmd)
if not (game.player.admin or is_mod(game.player.name)) then
if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
@ -87,7 +98,7 @@ function walkabout(cmd)
if distance ~= "very far" then
distance = params[2]
if tonumber(params[3]) == nil then
game.player.print(params[3] .. " is not a number.")
player_print(params[3] .. " is not a number.")
return
else
duration = tonumber(params[3])
@ -96,7 +107,7 @@ function walkabout(cmd)
elseif #params == 4 then
distance = params[2] .. " " .. params[3]
if tonumber(params[4]) == nil then
game.player.print(params[4] .. " is not a number.")
player_print(params[4] .. " is not a number.")
return
else
duration = tonumber(params[4])
@ -123,7 +134,7 @@ function walkabout(cmd)
local y = 1
local player = game.players[player_name]
if player == nil or global.walking[player_name:lower()] then
game.player.print(player_name .. " could not go on a walkabout.")
player_print(player_name .. " could not go on a walkabout.")
return
end
global.walking[player_name:lower()] = true
@ -173,7 +184,7 @@ function return_player(args)
end
function on_set_time(cmd)
if not (game.player.admin or is_regular(game.player.name) or is_mod(game.player.name)) then
if not ((not game.player) or game.player.admin or is_regular(game.player.name) or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
@ -182,80 +193,80 @@ function on_set_time(cmd)
local params_numeric = {}
if cmd.parameter == nil then
game.player.print("Setting clock failed. Usage: /settime <day> <month> <hour> <minute>")
player_print("Setting clock failed. Usage: /settime <day> <month> <hour> <minute>")
return
end
for param in string.gmatch(cmd.parameter, "%w+") do table.insert(params, param) end
if params[4] == nil then
game.player.print("Setting clock failed. Usage: /settime <day> <month> <hour> <minute>")
player_print("Setting clock failed. Usage: /settime <day> <month> <hour> <minute>")
return
end
for _, param in pairs(params) do
if tonumber(param) == nil then
game.player.print("Don't be stupid.")
player_print("Don't be stupid.")
return
end
table.insert(params_numeric, tonumber(param))
end
if (params_numeric[2] > 12) or (params_numeric[2] < 1) or (params_numeric[1] > 31) or (params_numeric[1] < 1) or (params_numeric[2] % 2 == 0 and params_numeric[1] > 30) or (params_numeric[3] > 24) or (params_numeric[3] < 0) or (params_numeric[4] > 60) or (params_numeric[4] < 0) then
game.player.print("Don't be stupid.")
player_print("Don't be stupid.")
return
end
set_time(params_numeric[1], params_numeric[2], params_numeric[3], params_numeric[4])
end
local function clock()
game.player.print(format_time(game.tick))
player_print(format_time(game.tick))
end
local function regular(cmd)
if not (game.player.admin or is_mod(game.player.name)) then
if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
if cmd.parameter == nil then
game.player.print("Command failed. Usage: /regular <promote, demote>, <player>")
player_print("Command failed. Usage: /regular <promote, demote>, <player>")
return
end
local params = {}
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end
for param in string.gmatch(cmd.parameter, "%w+") do table.insert(params, param) end
if params[2] == nil then
game.player.print("Command failed. Usage: /regular <promote, demote>, <player>")
player_print("Command failed. Usage: /regular <promote, demote>, <player>")
return
elseif (params[1] == "promote") then
add_regular(params[2])
elseif (params[1] == "demote") then
remove_regular(params[2])
else
game.player.print("Command failed. Usage: /regular <promote, demote>, <player>")
player_print("Command failed. Usage: /regular <promote, demote>, <player>")
end
end
local function mod(cmd)
if not game.player.admin then
if game.player and not game.player.admin then
cant_run(cmd.name)
return
end
if cmd.parameter == nil then
game.player.print("Command failed. Usage: /mod <promote, demote>, <player>")
player_print("Command failed. Usage: /mod <promote, demote>, <player>")
return
end
local params = {}
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end
for param in string.gmatch(cmd.parameter, "%w+") do table.insert(params, param) end
if params[2] == nil then
game.player.print("Command failed. Usage: /mod <promote, demote>, <player>")
player_print("Command failed. Usage: /mod <promote, demote>, <player>")
return
elseif (params[1] == "promote") then
add_mod(params[2])
elseif (params[1] == "demote") then
remove_mod(params[2])
else
game.player.print("Command failed. Usage: /mod <promote, demote>, <player>")
player_print("Command failed. Usage: /mod <promote, demote>, <player>")
end
end
@ -264,20 +275,20 @@ local function afk()
if v.afk_time > 300 then
local time = " "
if v.afk_time > 21600 then
time = time .. math.floor(v.afk_time / 21600) .. " hours "
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)
player_print(v.name .. " has been afk for" .. time)
end
end
end
local function tag(cmd)
if not game.player.admin then
if game.player and not game.player.admin then
cant_run(cmd.name)
return
end
@ -285,29 +296,37 @@ local function tag(cmd)
local params = {}
for param in string.gmatch(cmd.parameter, "%w+") do table.insert(params, param) end
if #params < 2 then
game.player.print("Usage: <player> <tag> Sets a players tag.")
player_print("Usage: <player> <tag> Sets a players tag.")
elseif game.players[params[1]] == nil then
game.player.print("Player does not exist.")
player_print("Player does not exist.")
else
local tag = string.sub(cmd.parameter, params[1]:len() + 2)
game.players[params[1]].tag = "[" .. tag .. "]"
game.print(params[1] .. " joined [" .. tag .. "].")
end
else
game.player.print('Usage: /tag <player> <tag> Sets a players tag.')
player_print('Usage: /tag <player> <tag> Sets a players tag.')
end
end
local function follow(cmd)
if not game.player then
log("<Server can't do that.")
return
end
if cmd.parameter ~= nil and game.players[cmd.parameter] ~= nil then
global.follows[game.player.name] = cmd.parameter
global.follows.n_entries = global.follows.n_entries + 1
else
game.player.print("Usage: /follow <player> makes you follow the player. Use /unfollow to stop following a player.")
player_print("Usage: /follow <player> makes you follow the player. Use /unfollow to stop following a player.")
end
end
local function unfollow(cmd)
if not game.player then
log("<Server can't do that.")
return
end
if global.follows[game.player.name] ~= nil then
global.follows[game.player.name] = nil
global.follows.n_entries = global.follows.n_entries - 1
@ -316,7 +335,7 @@ end
global.wells = {}
local function well(cmd)
if not game.player.admin then
if not game.player or not game.player.admin then
cant_run(cmd.name)
return
end
@ -330,14 +349,14 @@ local function well(cmd)
if game.item_prototypes[params[1]] and params[2] and tonumber(params[2]) then
local ips = tonumber(params[2])
if ips < 1 then
game.player.print("Items per second must be at least 1.")
player_print("Items per second must be at least 1.")
return
end
local chest = game.surfaces[1].create_entity({name = "steel-chest", force = game.player.force, position = game.player.position})
table.insert(global.wells, {chest = chest, item = params[1], items_per_second = math.floor(ips)})
refill_well()
else
game.player.print("Usage: /well <item> <items per second>.")
player_print("Usage: /well <item> <items per second>.")
end
end
@ -349,6 +368,7 @@ local function built_entity(event)
local entity = event.created_entity
if entity.type ~= "entity-ghost" then return end
game.players[index].teleport(entity.position)
entity.destroy()
end
@ -357,9 +377,9 @@ end
Event.register(defines.events.on_built_entity, built_entity )
local function toggle_tp_mode(cmd)
if not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
if not game.player or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name)
return
end
local index = game.player.index
@ -367,10 +387,10 @@ local function toggle_tp_mode(cmd)
if toggled then
global.tp_players[index] = nil
game.player.print("tp mode is now off")
player_print("tp mode is now off")
else
global.tp_players[index] = true
game.player.print("tp mode is now on - place a ghost entity to teleport there.")
player_print("tp mode is now on - place a ghost entity to teleport there.")
end
end
@ -387,7 +407,7 @@ commands.add_command("regulars", 'Prints a list of game regulars.', print_regula
commands.add_command("regular", '<promote, demote>, <player> Change regular status of a player. (Admins and moderators)', regular)
commands.add_command("mods", 'Prints a list of game mods.', print_mods)
commands.add_command("mod", '<promote, demote>, <player> Changes moderator status of a player. (Admins only)', mod)
commands.add_command("afktime", 'Shows how long players have been afk.', afk)
commands.add_command("afk", 'Shows how long players have been afk.', afk)
commands.add_command("tag", '<player> <tag> Sets a players tag. (Admins only)', tag)
commands.add_command("follow", '<player> makes you follow the player. Use /unfollow to stop following a player.', follow)
commands.add_command("unfollow", 'stops following a player.', unfollow)

View File

@ -22,7 +22,7 @@ function market()
end
function spawn_market(cmd)
if not game.player.admin then
if not game.player or not game.player.admin then
cant_run(cmd.name)
return
end

View File

@ -2,6 +2,11 @@ function change_entry(name, position, action)
game.write_file("privilege_changes.txt", "[" .. format_time(game.tick) .. "] " .. position .. ": " .. action .. " " .. name .. "\n", true)
end
function get_actor()
if game.player then return game.player.name end
return "<server>"
end
function is_mod(player_name)
return not (global.scenario.variables.mods[player_name] == nil)
end
@ -11,51 +16,55 @@ function is_regular(player_name)
end
function add_regular(player_name)
if is_regular(player_name) then game.player.print(player_name .. " is already a regular.")
local actor = get_actor()
if is_regular(player_name) then player_print.print(player_name .. " is already a regular.")
else
if game.players[player_name] then
game.print(game.player.name .. " promoted " .. player_name .. " to regular.")
game.print(actor .. " promoted " .. player_name .. " to regular.")
change_entry(player_name, "regulars", "add")
global.scenario.variables.regulars[player_name] = ""
else
game.player.print(player_name .. " does not exist.")
player_print(player_name .. " does not exist.")
end
end
end
end
function add_mod(player_name)
if is_mod(player_name) then game.player.print(player_name .. " is already a moderator.")
local actor = get_actor()
if is_mod(player_name) then player_print(player_name .. " is already a moderator.")
else
if game.players[player_name] then
game.print(game.player.name .. " promoted " .. player_name .. " to moderator.")
game.print(actor .. " promoted " .. player_name .. " to moderator.")
change_entry(player_name, "regulars", "remove")
global.scenario.variables.mods[player_name] = ""
else
game.player.print(player_name .. " does not exist.")
player_print(player_name .. " does not exist.")
end
end
end
end
function remove_regular(player_name)
if is_regular(player_name) then game.print(player_name .. " was demoted from regular by " .. game.player.name .. ".") end
local actor = get_actor()
if is_regular(player_name) then game.print(player_name .. " was demoted from regular by " .. actor .. ".") end
global.scenario.variables.regulars[player_name] = nil
change_entry(player_name, "mods ", "add")
end
function remove_mod(player_name)
if is_mod(player_name) then game.print(player_name .. " was demoted from mod by " .. game.player.name .. ".") end
local actor = get_actor()
if is_mod(player_name) then game.print(player_name .. " was demoted from mod by " .. actor .. ".") end
global.scenario.variables.mods[player_name] = nil
change_entry(player_name, "mods ", "remove")
end
function print_regulars()
for k,_ in pairs(global.scenario.variables.regulars) do
game.player.print(k)
player_print(k)
end
end
function print_mods()
for k,_ in pairs(global.scenario.variables.mods) do
game.player.print(k)
player_print(k)
end
end