diff options
author | Krytarik Raido <krytarik@tuxgarage.com> | 2017-09-17 02:23:04 +0200 |
---|---|---|
committer | Krytarik Raido <krytarik@tuxgarage.com> | 2017-09-17 22:45:04 +0200 |
commit | dc59fc6f20f9aeff1635e2049400d9d1091df46b (patch) | |
tree | 089a80dbe316885645fda6646895c377a519a077 | |
parent | db531fd4775033ee85b3ef5ade3f4d045a9bdb57 (diff) | |
download | gm-scripts-master.tar.bz2 gm-scripts-master.tar.xz gm-scripts-master.tar.zst |
-rw-r--r-- | uftow-stats-simple.user.js | 134 | ||||
-rw-r--r-- | uftow-stats.user.js | 144 |
2 files changed, 158 insertions, 120 deletions
diff --git a/uftow-stats-simple.user.js b/uftow-stats-simple.user.js index f04c987..c8993b7 100644 --- a/uftow-stats-simple.user.js +++ b/uftow-stats-simple.user.js @@ -2,7 +2,7 @@ // @name uftow-stats-simple // @namespace uftow-stats-simple // @include https://ubuntuforums.org/showthread.php?t=1579442* -// @version 1.3.0 +// @version 1.4.0 // @Description Generates stats for Tug of War - simple variant. // @grant GM_log // @grant GM_getValue @@ -15,7 +15,7 @@ var towStats = { alreadyHasStartCounting:false, scores:new Array(), - lastPostNumber:null, + lastPostNumber:0, lastPoster:null, startPostNumber:null, endPostNumber:null, @@ -24,12 +24,17 @@ var towStats = { 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)); 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; @@ -71,6 +76,13 @@ var towStats = { } }, getSummary: function(){ + if(this.scores.length>=2 && !(this.endPostNumber!=null && this.lastPostNumber<this.endPostNumber)){ + window.alert("Counting complete.\n"+ + "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 totalParticipants=0; var totalPosts=0; @@ -131,57 +143,59 @@ var towStats = { } return new Array(statsText); }, - 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.lastPostNumber=parseInt(GM_getValue("lastPostNumber")); - this.startPostNumber=parseInt(GM_getValue("startPostNumber")); - this.endPostNumber=GM_getValue("endPostNumber"); - if(this.endPostNumber!=null){ - this.endPostNumber=parseInt(this.endPostNumber); - } - for(i=0;i<scoresCount;i++){ - this.scores.push(GM_getValue("scores."+i)); - } - return true; }, startCounting: function(){ + this.clearStats(); + answerStart=window.prompt("Specifiy a post number to begin counting.\n"+ + "If you do not know the post number, click Cancel."); + if(answerStart!=null){ + startPost=parseInt(answerStart); + this.startPostNumber=startPost; + answerEnd=window.prompt("Specifiy a post number to end counting.\n"+ + "If you want to count until the end, click Cancel."); + if(answerEnd!=null){ + this.endPostNumber=parseInt(answerEnd); + } + GM_setValue("statsActive",true); + this.doCounting(startPost); + } + }, + continueCounting: function(){ if(this.restoreStats()){ startPost=this.lastPostNumber+1; - }else{ - answerStart=window.prompt("Specifiy a post number to begin counting.\n"+ - "If you do not know the post number, click Cancel."); - if(answerStart!=null){ - startPost=parseInt(answerStart); - this.startPostNumber=startPost; - answerEnd=window.prompt("Specifiy a post number to end counting.\n"+ - "If you want to count until the end, click Cancel."); - if(answerEnd!=null){ - this.endPostNumber=parseInt(answerEnd); - } - GM_setValue("statsActive",true); - }else{ - this.clearStats(); - return false; - } + GM_setValue("statsActive",true); + this.doCounting(startPost); } + }, + doCounting: function(startPost){ if(!this.recordPosts(startPost)){ + this.endCounting(); return false; } this.saveStats(); if((this.endPostNumber!=null && this.lastPostNumber>=this.endPostNumber) || !this.goToNextPage()){ - if(this.scores.length>=2 && !(this.endPostNumber!=null && this.lastPostNumber<this.endPostNumber)){ - window.alert("Counting complete.\n"+ - "Stats will be displayed in the next dialog box."); - this.getSummary(); - }else{ - window.alert("Tug of War is yet to be completed."); - } - this.clearStats(); + this.getSummary(); + this.endCounting(); + } + }, + endCounting: function(){ + GM_setValue("statsActive",false); + if(!this.alreadyHasStartCounting){ + this.addStartCounting(); + } + }, + showSummary: function(){ + if(this.restoreStats()){ + this.getSummary(); } }, saveStats: function(){ @@ -194,20 +208,30 @@ var towStats = { GM_setValue("lastPoster",this.lastPoster); GM_setValue("startPostNumber",this.startPostNumber); GM_setValue("endPostNumber",this.endPostNumber); + 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.lastPostNumber=parseInt(GM_getValue("lastPostNumber")); + this.startPostNumber=parseInt(GM_getValue("startPostNumber")); + this.endPostNumber=GM_getValue("endPostNumber"); + if(this.endPostNumber!=null){ + this.endPostNumber=parseInt(this.endPostNumber); + } + 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.lastPostNumber=null; + this.lastPostNumber=0; this.lastPoster=null; this.startPostNumber=null; this.endPostNumber=null; @@ -215,10 +239,6 @@ var towStats = { 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; - } } }; 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; - } } }; |