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

100 lines
3.1 KiB
Lua
Raw Normal View History

2019-02-03 06:58:33 +02:00
-- Dependencies
local Game = require 'utils.game'
local Command = require 'utils.command'
local Donator = require 'features.donator'
local Color = require 'resources.color_presets'
2019-02-15 18:18:12 +02:00
local format = string.format
2019-02-03 06:58:33 +02:00
-- Local functions
2019-02-15 18:18:12 +02:00
--- Saves the player's message
2019-02-21 01:24:35 +02:00
local function add_message(args, player, table_name)
2019-02-15 18:18:12 +02:00
local str = tostring(args.value)
if not str or str == 'false' then
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.add_message_fail_not_string'}, Color.fail)
2019-02-15 18:18:12 +02:00
return
end
2019-02-21 01:24:35 +02:00
Donator.add_donator_message(player.name, table_name, str)
Game.player_print({'donator_commands.add_message_success'}, Color.success)
2019-02-15 18:18:12 +02:00
end
--- Deletes one of the player's message
2019-02-21 01:24:35 +02:00
local function delete_message(args, player, table_name)
2019-02-15 18:18:12 +02:00
local num = tonumber(args.value)
if not num then
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.delete_message_fail_not_number'}, Color.fail)
2019-02-15 18:18:12 +02:00
return
end
2019-02-21 01:24:35 +02:00
local message = Donator.delete_donator_message(player.name, table_name, num)
2019-02-15 18:18:12 +02:00
if message then
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.delete_message_success', message}, Color.success)
2019-02-15 18:18:12 +02:00
else
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.delete_message_fail_no_message'}, Color.fail)
2019-02-15 18:18:12 +02:00
end
end
--- Lists the player's messages
2019-02-21 01:24:35 +02:00
local function list_messages(player, table_name)
local messages = Donator.get_donator_messages(player.name, table_name)
2019-02-15 18:18:12 +02:00
if messages then
for k, v in pairs(messages) do
Game.player_print(format('[%s] %s', k, v))
end
else
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.list_message_fail_no_messages'}, Color.warning)
2019-02-15 18:18:12 +02:00
end
end
2019-02-21 01:24:35 +02:00
local function command_path_decider(args, player, table_name)
2019-02-15 18:18:12 +02:00
local multi = args['add|delete|list']
if multi == 'add' then
2019-02-21 01:24:35 +02:00
add_message(args, player, table_name)
2019-02-15 18:18:12 +02:00
elseif multi == 'delete' then
2019-02-21 01:24:35 +02:00
delete_message(args, player, table_name)
2019-02-15 18:18:12 +02:00
elseif multi == 'list' then
2019-02-21 01:24:35 +02:00
list_messages(player, table_name)
2019-02-15 18:18:12 +02:00
else
2019-02-21 01:24:35 +02:00
Game.player_print({'donator_commands.donator_message_wrong_arg1'}, Color.white)
2019-02-15 18:18:12 +02:00
end
2019-02-03 06:58:33 +02:00
end
2019-02-21 01:24:35 +02:00
--- Decides which function to call depending on the first arg to the command
local function donator_join_message_command(args, player)
local table_name = 'join_messages'
command_path_decider(args, player, table_name)
end
--- Decides which function to call depending on the first arg to the command
local function donator_death_message_command(args, player)
local table_name = 'death_messages'
command_path_decider(args, player, table_name)
end
2019-02-03 06:58:33 +02:00
-- Commands
Command.add(
'donator-join-message',
{
2019-02-21 01:24:35 +02:00
description = 'Adds, deletes, or lists donator on-join messages.',
2019-02-15 18:18:12 +02:00
arguments = {'add|delete|list', 'value'},
default_values = {value = false},
2019-02-03 06:58:33 +02:00
capture_excess_arguments = true,
donator_only = true
},
2019-02-15 18:18:12 +02:00
donator_join_message_command
2019-02-03 06:58:33 +02:00
)
2019-02-21 01:24:35 +02:00
Command.add(
'donator-death-message',
{
description = 'Adds, deletes, or lists donator death messages.',
arguments = {'add|delete|list', 'value'},
default_values = {value = false},
capture_excess_arguments = true,
donator_only = true
},
donator_death_message_command
)