2019-02-15 18:39:30 +02:00
-- This module contains features for donators and the permissions system for donators: who is a donator, what flags they have, adding/modifying donator data, etc.
2019-01-30 05:52:43 +02:00
local Event = require ' utils.event '
local Server = require ' features.server '
local Game = require ' utils.game '
local Token = require ' utils.token '
local table = require ' utils.table '
local Global = require ' utils.global '
local Task = require ' utils.task '
2019-02-21 01:24:35 +02:00
local Color = require ' resources.color_presets '
2019-01-30 05:52:43 +02:00
2019-02-02 05:24:32 +02:00
local concat = table.concat
local insert = table.insert
2019-02-15 18:18:12 +02:00
local remove = table.remove
2019-02-15 18:39:30 +02:00
local set_data = Server.set_data
2019-02-15 18:18:12 +02:00
local random = math.random
2019-02-02 05:24:32 +02:00
local donator_data_set = ' donators '
2019-01-30 05:52:43 +02:00
local donators = { } -- global register
Global.register (
{
donators = donators
} ,
function ( tbl )
donators = tbl.donators
end
)
local Public = { }
--- Prints the donator message with the color returned from the server
local print_after_timeout =
Token.register (
function ( data )
local player = data.player
if not player.valid then
return
end
2019-02-23 17:54:36 +02:00
if data.death then
game.print ( { ' donator.death_message ' } , Color.white )
end
2019-01-30 05:52:43 +02:00
game.print ( data.message , player.chat_color )
end
)
--- When a player joins, set a 1s timer to retrieve their color before printing their welcome message
local function player_joined ( event )
local player = Game.get_player_by_index ( event.player_index )
if not player or not player.valid then
return
end
local d = donators [ player.name ]
if not d then
return nil
end
2019-02-15 18:39:30 +02:00
local messages = d.welcome_messages
2019-02-23 17:56:25 +02:00
if not messages or not # messages > 0 then
2019-01-30 05:52:43 +02:00
return
end
2019-02-15 18:18:12 +02:00
local message = messages [ random ( # messages ) ]
2019-02-02 05:24:32 +02:00
message = concat ( { ' *** ' , message , ' *** ' } )
2019-01-30 05:52:43 +02:00
Task.set_timeout_in_ticks ( 60 , print_after_timeout , { player = player , message = message } )
end
2019-02-21 01:24:35 +02:00
local function player_died ( event )
local player = Game.get_player_by_index ( event.player_index )
if not player or not player.valid then
return
end
local d = donators [ player.name ]
if not d then
return nil
end
local messages = d.death_messages
2019-02-23 17:56:25 +02:00
if not messages or not # messages > 0 then
2019-02-21 01:24:35 +02:00
return
end
local message = messages [ random ( # messages ) ]
message = concat ( { ' *** ' , message , ' *** ' } )
2019-02-23 17:54:36 +02:00
Task.set_timeout_in_ticks ( 30 , print_after_timeout , { player = player , message = message , death = true } )
2019-02-21 01:24:35 +02:00
end
2019-02-02 22:57:12 +02:00
--- Returns the table of donators
2019-02-02 23:41:43 +02:00
-- @return <table>
2019-02-08 23:54:23 +02:00
function Public . get_donators_table ( )
2019-02-02 22:57:12 +02:00
return donators
end
2019-01-30 05:52:43 +02:00
--- Checks if a player is a donator
-- @param player_name <string>
-- @return <boolean>
function Public . is_donator ( player_name )
return donators [ player_name ] ~= nil
end
--- Checks if a player has a specific donator perk
-- @param player_name <string>
-- @param perf_flag <number>
-- @return <boolean>
function Public . player_has_donator_perk ( player_name , perk_flag )
local d = donators [ player_name ]
if not d then
return false
end
local flags = d.perk_flags
if not flags then
return false
end
return bit32.band ( flags , perk_flag ) == perk_flag
end
2019-02-11 07:48:54 +02:00
--- Sets the data for a donator, all existing data for the entry is removed
2019-01-30 05:52:43 +02:00
-- @param player_name <string>
2019-02-02 05:24:32 +02:00
-- @param data <table>
function Public . set_donator_data ( player_name , data )
2019-01-30 05:52:43 +02:00
donators [ player_name ] = data
2019-02-15 18:39:30 +02:00
set_data ( donator_data_set , player_name , data )
2019-02-02 05:24:32 +02:00
end
2019-02-11 07:48:54 +02:00
--- Changes the data for a donator with any data that is sent, only overwritten data is affected
2019-02-02 05:24:32 +02:00
-- @param player_name <string>
-- @param data <table>
function Public . change_donator_data ( player_name , data )
for k , v in pairs ( data ) do
donators [ player_name ] [ k ] = v
end
2019-02-15 18:39:30 +02:00
set_data ( donator_data_set , player_name , donators [ player_name ] )
2019-01-30 05:52:43 +02:00
end
2019-02-21 01:24:35 +02:00
--- Adds a donator message to the appropriate table
2019-02-15 18:18:12 +02:00
-- @param player_name <string>
2019-02-21 01:24:35 +02:00
-- @param table_name <string> the name table to change the message in
2019-02-15 18:18:12 +02:00
-- @param str <string>
2019-02-21 01:24:35 +02:00
function Public . add_donator_message ( player_name , table_name , str )
2019-02-15 18:18:12 +02:00
local d_table = donators [ player_name ]
2019-02-21 01:24:35 +02:00
if not d_table [ table_name ] then
d_table [ table_name ] = { }
2019-02-15 18:18:12 +02:00
end
2019-02-21 01:24:35 +02:00
d_table [ table_name ] [ # d_table [ table_name ] + 1 ] = str
2019-02-15 18:18:12 +02:00
set_data ( donator_data_set , player_name , d_table )
end
2019-02-21 01:24:35 +02:00
--- Deletes the indicated donator message from the appropriate table
2019-02-15 18:18:12 +02:00
-- @param player_name <string>
2019-02-21 01:24:35 +02:00
-- @param table_name <string> the name table to change the message in
2019-02-15 18:18:12 +02:00
-- @param num <number>
-- @return <string|nil> the value that was deleted, nil if nothing to delete
2019-02-21 01:24:35 +02:00
function Public . delete_donator_message ( player_name , table_name , num )
2019-02-15 18:18:12 +02:00
local d_table = donators [ player_name ]
2019-02-21 01:24:35 +02:00
if not d_table [ table_name ] or not d_table [ table_name ] [ num ] then
2019-02-15 18:18:12 +02:00
return
end
2019-02-21 01:24:35 +02:00
local del_msg = remove ( d_table [ table_name ] , num )
2019-02-15 18:18:12 +02:00
set_data ( donator_data_set , player_name , d_table )
return del_msg
end
2019-02-21 01:24:35 +02:00
--- Returns the list of messages from the appropriate table
2019-02-15 18:18:12 +02:00
-- @param player_name <string>
2019-02-21 01:24:35 +02:00
-- @param table_name <string> the name table to change the message in
2019-02-15 18:18:12 +02:00
-- @return <table|nil> an array of strings or nil if no messages
2019-02-21 01:24:35 +02:00
function Public . get_donator_messages ( player_name , table_name )
return donators [ player_name ] [ table_name ]
2019-02-15 18:18:12 +02:00
end
2019-02-11 07:48:54 +02:00
--- Writes the data called back from the server into the donators table, overwriting any matching entries
2019-01-30 05:52:43 +02:00
local sync_donators_callback =
Token.register (
function ( data )
2019-02-02 05:24:32 +02:00
for k , v in pairs ( data.entries ) do
donators [ k ] = v
end
2019-01-30 05:52:43 +02:00
end
)
--- Signals the server to retrieve the donators data set
function Public . sync_donators ( )
2019-02-02 05:24:32 +02:00
Server.try_get_all_data ( donator_data_set , sync_donators_callback )
2019-01-30 05:52:43 +02:00
end
--- Prints a list of donators
function Public . print_donators ( )
local result = { }
2019-02-02 05:24:32 +02:00
for k , _ in pairs ( donators ) do
insert ( result , k )
2019-01-30 05:52:43 +02:00
end
2019-02-02 05:24:32 +02:00
result = concat ( result , ' , ' )
2019-01-30 05:52:43 +02:00
Game.player_print ( result )
end
Event.add (
Server.events . on_server_started ,
function ( )
Public.sync_donators ( )
end
)
Server.on_data_set_changed (
2019-02-02 05:24:32 +02:00
donator_data_set ,
2019-01-30 05:52:43 +02:00
function ( data )
2019-02-02 05:24:32 +02:00
donators [ data.key ] = data.value
2019-01-30 05:52:43 +02:00
end
)
Event.add ( defines.events . on_player_joined_game , player_joined )
2019-02-21 01:24:35 +02:00
Event.add ( defines.events . on_player_died , player_died )
2019-01-30 05:52:43 +02:00
return Public