2015-11-29 10:45:00 +01:00
|
|
|
-- Plugin for the Hearthstone database provided by hearthstonejson.com.
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local hearthstone = {}
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
--local HTTPS = require('ssl.https')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-08-14 04:26:44 +02:00
|
|
|
local HTTPS = require('ssl.https')
|
2016-02-21 20:28:40 +01:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function hearthstone:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
hearthstone.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hearthstone', true):t('hs').table
|
|
|
|
hearthstone.command = 'hearthstone <query>'
|
2016-08-14 04:26:44 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
if not self.database.hearthstone or os.time() > self.database.hearthstone.expiration then
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
print('Downloading Hearthstone database...')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local jstr, res = HTTPS.request('https://api.hearthstonejson.com/v1/latest/enUS/cards.json')
|
|
|
|
if not jstr or res ~= 200 then
|
|
|
|
print('Error connecting to hearthstonejson.com.')
|
|
|
|
print('hearthstone.lua will not be enabled.')
|
|
|
|
hearthstone.command = nil
|
|
|
|
hearthstone.triggers = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.database.hearthstone = JSON.decode(jstr)
|
|
|
|
self.database.hearthstone.expiration = os.time() + 600000
|
2016-02-21 20:28:40 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
print('Download complete! It will be stored for a week.')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
end
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
hearthstone.doc = config.cmd_pat .. [[hearthstone <query>
|
2016-05-27 05:28:44 +02:00
|
|
|
Returns Hearthstone card info.
|
2016-07-25 11:03:35 +02:00
|
|
|
Alias: ]] .. config.cmd_pat .. 'hs'
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local function format_card(card)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
-- unused?
|
|
|
|
local info
|
|
|
|
if card.text then
|
|
|
|
info = card.text:gsub('</?.->',''):gsub('%$','')
|
|
|
|
if card.flavor then
|
|
|
|
info = info .. '\n_' .. card.flavor .. '_'
|
|
|
|
end
|
|
|
|
elseif card.flavor then
|
|
|
|
info = card.flavor
|
|
|
|
else
|
|
|
|
info = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local s = '*' .. card.name .. '*\n' .. ctype
|
|
|
|
if stats then
|
|
|
|
s = s .. '\n' .. stats
|
|
|
|
end
|
|
|
|
if info then
|
|
|
|
s = s .. '\n' .. info
|
|
|
|
end
|
|
|
|
|
|
|
|
return s
|
2015-11-29 10:45:00 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function hearthstone:action(msg, config)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
|
|
|
utilities.send_reply(self, msg, hearthstone.doc, true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local output = ''
|
|
|
|
for _,v in pairs(self.database.hearthstone) do
|
|
|
|
if type(v) == 'table' and string.lower(v.name):match(input) then
|
|
|
|
output = output .. format_card(v) .. '\n\n'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
output = utilities.trim(output)
|
|
|
|
if output:len() == 0 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
utilities.send_message(self, msg.chat.id, output, true, msg.message_id, true)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return hearthstone
|