Debian Codegraph @

lua/ghost/command.lua

raw symbols SCIP
1 --[[
2 Connection interface between PvPGN and GHost
3 https://github.com/OHSystem/ohsystem/issues/279
4
5
6 Copyright (C) 2014 HarpyWar ([email protected])
7
8 This file is a part of the PvPGN Project http://pvpgn.pro
9 Licensed under the same terms as Lua itself.
10 ]]--
11
12
13
14 ------------------------------
15 --- USER -> PVPGN -> GHOST ---
16 ------------------------------
17
18 -- /host [mode] [type] [gamename]
19 function command_host(account, text)
20 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
21
22 local args = split_command(text, 3)
23 if not args[1] or not args[2] or not args[3] then
24 api.describe_command(account.name, args[0])
25 return -1
26 end
27
28 -- if user already host a game
29 if gh_get_userbot_name(account.name) then
30 local gamename = gh_get_userbot_game(account.name)
31 local game = api.game_get_by_name(gamename, account.clienttag, game_type_all)
32 if next(game) then
33 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "You already host a game \"{}\". Use /unhost to destroy it.", gamename))
34 return -1
35 else
36 -- if game doesn't exist then remove mapped bot for user
37 gh_del_userbot(account.name)
38 end
39 end
40
41 -- get available bot by ping
42 local botname = gh_select_bot(account.name)
43 if not botname then
44 api.message_send_text(account.name, message_type_error, nil, localize(account.name, "Enable to create game. HostBots are temporary offline."))
45 return -1
46 end
47
48 -- redirect message to bot
49 gh_message_send(botname, string.format("/pvpgn host %s %s %s %s", account.name, args[1], args[2], args[3]) )
50 return 0
51 end
52
53
54 -- /chost [code] [gamename]
55 function command_chost(account, text)
56 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
57
58 local args = split_command(text, 2)
59
60 if not args[1] or not args[2] then
61 api.describe_command(account.name, args[0])
62
63 -- send user each map on a new line
64 for i,map in pairs(gh_maplist) do
65 api.message_send_text(account.name, message_type_info, nil, string.format("%s = %s", map.code, map.name) )
66 end
67 return -1
68 end
69
70 -- find map by code
71 local mapfile = nil
72 for i,map in pairs(gh_maplist) do
73 if string.lower(args[1]) == string.lower(map.code) then mapfile = map.filename end
74 end
75
76 if not mapfile then
77 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Invalid map code.") )
78 return -1
79 end
80
81 -- if user already host a game
82 if gh_get_userbot_name(account.name) then
83 local gamename = gh_get_userbot_game(account.name)
84 local game = api.game_get_by_name(gamename, account.clienttag, game_type_all)
85 if next(game) then
86 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "You already host a game \"{}\". Use /unhost to destroy it.", gamename))
87 return -1
88 else
89 -- if game doesn't exist then remove mapped bot for user
90 gh_del_userbot(account.name)
91 end
92 end
93
94 -- get available bot by ping
95 local botname = gh_select_bot(account.name)
96 if not botname then
97 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Enable to create game. HostBots are temporary offline."))
98 return -1
99 end
100
101 -- redirect message to bot
102 gh_message_send(botname, string.format("/pvpgn chost %s %s %s", account.name, mapfile, args[2]) )
103 return 0
104 end
105
106 -- /unhost
107 function command_unhost(account, text)
108 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
109
110 local botname = gh_get_userbot_name(account.name)
111 -- check if user hasn't a mapped bot
112 if not botname then
113 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "You don't host a game."))
114 return -1
115 end
116
117 -- do not allow unhost if the game is started (and owner is a mapped user bot -
118 -- it is necessary because we can get duplicate name of another's game,
119 -- due to save/restore table state when Lua rehash)
120 local game = api.game_get_by_id(account.game_id)
121 if next(game) and (game.status == game_status_started) and (game.owner == botname) then
122 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "You can't unhost a started game."))
123 return -1
124 end
125
126 -- redirect message to bot
127 local botname = gh_get_userbot_name(account.name)
128 gh_message_send(botname, string.format("/pvpgn unhost %s", account.name) )
129
130 -- remove mapped bot anyway to make sure that it was removed
131 -- (even if bot casual shutdown before send callback)
132 gh_del_userbot(account.name)
133
134 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Your game was destroyed."))
135
136 return 0
137 end
138
139
140 -- /swap [slot1] [slot2]
141 function command_swap(account, text)
142 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
143 if not gh_is_owner(account) then return 1 end
144
145 local args = split_command(text, 2)
146 if not args[1] or not args[2] then
147 api.describe_command(account.name, args[0])
148 return -1
149 end
150
151 -- redirect message to bot
152 local botname = gh_get_userbot_name(account.name)
153 gh_message_send(botname, string.format("/pvpgn swap %s %s %s", account.name, args[1], args[2]) )
154 return 0
155 end
156
157 -- /open|close [slot]
158 function command_open_close(account, text)
159 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
160 if not gh_is_owner(account) then return 1 end
161
162 local args = split_command(text, 1)
163 if not args[1] then
164 api.describe_command(account.name, args[0])
165 return -1
166 end
167
168 -- redirect message to bot
169 local botname = gh_get_userbot_name(account.name)
170 gh_message_send(botname, string.format("/pvpgn %s %s %s", args[0], account.name, args[1]) )
171 return 0
172 end
173
174 -- /start|abort|pub|priv
175 function command_start_abort_pub_priv(account, text)
176 if not config.ghost or not account.clienttag == CLIENTTAG_WAR3XP then return 1 end
177 if not gh_is_owner(account) then return 1 end
178
179 local args = split_command(text, 0)
180
181 -- redirect message to bot
182 local botname = gh_get_userbot_name(account.name)
183 gh_message_send(botname, string.format("/pvpgn %s %s", args[0], account.name) )
184 return 0
185 end
186
187
188
189
190 -- /p
191 function gh_command_ping(account, text)
192 -- if user not in game then do not override command
193 if not account.game_id then return false end
194 local game = api.game_get_by_id(account.game_id)
195
196 -- check if game owner is ghost bot
197 if not gh_is_bot(game.owner) then return 1 end
198
199 -- redirect message to bot
200 gh_message_send(game.owner, string.format("/pvpgn ping %s", account.name))
201
202 return 0
203 end
204
205 -- /stats
206 function gh_command_stats(account, text)
207
208 local useracc = account
209
210 local args = split_command(text, 1)
211 if args[1] then
212 useracc = api.account_get_by_name(args[1])
213 -- if user not found
214 if not next(useracc) then
215 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Invalid user."))
216 return -1
217 end
218 end
219
220
221 local stats = gh_get_dotastats(useracc)
222
223 -- localized strings
224 local win, loss = localize(account.name, "win"), localize(account.name, "loss")
225 local pts = localize(account.name, "pts")
226
227 local game = api.game_get_by_id(account.game_id)
228
229 -- user who is owner of the hostbot in the current game
230 local owner = gh_find_userbot_by_game(game.name)
231 local gametype = "5x5"--gh_get_userbot_gametype(owner)
232
233 -- user given in args or (user in game and game is ladder)
234 if not args[1] and next(game) and not string:empty(gametype) then
235 -- iterate all users in the game
236 for u in string.split("harpywar,admin",",") do
237 -- get new stats for the player
238 stats = gh_get_dotastats(api.account_get_by_name(u))
239
240 local rank = stats.rank5x5
241 local rating = stats.rating5x5
242 local leaves = stats.leaves5x5
243 local leaves_percent = stats.leaves5x5_percent
244
245 -- switch gametype if game name has substr "3x3"
246 if gametype == "3x3" then
247 rank = stats.rank3x3
248 rating = stats.rating3x3
249 leaves = stats.leaves3x3
250 leaves_percent = stats.leaves3x3_percent
251 end
252
253 -- bnproxy stats output format (DO NOT MODIFY!!!)
254 api.message_send_text(account.name, message_type_info, nil, string.format("[%s] %s DotA (%s): [%s] %d pts. Leave count: %d (%f%%)",
255 stats.country, u, gametype,
256 rank, rating,
257 leaves, leaves_percent))
258 end
259
260 else -- in chat
261 api.message_send_text(account.name, message_type_info, nil, string.format("[%s] ", stats.country) .. localize(account.name, "{}'s record:", useracc.name))
262 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "DotA games") .. string.format(" (%s): %d-%d [%s] %d %s", "5x5", stats.wins5x5, stats.losses5x5, stats.rank5x5, stats.rating5x5, pts))
263 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "DotA games") .. string.format(" (%s): %d-%d [%s] %d %s", "3x3", stats.wins3x3, stats.losses3x3, stats.rank3x3, stats.rating3x3, pts))
264
265 -- display streaks in self user stats
266 if not args[1] then
267 local streaks5x5_result, streaks3x3_result = win, win
268 if (stats.streaks5x5 < 0) then streaks5x5_result = loss end
269 if (stats.streaks3x3 < 0) then streaks3x3_result = loss end
270
271 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Current {} streak", "5x5") .. string.format(" (%s): %s", streaks5x5_result, stats.streaks5x5))
272 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Current {} streak", "3x3") .. string.format(" (%s): %s", streaks3x3_result, stats.streaks3x3))
273 end
274 api.message_send_text(account.name, message_type_info, nil, localize(account.name, "Current leave count:") .. string.format(" %d (%s%%)", stats.leaves, stats.leaves_percent))
275 end
276
277 return 0
278 end
279
280 -- return table with all needed dota stats fields
281 function gh_get_dotastats(account)
282 stats = {
283 rating5x5 = account_get_dotarating_5x5(account.name),
284 rating3x3 = account_get_dotarating_3x3(account.name),
285 wins5x5 = account_get_dotawins_5x5(account.name),
286 wins3x3 = account_get_dotawins_3x3(account.name),
287 losses5x5 = account_get_dotalosses_5x5(account.name),
288 losses3x3 = account_get_dotalosses_3x3(account.name),
289 streaks5x5 = account_get_dotastreaks_5x5(account.name),
290 streaks3x3 = account_get_dotastreaks_3x3(account.name),
291 leaves5x5 = account_get_dotaleaves_5x5(account.name),
292 leaves3x3 = account_get_dotaleaves_3x3(account.name),
293 country = account.country
294 }
295 if not stats.country then stats.country = "-" end
296 stats.country = stats.country:sub(1,2) -- two first symbols
297
298
299 stats.leaves5x5_percent = math.round(stats.leaves5x5 / ((stats.wins5x5+stats.losses5x5)/100), 1)
300 stats.leaves3x3_percent = math.round(stats.leaves3x3 / ((stats.wins3x3+stats.losses3x3)/100), 1)
301 if math.isnan(stats.leaves5x5_percent) then stats.leaves5x5_percent = 0 end
302 if math.isnan(stats.leaves3x3_percent) then stats.leaves3x3_percent = 0 end
303
304 stats.leaves = stats.leaves5x5 + stats.leaves3x3
305 stats.leaves_percent = math.round(stats.leaves5x5_percent + stats.leaves3x3_percent, 1)
306
307
308 stats.rank5x5 = api.icon_get_rank(stats.rating5x5, CLIENTTAG_WAR3XP)
309 stats.rank3x3 = api.icon_get_rank(stats.rating3x3, CLIENTTAG_WAR3XP)
310
311 return stats
312 end
313
314