· 9 years ago · Oct 11, 2016, 09:44 PM
1muteTimer = { }
2
3addEventHandler ( "onResourceStart", resourceRoot,
4 function ( )
5 db = dbConnect ( "mysql", "dbname=".. mysql_database ..";host=".. mysql_host ..";port=".. mysql_port ..";unix_socket=/var/run/mysqld/mysqld.sock", mysql_user, mysql_password, "share=0", "batch=0", "autoreconnect=1" )
6 if ( db ) then
7 dbExec ( db, "CREATE TABLE IF NOT EXISTS mutes (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, account TEXT, mutetime TEXT)" )
8 dbQuery ( mutesCallback, db, "SELECT * FROM mutes" )
9 outputDebugString ( "Admin system: Connected to mysql database successfuly." )
10 else
11 outputDebugString ( "Admin system: Failed to connect to mysql database.", 1 )
12 end
13 end
14)
15
16function mutesCallback ( query )
17 local result = dbPoll ( query, 0 )
18 if ( type ( result ) == "table" ) then
19 for _, row in ipairs ( result ) do
20 local account = getAccount ( tostring ( row.account ) )
21 if ( account and not isGuestAccount ( account ) ) then
22 local thePlayer = getAccountPlayer ( account )
23 if ( getElementType ( thePlayer ) == "player" ) then
24 local remaining = tostring ( row.mutetime )
25 adminMutePlayer ( thePlayer, remaining, false )
26 end
27 end
28 end
29 end
30end
31
32addEventHandler ( "onResourceStop", resourceRoot,
33 function ( )
34 for _, player in ipairs ( getElementsByType ( "player" ) ) do
35 local account = getPlayerAccount ( player )
36 if ( not isGuestAccount ( account ) ) then
37 local accountName = getAccountName ( account )
38 if ( muteTimer [ accountName ] and isPlayerMuted ( player ) ) then
39 savePlayerMuteTimeLeft ( player )
40 end
41 end
42 end
43 end
44)
45
46addEventHandler ( "onPlayerLogin", root,
47 function ( _, account )
48 if ( not isGuestAccount ( account ) ) then
49 local accountName = getAccountName ( account )
50 if ( muteTimer [ accountName ] ) then
51 local remaining = muteTimer [ accountName ]
52 adminMutePlayer ( source, remaining, false )
53 end
54 end
55 end
56)
57
58addEventHandler ( "onPlayerQuit", root,
59 function ( )
60 local account = getPlayerAccount ( source )
61 if ( not isGuestAccount ( account ) ) then
62 local accountName = getAccountName ( account )
63 if ( muteTimer [ accountName ] and isPlayerMuted ( source ) ) then
64 savePlayerMuteTimeLeft ( source )
65 end
66 end
67 end
68)
69
70addEvent ( "adminPanel:adminActions", true )
71addEventHandler ( "adminPanel:adminActions", root,
72 function ( action, thePlayer, duration, reason )
73 if isElement ( thePlayer ) then
74 if ( action == "mute" ) then
75 if ( not isPlayerMuted ( thePlayer ) ) then
76 if ( duration == 0 ) then
77 if adminMutePlayer ( thePlayer, "permanently", reason ) then
78 outputChatBox ( getPlayerName ( source ) .." permanently muted ".. getPlayerName ( thePlayer ).." (".. tostring ( reason ) ..")", root, 255, 0, 0 )
79 triggerClientEvent ( source, "adminPanel:closeMuteWindow", source )
80 end
81 else
82 if adminMutePlayer ( thePlayer, duration, reason ) then
83 outputChatBox ( getPlayerName ( source ) .." muted ".. getPlayerName ( thePlayer ).." for ".. tostring ( duration ) .." seconds (".. tostring ( reason ) ..")", root, 255, 0, 0 )
84 triggerClientEvent ( source, "adminPanel:closeMuteWindow", source )
85 end
86 end
87 end
88 elseif ( action == "unMute" ) then
89 if isPlayerMuted ( thePlayer ) then
90 if adminUnMutePlayer ( thePlayer ) then
91 outputChatBox ( getPlayerName ( source ) .." unmuted ".. getPlayerName ( thePlayer ), root, 255, 165, 0 )
92 end
93 end
94 end
95 end
96 end
97)
98
99--##Utils functions
100function adminMutePlayer ( thePlayer, theDuration, theReason )
101 if ( not db ) then
102 return false
103 end
104
105 if ( isElement ( thePlayer ) and theDuration ) then
106 adminUnMutePlayer ( thePlayer )
107 local account = getPlayerAccount ( thePlayer )
108 local accountName = getAccountName ( account )
109 if dbExec ( db, "INSERT INTO mutes SET account = ?, mutetime = ?", tostring ( accountName ) , tostring ( theDuration ) ) then
110 setPlayerMuted ( thePlayer, true )
111 if ( theDuration == "permanently" ) then
112 muteTimer [ accountName ] = "permanently"
113 else
114 muteTimer [ accountName ] = setTimer (
115 function ( thePlayer )
116 if isElement ( thePlayer ) then
117 adminUnMutePlayer ( thePlayer )
118 end
119 end
120 ,theDuration * 1000, 1, thePlayer
121 )
122 end
123
124 return true
125 end
126 end
127
128 return false
129end
130
131function adminUnMutePlayer ( thePlayer )
132 if ( not db ) then
133 return false
134 end
135
136 local account = getPlayerAccount ( thePlayer )
137 local accountName = getAccountName ( account )
138 if ( muteTimer [ accountName ] ) then
139 if dbExec ( db, "DELETE FROM mutes WHERE account = ?", tostring ( accountName ) ) then
140 setPlayerMuted ( thePlayer, false )
141 if isTimer ( muteTimer [ accountName ] ) then
142 killTimer ( muteTimer [ accountName ] )
143 end
144 muteTimer [ accountName ] = nil
145
146 return true
147 end
148 end
149
150 return false
151end
152
153function getMuteTimeRemaining ( thePlayer )
154 if ( not isElement ( thePlayer ) ) then
155 return false
156 end
157
158 local remaining = false
159 local account = getPlayerAccount ( thePlayer )
160 local accountName = getAccountName ( account )
161 if ( muteTimer [ accountName ] ) then
162 if ( muteTimer [ accountName ] == "permanently" ) then
163 remaining = "Permanently"
164 else
165 if isTimer ( muteTimer [ accountName ] ) then
166 remaining = math.floor ( getTimerDetails ( muteTimer [ accountName ] ) / 1000 )
167 end
168 end
169 else
170 return false
171 end
172
173 return remaining
174end
175
176function savePlayerMuteTimeLeft ( thePlayer )
177 if ( not db ) then
178 return false
179 end
180
181 if isElement ( thePlayer ) then
182 local accountName = getAccountName ( getPlayerAccount ( thePlayer ) )
183 local remaining = getMuteTimeRemaining ( thePlayer )
184 if ( remaining ) then
185 if dbExec ( db, "UPDATE mutes SET mutetime = ? WHERE account = ?", tostring ( remaining ), tostring ( accountName ) ) then
186 muteTimer [ accountName ] = tostring ( remaining )
187
188 return true
189 end
190 end
191 end
192
193 return false
194end
195
196--##DEV Command
197addCommandHandler ( "isnil",
198 function ( thePlayer )
199 local accountName = getAccountName ( getPlayerAccount ( thePlayer ) )
200 outputChatBox ( type ( muteTimer [ accountName ] ), thePlayer )
201 outputChatBox ( tostring ( getMuteTimeRemaining ( thePlayer ) ), thePlayer )
202 end
203)