2015-11-29 10:45:00 +01:00
|
|
|
-- Plugin for the Hearthstone database provided by hearthstonejson.com.
|
|
|
|
|
2016-02-21 20:28:40 +01:00
|
|
|
if not database.hearthstone or os.time() > database.hearthstone.expiration then
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-02-21 20:28:40 +01:00
|
|
|
print('Downloading Hearthstone database...')
|
|
|
|
|
2016-03-31 13:53:12 +02:00
|
|
|
database.hearthstone = {
|
|
|
|
expiration = os.time() + 600000
|
|
|
|
}
|
2015-11-29 10:45:00 +01:00
|
|
|
|
|
|
|
local jstr, res = HTTPS.request('http://hearthstonejson.com/json/AllSets.json')
|
|
|
|
if res ~= 200 then
|
|
|
|
print('Error connecting to hearthstonejson.com.')
|
|
|
|
print('hearthstone.lua will not be enabled.')
|
2016-02-21 20:28:40 +01:00
|
|
|
return
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
|
|
|
for k,v in pairs(jdat) do
|
|
|
|
for key,val in pairs(v) do
|
2016-02-21 20:28:40 +01:00
|
|
|
table.insert(database.hearthstone, val)
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-04 23:08:21 +01:00
|
|
|
print('Download complete! It will be stored for a week.')
|
2016-02-21 20:28:40 +01:00
|
|
|
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local command = 'hearthstone <query>'
|
|
|
|
local doc = [[```
|
|
|
|
/hearthstone <query>
|
|
|
|
Returns Hearthstone card info.
|
2016-03-26 11:12:01 +01:00
|
|
|
Alias: /hs
|
2016-01-08 14:44:37 +01:00
|
|
|
```]]
|
2015-11-29 10:45:00 +01:00
|
|
|
|
|
|
|
local triggers = {
|
2016-01-08 04:30:12 +01:00
|
|
|
'^/hearthstone[@'..bot.username..']*',
|
|
|
|
'^/hs[@'..bot.username..']*$',
|
|
|
|
'^/hs[@'..bot.username..']* '
|
2015-11-29 10:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
local format_card = function(card)
|
|
|
|
|
|
|
|
local ctype = card.type
|
|
|
|
if card.race then
|
|
|
|
ctype = card.race
|
|
|
|
end
|
|
|
|
if card.rarity then
|
|
|
|
ctype = card.rarity .. ' ' .. ctype
|
|
|
|
end
|
|
|
|
if card.playerClass then
|
|
|
|
ctype = ctype .. ' (' .. card.playerClass .. ')'
|
|
|
|
elseif card.faction then
|
|
|
|
ctype = ctype .. ' (' .. card.faction .. ')'
|
|
|
|
end
|
|
|
|
|
|
|
|
local stats
|
|
|
|
if card.cost then
|
|
|
|
stats = card.cost .. 'c'
|
|
|
|
if card.attack then
|
|
|
|
stats = stats .. ' | ' .. card.attack .. 'a'
|
|
|
|
end
|
|
|
|
if card.health then
|
|
|
|
stats = stats .. ' | ' .. card.health .. 'h'
|
|
|
|
end
|
|
|
|
if card.durability then
|
|
|
|
stats = stats .. ' | ' .. card.durability .. 'd'
|
|
|
|
end
|
|
|
|
elseif card.health then
|
|
|
|
stats = card.health .. 'h'
|
|
|
|
end
|
|
|
|
|
|
|
|
local info = ''
|
|
|
|
if card.text then
|
|
|
|
info = card.text:gsub('</?.->',''):gsub('%$','')
|
|
|
|
if card.flavor then
|
2016-01-08 14:44:37 +01:00
|
|
|
info = info .. '\n_' .. card.flavor .. '_'
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
elseif card.flavor then
|
|
|
|
info = card.flavor
|
|
|
|
else
|
|
|
|
info = nil
|
|
|
|
end
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
local s = '*' .. card.name .. '*\n' .. ctype
|
2015-11-29 10:45:00 +01:00
|
|
|
if stats then
|
|
|
|
s = s .. '\n' .. stats
|
|
|
|
end
|
|
|
|
if info then
|
|
|
|
s = s .. '\n' .. info
|
|
|
|
end
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
local action = function(msg)
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text_lower)
|
2015-11-29 10:45:00 +01:00
|
|
|
if not input then
|
2016-01-08 14:44:37 +01:00
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
2015-11-29 10:45:00 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local output = ''
|
2016-02-21 20:28:40 +01:00
|
|
|
for k,v in pairs(database.hearthstone) do
|
2016-02-22 04:09:02 +01:00
|
|
|
if type(v) == 'table' and string.lower(v.name):match(input) then
|
2015-11-29 10:45:00 +01:00
|
|
|
output = output .. format_card(v) .. '\n\n'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
output = output:trim()
|
|
|
|
if output:len() == 0 then
|
|
|
|
sendReply(msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
sendMessage(msg.chat.id, output, true, msg.message_id, true)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers,
|
2016-01-08 14:44:37 +01:00
|
|
|
doc = doc,
|
|
|
|
command = command
|
2015-11-29 10:45:00 +01:00
|
|
|
}
|