2019-01-15 15:27:48 +02:00
local table = require ' utils.table '
2018-11-23 19:08:44 +02:00
local Game = require ' utils.game '
local Event = require ' utils.event '
local naming_words = require ' resources.naming_words '
2018-12-22 01:38:47 +02:00
local Utils = require ' utils.core '
local Global = require ' utils.global '
2019-01-30 05:52:43 +02:00
local Rank = require ' features.rank_system '
2018-12-22 01:38:47 +02:00
local ScenarioInfo = require ' features.gui.info '
2019-01-02 17:34:17 +02:00
local Command = require ' utils.command '
2019-01-30 05:52:43 +02:00
local Ranks = require ' resources.ranks '
2018-12-22 01:38:47 +02:00
local format = string.format
2019-01-21 21:28:21 +02:00
local random = math.random
2018-12-22 01:38:47 +02:00
2019-01-02 17:34:17 +02:00
ScenarioInfo.add_map_extra_info ( ' - On this map you will be assigned a silly name. \n ' .. ' - If you dislike your name you can /name-restore or /name-roll for a new one ' )
2018-12-22 01:38:47 +02:00
global.silly_regulars = { }
local data_silly_names = { }
data_silly_names.silly_names = { }
data_silly_names.silly_name_store = { }
data_silly_names.silly_names_count = { 0 }
data_silly_names.silly_name_store = { }
data_silly_names.actual_name = { }
local name_combinations = # naming_words.adverbs * # naming_words.adjectives * 1
local table_size_ceiling = math.min ( name_combinations * 0.25 , 10000 )
Global.register (
{
data_silly_names = data_silly_names
} ,
function ( tbl )
data_silly_names = tbl.data_silly_names
end
)
--- Takes a player's real name, current silly name, and old silly name and adjusts
-- the silly_regulars table accordingly
local function check_regular ( real_name , silly_name , old_silly_name )
2019-01-30 05:52:43 +02:00
if Rank.equal ( real_name , Ranks.regular ) then
2018-12-22 01:38:47 +02:00
global.silly_regulars [ silly_name ] = true
if old_silly_name then
global.silly_regulars [ old_silly_name ] = nil
end
end
end
2018-11-23 19:08:44 +02:00
--- Creates name by combining elements from the passed table
2018-12-22 01:38:47 +02:00
-- @param words_table including adverbs, adjectives, and nouns
-- @param player_name string with player's name
-- @returns string with player's silly name
-- TODO: Config option to set the name style
local function create_name ( words_table , player_name )
2019-01-02 17:34:17 +02:00
local adverb , adjective --, noun
2019-01-21 21:28:21 +02:00
adverb = words_table [ random ( # words_table ) ]
adjective = words_table [ random ( # words_table ) ]
--noun = words_table[random(#words_table)]
2018-12-29 03:20:23 +02:00
local name = format ( ' %s_%s_%s ' , adverb , adjective , player_name )
return string.gsub ( name , " %s+ " , " _ " )
2018-11-23 19:08:44 +02:00
end
--- Calls create_name until a unique name is returned
2018-12-22 01:38:47 +02:00
-- @param words_table including adverbs, adjectives, and nouns
-- @param player_name string with player's name
-- @returns string with player's silly name
local function create_unique_name ( words_table , player_name )
local silly_names = data_silly_names.silly_names
local name = create_name ( words_table , player_name )
2018-11-23 19:08:44 +02:00
while table.contains ( silly_names , name ) do
2018-12-22 01:38:47 +02:00
name = create_name ( words_table , player_name )
2018-11-23 19:08:44 +02:00
end
return name
end
--- Assigns a player a name, stores their old and silly names
2018-12-22 01:38:47 +02:00
-- @param player LuaPlayer, the player to change the name of
2018-11-23 19:08:44 +02:00
local function name_player ( player )
2018-12-22 01:38:47 +02:00
local real_name = data_silly_names.actual_name [ player.index ] or player.name
local old_silly_name
-- If we don't have a player's actual name yet, store it
if data_silly_names.actual_name [ player.index ] then
old_silly_name = player.name
else
data_silly_names.actual_name [ player.index ] = real_name
2018-11-23 19:08:44 +02:00
end
2018-12-22 01:38:47 +02:00
-- Because create_unique_name enters a while loop looking for a _unique_ name,
-- we ensure the table never contains all possible combinations by having a ceiling
if data_silly_names.silly_names_count [ 1 ] > table_size_ceiling then
table.clear_table ( data_silly_names.silly_names , true )
data_silly_names.silly_names_count [ 1 ] = 0
2018-11-23 19:08:44 +02:00
end
2018-12-22 01:38:47 +02:00
local name = create_unique_name ( naming_words , real_name )
data_silly_names.silly_names [ # data_silly_names.silly_names + 1 ] = name
data_silly_names.silly_names_count [ 1 ] = data_silly_names.silly_names_count [ 1 ] + 1
2018-11-23 19:08:44 +02:00
2018-12-22 01:38:47 +02:00
local str = format ( ' %s will now be known as: %s ' , player.name , name )
2018-11-23 19:08:44 +02:00
game.print ( str )
2018-12-22 01:38:47 +02:00
local admin_str = format ( ' %s (ID: %s) ' , str , player.index )
2019-01-02 17:34:17 +02:00
Utils.print_admins ( admin_str , nil )
2018-11-23 19:08:44 +02:00
player.name = name
2018-12-22 01:38:47 +02:00
-- After they have their name, we need to ensure compatibility with the regulars system
check_regular ( real_name , name , old_silly_name )
2018-11-23 19:08:44 +02:00
end
--- Restores a player's actual name
2019-01-02 17:34:17 +02:00
local function restore_name ( data , command_player )
2018-12-22 01:38:47 +02:00
local player
2019-01-02 17:34:17 +02:00
if data and data.player_index then
2018-12-22 01:38:47 +02:00
player = Game.get_player_by_index ( data.player_index )
else
2019-01-02 17:34:17 +02:00
player = command_player
2018-12-22 01:38:47 +02:00
end
local silly_name = player.name
data_silly_names.silly_name_store [ player.index ] = player.name
player.name = data_silly_names.actual_name [ player.index ]
2019-01-02 17:34:17 +02:00
if command_player then
2018-12-22 01:38:47 +02:00
player.print ( ' Your true name has been restored. ' )
local str = silly_name .. ' will now be known as: ' .. player.name
2019-01-02 17:34:17 +02:00
Utils.print_admins ( str .. ' (ID: ' .. player.index .. ' ) ' , nil )
2018-12-22 01:38:47 +02:00
end
2018-11-23 19:08:44 +02:00
end
--- Passes _event_ on to name_players
2018-12-22 01:38:47 +02:00
local function player_joined ( event )
2018-11-23 19:08:44 +02:00
local player = Game.get_player_by_index ( event.player_index )
2018-12-22 01:38:47 +02:00
if data_silly_names.silly_name_store [ event.player_index ] then
player.name = data_silly_names.silly_name_store [ event.player_index ]
else
name_player ( player )
end
2018-11-23 19:08:44 +02:00
end
--- Passes target or player on to name_players
2019-01-02 17:34:17 +02:00
local function name_player_command ( args , player )
2018-11-23 19:08:44 +02:00
local target
2019-01-02 17:34:17 +02:00
local target_name = args.player
2018-11-23 19:08:44 +02:00
2019-01-02 17:34:17 +02:00
if target_name then
target = game.players [ target_name ]
2018-11-23 19:08:44 +02:00
if player and not player.admin then
-- Yes param, yes player, no admin/server = fail, non-admins, non-server cannot use command on others
Game.player_print ( " Sorry you don't have permission to use the roll-name command on other players. " )
return
else
-- Yes param, yes admin/server = check target
if target then
-- Yes param, yes admin/server, yes target = change name
name_player ( target )
return
else
-- Yes param, yes admin/server, no target = fail, wrong player name
2019-01-02 17:34:17 +02:00
Game.player_print ( table.concat { " Sorry, player ' " , target_name , " ' was not found. " } )
2018-11-23 19:08:44 +02:00
return
end
end
else
2019-01-02 17:34:17 +02:00
-- No param = change self name
2018-11-23 19:08:44 +02:00
name_player ( player )
return
end
end
--- Prints the original name of the target
2019-01-02 17:34:17 +02:00
local function check_name ( args )
local current_name = args.player
2018-11-23 19:08:44 +02:00
local target = game.players [ current_name ]
if not target then
Game.player_print ( ' player ' .. current_name .. ' not found ' )
return
end
2018-12-22 01:38:47 +02:00
local actual_name = data_silly_names.actual_name [ target.index ]
2018-11-23 19:08:44 +02:00
Game.player_print ( target.name .. ' is actually: ' .. actual_name )
end
--- Prints the index of the target
2019-01-02 17:34:17 +02:00
local function get_player_id ( args )
local target_name = args.player
local target = game.players [ target_name ]
if not target then
Game.player_print ( ' player ' .. target_name .. ' not found ' )
2018-11-23 19:08:44 +02:00
return
end
2019-01-02 17:34:17 +02:00
Game.player_print ( format ( ' name: %s -- index: %s ' , target_name , target.index ) )
2018-11-23 19:08:44 +02:00
end
2018-12-22 01:38:47 +02:00
Event.add ( defines.events . on_player_joined_game , player_joined )
Event.add ( defines.events . on_pre_player_left_game , restore_name )
2018-11-23 19:08:44 +02:00
2019-01-02 17:34:17 +02:00
Command.add (
' name-roll ' ,
{
description = ' Assigns you a random, silly name. (Admins can use this command on players) ' ,
arguments = { ' player ' } ,
default_values = { player = false }
} ,
name_player_command
)
Command.add (
' name-restore ' ,
{
description = ' Removes your fun/silly name and gives you back your actual name. ' ,
} ,
restore_name
)
Command.add (
' name-check ' ,
{
description = ' Check the original name of a player with a silly name ' ,
arguments = { ' player ' } ,
allowed_by_server = true
} ,
check_name
)
Command.add (
' get-player-id ' ,
{
description = ' Gets the index of a player ' ,
arguments = { ' player ' } ,
2019-01-30 05:52:43 +02:00
required_rank = Ranks.admin ,
2019-01-02 17:34:17 +02:00
allowed_by_server = true
} ,
get_player_id
)