// ==UserScript== // @name uftow-stats // @namespace uftow-stats // @include https://ubuntuforums.org/showthread.php?t=1579442* // @version 1.3.0 // @Description Generates stats for Tug of War. // @grant GM_log // @grant GM_getValue // @grant GM_setValue // @grant GM_listValues // @grant GM_deleteValue // @grant GM_registerMenuCommand // ==/UserScript== var towConfig = { multipliers:new Array( //Forum Moderator 'images/rank_uf_moderator_2013-07.png',-2, //Forum Super Moderator 'images/rank_uf_super_moderator_2014_12.png',-3, //Forum Admin 'images/rank_uf_admin_2013-07.png',-4, //LoCo mods 'font-weight: bold; color: #339900;',2, //Ubuntu Member 'images/rank_ubuntumember_2013-02.png',2, //Kubuntu Member - unchanged (2013.02.17) 'images/rebrand/ranks/rankkubuntumember.png',2, //Staff Emeritus 'images/rank_oldstaff_2013-02.png',3, //Forum Donators - unchanged (2013.02.13) 'images/rank_forum_donators.png',4, //Ubuntu Developers - unchanged (2013.02.13) 'images/rank_ubuntudeveloper.png',4, //Ubuntu One Developers - Unable to verify whether or not this has changed (2013.02.13) 'http://kryogenix.org/random/u1-forums-rank-logo.png',4 ) }; var towStats = { alreadyHasStartCounting:false, scores:new Array(), currentScore:0, lastPostNumber:null, lastPoster:null, winner:null, marker:null, markerPost:null, init: function(){ this.postContainers=document.getElementsByClassName('postcontainer'); this.firstPostNumber=parseInt(this.postContainers[0].getElementsByClassName('postcounter')[0].textContent.substr(1)); this.lastPostNumber=parseInt(this.postContainers[this.postContainers.length-1].getElementsByClassName('postcounter')[0].textContent.substr(1)); this.currentScore=0; if(!GM_getValue("statsActive",false)){ GM_registerMenuCommand("Start counting posts",function(){towStats.startCounting();},'S'); this.alreadyHasStartCounting=true; }else{ this.startCounting(); } GM_registerMenuCommand("Clear counter",function(){towStats.clearStats();},'A'); }, recordPosts: function(startPost){ var postNumber; var startDiff=startPost-this.firstPostNumber; var postInfo; if(startDiff<0 || startPost>this.firstPostNumber+this.postContainers.length-1){ alert("Post #"+startPost+" is not on this page."); return false; } for (var i=startDiff;i=200){ this.winner='community'; }if(this.winner!=null){ this.marker=postInfo[0]; this.markerPost=postInfo[3]; break; } //GM_log("Post: "+postInfo[3]+", Round score: "+this.currentScore); } return true; }, getPostInfo: function(postElement){ var userSpanTag=postElement.getElementsByClassName('username')[0].getElementsByTagName('span')[0]; var userName=userSpanTag.textContent; var css=userSpanTag.getAttribute('style'); var rankURL=postElement.getElementsByClassName('rank')[0].getElementsByTagName('img')[0].getAttribute('src'); var postNumber=parseInt(postElement.getElementsByClassName('postcounter')[0].textContent.substr(1)); return new Array(userName,css,rankURL,postNumber); }, scorePost: function(postInfo){ this.lastPoster=postInfo[0]; var groupImgIndex=towConfig.multipliers.indexOf(postInfo[2]); if(groupImgIndex==-1){ groupImgIndex=towConfig.multipliers.indexOf(postInfo[1]); } multiplier=(groupImgIndex==-1)?1:towConfig.multipliers[groupImgIndex+1]; this.currentScore+=multiplier; userIndex=this.scores.indexOf(postInfo[0]); //The second check in the if statement makes sure users with usernames being actual numbers don't break this script if(userIndex!=-1&&userIndex%3==0){ this.scores[userIndex+2]++; }else{ this.scores.push(postInfo[0]); this.scores.push(multiplier); this.scores.push(1); } }, getSummary: function(){ var alertText="[B]TUG OF WAR LAST ROUND STATS[/B]\n"; var totalPoints=0; var totalPosts=0; var totalModPoints=0; var totalModPosts=0; var totalCommunityPoints=0; var totalCommunityPosts=0; var totalWinningPoints=0; var minimumRequiredMVP; var mvpList=new Array(); var communityParticipants=new Array(); var modParticipants=new Array(); for(var i=1;i0){ totalCommunityPoints+=this.scores[i+1]*this.scores[i]; totalCommunityPosts+=this.scores[i+1]; } totalPosts+=this.scores[i+1]; } totalWinningPoints=(this.winner=='community')?totalCommunityPoints:totalModPoints; minimumRequiredMVP=Math.round(totalWinningPoints*0.15); alertText+="[B]Total points:[/B] "+totalPoints+" from "+totalPosts+" posts\n"; alertText+="[B]Total community points:[/B] "; alertText+=((totalCommunityPosts>0)?totalCommunityPoints+" from "+totalCommunityPosts+" posts":"None")+"\n"; alertText+="[B]Total mod points:[/B] "; alertText+=((totalModPosts>0)?totalModPoints+" from "+totalModPosts+" posts":"None")+"\n"; alertText+="[B]Minimum required for MVP* ("+totalWinningPoints+" x 0.15):[/B] "+minimumRequiredMVP+"\n\n"; for(var i=0;i=participants.length){ break; } if(participants[i+2]*participants[i+1]=minimumRequiredMVP){ //GM_log("Adding "+participants[i]+" to the MVP list."); mvpList.push(participants[i]); } } if(participants.length==0){ statsText+="None\n"; } return new Array(statsText,mvpList); }, restoreStats: function(){ if(!GM_getValue("statsActive",false)){ GM_setValue("statsActive",true); return false; } var scoresCount=GM_getValue("scores.count",0); this.lastPoster=GM_getValue("lastPoster",null); this.currentScore=parseInt(GM_getValue("currentScore")); this.lastPostNumber=parseInt(GM_getValue("lastPostNumber")); for(i=0;i