Debian Codegraph @

lua/ghost/command_callback.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 --- GHOST -> PVPGN (CALLBACK) ---
16 ---------------------------------
17
18 -- /ghost [cmd] [code] {args}
19 function command_ghost(account, text)
20 if not config.ghost then return 1 end
21
22 -- restrict commands for authorized bots only
23 if not gh_is_bot(account.name) then return 1 end
24
25 local args = split_command(text, 5)
26 local cmd = args[1]
27 local code = args[2]
28
29 if code ~= "ok" then
30 if code == "err" then
31 local err_message = args[3]
32 -- HINT: you can handle error here
33 end
34 return -1
35 end
36
37 -- /ghost init
38 if (cmd == "init") then
39 INFO("GHost bot is connected (" .. account.name .. ")")
40
41 -- /ghost gameresult [players] [ratings] [results]
42 elseif (cmd == "gameresult") then
43 gh_callback_gameresult(args[3], args[4], args[5])
44
45 -- /ghost host [code] [user] [gamename]
46 elseif (cmd == "host") then
47 args = split_command(text, 4) -- support game name with spaces
48 gh_callback_host(args[3], account.name, args[4])
49 -- /ghost chost [code] [user] [gamename]
50 elseif (cmd == "chost") then
51 args = split_command(text, 4) -- support game name with spaces
52 gh_callback_chost(args[3], account.name, args[4])
53
54 -- /ghost unhost [code] [user] [gamename]
55 elseif (cmd == "unhost") then
56 gh_callback_unhost(args[3], args[4])
57
58 -- /ghost ping [code] [user] [players] [pings]
59 elseif (cmd == "ping") then
60 gh_callback_ping(args[3], args[4], args[5])
61
62 -- /ghost swap [code] [user] [username1] [username2]
63 elseif (cmd == "swap") then
64 -- HINT: you can notify user about success usage
65
66 -- /ghost open [code] [user] [slot]
67 elseif (cmd == "open") or (cmd == "close") then
68 -- HINT: you can notify user about success usage
69
70 -- /ghost [cmd] [code] [user] [message]
71 elseif (cmd == "start") or (cmd == "abort")
72 or (cmd == "pub") or (cmd == "priv") then
73 -- HINT: you can notify user about success usage
74 end
75
76 -- FIXME: may be do not log commands from bot? (return -1)
77 return 0
78 end
79
80 function gh_callback_host(username, botname, gamename)
81 local gametype = "5x5"
82 if string.find(game.name, "3x3") then gametype = "3x3" end
83
84 gh_set_userbot(username, botname, gamename, gametype)
85 api.message_send_text(username, message_type_info, nil, localize(username, "Game \"{}\" is created by {}. You are an owner.", gamename, botname))
86 end
87 function gh_callback_chost(username, botname, gamename)
88 gh_set_userbot(username, botname, gamename, "")
89 api.message_send_text(username, message_type_info, nil, localize(username, "Game \"{}\" is created by {}. You are an owner.", gamename, botname))
90 end
91
92
93 function gh_callback_unhost(username, gamename)
94 gh_del_userbot(username)
95 api.message_send_text(username, message_type_info, nil, localize(username, "The game \"{}\" was destroyed.", gamename))
96 end
97
98 function gh_callback_ping(username, players, pings)
99 -- make sure that user is still in a game
100 local account = api.account_get_by_name(username)
101 local game = api.game_get_by_id(account.game_id)
102 if not next(game) then return end
103 -- make sure game owner is a bot
104 if not gh_is_bot(game.owner) then return end
105
106 -- get silent flag (do not send result to user)
107 -- it's needed to do not show ping when user join a game, but we want to save the ping into database
108 local silent = gh_get_silentflag(username)
109
110 local users = {}
111
112 i = 1
113 -- fill usernames
114 for v in string.split(players, ",") do
115 -- init each user table
116 users[i] = {}
117 users[i]["name"] = v
118 i = i + 1
119 end
120 i = 1
121 -- fill pings
122 for v in string.split(pings, ",") do
123 users[i]["ping"] = v
124 i = i + 1
125 end
126
127 local latency_all = ""
128 local ms = localize(username, "ms")
129 -- iterate each user
130 for i,u in pairs(users) do
131 -- load user pings
132 local pings = account_get_botping(u["name"])
133 local is_found = false
134 for k,v in pairs(pings) do
135 -- update user ping for current bot
136 if (v.bot == game.owner) then
137 pings[k].ping = u["ping"]
138 is_found = true
139 end
140 end
141 -- if bot not found in database pings then add a new row
142 if not is_found then
143 local item = {
144 date = os.time(),
145 bot = game.owner,
146 ping = u["ping"]
147 }
148 table.insert(pings, item)
149 end
150
151 -- save updated pings
152 account_set_botping(u["name"], pings)
153
154 latency_all = latency_all .. string.format("%s: [%s %s]; ", u["name"], u["ping"], ms)
155
156 -- if game started send ping of each players on a new line
157 if (game.status == game_status_started) and not (silent) then
158 api.message_send_text(username, message_type_info, nil, localize(username, "{}'s latency to {}: {} {}", u["name"], game.owner, u["ping"], ms))
159 end
160 end
161
162 -- if game not started send pings of all players in a single line
163 if not (game.status == game_status_started) and not (silent) then
164 api.message_send_text(username, message_type_info, nil, localize(username, "Latency: {}", latency_all))
165 end
166 end
167
168
169 function gh_callback_gameresult(players, ratings, results)
170 local users = {}
171
172 -- fill table users
173 i = 1
174 for v in string.split(players, ",") do
175 -- init each user table
176 users[i] = {}
177 users[i]["name"] = v
178 i = i + 1
179 end
180 i = 1
181 for v in string.split(ratings, ",") do
182 users[i]["rating"] = tonumber(v)
183 i = i + 1
184 end
185 i = 1
186 for v in string.split(results, ",") do
187 users[i]["result"] = tonumber(v)
188 i = i + 1
189 end
190
191 -- iterate each user
192 for i,u in pairs(users) do
193 local rating, wins, losses, leaves, streaks = 0,0,0,0,0
194 local gametype = ""
195
196 -- get stats
197 if #users == 6 then
198 gametype = "3x3"
199 rating = account_get_dotarating_3x3(u["name"])
200 wins = account_get_dotawins_3x3(u["name"])
201 losses = account_get_dotalosses_3x3(u["name"])
202 leaves = account_get_dotaleaves_3x3(u["name"])
203 streaks = account_get_dotastreaks_3x3(u["name"])
204 elseif #users == 10 then
205 gametype = "5x5"
206 rating = account_get_dotarating_5x5(u["name"])
207 wins = account_get_dotawins_5x5(u["name"])
208 losses = account_get_dotalosses_5x5(u["name"])
209 leaves = account_get_dotaleaves_5x5(u["name"])
210 streaks = account_get_dotastreaks_5x5(u["name"])
211 else
212 -- bad result
213 ERROR(string.format("Bad game result (%s %s %s)", players, ratings, results))
214 return
215 end
216
217 -- calc new stats
218 if u["result"] == 2 then -- leave & win
219 leaves = leaves + 1
220 wins = wins + 1
221 elseif u["result"] == 1 then -- win
222 wins = wins + 1
223 elseif u["result"] == 0 then -- loss
224 losses = losses + 1
225 elseif u["result"] == -1 then -- leave & loss
226 leaves = leaves + 1
227 losses = losses + 1
228 end
229
230 local result_str = localize(u["name"], "won")
231
232 if streaks >= 0 then -- win streak
233 if u["result"] > 0 then
234 streaks = streaks + 1
235 else
236 streaks = -1
237 end
238 else -- loss streak
239 if u["result"] <= 0 then
240 streaks = streaks - 1
241 else
242 streaks = 1
243 end
244 result_str = localize(u["name"], "lose")
245 end
246
247 -- update stats
248 if #users == 6 then
249 account_set_dotarating_3x3(u["name"], u["rating"])
250 account_set_dotawins_3x3(u["name"], wins)
251 account_set_dotalosses_3x3(u["name"], losses)
252 account_set_dotaleaves_3x3(u["name"], leaves)
253 account_set_dotastreaks_3x3(u["name"], streaks)
254 elseif #users == 10 then
255 account_set_dotarating_5x5(u["name"], u["rating"])
256 account_set_dotawins_5x5(u["name"], wins)
257 account_set_dotalosses_5x5(u["name"], losses)
258 account_set_dotaleaves_5x5(u["name"], leaves)
259 account_set_dotastreaks_5x5(u["name"], streaks)
260 end
261
262 -- diff between old and new rating
263 local points = u["rating"]-rating
264
265 -- inform user about new stats
266 api.message_send_text(u["name"], message_type_info, nil, localize(u["name"], "You {} {} points.", result_str, points))
267 api.message_send_text(u["name"], message_type_info, nil, localize(u["name"], "New DotA ({}) rating: {} pts. Current {} streak: {}", gametype, rating, result_str, streaks))
268 end
269
270
271 end