diff --git a/config/roles.lua b/config/roles.lua index 2e239c19..b203201d 100644 --- a/config/roles.lua +++ b/config/roles.lua @@ -147,7 +147,7 @@ Roles.new_role('Regular','Reg') end) --- Guest/Default role -Roles.new_role('Guest','') +local default = Roles.new_role('Guest','') :set_permission_group('Guest') :set_custom_color{r=185,g=187,b=160} :allow{ @@ -164,15 +164,14 @@ Roles.new_role('Jail') :set_permission_group('Restricted') :set_custom_color{r=50,g=50,b=50} :set_block_auto_promote(true) -:allow{ -} +:disallow(default.allowed) --- System defaults which are required to be set Roles.set_root('System') Roles.set_default('Guest') Roles.define_role_order{ - 'System', + 'System', -- Best to keep root at top 'Senior Administrator', 'Administrator', 'Moderator', @@ -184,8 +183,8 @@ Roles.define_role_order{ 'Veteran', 'Member', 'Regular', - 'Guest', - 'Jail' + 'Jail', + 'Guest' -- Default must be last if you want to apply restrictions to other roles } Roles.override_player_roles{ diff --git a/expcore/common.lua b/expcore/common.lua index 4c3478fa..096e0eeb 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -497,10 +497,39 @@ function Public.table_keysort(tbl) return _tbl end +--- Returns a message with valid chat tags to change its colour +-- @tparam message string the message that will be in the output +-- @tparam color table a color which contains r,g,b as its keys +-- @treturn string the message with the color tags included function Public.format_chat_colour(message,color) color = color or Colours.white local color_tag = '[color='..math.round(color.r,3)..','..math.round(color.g,3)..','..math.round(color.b,3)..']' return string.format('%s%s[/color]',color_tag,message) end +--- Returns a message with valid chat tags to change its colour, using localization +-- @tparam message ?string|table the message that will be in the output +-- @tparam color table a color which contains r,g,b as its keys +-- @treturn table the message with the color tags included +function Public.format_chat_colour_localized(message,color) + color = color or Colours.white + color = math.round(color.r,3)..','..math.round(color.g,3)..','..math.round(color.b,3) + return {'color-tag',color,message} +end + +--- Returns the players name in the players color +-- @tparam player LuaPlayer the player to use the name and color of +-- @tparam[opt=false] raw_string boolean when true a string is returned rather than a localized string +-- @treturn table the players name with tags for the players color +function Public.format_chat_player_name(player,raw_string) + player = Game.get_player_from_any(player) + local player_name = player and player.name or '' + local player_chat_colour = player and player.chat_color or Colors.white + if raw_string then + return Public.format_chat_colour(player_name,player_chat_colour) + else + return Public.format_chat_colour_localized(player_name,player_chat_colour) + end +end + return Public \ No newline at end of file diff --git a/expcore/roles.lua b/expcore/roles.lua index 6cf5b71b..97107404 100644 --- a/expcore/roles.lua +++ b/expcore/roles.lua @@ -114,8 +114,8 @@ Roles.get_player_roles(player) --- Gets all the roles of the given player, this will always contain the default role Roles.get_player_highest_role(player) --- Gets the highest role which the player has, can be used to compeer one player to another - Roles.assign_player(player,roles,by_player_name) --- Gives a player the given role(s) with an option to pass a by player name used in the log - Roles.unassign_player(player,roles,by_player_name) --- Removes a player from the given role(s) with an option to pass a by player name used in the log + Roles.assign_player(player,roles,by_player_name,silent) --- Gives a player the given role(s) with an option to pass a by player name used in the log + Roles.unassign_player(player,roles,by_player_name,silent) --- Removes a player from the given role(s) with an option to pass a by player name used in the log Roles.override_player_roles(roles) --- Overrides all player roles with the given table of roles, useful to mass set roles on game start Roles.player_has_role(player,search_role) --- A test for weather a player has the given role @@ -170,15 +170,31 @@ local Roles = { _prototype={} } +--- When global is loaded it will have the metatable re-assigned to the roles +Global.register(Roles.config,function(tbl) + Roles.config = tbl + for _,role in pairs(Roles.config.roles) do + setmetatable(role,{__index=Roles._prototype}) + local parent = Roles.config.roles[role.parent] + if parent then + setmetatable(role.allowed_actions, {__index=parent.allowed_actions}) + end + end +end) + --- Internal function used to trigger a few different things when roles are changed -local function emit_player_roles_updated(player,type,roles,by_player_name) +-- this is the raw internal trigger as the other function is called at other times +-- there is a second half called role_update which triggers after the event call, it also is called when a player joins +local function emit_player_roles_updated(player,type,roles,by_player_name,skip_game_print) by_player_name = game.player and game.player.name or by_player_name or '' + local by_player = Game.get_player_from_any(by_player_name) + local by_player_index = by_player and by_player.index or 0 + -- get the event id from the type of emit local event = Roles.player_role_assigned if type == 'unassign' then event = Roles.player_role_unassigned end - local by_player = Game.get_player_from_any(by_player_name) - local by_player_index = by_player and by_player.index or 0 + -- convert the roles to objects and get the names of the roles local role_names = {} for index,role in pairs(roles) do role = Roles.get_role_from_any(role) @@ -187,7 +203,15 @@ local function emit_player_roles_updated(player,type,roles,by_player_name) table.insert(role_names,role.name) end end - game.print({'expcore-roles.game-message-'..type,player.name,table.concat(role_names,', '),by_player_name},Colours.cyan) + -- output to all the different locations: game print, player sound, event trigger and role log + if not skip_game_print then + game.print({'expcore-roles.game-message-'..type,player.name,table.concat(role_names,', '),by_player_name},Colours.cyan) + end + if type == 'assign' then + player.play_sound{path='utility/achievement_unlocked'} + else + player.play_sound{path='utility/game_lost'} + end script.raise_event(event,{ name=Roles.player_roles_updated, tick=game.tick, @@ -203,18 +227,6 @@ local function emit_player_roles_updated(player,type,roles,by_player_name) }..'\n',true,0) end ---- When global is loaded it will have the metatable re-assigned to the roles -Global.register(Roles.config,function(tbl) - Roles.config = tbl - for _,role in pairs(Roles.config.roles) do - setmetatable(role,{__index=Roles._prototype}) - local parent = Roles.config.roles[role.parent] - if parent then - setmetatable(role.allowed_actions, {__index=parent.allowed_actions}) - end - end -end) - --- Returns a string which contains all roles in index order displaying all data for them -- @treturn string the debug output string function Roles.debug() @@ -293,7 +305,8 @@ end -- @tparam player LuaPlayer the player that will be assigned the roles -- @tparam role table a table of roles that the player will be given, can be one role and can be role names -- @tparam[opt=] by_player_name string the name of the player that will be shown in the log -function Roles.assign_player(player,roles,by_player_name) +-- @tparam[opt=false] silent boolean when true there will be no game message printed +function Roles.assign_player(player,roles,by_player_name,silent) player = Game.get_player_from_any(player) if not player then return end if type(roles) ~= 'table' or roles.name then @@ -305,14 +318,15 @@ function Roles.assign_player(player,roles,by_player_name) role:add_player(player,false,true) end end - emit_player_roles_updated(player,'assign',roles,by_player_name) + emit_player_roles_updated(player,'assign',roles,by_player_name,silent) end --- Removes a player from the given role(s) with an option to pass a by player name used in the log -- @tparam player LuaPlayer the player that will have the roles removed -- @tparam roles table a table of roles to be removed from the player, can be one role and can be role names -- @tparam[opt=] by_player_name string the name of the player that will be shown in the logs -function Roles.unassign_player(player,roles,by_player_name) +-- @tparam[opt=false] silent boolean when true there will be no game message printed +function Roles.unassign_player(player,roles,by_player_name,silent) player = Game.get_player_from_any(player) if not player then return end if type(roles) ~= 'table' or roles.name then @@ -324,7 +338,7 @@ function Roles.unassign_player(player,roles,by_player_name) role:remove_player(player,false,true) end end - emit_player_roles_updated(player,'unassign',roles,by_player_name) + emit_player_roles_updated(player,'unassign',roles,by_player_name,silent) end --- Overrides all player roles with the given table of roles, useful to mass set roles on game start diff --git a/locale/en/commands.cfg b/locale/en/commands.cfg index 393aae20..0e8549d0 100644 --- a/locale/en/commands.cfg +++ b/locale/en/commands.cfg @@ -1,6 +1,6 @@ [exp-commands] kill-already-dead=You are already dead. -admin-chat-format=[Admin Chat] [color=__3__]__1__[/color]: __2__ +admin-chat-format=[Admin Chat] __1__: __2__ tp-no-position-found=No position to teleport to was found, please try again later. tp-to-self=Player can not be teleported to themselves. chelp-title=Help results for "__1__": @@ -9,6 +9,10 @@ chelp-format=/__1__ __2__ - __3__ __4__ chelp-alias=Alias: __1__ chelp-out-of-range=__1__ is an invalid page number. roles-higher-role=The role you tried to assign is higher than your highest. -roles-list=All roles are: [color=__1__]__2__[/color] -roles-list-player=[color=__1__]__2__[/color] has: [color=__3__]__4__[/color] -roles-list-element=__1__, [color=__2__]__3__[/color] \ No newline at end of file +roles-list=All roles are: __1__ +roles-list-player=__1__ has: __2__ +roles-list-element=__1__, __2__ +jail-give=__1__ was jailed by __2__. Reason: __3__ +jail-remove=__1__ was unjailed by __2__. +jail-already-jailed=__1__ is already in jail. +jail-not-jailed=__1__ is not currently in jail. \ No newline at end of file diff --git a/locale/en/expcore.cfg b/locale/en/expcore.cfg index 82d68681..e725bd91 100644 --- a/locale/en/expcore.cfg +++ b/locale/en/expcore.cfg @@ -1,4 +1,5 @@ time-symbol-days-short=__1__d +color-tag=[color=__1__]__2__[/color] [expcore-commands] unauthorized=Unauthorized, Access is denied due to invalid credentials diff --git a/modules/addons/compilatron.lua b/modules/addons/compilatron.lua index f184baf5..29fc2bfe 100644 --- a/modules/addons/compilatron.lua +++ b/modules/addons/compilatron.lua @@ -7,20 +7,18 @@ local config = require 'config.compilatron' local messages = config.messages local locations = config.locations -local compilatrons = {} -local current_messages = {} -Global.register( - { - compilatrons = compilatrons, - current_messages = current_messages - }, - function(tbl) - compilatrons = tbl.compilatrons - current_messages = tbl.current_messages - end -) +local Public = { + compilatrons={}, + current_messages={} +} -local Public = {} +Global.register({ + Public.compilatrons, + Public.current_messages +},function(tbl) + Public.compilatrons=tbl[1] + Public.current_messages=tbl[2] +end) --- This will re-create the speech bubble after it de-spawns called with set_timeout local callback = @@ -33,17 +31,17 @@ local callback = ent.surface.create_entity( {name = 'compi-speech-bubble', text = messages[name][msg_number], position = {0, 0}, source = ent} ) - current_messages[name] = {message = message, msg_number = msg_number} + Public.global.current_messages[name] = {message = message, msg_number = msg_number} end ) --- This will move the messages onto the next message in the loop local function circle_messages() - for name, ent in pairs(compilatrons) do + for name, ent in pairs(Public.global.compilatrons) do if not ent.valid then Public.spawn_compilatron(game.players[1].surface,name) end - local current_message = current_messages[name] + local current_message = Public.global.current_messages[name] local msg_number local message if current_message ~= nil then @@ -73,12 +71,12 @@ function Public.add_compilatron(entity, name) if name == nil then return end - compilatrons[name] = entity + Public.global.compilatrons[name] = entity local message = entity.surface.create_entity( {name = 'compi-speech-bubble', text = messages[name][1], position = {0, 0}, source = entity} ) - current_messages[name] = {message = message, msg_number = 1} + Public.global.current_messages[name] = {message = message, msg_number = 1} end --- This spawns a new compilatron on a surface with the given location tag (not a position) diff --git a/modules/addons/death-logger.lua b/modules/addons/death-logger.lua index 938e550f..2d8be85c 100644 --- a/modules/addons/death-logger.lua +++ b/modules/addons/death-logger.lua @@ -94,6 +94,4 @@ if config.auto_collect_bodies then end -- this is so other modules can access the logs -return function() - return deaths -end \ No newline at end of file +return deaths \ No newline at end of file diff --git a/modules/addons/jail-control.lua b/modules/addons/jail-control.lua new file mode 100644 index 00000000..f404a81d --- /dev/null +++ b/modules/addons/jail-control.lua @@ -0,0 +1,41 @@ +local Roles = require 'expcore.roles' +local Game = require 'utils.game' +local Global = require 'utils.global' + +local Public = { + old_roles = {} +} +Global.register(Public.old_roles,function(tbl) + Public.old_roles=tbl +end) + +--- Jails a player, this is only the logic there is no output to players +-- @tparam player LuaPlayer the player that will be jailed, must not be in jail +-- @tparam[opt=''] by_player_name string the name of the player doing the action used in logs +-- @treturn the number of roles that were removed, nil if there was an error +function Public.jail_player(player,by_player_name) + player = Game.get_player_from_any(player) + if not player then return end + if Roles.player_has_role(player,'Jail') then return end + local old_roles = Role.get_player_roles(player) + Public.old_roles[player.name] = old_roles + Roles.unassign_player(player,old_roles,by_player_name,true) + Roles.assign_player(player,'Jail',by_player_name,true) + return #old_roles +end + +--- Unjails a player, this is only the logic there is no output to players +-- @tparam player LuaPlayer the player that will be unjailed, must be in jail +-- @tparam[opt=''] by_player_name string string the name of the player who is doing the action +-- @treturn the number of roles that were added, nil if there was an error +function Public.unjail_player(player,by_player_name) + player = Game.get_player_from_any(player) + if not player then return end + if not Roles.player_has_role(player,'Jail') then return end + local old_roles = Public.old_roles[player.name] + Roles.unassign_player(player,'Jail',by_player_name,true) + Roles.assign_player(player,old_roles,by_player_name,true) + return #old_roles +end + +return Public \ No newline at end of file diff --git a/modules/addons/spawn-area.lua b/modules/addons/spawn-area.lua index 04c341fa..71f4d062 100644 --- a/modules/addons/spawn-area.lua +++ b/modules/addons/spawn-area.lua @@ -141,4 +141,7 @@ Event.add(defines.events.on_player_created, function(event) spawn_belts(s,p) spawn_turrets() player.teleport(p,s) -end) \ No newline at end of file +end) + +-- Way to access global table +return turrets \ No newline at end of file diff --git a/modules/commands/admin-chat.lua b/modules/commands/admin-chat.lua index c3fb6e5f..e9f32b50 100644 --- a/modules/commands/admin-chat.lua +++ b/modules/commands/admin-chat.lua @@ -1,4 +1,5 @@ local Commands = require 'expcore.commands' +local format_chat_player_name = ext_require('expcore.common','format_chat_player_name') require 'config.command_parse_general' Commands.new_command('admin-chat','Sends a message in chat that only admins can see.') @@ -7,12 +8,10 @@ Commands.new_command('admin-chat','Sends a message in chat that only admins can :set_flag('admin_only',true) :add_alias('ac') :register(function(player,message,raw) - local pcc = player and player.chat_color or {r=255,g=255,b=255} - local player_name = player and player.name or '' - local colour = string.format('%s,%s,%s',pcc.r,pcc.g,pcc.b) + local player_name_colour = format_chat_player_name(player) for _,return_player in pairs(game.connected_players) do if return_player.admin then - return_player.print{'exp-commands.admin-chat-format',player_name,message,colour} + return_player.print{'exp-commands.admin-chat-format',player_name_colour,message} end end return Commands.success -- prevents command complete message from showing diff --git a/modules/commands/help.lua b/modules/commands/help.lua index c4a78567..419a23ac 100644 --- a/modules/commands/help.lua +++ b/modules/commands/help.lua @@ -72,4 +72,7 @@ Commands.new_command('chelp','Searches for a keyword in all commands you are all end -- blocks command complete message return Commands.success -end) \ No newline at end of file +end) + +-- way to access global +return search_cache \ No newline at end of file diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 5f1b2b42..6e5b89b0 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -91,4 +91,8 @@ add_interface_callback('position',function(player) return player.position end) add_interface_callback('entity',function(player) return player.selected end) add_interface_callback('tile',function(player) return player.surface.get_tile(player.position) end) -return add_interface_callback \ No newline at end of file +return { + add_interface_callback=add_interface_callback, + interface_env=interface_env, + interface_callbacks=interface_callbacks +} \ No newline at end of file diff --git a/modules/commands/jail.lua b/modules/commands/jail.lua new file mode 100644 index 00000000..04ae8a6d --- /dev/null +++ b/modules/commands/jail.lua @@ -0,0 +1,32 @@ +local Commands = require 'expcore.commands' +local JailControl = require 'modules.addons.jail-control' +local format_chat_player_name = ext_require('expcore.common','format_chat_player_name') +require 'config.command_parse_roles' + +Commands.new_command('jail','Puts a player into jail and removes all other roles') +:add_param('player',false,'player-role') +:add_param('reason',true) +:enable_auto_concat() +:register(function(player,action_player,reason,raw) + reason = reason or 'Non Given.' + local action_player_name_color = format_chat_player_name(action_player) + local by_player_name_color = format_chat_player_name(player) + if JailControl.jail_player(action_player,player.name) then + game.print{'exp-commands.jail-give',action_player_name_color,by_player_name_color,reason} + else + return Commands.error{'exp-commands.jail-already-jailed',action_player_name_color} + end +end) + +Commands.new_command('unjail','Puts a player into jail and removes all other roles') +:add_param('player',false,'player-role') +:enable_auto_concat() +:register(function(player,action_player,raw) + local action_player_name_color = format_chat_player_name(action_player) + local by_player_name_color = format_chat_player_name(player) + if JailControl.unjail_player(action_player,player.name) then + game.print{'exp-commands.jail-remove',action_player_name_color,by_player_name_color} + else + return Commands.error{'exp-commands.jail-not-jailed',action_player_name_color} + end +end) \ No newline at end of file diff --git a/modules/commands/roles.lua b/modules/commands/roles.lua index 9ae0d0b8..e19d521c 100644 --- a/modules/commands/roles.lua +++ b/modules/commands/roles.lua @@ -1,6 +1,10 @@ local Commands = require 'expcore.commands' local Roles = require 'expcore.roles' local Colours = require 'resources.color_presets' +local format_chat_player_name, format_chat_colour_localized = ext_require('expcore.common', + 'format_chat_player_name', + 'format_chat_colour_localized' +) Commands.new_command('assign-role','Assigns a role to a player') :add_param('player',false,'player-role') @@ -42,16 +46,15 @@ Commands.new_command('list-roles','Lists all roles in they correct order') for index,role in pairs(roles) do role = Roles.get_role_from_any(role) local colour = role.custom_color or Colours.white - colour = string.format('%d,%d,%d',colour.r,colour.g,colour.b) + local role_name = format_chat_colour_localized(role.name,colour) if index == 1 then - message = {'exp-commands.roles-list',colour,role.name} + message = {'exp-commands.roles-list',role_name} if action_player ~= '' then - local player_colour = action_player.color - player_colour = string.format('%d,%d,%d',player_colour.r*255,player_colour.g*255,player_colour.b*255) - message = {'exp-commands.roles-list-player',player_colour,action_player.name,colour,role.name} + local player_name_colour = format_chat_player_name(action_player) + message = {'exp-commands.roles-list-player',player_name_colour,role_name} end else - message = {'exp-commands.roles-list-element',message,colour,role.name} + message = {'exp-commands.roles-list-element',message,role_name} end end return Commands.success(message)