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-02-21 20:28:40 +01:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function hearthstone:init(config)
|
2016-04-11 06:04:47 +02:00
|
|
|
if not self.database.hearthstone or os.time() > self.database.hearthstone.expiration then
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
print('Downloading Hearthstone database...')
|
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
-- This stuff doesn't play well with lua-sec. Disable it for now; hack in curl.
|
|
|
|
--local jstr, res = HTTPS.request('https://api.hearthstonejson.com/v1/latest/enUS/cards.json')
|
|
|
|
--if res ~= 200 then
|
|
|
|
-- print('Error connecting to hearthstonejson.com.')
|
|
|
|
-- print('hearthstone.lua will not be enabled.')
|
|
|
|
-- return
|
|
|
|
--end
|
|
|
|
--local jdat = JSON.decode(jstr)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
local s = io.popen('curl -s https://api.hearthstonejson.com/v1/latest/enUS/cards.json'):read('*all')
|
|
|
|
local d = JSON.decode(s)
|
|
|
|
|
|
|
|
if not d then
|
2016-04-11 06:04:47 +02:00
|
|
|
print('Error connecting to hearthstonejson.com.')
|
|
|
|
print('hearthstone.lua will not be enabled.')
|
|
|
|
return
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
|
2016-04-12 15:47:30 +02:00
|
|
|
self.database.hearthstone = d
|
|
|
|
self.database.hearthstone.expiration = os.time() + 600000
|
2016-02-21 20:28:40 +01:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
print('Download complete! It will be stored for a week.')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
hearthstone.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hearthstone', true):t('hs').table
|
|
|
|
hearthstone.doc = [[```
|
|
|
|
]]..config.cmd_pat..[[hearthstone <query>
|
|
|
|
Returns Hearthstone card info.
|
|
|
|
Alias: ]]..config.cmd_pat..[[hs
|
|
|
|
```]]
|
2015-11-29 10:45:00 +01:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
hearthstone.command = 'hearthstone <query>'
|
2015-11-29 10:45:00 +01:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local function format_card(card)
|
2015-11-29 10:45:00 +01: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
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
-- unused?
|
2016-05-29 19:08:39 +02:00
|
|
|
local info
|
2015-11-29 10:45:00 +01:00
|
|
|
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
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function hearthstone:action(msg, config)
|
2015-11-29 10:45:00 +01:00
|
|
|
|
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-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, hearthstone.doc, true, msg.message_id, true)
|
2015-11-29 10:45:00 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local output = ''
|
2016-04-11 06:04:47 +02:00
|
|
|
for _,v in pairs(self.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
|
|
|
|
|
2016-04-15 21:07:23 +02:00
|
|
|
output = utilities.trim(output)
|
2015-11-29 10:45:00 +01:00
|
|
|
if output:len() == 0 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2015-11-29 10:45:00 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
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
|