Skip to content

Instantly share code, notes, and snippets.

@AccaliaDeElementia
Created May 20, 2016 12:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AccaliaDeElementia/4e28ff517c0bc0895d5f9fda0f046df6 to your computer and use it in GitHub Desktop.
Mafia Modscripts
// ==UserScript==
// @name Mafia Mod Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://what.thedailywtf.com/*
// @grant none
// ==/UserScript==
(function () {
/* jshint esnext: true */
'use strict';
function emit(method, data) {
return new Promise((resolve, reject) => {
window.socket.emit(method, data, (err, result) => {
if (err) {
reject(err);
} else {
setTimeout(() => resolve(result), 1000);
}
});
});
}
function getData(url) {
return new Promise((resolve, reject) => {
$.ajax({
error: (_, __, e) => reject(e),
success: (data) => resolve(data),
url: url
});
});
}
function getMembers(groupSlug) {
return getData(`/api/groups/${groupSlug}`).then((data) => {
const people = data.group.members.map((m) => m.username);
people.shift(); //remove GM
people.shift(); //remove @vote
return people;
});
}
localStorage.mafiaHelper = localStorage.mafiaHelper || '{}';
const mafiaMenu = $(`<li id="mafiamenu"><a href="#" title="MafiaHelper" data-placement="bottom"><i class="fa fa-fw"></i></a></li>`);
$('#nav-dropdown ul:nth-of-type(2) li:first').before(mafiaMenu);
mafiaMenu.find('a').tooltip();
const mafiaModal = $('<div id="mafiaModal" class="modal fade" tabindex="-1" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">Mafia Helper</h4></div><div class="modal-body" style="max-height:700px; overflow-y: scroll"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div>');
$('body').append(mafiaModal);
function randomizeIcon(){
const target = Math.random() < 0.5 ? 'fa-rebel': 'fa-empire';
$('#mafiamenu i').removeClass('fa-rebel').removeClass('fa-empire').addClass(target);
}
randomizeIcon();
setInterval(randomizeIcon, 60 * 1000);
const mafiaAlert = $('<div id="mafiaModal" class="modal fade" tabindex="-1" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">MafiaHelper</h4></div><div class="modal-body"><p>One fine body&hellip;</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div>');
$('body').append(mafiaModal);
function showAlert(text, title) {
title = title || 'Attention!';
mafiaAlert.find('h4').text(title);
mafiaAlert.find('p').text(text);
mafiaAlert.modal();
}
function getLocalData() {
let data = {};
try {
data = JSON.parse(localStorage.mafiaHelper);
} catch (e) {}
return data;
}
function input(name, title) {
return $(`<div class="form-group"><label for="mafiahelper-${name}">${title}</label><input type="text" class="form-control" id="mafiahelper-${name}" placeholder="${title}"></div>`);
}
function textarea(name, title) {
return $(`<div class="form-group"><label for="mafiahelper-${name}">${title}</label><textarea rows="5" class="form-control" id="mafiahelper-${name}" placeholder="${title}"></textarea></div>`);
}
function button(text, type) {
type = type || 'default';
return $(`<button type="button" class="btn btn-${type}">${text}</button>`);
}
function SaveConfig() {
let data = {};
try {
data = JSON.parse(localStorage.mafiaHelper);
} catch (e) {}
data.threadId = parseInt(mafiaModal.find('#mafiahelper-threadId').val(), 10) || 0;
data.clubDedId = parseInt(mafiaModal.find('#mafiahelper-clubDedId').val(), 10) || 0;
['players', 'club-ded', 'prefix', 'checkin', 'checkinClean'].forEach((key) => {
data[key] = mafiaModal.find(`#mafiahelper-${key}`).val();
});
localStorage.mafiaHelper = JSON.stringify(data);
showAlert('Data saved!');
}
function addText(form, data, name, title) {
const control = textarea(name, title);
if (data[name]) {
control.find('textarea').val(data[name]);
}
form.append(control);
}
function addInput(form, data, name, title, type) {
type = type || input;
const control = type(name, title);
if (data[name]) {
control.find(':input').val(data[name]);
}
form.append(control);
}
function ConfigForm() {
const data = getLocalData();
const form = $('<form></form>');
addInput(form, data, 'threadId', 'Game Thread');
addInput(form, data, 'clubDedId', 'Club Ded Thread');
addInput(form, data, 'players', 'Players Group Slug');
addInput(form, data, 'club-ded', 'Club Ded Group Slug');
addInput(form, data, 'prefix', 'Checkin Prefix', textarea);
addInput(form, data, 'checkin', 'Checkin Suffix', textarea);
addInput(form, data, 'checkinClean', 'Clean Checkin Suffix', textarea);
const div = $('<div></div>');
const back = button('Back');
back.click(MainMenu);
div.append(back);
const save = button('Save Configuration', 'primary');
save.click(SaveConfig);
div.append(save);
form.append(div);
mafiaModal.find('.modal-body').empty().append(form);
}
function RunCheckin() {
const form = $('<form></form>');
const result = addText(form, {
result: 'Processing...'
}, 'result', 'Checkin Results');
const data = getLocalData();
getMembers(data.players).then((players) => {
const mafia = {};
players.forEach((name) => {
mafia[name] = 0;
});
let timestamp = new Date().getTime();
const cutoff = timestamp - 24 * 60 * 60 * 1000;
const getCount = () => emit('topics.getTopic', data.threadId).then(topic => topic.postcount);
const processChunk = (topic) => {
if (topic.posts.length < 1 || timestamp < cutoff) {
return Promise.resolve(0);
}
topic.posts.forEach((post) => {
mafia[post.user.username] = Math.max(mafia[post.user.username], post.timestamp);
timestamp = Math.min(timestamp, post.timestamp);
});
return Promise.resolve(topic.posts.length);
};
const doChunk = (start) => {
if (start < 1) {
return Promise.resolve();
}
return emit('topics.loadMore', {
tid: data.threadId,
after: start,
direction: -1
}).then(processChunk).then((count) => doChunk(count ? start - count : 0));
};
const formatMessage = () => {
console.log('hi', mafia);
let text = data.prefix;
const mia = Object.keys(mafia).filter((key) => mafia[key] < cutoff);
if (mia.length > 0) {
text += '\n\n---\n\nThe following players have not posted in 24 hours or more:\n\n';
mia.forEach((user) => {
text += '* @' + user + '\n';
});
text += `\n\n---\n\n${data.checkin}`;
} else {
text += `'\n\n---\n\n${data.checkinClean}`;
}
return text;
};
getCount()
.then(doChunk)
.then(formatMessage)
.then((message) => emit('posts.reply', {
tid: data.threadId,
content: message
})
.then(()=>message))
.then((message) => {
mafiaModal.find('#mafiahelper-result').val(message);
})
.catch((error) => {
console.error(error.message,error.stack);
mafiaModal.find('#mafiahelper-result').val(error.message);
});
});
mafiaModal.find('.modal-body').empty().append(form);
}
function lineButton(form, title, onclick) {
const btn = button(title);
btn.click(onclick);
const div = $('<div></div>');
form.append(div);
div.append(btn);
return btn;
}
function MainMenu() {
const form = $('<form></form>');
lineButton(form, 'Run Checkin', RunCheckin);
lineButton(form, 'Mafia Setup', ConfigForm);
mafiaModal.find('.modal-body').empty().append(form);
}
mafiaMenu.click(() => {
MainMenu();
mafiaModal.modal();
});
})();
function emit(method, data) {
return new Promise((resolve, reject) => {
window.socket.emit(method, data, (err, result) => {
if (err) {
reject(err);
} else {
setTimeout(()=>resolve(result),5000);
}
});
});
}
function getUserId(username) {
return emit('user.getUserByUsername', username).then((res) => res.uid);
}
function createChatRoom(toid) {
return emit('modules.chats.newRoom', {
touid: toid
});
}
function renameChatRoom(id, name) {
return emit('modules.chats.renameRoom', {
roomId: id,
newName: name
}).then(() => id);
}
function sendChat(id, text) {
return emit('modules.chats.send', {
roomId: id,
message: text
});
}
function createAndSend(username, name, text) {
return getUserId(username)
.then((uid) => createChatRoom(uid))
.then((roomId) => renameChatRoom(roomId, name))
.then((roomId) => sendChat(roomId, text))
.then(() => console.log('role card sent to ' + username));
}
const mafia_config = [
['japonicus', "You're the councilor for morality, Due to your appointed office you are a living embodiment of morality. You are preparing for the upcoming election cycle and hope to get elected as councilor for anger\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."],
['Yamikuronue', "You're the councilor for lust, Due to your appointed office you are a living embodiment of lust. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['Maciejasjmj', "You're the councilor for distraction, Due to your appointed office you are a living embodiment of distraction. You are preparing for the upcoming election cycle and hope to get elected as councilor for surprise\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."],
['RaceProUK', "You're the councilor for optimism, Due to your appointed office you are a living embodiment of optimism. You are preparing for the upcoming election cycle and hope to get elected as councilor for serenity.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."],
['Jaloopa', "You're the councilor for joy, Due to your appointed office you are a living embodiment of joy. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['Tsaukpaetra', "You're the councilor for boredom, Due to your appointed office you are a living embodiment of boredom. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['sloosecannon', "You're the councilor for fear, Due to your appointed office you are a living embodiment of fear. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['aliceif', "You're the councilor for logic, Due to your appointed office you are a living embodiment of logic. You are preparing for the upcoming election cycle and hope to get elected as councilor for curiosity\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."],
['Cheater', "You're the councilor for anticipation, Due to your appointed office you are a living embodiment of anticipation. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['Captain', "You're the councilor for curiosity, Due to your appointed office you are a living embodiment of curiosity. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['abarker', "You're the councilor for surprise, Due to your appointed office you are a living embodiment of surprise. You are preparing for the upcoming election cycle and hope to get elected as councilor for anticipation.\n\n---\n\nYou are the leader of a sleeper cell in the conspiracy to overthrow the council. This cell was to lie in wait as a backup to the coup attempt. Now that the coup has failed you must now act to take control of the council.\n\nYou may communicate with your fellow conspirators via [secret notes](https://what.thedailywtf.com/topic/19712/mafia-vi-anti-spellars-thread) at any time, except by that time between a lynching or end of day timer expiring and the night phase post being made, as covered by [Rule 14](https://what.thedailywtf.com/topic/19835/mafia-vi-spellars-town-council-game-thred/1)\n\n--\n\nEach day you may designate a target to recruit to your cause. At the conclusion of the day you will attempt to recruit the target to your cause. This recruit attempt is not guaranteed and will be more likely to fail the more members your cell has.\n\nShould the hammer fall before you indicate a target to recruit you will not attempt to recruit anyone in the night phase.\n\nYou win when you and the members of your cell are the only surviving players, or when nothing can prevent same"],
['fbmac', "You're the councilor for awe, Due to your appointed office you are a living embodiment of awe. You are preparing for the upcoming election cycle and hope to get reelected to your current position.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threats to the council are eliminated."],
['xaade', "You're the councilor for serenity, Due to your appointed office you are a living embodiment of serenity. You are preparing for the upcoming election cycle and hope to get elected as councilor for awe.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."],
['cloak15', "You're the councilor for anger, Due to your appointed office you are a living embodiment of anger. You are preparing for the upcoming election cycle and hope to get elected as councilor for morality.\n\n---\n\nYou are loyal to the council chairperson Accalia. Her death at the hands of a shadowy conspiracy has rattled you, who could possibly be so disloyal so as to attempt a coup? @Boomzilla mentioned there were more conspirators. You must find and eliminate the other conspirators, no one is safe until you do.\n\nYou win when all threat to the council are eliminated."]
];
function sendit() {
if (mafia_config.length < 1) {
return Promise.resolve();
}
const cfg = mafia_config.shift();
return createAndSend(cfg[0], 'Mafia VI - Spellars Roll Card', cfg[1]).then(sendit);
}
sendit().then(() => console.log('all role cards sent'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment