diff options
Diffstat (limited to 'uftow-stats.user.js')
-rw-r--r-- | uftow-stats.user.js | 144 |
1 files changed, 81 insertions, 63 deletions
diff --git a/uftow-stats.user.js b/uftow-stats.user.js index db2a51e..cf48162 100644 --- a/uftow-stats.user.js +++ b/uftow-stats.user.js @@ -2,7 +2,7 @@ // @name uftow-stats // @namespace uftow-stats // @include https://ubuntuforums.org/showthread.php?t=1579442* -// @version 1.3.0 +// @version 1.4.0 // @Description Generates stats for Tug of War. // @grant GM_log // @grant GM_getValue @@ -40,23 +40,26 @@ var towStats = { alreadyHasStartCounting:false, scores:new Array(), currentScore:0, - lastPostNumber:null, + lastPostNumber:0, 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; + this.addStartCounting(); }else{ - this.startCounting(); + this.continueCounting(); } - GM_registerMenuCommand("Clear counter",function(){towStats.clearStats();},'A'); + GM_registerMenuCommand("Tug of War: Clear counter",function(){towStats.clearStats();},'A'); + }, + addStartCounting: function(){ + GM_registerMenuCommand("Tug of War: Start counting",function(){towStats.startCounting();},'S'); + GM_registerMenuCommand("Tug of War: Continue counting",function(){towStats.continueCounting();},'C'); + GM_registerMenuCommand("Tug of War: Show last results",function(){towStats.showSummary();},'R'); + this.alreadyHasStartCounting=true; }, recordPosts: function(startPost){ var postNumber; @@ -79,8 +82,6 @@ var towStats = { }else if(this.currentScore>=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); @@ -114,6 +115,18 @@ var towStats = { } }, getSummary: function(){ + if(this.winner!=null){ + window.alert("Counting complete.\n"+ + "The "+((this.currentScore<0)?'-':'')+"200 mark is at post #"+this.lastPostNumber+"\n"+ + "Stats will be displayed in the next dialog box."); + }else if(this.scores.length>=3){ + window.alert("Tug of War is yet to be completed.\n"+ + "Its score is "+this.currentScore+" points as of post #"+this.lastPostNumber+"\n"+ + "Intermediate stats will be displayed in the next dialog box."); + }else{ + window.alert("Tug of War is yet to be completed."); + return false; + } var alertText="[B]TUG OF WAR LAST ROUND STATS[/B]\n"; var totalPoints=0; var totalPosts=0; @@ -138,7 +151,7 @@ var towStats = { } totalPosts+=this.scores[i+1]; } - totalWinningPoints=(this.winner=='community')?totalCommunityPoints:totalModPoints; + totalWinningPoints=(this.currentScore>=0)?totalCommunityPoints:totalModPoints; minimumRequiredMVP=Math.round(totalWinningPoints*0.15); alertText+="[B]Total points:[/B] "+totalPoints+" from "+totalPosts+" posts\n"; alertText+="[B]Total community points:[/B] "; @@ -164,11 +177,11 @@ var towStats = { modStats=this.getListText(modParticipants,minimumRequiredMVP); alertText+=modStats[0]+"\n"; alertText+="[B]LAST ROUND MVPs[/B]\n"; - alertText+="- "+this.marker+" (at post #"+this.markerPost+")\n"; - //GM_log("MVP order: "+this.marker+" made the mark at post #"+this.markerPost); - mvpList=(this.winner=='community')?communityStats[1]:modStats[1]; + alertText+="- "+this.lastPoster+" (at post #"+this.lastPostNumber+")\n"; + //GM_log("MVP order: "+this.lastPoster+" made the mark at post #"+this.lastPostNumber); + mvpList=(this.currentScore>=0)?communityStats[1]:modStats[1]; for(var i=0;i<mvpList.length;i++){ - if(mvpList[i]!=this.marker){ + if(mvpList[i]!=this.lastPoster){ alertText+="- "+mvpList[i]+"\n"; //GM_log("MVP order: "+mvpList[i]+" has spot #"+(i+2)); } @@ -216,51 +229,54 @@ var towStats = { } return new Array(statsText,mvpList); }, - restoreStats: function(){ - if(!GM_getValue("statsActive",false)){ - GM_setValue("statsActive",true); + goToNextPage: function(){ + var pageLinks=document.getElementsByClassName('pagination_top')[0]; + var nextLinkContainer=pageLinks.getElementsByClassName('prev_next')[1]; + if(nextLinkContainer!=undefined){ + nextLinkContainer.getElementsByTagName('a')[0].click(); + return true; + }else{ 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<scoresCount;i++){ - this.scores.push(GM_getValue("scores."+i)); - } - return true; }, startCounting: function(){ + this.clearStats(); + theAnswer=window.prompt("Specifiy a post number to begin counting.\n"+ + "If you do not know the post number, click Cancel."); + if(theAnswer!=null){ + startPost=parseInt(theAnswer); + GM_setValue("statsActive",true); + this.doCounting(startPost); + } + }, + continueCounting: function(){ if(this.restoreStats()){ startPost=this.lastPostNumber+1; - }else{ - theAnswer=window.prompt("Specifiy a post number to begin counting.\n"+ - "If you do not know the post number, click Cancel."); - if(theAnswer!=null){ - startPost=parseInt(theAnswer); - GM_setValue("statsActive",true); - }else{ - this.clearStats(); - return false; - } + GM_setValue("statsActive",true); + this.doCounting(startPost); } - //GM_log("Round score before counting: "+this.currentScore); + }, + doCounting: function(startPost){ + //GM_log("Current round score: "+this.currentScore); if(!this.recordPosts(startPost)){ + this.endCounting(); return false; } - if(this.winner!=null){ - window.alert("Counting complete.\n"+ - "The "+((this.winner=='mods')?'-':'')+"200 mark is at post #"+this.markerPost+"\n"+ - "Stats will be displayed in the next dialog box."); + this.saveStats(); + if(this.winner!=null || !this.goToNextPage()){ + this.getSummary(); + this.endCounting(); + } + }, + endCounting: function(){ + GM_setValue("statsActive",false); + if(!this.alreadyHasStartCounting){ + this.addStartCounting(); + } + }, + showSummary: function(){ + if(this.restoreStats()){ this.getSummary(); - this.clearStats(); - }else{ - this.saveStats(); - if(!this.goToNextPage()){ - window.alert("Tug of War is yet to be completed.\n"+ - "Its score is "+this.currentScore+" points as of post #"+this.lastPostNumber); - this.clearStats(); - } } }, saveStats: function(){ @@ -273,33 +289,35 @@ var towStats = { GM_setValue("scores.count",this.scores.length); } GM_setValue("lastPoster",this.lastPoster); + GM_setValue("winner",this.winner); + GM_setValue("statsCached",true); }, - goToNextPage: function(){ - var pageLinks=document.getElementsByClassName('pagination_top')[0]; - var nextLinkContainer=pageLinks.getElementsByClassName('prev_next')[1]; - if(nextLinkContainer!=undefined){ - nextLinkContainer.getElementsByTagName('a')[0].click(); - return true; - }else{ + restoreStats: function(){ + if(!GM_getValue("statsCached",false)){ + window.alert("No cached stats.") return false; } + var scoresCount=GM_getValue("scores.count",0); + this.lastPoster=GM_getValue("lastPoster",null); + this.winner=GM_getValue("winner",null); + this.currentScore=parseInt(GM_getValue("currentScore")); + this.lastPostNumber=parseInt(GM_getValue("lastPostNumber")); + this.scores=new Array(); + for(i=0;i<scoresCount;i++){ + this.scores.push(GM_getValue("scores."+i)); + } + return true; }, clearStats: function(){ this.scores=new Array(); this.currentScore=0; - this.lastPostNumber=null; + this.lastPostNumber=0; this.lastPoster=null; this.winner=null; - this.marker=null; - this.markerPost=null; keys=GM_listValues(); for (var i=0,key=null; key=keys[i];i++) { GM_deleteValue(key); } - if(!this.alreadyHasStartCounting){ - GM_registerMenuCommand("Start counting posts",function(){towStats.startCounting()},'S'); - this.alreadyHasStartCounting=true; - } } }; |