summaryrefslogtreecommitdiffstats
path: root/uftow-stats-simple.user.js
blob: c8993b7a933880030b9168a2d0f4b579e5a90e97 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// ==UserScript==
// @name        uftow-stats-simple
// @namespace   uftow-stats-simple
// @include     https://ubuntuforums.org/showthread.php?t=1579442*
// @version     1.4.0
// @Description Generates stats for Tug of War - simple variant.
// @grant		GM_log
// @grant		GM_getValue
// @grant		GM_setValue
// @grant		GM_listValues
// @grant		GM_deleteValue
// @grant		GM_registerMenuCommand
// ==/UserScript==

var towStats = {
	alreadyHasStartCounting:false,
	scores:new Array(),
	lastPostNumber:0,
	lastPoster:null,
	startPostNumber:null,
	endPostNumber: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));
		if(!GM_getValue("statsActive",false)){
			this.addStartCounting();
		}else{
			this.continueCounting();
		}
		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;
		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<this.postContainers.length;i++){
			postInfo=this.getPostInfo(this.postContainers[i]);
			this.lastPostNumber=postInfo[1];
			if(this.lastPoster==postInfo[0]){
				//GM_log("Post #"+this.lastPostNumber+" is a duplicate and has been skipped.");
				continue;
			}
			if(this.endPostNumber!=null && this.lastPostNumber>this.endPostNumber){
				break;
			}
			this.scorePost(postInfo);
		}
		return true;
	},
	getPostInfo: function(postElement){
		var userSpanTag=postElement.getElementsByClassName('username')[0].getElementsByTagName('span')[0];
		var userName=userSpanTag.textContent;
		var postNumber=parseInt(postElement.getElementsByClassName('postcounter')[0].textContent.substr(1));
		return new Array(userName,postNumber);
	},
	scorePost: function(postInfo){
		this.lastPoster=postInfo[0];
		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%2==0){
			this.scores[userIndex+1]++;
		}else{
			this.scores.push(postInfo[0]);
			this.scores.push(1);
		}
	},
	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;
		for(var i=1;i<this.scores.length;i+=2){
			//GM_log("STATS: User: "+this.scores[i-1]+", Posts: "+this.scores[i]);
			totalParticipants++;
			totalPosts+=this.scores[i];
		}
		alertText+="[B]Post range:[/B] "+this.startPostNumber+" - "+
			((this.endPostNumber!=null)?this.endPostNumber:this.lastPostNumber)+"\n";
		alertText+="[B]Post count:[/B] "+totalPosts+"\n";
		alertText+="[B]Participants:[/B] "+totalParticipants+"\n\n";
		alertText+="[B]PARTICIPANTS[/B]";
		alertText+=this.getListText(this.scores);
		window.alert(alertText);
	},
	getListText: function(participants){
		var statsText="";
		var thisScore;
		var currScore=0;
		var reCheck=true;
		var tempSpot;
		while(reCheck){
			reCheck=false;
			for(var i=0;i<participants.length;i+=2){
				if(i+2>=participants.length){
					break;
				}
				if(participants[i+1]<participants[i+3]){
					tempSpot=new Array(participants[i],participants[i+1]);
					participants[i]=participants[i+2];
					participants[i+1]=participants[i+3];
					participants[i+2]=tempSpot[0];
					participants[i+3]=tempSpot[1];
					reCheck=true;
				}
			}
		}
		//List individually
		//for(var i=0;i<participants.length;i+=2){
		//	thisScore=participants[i+1];
		//	statsText+="[B]"+participants[i]+":[/B] "+participants[i+1]+" ";
		//	statsText+="post"+((thisScore!=1)?"s":"");
		//	statsText+="\n";
		//}
		//Group by post count
		for(var i=0;i<participants.length;i+=2){
			thisScore=participants[i+1];
			if(thisScore!=currScore){
				currScore=thisScore;
				statsText+="\n[B]"+thisScore+" post"+((thisScore!=1)?"s":"")+":[/B] "+participants[i];
			}else{
				statsText+=", "+participants[i];
			}
		}
		if(participants.length==0){
			statsText+="None\n";
		}
		return new Array(statsText);
	},
	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;
		}
	},
	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;
			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()){
			this.getSummary();
			this.endCounting();
		}
	},
	endCounting: function(){
		GM_setValue("statsActive",false);
		if(!this.alreadyHasStartCounting){
			this.addStartCounting();
		}
	},
	showSummary: function(){
		if(this.restoreStats()){
			this.getSummary();
		}
	},
	saveStats: function(){
		//GM_log("Last post on this page: "+this.lastPostNumber);
		GM_setValue("lastPostNumber",this.lastPostNumber);
		for(i=0;i<this.scores.length;i++){
			GM_setValue("scores."+i,this.scores[i]);
			GM_setValue("scores.count",this.scores.length);
		}
		GM_setValue("lastPoster",this.lastPoster);
		GM_setValue("startPostNumber",this.startPostNumber);
		GM_setValue("endPostNumber",this.endPostNumber);
		GM_setValue("statsCached",true);
	},
	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=0;
		this.lastPoster=null;
		this.startPostNumber=null;
		this.endPostNumber=null;
		keys=GM_listValues();
		for (var i=0,key=null; key=keys[i];i++) {
			GM_deleteValue(key);
		}
	}
};

towStats.init();