| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
function command_ghost(account, text) |
| 20 |
if not config.ghost then return 1 end |
| 21 |
|
| 22 |
|
| 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 |
|
| 33 |
end |
| 34 |
return -1 |
| 35 |
end |
| 36 |
|
| 37 |
|
| 38 |
if (cmd == "init") then |
| 39 |
INFO("GHost bot is connected (" .. account.name .. ")") |
| 40 |
|
| 41 |
|
| 42 |
elseif (cmd == "gameresult") then |
| 43 |
gh_callback_gameresult(args[3], args[4], args[5]) |
| 44 |
|
| 45 |
|
| 46 |
elseif (cmd == "host") then |
| 47 |
args = split_command(text, 4) |
| 48 |
gh_callback_host(args[3], account.name, args[4]) |
| 49 |
|
| 50 |
elseif (cmd == "chost") then |
| 51 |
args = split_command(text, 4) |
| 52 |
gh_callback_chost(args[3], account.name, args[4]) |
| 53 |
|
| 54 |
|
| 55 |
elseif (cmd == "unhost") then |
| 56 |
gh_callback_unhost(args[3], args[4]) |
| 57 |
|
| 58 |
|
| 59 |
elseif (cmd == "ping") then |
| 60 |
gh_callback_ping(args[3], args[4], args[5]) |
| 61 |
|
| 62 |
|
| 63 |
elseif (cmd == "swap") then |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
elseif (cmd == "open") or (cmd == "close") then |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
elseif (cmd == "start") or (cmd == "abort") |
| 72 |
or (cmd == "pub") or (cmd == "priv") then |
| 73 |
|
| 74 |
end |
| 75 |
|
| 76 |
|
| 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 |
|
| 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 |
|
| 104 |
if not gh_is_bot(game.owner) then return end |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
local silent = gh_get_silentflag(username) |
| 109 |
|
| 110 |
local users = {} |
| 111 |
|
| 112 |
i = 1 |
| 113 |
|
| 114 |
for v in string.split(players, ",") do |
| 115 |
|
| 116 |
users[i] = {} |
| 117 |
users[i]["name"] = v |
| 118 |
i = i + 1 |
| 119 |
end |
| 120 |
i = 1 |
| 121 |
|
| 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 |
|
| 130 |
for i,u in pairs(users) do |
| 131 |
|
| 132 |
local pings = account_get_botping(u["name"]) |
| 133 |
local is_found = false |
| 134 |
for k,v in pairs(pings) do |
| 135 |
|
| 136 |
if (v.bot == game.owner) then |
| 137 |
pings[k].ping = u["ping"] |
| 138 |
is_found = true |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 173 |
i = 1 |
| 174 |
for v in string.split(players, ",") do |
| 175 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 213 |
ERROR(string.format("Bad game result (%s %s %s)", players, ratings, results)) |
| 214 |
return |
| 215 |
end |
| 216 |
|
| 217 |
|
| 218 |
if u["result"] == 2 then |
| 219 |
leaves = leaves + 1 |
| 220 |
wins = wins + 1 |
| 221 |
elseif u["result"] == 1 then |
| 222 |
wins = wins + 1 |
| 223 |
elseif u["result"] == 0 then |
| 224 |
losses = losses + 1 |
| 225 |
elseif u["result"] == -1 then |
| 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 |
| 233 |
if u["result"] > 0 then |
| 234 |
streaks = streaks + 1 |
| 235 |
else |
| 236 |
streaks = -1 |
| 237 |
end |
| 238 |
else |
| 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 |
|
| 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 |
|
| 263 |
local points = u["rating"]-rating |
| 264 |
|
| 265 |
|
| 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 |