File: helper.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 (160 lines) | stat: -rw-r--r-- 4,030 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
--[[
	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.
]]--


-- table key=value (user=bot)
-- it fills first when user uses a command
gh_user2bot = {}

-- array with users who will not receive a response from /ping
silentpings = {}

-- return a user mapped bot name
-- (nil if bot not mapped)
function gh_get_userbot_name(username)
	if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
	return gh_user2bot[username].bot
end
-- return a user mapped game name that created by bot
function gh_get_userbot_game(username)
	if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
	return gh_user2bot[username].game
end
-- return a user mapped game type that created by bot
function gh_get_userbot_gametype(username)
	if not next(gh_user2bot) or not gh_user2bot[username] then return nil end
	return gh_user2bot[username].gametype
end
-- find gamename in games and return owner of the game (username)
-- return false if not found
function gh_find_userbot_by_game(gamename)
	for u,bot in pairs(gh_user2bot) do
		if (bot.game == gamename) then return u end
	end
	return false
end

function gh_set_userbot(username, botname, gamename, gametype)
	gh_user2bot[username] = { bot = botname, game = gamename, type = gametype, time = os.time() }
end
function gh_del_userbot(username)
	gh_user2bot[username] = nil
end

-- save table to file
function gh_save_userbots(filename)
	table.save(gh_user2bot, filename)
	return true
end
-- load table from file
function gh_load_userbots(filename)
	gh_user2bot = table.load(filename)
	
	-- debug info
	if next(gh_user2bot) then
		TRACE("Users assigned to bots:")
		for u,bot in pairs(gh_user2bot) do
			TRACE(u..": "..bot.bot.." ("..bot.game..", "..bot.time..")")
		end
	end
end




-- add user into a silenttable
function gh_set_silentflag(username)
	table.insert(silentpings, account.name)
end
-- return true if user in a silenttable, and remove it
function gh_get_silentflag(username)
	for k,v in pairs(silentpings) do
		if v == username then
			table.remove(silentpings, k)
			return true
		end
	end
	return false
end


-- Get path to GHost directory
function gh_directory()
	return config.scriptdir .. "/ghost"
end



-- is user owner of bot in current game
function gh_is_owner(account)
	-- 1) user in game
	if not account.game_id then return false end

	local game = game_get_by_id(account.game_id)
	-- 2) user owner of the bot in current game
	if not game.owner == gh_get_userbot_name(account.name) then return false end
	
	return true
end




-- is bot in authorized config list?
function gh_is_bot(username)
	for i,bot in pairs(config.ghost_bots) do
		if string.lower(bot) == string.lower(username) then return true end
	end
	return false
end

	
-- redirect command from user to bot through PvPGN
function gh_message_send(botname, text)
	-- send message to ghost from the server 
	api.message_send_text(botname, message_type_whisper, nil, text)
end

-- return bot name (the best for user by ping)
-- return nil if no available bots
function gh_select_bot(username)
	local pings = account_get_botping(username)
	local botname = nil
	
	-- iterate all bots in config
	for i,bot in pairs(config.ghost_bots) do
		local is_found = false
		for k,v in pairs(pings) do
			if bot == v.bot then is_found = true end
		end
		-- if user has has no ping for the bot yet - use it
		if not is_found then
			botname = bot
		end
	end
	
	-- if user has all bots pings from config
	if not botname then
		-- select the best bot by ping (the table pings already sorted by ping)
		botname = pings[1].bot
	end

	local botacc = api.account_get_by_name(botname)
	-- if bot is offline then use first available
	-- FIXME: use next available by the best ping?
	if botacc.online == "false" then
		botname = nil
		for i,bot in pairs(config.ghost_bots) do
			botacc = api.account_get_by_name(bot)
			if botacc.online == "true" then return bot end
		end
	end
	
	return botname
end