-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add !mtr command implementation #38
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,166 @@ | ||
const _ = require('lodash'); | ||
const cheerio = require('cheerio'); | ||
const rp = require('request-promise-native'); | ||
const Table = require('tty-table'); | ||
const log = require('log4js').getLogger('mtr'); | ||
|
||
const IPG_ADDRESS = process.env.IPG_ADDRESS || 'https://sites.google.com/site/mtgfamiliar/rules/MagicTournamentRules-light.html'; | ||
|
||
class MTR { | ||
constructor() { | ||
this.Location = "http://blogs.magicjudges.org/rules/mtr/"; | ||
this.commands = ["mtr"]; | ||
constructor(initialize = true) { | ||
this.location = 'http://blogs.magicjudges.org/rules/mtr'; | ||
this.maxLength = 1950; | ||
this.commands = ['mtr']; | ||
this.mtrData = { | ||
chapters: {appendices: {title: 'Appendices', sections: []}}, | ||
sections: {} | ||
}; | ||
|
||
if (initialize) { | ||
this.download(IPG_ADDRESS).then(mtrDocument => this.init(mtrDocument)); | ||
} | ||
} | ||
|
||
download(url) { | ||
return rp({url: url, simple: false, resolveWithFullResponse: true }).then(response => { | ||
if (response.statusCode === 200) { | ||
return response.body; | ||
} else { | ||
log.error('Error loading MTR, server returned status code ' + response.statusCode); | ||
} | ||
}).catch(e => log.error('Error loading MTR: ' + e, e)); | ||
} | ||
|
||
init(mtrDocument) { | ||
const $ = cheerio.load(mtrDocument); | ||
this.cleanup($); | ||
this.handleChapters($); | ||
this.handleSections($); | ||
log.info('MTR Ready'); | ||
} | ||
|
||
cleanup($) { | ||
// wrap standalone text nodes in p tags | ||
const nodes = $('body').contents(); | ||
for (let i = 0; i < nodes.length; i++) { | ||
const node = nodes[i]; | ||
// Text Node | ||
if (node.nodeType === 3) { | ||
$(node).wrap('p'); | ||
} | ||
} | ||
|
||
// strip out p tags containing only whitespace | ||
$('p').filter((i, e) => /^\s*$/.test($(e).text())).remove(); | ||
|
||
// mark chapter headers | ||
$('h4').filter((i, e) => /^\d+\.\s/.test($(e).text().trim())).addClass('chapter-header'); | ||
// mark section headers | ||
$('h4').filter((i, e) => /^(\d+\.\d+\s|Appendix)/.test($(e).text().trim())).addClass('section-header'); | ||
} | ||
|
||
handleChapters($) { | ||
$('.chapter-header').each((i, e) => { | ||
const title = $(e).text().trim(); | ||
const number = title.split('.', 1)[0]; | ||
this.mtrData.chapters[number] = { | ||
title: title, | ||
sections: [] | ||
}; | ||
}); | ||
} | ||
|
||
handleSections($) { | ||
$('.section-header').each((i, e) => { | ||
|
||
const title = $(e).text().trim(); | ||
const key = title.startsWith('Appendix') ? _.kebabCase(title.split('-', 1)[0]) : title.split(/\s/, 1)[0]; | ||
const chapter = key.startsWith('appendix') ? 'appendices' : key.split('.', 1)[0]; | ||
const content = this.handleSectionContent($, $(e), title, key); | ||
|
||
this.mtrData.sections[key] = { | ||
title: title, | ||
content: content | ||
}; | ||
this.mtrData.chapters[chapter].sections.push(key); | ||
}); | ||
} | ||
|
||
handleSectionContent($, sectionHeader, title, number) { | ||
/* on most sections we can just use the text, special cases are: | ||
* - banlists (sections ending in deck construction), these are basically long lists of sets and cards | ||
* - some sections containing tables (draft timings and recommended number of rounds) | ||
*/ | ||
if (/Format Deck Construction$/.test(title)) { | ||
// Asking a bot for the banlist has to be one of the worst ways to inquire about card legality that I can imagine, | ||
// defer handling this until I'm really bored and redirect people to the annotated mtr in the meantime | ||
return `You can find the full text of ${title} on <${this.location}${number.replace('.', '-')}>`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, very nice. 😁 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Come to think of it, this is actually a separate issue: #18 |
||
} | ||
|
||
// there are some headers which are neiter section nor chapter headers interspersed in the secions | ||
const sectionContent = sectionHeader.nextUntil('.section-header,.chapter-header').wrap('<div></div>').parent(); | ||
sectionContent.find('h4').replaceWith((i, e) => `<p>\n\n**${$(e).text().trim()}**\n\n</p>`); | ||
|
||
// replace tables with an ASCII representation | ||
sectionContent.find('table').replaceWith((i, e) => { | ||
const tableString = this.generateTextTable($, $(e)); | ||
return `<p>\n${tableString}\n</p>`; | ||
}); | ||
// mark each line as a Codeblock (uses monospace font), otherwise message splitting will mess up the formatting | ||
return sectionContent.text().trim().replace(/\n\s+\n/, '\n\n'); | ||
} | ||
|
||
generateTextTable($, table) { | ||
const rows = table.find('tr:has("td,th")').map((i, e) => $(e).children()).get(); | ||
const data = rows.map(r => r.map((i, e) => $(e).text().trim()).get()); | ||
const textTable = new Table(null, data).render(); | ||
return textTable.split('\n').filter(l => !/^\s*$/.test(l)).map(l => '`' + l + '`').join('\n'); | ||
} | ||
|
||
formatChapter(chapter) { | ||
const availableSections = chapter.sections.map(s => `*${_.truncate(this.mtrData.sections[s].title)}* (${s})`).join(', '); | ||
return [ | ||
`**MTR - ${chapter.title}**`, | ||
`**Available Sections**: ${availableSections}` | ||
].join('\n\n'); | ||
} | ||
|
||
formatSection(section) { | ||
return [ | ||
`MTR - **${section.title}**`, | ||
section.content | ||
].join('\n\n'); | ||
} | ||
|
||
getCommands() { | ||
return this.commands; | ||
} | ||
|
||
find(parameter) { | ||
//todo | ||
if (parameter.indexOf('-') !== -1 || parameter.indexOf('.') !== -1) { | ||
// looks like a section query | ||
const section = this.mtrData.sections[parameter]; | ||
if (section) { | ||
return this.formatSection(section); | ||
} | ||
return 'This section does not exist. Try asking for a chapter to get a list of available sections for that chapter.'; | ||
} | ||
|
||
const chapter = this.mtrData.chapters[parameter]; | ||
if (chapter) { | ||
return this.formatChapter(chapter); | ||
} | ||
const availableChapters = _.values(this.mtrData.chapters).map(c => `*${c.title}*`).join(', '); | ||
return `This chapter does not exist.\n**Available Chapters**: ${availableChapters}`; | ||
} | ||
|
||
handleMessage(command, parameter, msg) { | ||
if (parameter) { | ||
return msg.channel.sendMessage(this.find(parameter)); | ||
const result = this.find(parameter.trim()); | ||
return msg.channel.sendMessage(result, {split: true}); | ||
} | ||
return msg.channel.sendMessage('**Magic Tournament Rules**: <' + this.Location + '>'); | ||
return msg.channel.sendMessage('**Magic Tournament Rules**: <' + this.location + '>'); | ||
} | ||
} | ||
|
||
module.exports = MTR; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IPG_ADDRESS
?