· 9 years ago · Oct 11, 2016, 09:42 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
99function adminMutePlayer ( thePlayer, theDuration, theReason )
100 if ( not db ) then
101 return false
102 end
103
104 if ( isElement ( thePlayer ) and theDuration ) then
105 adminUnMutePlayer ( thePlayer )
106 local account = getPlayerAccount ( thePlayer )
107 local accountName = getAccountName ( account )
108 if dbExec ( db, "INSERT INTO mutes SET account = ?, mutetime = ?", tostring ( accountName ) , tostring ( theDuration ) ) then
109 setPlayerMuted ( thePlayer, true )
110 if ( theDuration == "permanently" ) then
111 muteTimer [ accountName ] = "permanently"
112 else
113 muteTimer [ accountName ] = setTimer (
114 function ( thePlayer )
115 if isElement ( thePlayer ) then
116 adminUnMutePlayer ( thePlayer )
117 end
118 end
119 ,theDuration * 1000, 1, thePlayer
120 )
121 end
122
123 return true
124 end
125 end
126
127 return false
128end
129
130function adminUnMutePlayer ( thePlayer )
131 if ( not db ) then
132 return false
133 end
134
135 local account = getPlayerAccount ( thePlayer )
136 local accountName = getAccountName ( account )
137 if ( muteTimer [ accountName ] ) then
138 if dbExec ( db, "DELETE FROM mutes WHERE account = ?", tostring ( accountName ) ) then
139 setPlayerMuted ( thePlayer, false )
140 if isTimer ( muteTimer [ accountName ] ) then
141 killTimer ( muteTimer [ accountName ] )
142 end
143 muteTimer [ accountName ] = nil
144
145 return true
146 end
147 end
148
149 return false
150end
151
152function getMuteTimeRemaining ( thePlayer )
153 if ( not isElement ( thePlayer ) ) then
154 return false
155 end
156
157 local remaining = false
158 local account = getPlayerAccount ( thePlayer )
159 local accountName = getAccountName ( account )
160 if ( muteTimer [ accountName ] ) then
161 if ( muteTimer [ accountName ] == "permanently" ) then
162 remaining = "Permanently"
163 else
164 if isTimer ( muteTimer [ accountName ] ) then
165 remaining = math.floor ( getTimerDetails ( muteTimer [ accountName ] ) / 1000 )
166 end
167 end
168 else
169 return false
170 end
171
172 return remaining
173end
174
175function savePlayerMuteTimeLeft ( thePlayer )
176 if ( not db ) then
177 return false
178 end
179
180 if isElement ( thePlayer ) then
181 local accountName = getAccountName ( getPlayerAccount ( thePlayer ) )
182 local remaining = getMuteTimeRemaining ( thePlayer )
183 if ( remaining ) then
184 if dbExec ( db, "UPDATE mutes SET mutetime = ? WHERE account = ?", tostring ( remaining ), tostring ( accountName ) ) then
185 muteTimer [ accountName ] = tostring ( remaining )
186
187 return true
188 end
189 end
190 end
191
192 return false
193end
194
195--##DEV Command
196addCommandHandler ( "isnil",
197 function ( thePlayer )
198 local accountName = getAccountName ( getPlayerAccount ( thePlayer ) )
199 outputChatBox ( type ( muteTimer [ accountName ] ), thePlayer )
200 outputChatBox ( tostring ( getMuteTimeRemaining ( thePlayer ) ), thePlayer )
201 end
202)