-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.lua
99 lines (88 loc) · 2.66 KB
/
functions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---@class functions
local functions = {}
---@param message table
---@return string text
function functions.concatenateName(message)
local text = ""
for i, j in pairs(message) do
if i ~= 1 then
text = text .. " " .. j
end
end
return text
end
---@param name string
---@return integer|nil dbid
function functions.getGroupId(name)
local factionTable = HebiDB:getTable()
for _, Table in pairs(factionTable) do
for _, faction in pairs(Table) do
if faction.name == name then return faction.factionId end
end
end
return nil
end
---@param pid integer
---@return boolean isLeader
function functions.isLeader(pid)
local factionTable = HebiDB:getTable()
for _, table in pairs(factionTable) do
for _, faction in pairs(table) do
if faction.leader == ResdaynCore.functions.getDbID(Players[pid].name) then return true end
end
end
return false
end
---@param id integer
---@return boolean isUnique
function functions.checkForUniqueGroupID(id)
local factionTables = HebiDB:getTable()
for _, table in pairs(factionTables) do
for _, faction in pairs(table) do
if faction.factionId == id then return false end
end
end
return true
end
---@return integer id
function functions.generateFactionId()
local id = math.random(0, 999)
repeat
id = math.random(0, 999)
until functions.checkForUniqueGroupID(id)
return id
end
---@param pid integer
---@return integer|nil isInFaction
function functions.isInFaction(pid)
local dbTable = HebiDB:getTable()
for _, table in pairs(dbTable) do
for _, player in pairs(table) do
if Players[pid].name == player.name and player.factionId then return player.factionId end
end
end
return nil
end
---@param source integer SourcePlayerId
---@param target integer TargetPlayerId
---@return boolean isSameFaction
function functions.isPartOfSameFaction(source, target)
if not target then return false end
local sourceFac, targetFac = functions.isInFaction(source), functions.isInFaction(target)
if not (sourceFac or targetFac) and sourceFac == targetFac then return true end
return false
end
---@param target integer target player id
---@param factionId integer|nil faction id
function functions.changePlayerFaction(target, factionId)
if not target then return end
for i, table in pairs(HebiDB.Table) do
for j, player in pairs(table) do
if player.name == Players[target].name then
HebiDB.Table[i][j].factionId = factionId
return
end
end
end
end
return functions