Debian Codegraph @

lua/ghost/helper.lua

raw symbols SCIP
1 --[[
2 Copyright (C) 2014 HarpyWar ([email protected])
3
4 This file is a part of the PvPGN Project http://pvpgn.pro
5 Licensed under the same terms as Lua itself.
6 ]]--
7
8
9 -- table key=value (user=bot)
10 -- it fills first when user uses a command
11 gh_user2bot = {}
12
13 -- array with users who will not receive a response from /ping
14 silentpings = {}
15
16 -- return a user mapped bot name
17 -- (nil if bot not mapped)
18 function gh_get_userbot_name(username)
19 if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
20 return gh_user2bot[username].bot
21 end
22 -- return a user mapped game name that created by bot
23 function gh_get_userbot_game(username)
24 if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
25 return gh_user2bot[username].game
26 end
27 -- return a user mapped game type that created by bot
28 function gh_get_userbot_gametype(username)
29 if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
30 return gh_user2bot[username].gametype
31 end
32 -- find gamename in games and return owner of the game (username)
33 -- return false if not found
34 function gh_find_userbot_by_game(gamename)
35 for u,bot in pairs(gh_user2bot) do
36 if (bot.game == gamename) then return u end
37 end
38 return false
39 end
40
41 function gh_set_userbot(username, botname, gamename, gametype)
42 gh_user2bot[username] = { bot = botname, game = gamename, type = gametype, time = os.time() }
43 end
44 function gh_del_userbot(username)
45 gh_user2bot[username] = nil
46 end
47
48 -- save table to file
49 function gh_save_userbots(filename)
50 table.save(gh_user2bot, filename)
51 return true
52 end
53 -- load table from file
54 function gh_load_userbots(filename)
55 gh_user2bot = table.load(filename)
56
57 -- debug info
58 if next(gh_user2bot) then
59 TRACE("Users assigned to bots:")
60 for u,bot in pairs(gh_user2bot) do
61 TRACE(u..": "..bot.bot.." ("..bot.game..", "..bot.time..")")
62 end
63 end
64 end
65
66
67
68
69 -- add user into a silenttable
70 function gh_set_silentflag(username)
71 table.insert(silentpings, account.name)
72 end
73 -- return true if user in a silenttable, and remove it
74 function gh_get_silentflag(username)
75 for k,v in pairs(silentpings) do
76 if v == username then
77 table.remove(silentpings, k)
78 return true
79 end
80 end
81 return false
82 end
83
84
85 -- Get path to GHost directory
86 function gh_directory()
87 return config.scriptdir .. "/ghost"
88 end
89
90
91
92 -- is user owner of bot in current game
93 function gh_is_owner(account)
94 -- 1) user in game
95 if not account.game_id then return false end
96
97 local game = game_get_by_id(account.game_id)
98 -- 2) user owner of the bot in current game
99 if not game.owner == gh_get_userbot_name(account.name) then return false end
100
101 return true
102 end
103
104
105
106
107 -- is bot in authorized config list?
108 function gh_is_bot(username)
109 for i,bot in pairs(config.ghost_bots) do
110 if string.lower(bot) == string.lower(username) then return true end
111 end
112 return false
113 end
114
115
116 -- redirect command from user to bot through PvPGN
117 function gh_message_send(botname, text)
118 -- send message to ghost from the server
119 api.message_send_text(botname, message_type_whisper, nil, text)
120 end
121
122 -- return bot name (the best for user by ping)
123 -- return nil if no available bots
124 function gh_select_bot(username)
125 local pings = account_get_botping(username)
126 local botname = nil
127
128 -- iterate all bots in config
129 for i,bot in pairs(config.ghost_bots) do
130 local is_found = false
131 for k,v in pairs(pings) do
132 if bot == v.bot then is_found = true end
133 end
134 -- if user has has no ping for the bot yet - use it
135 if not is_found then
136 botname = bot
137 end
138 end
139
140 -- if user has all bots pings from config
141 if not botname then
142 -- select the best bot by ping (the table pings already sorted by ping)
143 botname = pings[1].bot
144 end
145
146 local botacc = api.account_get_by_name(botname)
147 -- if bot is offline then use first available
148 -- FIXME: use next available by the best ping?
149 if botacc.online == "false" then
150 botname = nil
151 for i,bot in pairs(config.ghost_bots) do
152 botacc = api.account_get_by_name(bot)
153 if botacc.online == "true" then return bot end
154 end
155 end
156
157 return botname
158 end
159
160