Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Count Votes*/
- /*jshint -W083 */
- var minVoteWeight = minVoteWeight || 1;
- var maxVoteWeight = maxVoteWeight || 6;
- var stories = stories || ["Alea Iacta Est", "All Alone", "Bait & Switch", "Confrontation II", "Danny & Taylor", "Hostage Situation", "I, Panacea", "Junior Hero", "Meet the Heberts", "MirrorVerse", "Nemesis", "NSW", "One More Trigger", "Really Bad End", "Recoil", "Security!", "Slippery Slope", "Trump Card", "War Games", "Wyvern"];
- var splitter = splitter || "Ack"; /*Change this if you want it to split on a different name (not sure why you'd want this...)*/
- var splitterContains = splitterContains || "Voting is CLOSED";
- var commonChanges = commonChanges || [
- ["lacta", "iacta"],
- ["&", "and", true],
- ["pancea", "panacea"]
- ]; /*Change from, change to, hide*/
- /*Keep these lowercase words (or things separated by spaces), please! Also accepts regex, so don't use brackets () or [], unless you put a backslash before them!*/
- $(".bbCodeBlock").remove(); /*bbCodeQuote and bbCodeCode?*/
- var messagesOnPage = $(".message:not(.quickReply)");
- var votes = [];
- var people = [];
- var voteBreakdown = [];
- var mistakes = "";
- function log(text){
- if (mistakes !== "") mistakes += "<br/>";
- mistakes += text;
- console.log(text);
- }
- function standardifyStoryName(storyName, vote, author) {
- var standardStoryName = storyName.toLowerCase().trim();
- var changesMade = [];
- var changesRequired = "The vote [" + (vote ? vote : "Vote doesn't exist") + "] underwent the following changes for recognition purposes: ";
- for (var changeIndex in commonChanges){
- var match = standardStoryName.match(new RegExp("(^| )" + commonChanges[changeIndex][0].toLowerCase() + "( |$)"));
- if (match !== -1 && match !== null){
- if (!commonChanges[changeIndex][2]){ /*If not "don't log" */
- changesMade.push(changeIndex);
- if (changesRequired.indexOf("purposes: ") !== changesRequired.length - ("purposes: ").length) changesRequired += ", ";
- changesRequired += commonChanges[changeIndex][0] + "→" + commonChanges[changeIndex][1];
- }
- standardStoryName = standardStoryName.replace(new RegExp("(^| )" + commonChanges[changeIndex][0].toLowerCase() + "( |$)"), "$1" + commonChanges[changeIndex][1].toLowerCase() + "$2");
- }
- }
- standardStoryName = standardStoryName.replace(/[^A-Za-z0-9 ]/g, "").replace(/\s+/, " ").trim();
- if (vote && changesMade.length > 0) log(changesRequired + ". - " + author);
- return standardStoryName;
- }
- function sanitiseLine(line) {
- //​ is U+200B, other zero-width things are removed, hyphens and dashes are more consistent, blah.
- return line.replace(/[\u002D\u007E\u00AD\u00B7\u058A\u05BE\u0F0C\u1400\u1680\u1806\u2010-\u2015\u2027\u2043\u2053\u207B\u208B\u2212\u2E17\u2E1A\u2E31\u2E33\u2E3A\u2E3B\u301C\u3030\u30A0\u30FB\uFE31\uFE32\uFE58\uFE63\uFF0D\uFF65]/g, "-").replace(/[—‒–ー-―‐➖]/g, "-").replace(/[\u180E\u200B-\u200F\uFEFF\0\b\t\v\f\r]/g, '').replace(/[\u0020\u00A0\u2000-\u200A\u202F\u205F\u3000]/g, " ").trim();
- }
- var currGroup = 0;
- messagesOnPage.each(function(index, ele){
- if (!people[currGroup]) people[currGroup] = [];
- if (!votes[currGroup]) votes[currGroup] = {};
- if (!voteBreakdown[currGroup]) voteBreakdown[currGroup] = {};
- var currMsg = $(this);
- if (currMsg.data("author") == splitter && $(".messageContent", currMsg).text().indexOf(splitterContains) != -1 && index+1 !== messagesOnPage.length) {
- currGroup += 1;
- } else {
- var msgText = $(".messageContent", currMsg).text();
- var lines = msgText.split("\n");
- var votesUsed = [];
- var countsUsed = [];
- votesMade = false;
- for (var lineIndex in lines){
- lines[lineIndex] = sanitiseLine(lines[lineIndex]);
- if (lines[lineIndex].indexOf("[X]") === 0) {
- //alreadyVoted = false;
- hasNewerVote = false;
- searchGroup = 0;
- messagesOnPage.each(function(ind, el){
- if ($(el).data("author") == splitter && $(".messageContent", el).text().indexOf(splitterContains) != -1) {
- searchGroup += 1;
- }
- if (ind > index && $(el).data("author") == currMsg.data("author") && searchGroup == currGroup) {
- for (var linInd in $(".messageContent", el).text().split("\n")){
- if ($(".messageContent", el).text().split("\n")[linInd].trim().indexOf("[X]") === 0){
- /*Newer post by same person has a vote in. Ignore this post.*/
- hasNewerVote = true;
- }
- }
- }
- });
- /*
- for (personIndex in people[currGroup]){
- if (people[currGroup][personIndex].indexOf(currMsg.data("author") + " - ") == 0){
- log("Ignoring [" + currMsg.data("author") + "]'s second vote post.");
- alreadyVoted = true;
- }
- }
- */
- if (hasNewerVote) { log("Ignoring a post by [" + currMsg.data("author") + "] in favour of a newer vote."); break; }
- /*It's a vote! (Or at least starts with "[X]") */
- if (lines[lineIndex].split("[X]")[1].split("-").length <= 1) {
- log("Invalid vote format [" + lines[lineIndex] + "] - " + currMsg.data("author"));
- continue;
- }
- var voteName = lines[lineIndex].split("[X]")[1].split("-")[0].trim().replace(/\s+/g, " ");
- var voteCount = lines[lineIndex].split("[X]")[1].split("-")[1].trim();
- if (voteCount.split(" ").length > 1) voteCount = voteCount.split(" ")[0].trim();
- if (Number(voteCount) != voteCount || Number(voteCount) % 1 !== 0 || Number(voteCount) > maxVoteWeight || Number(voteCount) < minVoteWeight) {
- log("Invalid vote number [" + lines[lineIndex] + "] - " + currMsg.data("author"));
- continue;
- }
- voteCount = Number(voteCount);
- var storyFound = false;
- voteName = standardifyStoryName(voteName, lines[lineIndex], currMsg.data("author"));
- for (var storyIndex in stories){
- if (standardifyStoryName(stories[storyIndex]) === voteName) {
- /*It's this story! Yay!*/
- voteName = stories[storyIndex];
- storyFound = true;
- }
- }
- if (!storyFound){
- log("The vote name in [" + lines[lineIndex] + "] was unrecognised. - " + currMsg.data("author"));
- continue;
- }
- var alreadyUsed = false;
- for (var voteType in votesUsed){
- if (votesUsed[voteType] === voteName) {
- /*This has already been voted for*/
- log("Invalid vote [" + lines[lineIndex] + "] - already voted for this story. - " + currMsg.data("author"));
- alreadyUsed = true;
- }
- }
- if (alreadyUsed) continue;
- alreadyUsed = false;
- for (var countType in countsUsed){
- if (voteCount == countsUsed[countType]){
- log("Invalid vote [" + lines[lineIndex] + "] - already voted for that count [" + voteCount + "]. - " + currMsg.data("author"));
- alreadyUsed = true;
- }
- }
- if (alreadyUsed) continue;
- if (!votes[currGroup][voteName]) votes[currGroup][voteName] = 0;
- votes[currGroup][voteName] += voteCount;
- if (!voteBreakdown[currGroup][voteName]) voteBreakdown[currGroup][voteName] = currMsg.data("author") + " - " + voteCount;
- else voteBreakdown[currGroup][voteName] += ", " + currMsg.data("author") + " - " + voteCount;
- votesUsed.push(voteName);
- countsUsed.push(voteCount);
- votesMade = true;
- }
- }
- if (votesMade && !hasNewerVote) {
- people[currGroup].push(currMsg.data("author") + " - " + countsUsed.join(",") + " - " + votesUsed.join(";"));
- }
- }
- });
- var sortedVotes = [];
- for (var i=0; i <= currGroup; i++){
- sortedVotes[i] = [];
- for (var storyIndex in stories) {
- var story = stories[storyIndex];
- var found = false;
- for (var storyName in votes[i]){
- if (storyName == story) found = true;
- }
- if (!found) votes[i][story] = 0;
- }
- for (var storyName in votes[i]){
- sortedVotes[i].push([storyName, votes[i][storyName]]);
- }
- sortedVotes[i].sort();
- }
- var displayWindow = $("<div>").attr("class", "voteCountDisplay").css({"width": "90%", "margin": "auto","background-color":"#eee","max-width":"900px","max-height":"600px","height":"80%","position":"fixed","top":"0","bottom":"0","left":"0","right":"0","z-index":"300","padding":"50px","color":"#000","overflow-y":"auto"});
- displayWindow.append($("<div>").css({"position":"relative","top":"-50px","float":"right",/*"border":"solid 1px #000",*/"cursor":"pointer","margin-right":"-50px","width":"25px","height":"25px","font-size":"14pt","text-align":"center","vertical-align":"middle"}).text("×").click(function(){displayWindow.remove();}).mouseenter(function(){$(this).css("background-color","#aaa");}).mouseout(function(){$(this).css("background-color","");}));
- displayWindow.append($("<h2>").text("Vote Counts").css({"font-weight":"bold","font-size":"15pt"}));
- if ($(".voteCountDisplay").length > 0) {
- displayWindow.append($("<p>").append($("<em>").css({"color":"red","font-style":"italic"}).text("You should probably reload the page before running the script a second time.")));
- $(".voteCountDisplay").each(function(){ if (this != displayWindow.get(0)) $(this).remove(); });
- }
- if (currGroup > 0) {
- displayWindow.append($("<p>").text("You appear to have ended " + (currGroup == 1 ? "a vote" : "several votes") + " on this page."));
- var voteCounts = $("<p>");
- for (var i=0; i <= currGroup; i++){
- if (currGroup == 1) {
- voteCounts.text(voteCounts.text() + people[i].length + " " + (people[i].length != 1 ? "people " : "person ") + "voted " + (i == currGroup ? "after you closed the vote" : "before"));
- } else {
- voteCounts.text(voteCounts.text() + people[i].length + " " + (people[i].length != 1 ? "people " : "person ") + "voted " + (i !== currGroup ? "in vote " + (i+1) : "after vote " + i));
- }
- if (i == currGroup - 1) voteCounts.text(voteCounts.text() + " and ");
- if (i < currGroup - 1) voteCounts.text(voteCounts.text() + ", ");
- if (i == currGroup) voteCounts.text(voteCounts.text() + ".");
- }
- displayWindow.append(voteCounts);
- displayWindow.append($("<br>"));
- displayWindow.append($("<p>").text("The following people voted:"));
- var votePeople = $("<p>");
- for (var i=0; i <= currGroup; i++){
- if (i !== 0) votePeople.text(votePeople.text() + "; ");
- for (y=0; y<people[i].length;y++) {
- if (y !== 0) votePeople.text(votePeople.text() + ", ");
- votePeople.text(votePeople.text() + people[i][y].split(" - ")[0]);
- }
- }
- displayWindow.append(votePeople);
- displayWindow.append($("<br>"));
- for (var i=0; i <= currGroup; i++){
- displayWindow.append($("<p>").css({"font-style":"italic"}).text((currGroup == 1 && i === 0 ? "Previous Vote" : currGroup == 1 && i == currGroup ? "New Vote" : "Vote " + (i+1))));
- var tabl = $("<table>").append($("<thead>").css({"font-weight":"bold"}).append($("<tr>").append($("<th>").text("Story Name").css({"min-width":"150px","max-width":"20%"})).append($("<th>").text("Vote Count").css({"padding-right":"15px","min-width":"80px"})).append($("<th>").text("Vote Breakdown ").append($("<span>").css("cursor", "pointer").text("×").click(function(){$("tr td:nth-child(3), tr th:nth-child(3)", $("table").has(this)).remove();})))));
- var tbod = $("<tbody>").appendTo(tabl);
- for (var y in sortedVotes[i]) {
- var voteBreakd = voteBreakdown[i][sortedVotes[i][y][0]] || "";
- var voteBreakdTD = $("<td>");
- if (voteBreakd !== ""){
- var peop = voteBreakd.split(", ");
- for (var z = 0; z < peop.length; z++){
- voteBreakdTD.append($("<span>").html(peop[z] + " ").append($("<span>").text("×").css("cursor", "pointer").click(function(){
- var perso = $(this).parent().text().split("-")[0].trim();
- var amt = Number($(this).parent().text().split("-")[1].trim().split("×")[0]);
- var tbcnt = $("td:nth-child(2)", $("tr").has(this));
- tbcnt.text(Number(tbcnt.text() - amt));
- $(this).parent().remove();
- })).append($("<span>").html((z!=peop.length-1?" ; ":""))));
- }
- }
- var tRow = $("<tr>").css({"background-color":(y % 2 === 0 ? "#ddd" : "#eee")}).append($("<td>").text(sortedVotes[i][y][0]).css({"padding-right":"10px","font-weight":"bold"})).append($("<td>").text(sortedVotes[i][y][1])).append(voteBreakdTD);
- tbod.append(tRow);
- }
- displayWindow.append(tabl);
- displayWindow.append($("<br>"));
- }
- } else {
- displayWindow.append($("<p>").text(people[0].length + " " + (people[0].length != 1 ? "people have" : "person has") + " voted on this page."));
- displayWindow.append($("<br>"));
- displayWindow.append($("<p>").text("The following people voted:"));
- var votePeople = $("<p>");
- for (var i=0; i <= currGroup; i++){
- if (i !== 0) votePeople.text(votePeople.text() + "; ");
- for (y=0; y<people[i].length;y++) {
- if (y !== 0) votePeople.text(votePeople.text() + ", ");
- votePeople.text(votePeople.text() + people[i][y].split(" - ")[0]);
- }
- }
- displayWindow.append(votePeople);
- displayWindow.append($("<br>"));
- for (var i=0; i <= currGroup; i++){
- displayWindow.append($("<p>").css({"font-style":"italic"}).text("Vote Counts"));
- var tabl = $("<table>").append($("<thead>").css({"font-weight":"bold"}).append($("<tr>").append($("<th>").text("Story Name").css({"min-width":"150px","max-width":"20%"})).append($("<th>").text("Vote Count").css({"padding-right":"15px","min-width":"80px"})).append($("<th>").text("Vote Breakdown ").append($("<span>").css("cursor", "pointer").text("×").click(function(){$("tr td:nth-child(3), tr th:nth-child(3)", $("table").has(this)).remove();})))));
- var tbod = $("<tbody>").appendTo(tabl);
- for (y in sortedVotes[i]) {
- var voteBreakd = voteBreakdown[i][sortedVotes[i][y][0]] || "";
- var voteBreakdTD = $("<td>");
- if (voteBreakd !== ""){
- var peop = voteBreakd.split(", ");
- for (var z = 0; z < peop.length; z++){
- voteBreakdTD.append($("<span>").html(peop[z] + " ").append($("<span>").text("×").css("cursor", "pointer").click(function(){
- var perso = $(this).parent().text().split("-")[0].trim();
- var amt = Number($(this).parent().text().split("-")[1].trim().split("×")[0]);
- var tbcnt = $("td:nth-child(2)", $("tr").has(this));
- tbcnt.text(Number(tbcnt.text() - amt));
- $(this).parent().remove();
- })).append($("<span>").html((z!=peop.length-1?" ; ":""))));
- }
- }
- var tRow = $("<tr>").css({"background-color":(y % 2 === 0 ? "#ddd" : "#eee")}).append($("<td>").text(sortedVotes[i][y][0]).css({"padding-right":"10px","font-weight":"bold"})).append($("<td>").text(sortedVotes[i][y][1])).append(voteBreakdTD);
- tbod.append(tRow);
- }
- displayWindow.append(tabl);
- displayWindow.append($("<br>"));
- }
- }
- displayWindow.append($("<br>"));
- displayWindow.append($("<h2>").text("Notes & Mistakes").css({"font-weight":"bold","font-size":"15pt"}));
- displayWindow.append($("<p>").html((mistakes !== "" ? mistakes : "None.")));
- $(document.body).append(displayWindow);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement