Advertisement
Throne3d

QuestionableQuesting Vote Counter (Ack)

Oct 7th, 2015
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Count Votes*/
  2. /*jshint -W083 */
  3. var minVoteWeight = minVoteWeight || 1;
  4. var maxVoteWeight = maxVoteWeight || 6;
  5. 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"];
  6. var splitter = splitter || "Ack"; /*Change this if you want it to split on a different name (not sure why you'd want this...)*/
  7. var splitterContains = splitterContains || "Voting is CLOSED";
  8. var commonChanges = commonChanges || [
  9.     ["lacta", "iacta"],
  10.     ["&", "and", true],
  11.     ["pancea", "panacea"]
  12. ]; /*Change from, change to, hide*/
  13. /*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!*/
  14.  
  15. $(".bbCodeBlock").remove(); /*bbCodeQuote and bbCodeCode?*/
  16. var messagesOnPage = $(".message:not(.quickReply)");
  17. var votes = [];
  18. var people = [];
  19. var voteBreakdown = [];
  20.  
  21. var mistakes = "";
  22.  
  23. function log(text){
  24.     if (mistakes !== "") mistakes += "<br/>";
  25.     mistakes += text;
  26.     console.log(text);
  27. }
  28.  
  29. function standardifyStoryName(storyName, vote, author) {
  30.     var standardStoryName = storyName.toLowerCase().trim();
  31.     var changesMade = [];
  32.     var changesRequired = "The vote [" + (vote ? vote : "Vote doesn't exist") + "] underwent the following changes for recognition purposes: ";
  33.  
  34.     for (var changeIndex in commonChanges){
  35.         var match = standardStoryName.match(new RegExp("(^| )" + commonChanges[changeIndex][0].toLowerCase() + "( |$)"));
  36.         if (match !== -1 && match !== null){
  37.             if (!commonChanges[changeIndex][2]){ /*If not "don't log" */
  38.                 changesMade.push(changeIndex);
  39.                 if (changesRequired.indexOf("purposes: ") !== changesRequired.length - ("purposes: ").length) changesRequired += ", ";
  40.                 changesRequired += commonChanges[changeIndex][0] + "→" + commonChanges[changeIndex][1];
  41.             }
  42.             standardStoryName = standardStoryName.replace(new RegExp("(^| )" + commonChanges[changeIndex][0].toLowerCase() + "( |$)"), "$1" + commonChanges[changeIndex][1].toLowerCase() + "$2");
  43.         }
  44.     }
  45.     standardStoryName = standardStoryName.replace(/[^A-Za-z0-9 ]/g, "").replace(/\s+/, " ").trim();
  46.     if (vote && changesMade.length > 0) log(changesRequired + ". - " + author);
  47.     return standardStoryName;
  48. }
  49.  
  50. function sanitiseLine(line) {
  51.     //&#8203; is U+200B, other zero-width things are removed, hyphens and dashes are more consistent, blah.
  52.     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();
  53. }
  54.  
  55. var currGroup = 0;
  56. messagesOnPage.each(function(index, ele){
  57.     if (!people[currGroup]) people[currGroup] = [];
  58.     if (!votes[currGroup]) votes[currGroup] = {};
  59.     if (!voteBreakdown[currGroup]) voteBreakdown[currGroup] = {};
  60.     var currMsg = $(this);
  61.     if (currMsg.data("author") == splitter && $(".messageContent", currMsg).text().indexOf(splitterContains) != -1 && index+1 !== messagesOnPage.length) {
  62.         currGroup += 1;
  63.     } else {
  64.         var msgText = $(".messageContent", currMsg).text();
  65.         var lines = msgText.split("\n");
  66.         var votesUsed = [];
  67.         var countsUsed = [];
  68.         votesMade = false;
  69.         for (var lineIndex in lines){
  70.             lines[lineIndex] = sanitiseLine(lines[lineIndex]);
  71.             if (lines[lineIndex].indexOf("[X]") === 0) {
  72.                 //alreadyVoted = false;
  73.                 hasNewerVote = false;
  74.                 searchGroup = 0;
  75.                 messagesOnPage.each(function(ind, el){
  76.                     if ($(el).data("author") == splitter && $(".messageContent", el).text().indexOf(splitterContains) != -1) {
  77.                         searchGroup += 1;
  78.                     }
  79.                     if (ind > index && $(el).data("author") == currMsg.data("author") && searchGroup == currGroup) {
  80.                         for (var linInd in $(".messageContent", el).text().split("\n")){
  81.                             if ($(".messageContent", el).text().split("\n")[linInd].trim().indexOf("[X]") === 0){
  82.                                 /*Newer post by same person has a vote in. Ignore this post.*/
  83.                                 hasNewerVote = true;
  84.                             }
  85.                         }
  86.                     }
  87.  
  88.                 });
  89.                 /*
  90.                 for (personIndex in people[currGroup]){
  91.                     if (people[currGroup][personIndex].indexOf(currMsg.data("author") + " - ") == 0){
  92.                         log("Ignoring [" + currMsg.data("author") + "]'s second vote post.");
  93.                         alreadyVoted = true;
  94.                     }
  95.                 }
  96.                 */
  97.                 if (hasNewerVote) { log("Ignoring a post by [" + currMsg.data("author") + "] in favour of a newer vote."); break; }
  98.                 /*It's a vote! (Or at least starts with "[X]") */
  99.                
  100.                 if (lines[lineIndex].split("[X]")[1].split("-").length <= 1) {
  101.                     log("Invalid vote format [" + lines[lineIndex] + "] - " + currMsg.data("author"));
  102.                     continue;
  103.                 }
  104.                 var voteName = lines[lineIndex].split("[X]")[1].split("-")[0].trim().replace(/\s+/g, " ");
  105.                 var voteCount = lines[lineIndex].split("[X]")[1].split("-")[1].trim();
  106.                 if (voteCount.split(" ").length > 1) voteCount = voteCount.split(" ")[0].trim();
  107.                 if (Number(voteCount) != voteCount || Number(voteCount) % 1 !== 0 || Number(voteCount) > maxVoteWeight || Number(voteCount) < minVoteWeight) {
  108.                     log("Invalid vote number [" + lines[lineIndex] + "] - " + currMsg.data("author"));
  109.                     continue;
  110.                 }
  111.                 voteCount = Number(voteCount);
  112.                 var storyFound = false;
  113.                 voteName = standardifyStoryName(voteName, lines[lineIndex], currMsg.data("author"));
  114.                 for (var storyIndex in stories){
  115.                     if (standardifyStoryName(stories[storyIndex]) === voteName) {
  116.                         /*It's this story! Yay!*/
  117.                         voteName = stories[storyIndex];
  118.                         storyFound = true;
  119.                     }
  120.                 }
  121.                 if (!storyFound){
  122.                     log("The vote name in [" + lines[lineIndex] + "] was unrecognised. - " + currMsg.data("author"));
  123.                     continue;
  124.                 }
  125.                 var alreadyUsed = false;
  126.                 for (var voteType in votesUsed){
  127.                     if (votesUsed[voteType] === voteName) {
  128.                         /*This has already been voted for*/
  129.                         log("Invalid vote [" + lines[lineIndex] + "] - already voted for this story. - " + currMsg.data("author"));
  130.                         alreadyUsed = true;
  131.                     }
  132.                 }
  133.                 if (alreadyUsed) continue;
  134.  
  135.                 alreadyUsed = false;
  136.                 for (var countType in countsUsed){
  137.                     if (voteCount == countsUsed[countType]){
  138.                         log("Invalid vote [" + lines[lineIndex] + "] - already voted for that count [" + voteCount + "]. - " + currMsg.data("author"));
  139.                         alreadyUsed = true;
  140.                     }
  141.                 }
  142.                 if (alreadyUsed) continue;
  143.  
  144.                 if (!votes[currGroup][voteName]) votes[currGroup][voteName] = 0;
  145.                 votes[currGroup][voteName] += voteCount;
  146.                 if (!voteBreakdown[currGroup][voteName]) voteBreakdown[currGroup][voteName] = currMsg.data("author") + "&nbsp;-&nbsp;" + voteCount;
  147.                 else voteBreakdown[currGroup][voteName] += ", " + currMsg.data("author") + "&nbsp;-&nbsp;" + voteCount;
  148.  
  149.                 votesUsed.push(voteName);
  150.                 countsUsed.push(voteCount);
  151.                 votesMade = true;
  152.  
  153.             }
  154.         }
  155.         if (votesMade && !hasNewerVote) {
  156.             people[currGroup].push(currMsg.data("author") + " - " + countsUsed.join(",") + " - " + votesUsed.join(";"));
  157.         }
  158.     }
  159. });
  160.  
  161. var sortedVotes = [];
  162. for (var i=0; i <= currGroup; i++){
  163.     sortedVotes[i] = [];
  164.     for (var storyIndex in stories) {
  165.         var story = stories[storyIndex];
  166.         var found = false;
  167.         for (var storyName in votes[i]){
  168.             if (storyName == story) found = true;
  169.         }
  170.         if (!found) votes[i][story] = 0;
  171.     }
  172.     for (var storyName in votes[i]){
  173.         sortedVotes[i].push([storyName, votes[i][storyName]]);
  174.     }
  175.     sortedVotes[i].sort();
  176. }
  177.  
  178.  
  179. 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"});
  180. 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","");}));
  181. displayWindow.append($("<h2>").text("Vote Counts").css({"font-weight":"bold","font-size":"15pt"}));
  182. if ($(".voteCountDisplay").length > 0) {
  183.     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.")));
  184.     $(".voteCountDisplay").each(function(){ if (this != displayWindow.get(0)) $(this).remove(); });
  185. }
  186. if (currGroup > 0) {
  187.     displayWindow.append($("<p>").text("You appear to have ended " + (currGroup == 1 ? "a vote" : "several votes") + " on this page."));
  188.     var voteCounts = $("<p>");
  189.     for (var i=0; i <= currGroup; i++){
  190.         if (currGroup == 1) {
  191.             voteCounts.text(voteCounts.text() + people[i].length + " " + (people[i].length != 1 ? "people " : "person ") + "voted " + (i == currGroup ? "after you closed the vote" : "before"));
  192.         } else {
  193.             voteCounts.text(voteCounts.text() + people[i].length + " " + (people[i].length != 1 ? "people " : "person ") + "voted " + (i !== currGroup ? "in vote " + (i+1) : "after vote " + i));
  194.         }
  195.         if (i == currGroup - 1) voteCounts.text(voteCounts.text() + " and ");
  196.         if (i < currGroup - 1) voteCounts.text(voteCounts.text() + ", ");
  197.         if (i == currGroup) voteCounts.text(voteCounts.text() + ".");
  198.     }
  199.     displayWindow.append(voteCounts);
  200.     displayWindow.append($("<br>"));
  201.     displayWindow.append($("<p>").text("The following people voted:"));
  202.     var votePeople = $("<p>");
  203.     for (var i=0; i <= currGroup; i++){
  204.         if (i !== 0) votePeople.text(votePeople.text() + "; ");
  205.         for (y=0; y<people[i].length;y++) {
  206.             if (y !== 0) votePeople.text(votePeople.text() + ", ");
  207.             votePeople.text(votePeople.text() + people[i][y].split(" - ")[0]);
  208.         }
  209.     }
  210.     displayWindow.append(votePeople);
  211.     displayWindow.append($("<br>"));
  212.     for (var i=0; i <= currGroup; i++){
  213.         displayWindow.append($("<p>").css({"font-style":"italic"}).text((currGroup == 1 && i === 0 ? "Previous Vote" : currGroup == 1 && i == currGroup ? "New Vote" : "Vote " + (i+1))));
  214.         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();})))));
  215.         var tbod = $("<tbody>").appendTo(tabl);
  216.         for (var y in sortedVotes[i]) {
  217.             var voteBreakd = voteBreakdown[i][sortedVotes[i][y][0]] || "";
  218.             var voteBreakdTD = $("<td>");
  219.             if (voteBreakd !== ""){
  220.                 var peop = voteBreakd.split(", ");
  221.                 for (var z = 0; z < peop.length; z++){
  222.                     voteBreakdTD.append($("<span>").html(peop[z] + "&nbsp;").append($("<span>").text("×").css("cursor", "pointer").click(function(){
  223.                         var perso = $(this).parent().text().split("-")[0].trim();
  224.                         var amt = Number($(this).parent().text().split("-")[1].trim().split("×")[0]);
  225.                         var tbcnt = $("td:nth-child(2)", $("tr").has(this));
  226.                         tbcnt.text(Number(tbcnt.text() - amt));
  227.                         $(this).parent().remove();
  228.                     })).append($("<span>").html((z!=peop.length-1?" ; ":""))));
  229.                 }
  230.             }
  231.  
  232.             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);
  233.             tbod.append(tRow);
  234.         }
  235.         displayWindow.append(tabl);
  236.         displayWindow.append($("<br>"));
  237.     }
  238. } else {
  239.     displayWindow.append($("<p>").text(people[0].length + " " + (people[0].length != 1 ? "people have" : "person has") + " voted on this page."));
  240.     displayWindow.append($("<br>"));
  241.     displayWindow.append($("<p>").text("The following people voted:"));
  242.     var votePeople = $("<p>");
  243.     for (var i=0; i <= currGroup; i++){
  244.         if (i !== 0) votePeople.text(votePeople.text() + "; ");
  245.         for (y=0; y<people[i].length;y++) {
  246.             if (y !== 0) votePeople.text(votePeople.text() + ", ");
  247.             votePeople.text(votePeople.text() + people[i][y].split(" - ")[0]);
  248.         }
  249.     }
  250.     displayWindow.append(votePeople);
  251.     displayWindow.append($("<br>"));
  252.     for (var i=0; i <= currGroup; i++){
  253.         displayWindow.append($("<p>").css({"font-style":"italic"}).text("Vote Counts"));
  254.         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();})))));
  255.         var tbod = $("<tbody>").appendTo(tabl);
  256.         for (y in sortedVotes[i]) {
  257.             var voteBreakd = voteBreakdown[i][sortedVotes[i][y][0]] || "";
  258.             var voteBreakdTD = $("<td>");
  259.             if (voteBreakd !== ""){
  260.                 var peop = voteBreakd.split(", ");
  261.                 for (var z = 0; z < peop.length; z++){
  262.                     voteBreakdTD.append($("<span>").html(peop[z] + "&nbsp;").append($("<span>").text("×").css("cursor", "pointer").click(function(){
  263.                         var perso = $(this).parent().text().split("-")[0].trim();
  264.                         var amt = Number($(this).parent().text().split("-")[1].trim().split("×")[0]);
  265.                         var tbcnt = $("td:nth-child(2)", $("tr").has(this));
  266.                         tbcnt.text(Number(tbcnt.text() - amt));
  267.                         $(this).parent().remove();
  268.                     })).append($("<span>").html((z!=peop.length-1?" ; ":""))));
  269.                 }
  270.             }
  271.  
  272.             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);
  273.             tbod.append(tRow);
  274.         }
  275.         displayWindow.append(tabl);
  276.         displayWindow.append($("<br>"));
  277.     }
  278. }
  279. displayWindow.append($("<br>"));
  280. displayWindow.append($("<h2>").text("Notes & Mistakes").css({"font-weight":"bold","font-size":"15pt"}));
  281. displayWindow.append($("<p>").html((mistakes !== "" ? mistakes : "None.")));
  282.  
  283. $(document.body).append(displayWindow);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement