File: command_callback.lua

package info (click to toggle)
pvpgn 1.99.7.2.1%2Bdfsg-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 8,300 kB
  • sloc: cpp: 118,407; xml: 6,367; sh: 1,558; ansic: 1,300; perl: 604; cs: 382; python: 308; php: 130; awk: 73; makefile: 19
file content (271 lines) | stat: -rw-r--r-- 7,990 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
--[[
	Connection interface between PvPGN and GHost
	https://github.com/OHSystem/ohsystem/issues/279

	
	Copyright (C) 2014 HarpyWar (harpywar@gmail.com)
	
	This file is a part of the PvPGN Project http://pvpgn.pro
	Licensed under the same terms as Lua itself.
]]--



---------------------------------
--- GHOST -> PVPGN (CALLBACK) ---
---------------------------------

-- /ghost [cmd] [code] {args}
function command_ghost(account, text)
	if not config.ghost then return 1 end
	
	-- restrict commands for authorized bots only
	if not gh_is_bot(account.name) then return 1 end
	
	local args = split_command(text, 5)
	local cmd = args[1]
	local code = args[2]
	
	if code ~= "ok" then
		if code == "err" then
			local err_message = args[3]
			-- HINT: you can handle error here
		end
		return -1
	end
	
	-- /ghost init
	if (cmd == "init") then
		INFO("GHost bot is connected (" .. account.name .. ")")
		
	-- /ghost gameresult [players] [ratings] [results]
	elseif (cmd == "gameresult") then
		gh_callback_gameresult(args[3], args[4], args[5])

	-- /ghost host [code] [user] [gamename]
	elseif (cmd == "host") then
		args = split_command(text, 4) -- support game name with spaces
		gh_callback_host(args[3], account.name, args[4])
	-- /ghost chost [code] [user] [gamename]
	elseif (cmd == "chost") then
		args = split_command(text, 4) -- support game name with spaces
		gh_callback_chost(args[3], account.name, args[4])
		
	-- /ghost unhost [code] [user] [gamename]
	elseif (cmd == "unhost") then
		gh_callback_unhost(args[3], args[4])
		
	-- /ghost ping [code] [user] [players] [pings]
	elseif (cmd == "ping") then
		gh_callback_ping(args[3], args[4], args[5])
		
	-- /ghost swap [code] [user] [username1] [username2]
	elseif (cmd == "swap") then
		-- HINT: you can notify user about success usage
		
	-- /ghost open [code] [user] [slot]
	elseif (cmd == "open") or (cmd == "close") then
		-- HINT: you can notify user about success usage 
		
	-- /ghost [cmd] [code] [user] [message]
	elseif (cmd == "start") or (cmd == "abort") 
		or (cmd == "pub") or (cmd == "priv") then
		-- HINT: you can notify user about success usage
	end
	
	-- FIXME: may be do not log commands from bot? (return -1)
	return 0
end

function gh_callback_host(username, botname, gamename)
	local gametype = "5x5"
	if string.find(game.name, "3x3") then gametype = "3x3" end
			
	gh_set_userbot(username, botname, gamename, gametype)
	api.message_send_text(username, message_type_info, nil, localize(username, "Game \"{}\" is created by {}. You are an owner.", gamename, botname))
end
function gh_callback_chost(username, botname, gamename)
	gh_set_userbot(username, botname, gamename, "")
	api.message_send_text(username, message_type_info, nil, localize(username, "Game \"{}\" is created by {}. You are an owner.", gamename, botname))
end


function gh_callback_unhost(username, gamename)
	gh_del_userbot(username)
	api.message_send_text(username, message_type_info, nil, localize(username, "The game \"{}\" was destroyed.", gamename))
end

function gh_callback_ping(username, players, pings)
	-- make sure that user is still in a game
	local account = api.account_get_by_name(username)
	local game = api.game_get_by_id(account.game_id)
	if not next(game) then return end
	-- make sure game owner is a bot
	if not gh_is_bot(game.owner) then return end

	-- get silent flag (do not send result to user)
	-- it's needed to do not show ping when user join a game, but we want to save the ping into database
	local silent = gh_get_silentflag(username)

	local users = {}

	i = 1
	-- fill usernames
	for v in string.split(players, ",") do
		-- init each user table
		users[i] = {}
		users[i]["name"] = v
		i = i + 1
	end
	i = 1
	-- fill pings
	for v in string.split(pings, ",") do
		users[i]["ping"] = v
		i = i + 1
	end
	
	local latency_all = ""
	local ms = localize(username, "ms")
	-- iterate each user
	for i,u in pairs(users) do
		-- load user pings
		local pings = account_get_botping(u["name"])
		local is_found = false
		for k,v in pairs(pings) do
			-- update user ping for current bot
			if (v.bot == game.owner) then
				pings[k].ping = u["ping"]
				is_found = true
			end
		end
		-- if bot not found in database pings then add a new row
		if not is_found then
			local item = { 
				date = os.time(), 
				bot = game.owner, 
				ping = u["ping"]
			}
			table.insert(pings, item)
		end
		
		-- save updated pings
		account_set_botping(u["name"], pings)

		latency_all = latency_all .. string.format("%s: [%s %s]; ", u["name"], u["ping"], ms)
		
		-- if game started send ping of each players on a new line
		if (game.status == game_status_started) and not (silent) then
			api.message_send_text(username, message_type_info, nil, localize(username, "{}'s latency to {}: {} {}", u["name"], game.owner, u["ping"], ms))
		end	
	end
	
	-- if game not started send pings of all players in a single line
	if not (game.status == game_status_started) and not (silent) then
		api.message_send_text(username, message_type_info, nil, localize(username, "Latency: {}", latency_all))
	end
end


function gh_callback_gameresult(players, ratings, results)
	local users = {}

	-- fill table users
	i = 1
	for v in string.split(players, ",") do
		-- init each user table
		users[i] = {}
		users[i]["name"] = v
		i = i + 1
	end
	i = 1
	for v in string.split(ratings, ",") do
		users[i]["rating"] = tonumber(v)
		i = i + 1
	end
	i = 1
	for v in string.split(results, ",") do
		users[i]["result"] = tonumber(v)
		i = i + 1
	end

	-- iterate each user
	for i,u in pairs(users) do
		local rating, wins, losses, leaves, streaks = 0,0,0,0,0
		local gametype = ""
		
		-- get stats
		if #users == 6 then
			gametype = "3x3"
			rating = account_get_dotarating_3x3(u["name"])
			wins = account_get_dotawins_3x3(u["name"])
			losses = account_get_dotalosses_3x3(u["name"])
			leaves = account_get_dotaleaves_3x3(u["name"])
			streaks = account_get_dotastreaks_3x3(u["name"])
		elseif #users == 10 then
			gametype = "5x5"
			rating = account_get_dotarating_5x5(u["name"])
			wins = account_get_dotawins_5x5(u["name"])
			losses = account_get_dotalosses_5x5(u["name"])
			leaves = account_get_dotaleaves_5x5(u["name"])
			streaks = account_get_dotastreaks_5x5(u["name"])
		else
			-- bad result
			ERROR(string.format("Bad game result (%s %s %s)", players, ratings, results))
			return
		end

		-- calc new stats
		if u["result"] == 2 then -- leave & win
			leaves = leaves + 1
			wins = wins + 1
		elseif u["result"] == 1 then -- win
			wins = wins + 1
		elseif u["result"] == 0 then -- loss
			losses = losses + 1
		elseif u["result"] == -1 then -- leave & loss
			leaves = leaves + 1
			losses = losses + 1
		end
		
		local result_str = localize(u["name"], "won")
		
		if streaks >= 0 then -- win streak
			if u["result"] > 0 then 
				streaks = streaks + 1
			else
				streaks = -1
			end
		else -- loss streak
			if u["result"] <= 0 then 
				streaks = streaks - 1
			else
				streaks = 1
			end
			result_str = localize(u["name"], "lose")
		end
		
	-- update stats
		if #users == 6 then
			account_set_dotarating_3x3(u["name"], u["rating"])
			account_set_dotawins_3x3(u["name"], wins)
			account_set_dotalosses_3x3(u["name"], losses)
			account_set_dotaleaves_3x3(u["name"], leaves)
			account_set_dotastreaks_3x3(u["name"], streaks)
		elseif #users == 10 then
			account_set_dotarating_5x5(u["name"], u["rating"])
			account_set_dotawins_5x5(u["name"], wins)
			account_set_dotalosses_5x5(u["name"], losses)
			account_set_dotaleaves_5x5(u["name"], leaves)
			account_set_dotastreaks_5x5(u["name"], streaks)
		end
		
		-- diff between old and new rating
		local points = u["rating"]-rating
		
		-- inform user about new stats
		api.message_send_text(u["name"], message_type_info, nil, localize(u["name"], "You {} {} points.", result_str, points))
		api.message_send_text(u["name"], message_type_info, nil, localize(u["name"], "New DotA ({}) rating: {} pts. Current {} streak: {}", gametype, rating, result_str, streaks))
	end
	

end