-
Notifications
You must be signed in to change notification settings - Fork 24
Add on: Trivia Exportable
Agustin San Roman edited this page Jun 26, 2017
·
1 revision
Gist: https://gist.github.com/asanrom/bbb14569517c6ad0c70a234c7c28cb05
/**
* Trivia exportable system for Showdown-ChatBot
* Install ad an add-on
*/
'use strict';
const Text = Tools('text');
exports.setup = function (App) {
return Tools('add-on').forApp(App).install({
serverMenuOptions: {
"triviaexport": {name : "Trivia-Exportable", url: "/triviaexport/", permission: "trivia", level: -1},
},
serverHandlers: {
"triviaexport": function (context, parts) {
if (!context.user || !context.user.can('trivia')) {
context.endWith403();
return;
}
let mod = App.modules.games.system.templates.trivia;
let ok = null;
if (context.post.save) {
for (let id in mod.data) {
delete mod.data[id];
}
let newData = (context.post.data || "").split('\n');
for (let line of newData) {
let spl = line.split('||');
if (spl.length !== 2) continue;
let clue = Text.trim(spl[0]);
let answers = spl[1].split(',').map(Text.trim).filter(u => u);
if (!clue || answers.length === 0) continue;
mod.addQuestion(clue, answers);
}
mod.db.write();
App.logServerAction(context.user.id, "Used Trivia-Export Tool");
ok = "Trivia data saved sucessfully";
}
let questions = [];
for (let id in mod.data) {
questions.push(mod.data[id].clue + "||" + mod.data[id].answers.join(', '));
}
let html = '';
html += '<h2>Trivia Data</h2>';
html += '<form method="post" action="">';
html += '<textarea name="data" cols="100" rows="30">';
html += questions.join('\n');
html += '</textarea>';
html += '<p><input type="submit" name="save" value="Save Changes" /></p>';
html += '</form>';
html += '<p>';
if (ok) {
html += '<span class="ok-msg">' + ok + '</span>';
}
html += '</p>';
context.endWithWebPage(html, {title: "Trivia (Exportable) - Showdown ChatBot"});
},
},
});
};Showdown ChatBot is distributed under the terms of the MIT License