summaryrefslogtreecommitdiff
path: root/html/includes/domstats.php
blob: dbdd3f7d597f134b968bfadbdd4b4cb219136699 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
require_once 'jpgraph/jpgraph.php';
require_once 'jpgraph/jpgraph_scatter.php'; 
require_once 'jpgraph/jpgraph_mgraph.php'; 
require_once 'jpgraph/jpgraph_bar.php';
require_once 'jpgraph/jpgraph_line.php';
require_once 'jpgraph/jpgraph_radar.php';

/*
Retrieve player names & player teams
Pretty much utstats code re-used
@return $playernames, $playerteams (arrays based on pid)
*/
function getPlayerTeam($uid) {
	
	$playernames = array();
	$playerteams = array();
	
	// Get List of Player IDs and Process What They Have Done
	$sql_player = "SELECT DISTINCT col4 FROM uts_temp_$uid WHERE col1 = 'player' AND col2 = 'rename' AND col4 <> ''";
	$q_player = mysql_query($sql_player) or die(mysql_error());
	
	while ($r_player = mysql_fetch_array($q_player)) {
		$playerid = $r_player[col4];

		// Get players last name used
		$r_player2 = small_query("SELECT col3 FROM uts_temp_$uid WHERE col1 = 'player' AND col2 = 'rename' AND col4 = $playerid ORDER BY id DESC LIMIT 0,1");
		$playername = addslashes($r_player2[col3]);

		// Get players last team
		$r_player3 = small_query("SELECT col4 FROM uts_temp_$uid WHERE col1 = 'player' AND col2 = 'TeamChange' AND col3 = $playerid AND col4 != 255 ORDER BY id DESC LIMIT 0,1");
		$playerteam = $r_player3[col4];

		$playernames[$playerid] = $playername;
		$playerteams[$playerid] = $playerteam;		
	}
	
	return array($playernames,$playerteams);

}

/*
Get time game starts, game ends, and ratio compared to real time
@return $time_gamestart, $time_gameend, $time_ratio_correction (difference in ut time & real time, typically 110%)
*/
function getGameStartEndRatio($uid) {

	// gather game start & end time
	$q_logdom = mysql_query("SELECT col0 FROM uts_temp_$uid WHERE col1='game_start' OR col1='game_end' ORDER BY id ASC")or die(mysql_error());

	$time_gamestart = mysql_result($q_logdom,0);
	$time_gameend = mysql_result($q_logdom,1);
	$time_ratio_correction = ($time_gameend-$time_gamestart)/1200;
			
	return array($time_gamestart, $time_gameend, $time_ratio_correction);
}

/*
Helper function to create the temporary table used for dom stats
*/
function createEmptyTempTable($table_name) {
		
	$sqlCreateTable = "
		CREATE TEMPORARY TABLE `$table_name` (
	 `id` int(11) NOT NULL AUTO_INCREMENT,
	 `cp` varchar(254) NOT NULL,
	 `teamid` int(11) NOT NULL,
	 `playerid` int(11) NOT NULL,
	 `playername` varchar(254) NOT NULL,
	 `start` float NOT NULL,
	 `end` float NOT NULL,
	 `time` float NOT NULL,
	 `scoret0` float NOT NULL,
	 `scoret1` float NOT NULL,
	 `realTimeEnd` float NOT NULL,
	 PRIMARY KEY (`id`) 
	 ) ENGINE=MyISAM 
	";
	mysql_query($sqlCreateTable) or die(mysql_error());
}
	
/*
Create & populate the dom stats table
@return name of temporary table
*/
function generateTempTable($uid) {
	global $playernames;
	global $playerteams;
	global $time_gamestart;
	global $time_gameend;
	global $time_ratio_correction;

	$tempTableName = "uts_tempidom_".$uid;
	
	createEmptyTempTable($tempTableName);
			
	// iterate over cp's. also capture game-end event as a quick hack to also do final iteration over final cp
	$q_logdom = mysql_query("SELECT * FROM uts_temp_$uid WHERE col1='controlpoint_capture' OR col1='game_end' ORDER BY col1, col2, id ASC")or die(mysql_error());
		
	$prev_time = 0;		
	
	// dynamicly create insert query to populate the table in 1 go, instead of multiple database calls
	$insertQuery = "INSERT INTO $tempTableName (cp, teamid, playerid, playername, start, end, time,scoret0,scoret1,realTimeEnd) VALUES";
	
	// basically loop over all capture events per CP. Each time calculate how long the previous owner had the point and translate this to dom points
	while($r_logdom = mysql_fetch_array($q_logdom)) {
		$points[0] = 0;
		$points[1] = 0;
		$ticks[0] = 0;
		$ticks[1] = 0;
			
		$start_time = $prev_time;
		
		$r_cp = $r_logdom['col2'];	
		$r_time = $r_logdom['col0'];
		
		// skip first capture event - no dom points given at start of map since no-one has cp		
		if($prev_time > 0) {
		
			// if switching to other cp
			if($prev_cp != $r_cp) {	
				$end_time = $time_gameend; // take game end time to calculate time cp was taken, since it's the last time it's taken + extra tick for end time
				
			} else {			
				$end_time = $r_time;		
			}	

			$time_diff = ($end_time - $start_time);
			
			// point has to be yours for at least a second before it starts to add points
			if($time_diff >= 1) {
			
				// after this second, it will start to add points during each time the timer runs (= each second)
				// thus, first run of timer is ceil of start time
				// also, floor of end time since having the point after the timer ran, but not until the next tick = no points
				// timing in log does not account for gamespeed, typically 1.1 ratio, which is corrected in these times
				// test was done to save the actual tick times based upon the dom_points stats event (happens each 5 ticks), but this doesn't result in significant better results to be worth the effort
							
				// save amount of timer 'ticks' that generate a score of 0.2
				$ticks[$r_teamid] = (floor($end_time/$time_ratio_correction)-ceil($start_time/$time_ratio_correction));
				
			}
					
			// if no tick happened, insert the event with 0  points		
			if($ticks[$r_teamid]==0) {
				
				$realTimeEnd=($start_time-$time_gamestart)/$time_ratio_correction/60;	
				$insertQuery .= " ('$prev_cp', '$r_teamid', '$r_pid', '$r_pname', '$start_time', '$end_time', '$time_diff', '".$points[0]."', '".$points[1]."', '$realTimeEnd'),";
			
			// if ticks happened, insert each seperate tick as an event
			} else {
					
				for($i=1;($ticks[$r_teamid]+1-$i) > 0;$i++) {
				
					$realTimeEnd=($start_time+$time_diff/$ticks[$r_teamid]*$i-$time_gamestart)/$time_ratio_correction/60;		
					$points[$r_teamid] = 0.2;
					
					$insertQuery .= " ('$prev_cp', '$r_teamid', '$r_pid', '$r_pname', '$start_time', '$end_time', '$time_diff', '".$points[0]."', '".$points[1]."', '$realTimeEnd'),";
					
				}
			}
			
		} 	
		
		// save data for current capture event
		$prev_time = $r_time;
		$prev_cp = $r_cp;
		
		$r_event = $r_logdom['col1'];
		$r_pid = $r_logdom['col3'];
		$r_teamid = mysql_real_escape_string($playerteams[$r_pid]);
		$r_pname = mysql_real_escape_string($playernames[$r_pid]);
		
	} 
	
	// populate table
	$insertQuery = rtrim($insertQuery,",");
	mysql_query($insertQuery) or die(mysql_error());
	
	return $tempTableName;
	
}

/*
Generate times when amp was taken
@return $amps[$team][$start_time,$end_time, $pid]
*/
function generateAmpTimes($uid) {
	global $playernames;
	global $playerteams;
	global $time_gamestart;
	global $time_gameend;
	global $time_ratio_correction;
	
	$amps = array();
	$prev_time = array();
	
	// Get activate & deactivate times & players
	$q_amps = mysql_query("SELECT col0,col1,col3 FROM uts_temp_$uid WHERE col2='Damage Amplifier' AND (col1='item_activate' OR col1='item_deactivate') ORDER BY id ASC");
	while($r_amps = mysql_fetch_array($q_amps)) {
		$time = ($r_amps[0]-$time_gamestart)/$time_ratio_correction/60;
		$event = $r_amps[1];
		$pid = $r_amps[2];
		
		// If amp is deactivated, calculate the time it was used
		if($event == "item_deactivate") {			
			$amps[$playerteams[$pid]][] = array($prev_time[$pid], $time,$pid);
			$prev_time[$pid] = 0;
			
		// If amp is activated & none yet acquired, save the time of the start of this amp run
		} else if($prev_time[$pid] == 0) {
			$prev_time[$pid] = $time;
		}
	}
	
	return $amps;
}

/*
Function to render the chart with amp runs
*/
function renderAmpBars($uid,$tempTableName) {
	global $playernames;
	global $ampTimes;
	global $matchid;
	global $domimage_color;
	global $domimages_folder;
	global $domimage_width;
	global $domimage_heigth;
	
	// Iterate over amp runs
	for($i=0;$i<2;$i++) {
		foreach ($ampTimes[$i] as $ampTaken) {
			$ampStart = $ampTaken[0];
			$ampEnd = $ampTaken[1];
			$pid = $ampTaken[2];
			
			// Only save amp runs longer than 20seconds
			if($ampEnd > ($ampStart+.33)) {
			
				// Get scores during amprun
				$q_scoresDuringAmp = mysql_query("SELECT SUM(scoret0),SUM(scoret1) FROM $tempTableName WHERE realTimeEnd > $ampStart AND realTimeEnd < $ampEnd");
				$r_scoresDuringAmps = mysql_fetch_array($q_scoresDuringAmp);
												
				$netPoints = $r_scoresDuringAmps[$i] - $r_scoresDuringAmps[1-$i];
				
				// Process time for readable label
				$totalTime = $ampEnd-$ampStart;
				$totalTimeMinutes = floor($totalTime);
				$totalTimeSeconds = round(($totalTime-$totalTimeMinutes)*60,0);
				
				$timeLabel = "";
				if($totalTimeMinutes>0) { $timeLabel .= $totalTimeMinutes."m"; }
				if($totalTimeSeconds>0) { $timeLabel .= $totalTimeSeconds."s"; }
				
				$playerName = $playernames[$pid];
								
				$ampRun['start'] = $ampStart;
				$ampRun['label'] = substr($playerName,0,15)." (".$timeLabel.")";
				$ampRun['points'] = $netPoints;
				$ampRun['team'] = $i;
				
				$data[] = $ampRun;
			}
		}
	}
	
	if(count($data)>0) {
		
		// Sort ampruns not on teams, but on start time, to make sure they are plotted chronocologicstic
		$data = array_sort($data,'start');
		
		$maxValue=0;
		// Process the ampruns to map it to 2 differnt bar plots per team
		foreach ($data as $ampRun) {
			$labels[] = $ampRun['label'];
			$values[$ampRun['team']][] = $ampRun['points'];
			
			$posValue = $ampRun['points']>0? $ampRun['points'] : $ampRun['points']*-1;
			
			if($posValue>$maxValue)
				$maxValue = $posValue;
			
			// Add 0 value for other team to ensure only 1 bar is plotted each time (either team 0 or team 1)
			$values[1-$ampRun['team']][] = 0;
		}
		
		$tickInterval = $maxValue>15?10:5;
		$maxValue = $tickInterval*ceil($maxValue/$tickInterval);
				
		// Create the graph
		// Some commands to enable horizontal bars & moved axis for proper formatting
		$graph = new Graph($domimage_width+8,$domimage_heigth+8);
		$graph->SetScale('textlin',$maxValue*-1,$maxValue);	  
		$graph->SetTickDensity(TICKD_SPARSE);
		$graph->Set90AndMargin('20','15','30','30');
		$graph->SetMarginColor($domimage_color['background']); 
		$graph->SetColor($domimage_color['background']); 
		$graph->SetFrame(true,$domimage_color['background'],0);
		$graph->ygrid->SetFill(true,$domimage_color['band'][0],$domimage_color['band'][1]); 
		$graph->ygrid->SetColor($domimage_color['background']); 
		
		$graph->title->Set('Net Score Amp Runs');
		$graph->title->SetFont(FF_VERDANA,FS_BOLD);
		$graph->title->SetColor($domimage_color['heading']);
		
		$graph->xaxis->SetPos('min');
		$graph->xaxis->SetTickLabels($labels);
		$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,7);
		$graph->xaxis->SetColor($domimage_color['font']);
		$graph->xaxis->SetLabelAlign('left','center');
		$graph->xaxis->SetColor($domimage_color['background'],$domimage_color['font']);
		
		$graph->yaxis->SetPos('max');
		$graph->yaxis->SetLabelAlign('center','top');
		$graph->yaxis->SetLabelSide(SIDE_RIGHT);
		$graph->yaxis->SetTickSide(SIDE_LEFT);
		$graph->yaxis->scale->ticks->set($tickInterval);
		$graph->yaxis->SetColor($domimage_color['background'],$domimage_color['font']);
				
		if(count($values[0])>0) { 
			$b1plot = new BarPlot($values[0]); 
			$graph->Add($b1plot);
			$b1plot->SetFillGradient($domimage_color['team'][0][0],$domimage_color['team'][0][1],GRAD_HOR);
			$b1plot->SetColor($domimage_color['team'][0][0]);
			
		} if(count($values[1])>0) { 
			$b2plot = new BarPlot($values[1]); 
			$graph->Add($b2plot);		
			$b2plot->SetFillGradient($domimage_color['team'][1][0],$domimage_color['team'][1][1],GRAD_HOR);
			$b2plot->SetColor($domimage_color['team'][1][0]);	
		}		
		
		$graph->Stroke($domimages_folder."/".$matchid."-ampruns.png");		
	}
}

/*
Generate the chart with pickups
*/
function renderDataPickups($uid) {
	global $matchid;
	global $domimage_color;
	global $domimages_folder;
	global $domimage_width;
	global $domimage_heigth;

	$q_pickups = mysql_query("SELECT SUM(pu_belt), SUM(pu_keg), SUM(pu_pads), SUM(pu_armour), SUM(pu_amp) FROM uts_player as p WHERE matchid = $matchid GROUP BY team") or die(mysql_error());
	
	while($r_pickups = mysql_fetch_row($q_pickups)) {
		$preData[] = $r_pickups;
	}
	
	$anyPickupDone = false;
	
	// Process data to convert these to percentages
	// Normal numbers don't plot nicely (fe. pads getting much higher pickups due to lower spawn time
	for($i=0;$i<5;$i++) {
		if($preData[0][$i]>0) {
			$data[0][$i] = round($preData[0][$i]/($preData[0][$i]+$preData[1][$i])*100,0);
			$data[1][$i] = 100-$data[0][$i];
			$anyPickupDone = true;			
		} else if($preData[1][$i]>0) {
			$data[0][$i] = 0;
			$data[1][$i] = 100;
			$anyPickupDone = true;
		} else {
			$data[0][$i] = 0;
			$data[1][$i] = 0;		
		}
	}
	
	if($anyPickupDone) {
		// Plot radargraph
		$titles = array('belt','keg','pads','armour','amp');
		$graph = new RadarGraph ($domimage_width+8,$domimage_heigth+8);
		$graph->SetScale('lin',0,100);
		$graph->yscale->ticks->Set(25,5);
		$graph->setMargin(30,30,30,30);
		$graph->SetMarginColor($domimage_color['background']); 
		$graph->SetColor($domimage_color['background']); 
		$graph->SetFrame(true,$domimage_color['background'],0);
		$graph->title->Set('Pickups %');
		$graph->title->SetFont(FF_VERDANA,FS_BOLD);
		$graph->title->SetColor($domimage_color['heading']);
		$graph->HideTickMarks();
		$graph->axis->HideLabels();
		
		$graph->SetTitles($titles);
		$graph->axis->title->SetFont(FF_VERDANA,FS_NORMAL,8);
		$graph->axis->title->SetColor($domimage_color['font']);
		$graph->grid->Show(true,true);
		$graph->grid->SetLineStyle('dashed');	 	 
		
		// Seperate plot within radar per team
		$plot1 = new RadarPlot($data[0]);	 
		$plot2 = new RadarPlot($data[1]);	 
		$graph->Add($plot1);
		$graph->Add($plot2);
		
		$plot1->SetFillColor($domimage_color['team'][0][0].'@0.5');
		$plot1->SetColor($domimage_color['team'][0][0]);
		$plot1->SetLineWeight(2);
		$plot2->SetFillColor($domimage_color['team'][1][0].'@0.5');
		$plot2->SetColor($domimage_color['team'][1][0]);
		$plot2->SetLineWeight(2);
		
		$graph->Stroke($domimages_folder."/".$matchid."-pickups.png");
	}
}

/*
Render dom points over time & net rate of change per minute over all cp's
*/
function renderDataTotal($uid,$tempTableName) {
	$query = "SELECT t.realTimeEnd,@t0sum := @t0sum + t.scoret0 AS cumulScoret0,@t1sum := @t1sum + t.scoret1 AS cumulScoret1 FROM $tempTableName t JOIN (SELECT @t0sum := 0) r JOIN (SELECT @t1sum := 0) s ORDER BY realTimeEnd ASC";
	$appex = "total";
	
	renderData($uid,$tempTableName,$query,$appex,"Total score","");
}

/*
Render dom points over time & net rate of change per minute per cp
*/
function renderDataCPs($uid,$tempTableName) {
	$q_cps = mysql_query("SELECT DISTINCT(cp) FROM $tempTableName");
	
	$i=0;
	while($r_cps = mysql_fetch_array($q_cps)) {
		$r_cp = $r_cps[0];
		$query = "SELECT t.realTimeEnd,@t0sum := @t0sum + t.scoret0 AS cumulScoret0,@t1sum := @t1sum + t.scoret1 AS cumulScoret1 FROM $tempTableName t JOIN (SELECT @t0sum := 0) r JOIN (SELECT @t1sum := 0) s WHERE t.CP = '$r_cp' ORDER BY realTimeEnd ASC";
		$appex = "cp".$i++;
		renderData($uid,$tempTableName,$query,$appex,"Score CP ".$r_cp,$r_cp);
		
	}	
}

/*
Render dom points over time & net rate of change per minute
*/
function renderData($uid,$tempTableName,$query,$appex,$title,$cp) {
	global $matchid;
	global $domimage_color;
	global $domimages_folder;
	global $domimage_width;
	global $domimage_heigth;
	
	// Use helper function to parse the data into different datasets for charts
	list($datax,$datay1,$datay2,$derivx,$derivy1,$derivy2) = parseData($query);	
	
	// If this is a cp plot, ensure enough space for plotting the names underneath
	if(strlen($cp)>0) {
	
		$g1heigth = $domimage_heigth*.65;
		$g2heigth = $domimage_heigth*.35;
		$g2marginlow = 50;
	
	// Else, use all space
	} else {
	
		$g1heigth = $domimage_heigth*.75;
		$g2heigth = $domimage_heigth*.25;
		$g2marginlow = 10;
	
	}
	
	// Dom points over time per team
	$graph1 = createGraph($title,$domimage_width,$g1heigth,array(30,10,30,30),"linlin");
	$sp11 = generateScatterLink($datax, $datay1,$domimage_color['team'][0][0],'FCallbackRedAmp');
	$sp12 = generateScatterLink($datax, $datay2,$domimage_color['team'][1][0],'FCallbackBlueAmp');
	$graph1->Add($sp11);
	$graph1->Add($sp12);
	
	// Net rate of change per minute
	$graph2 = createGraph("",$domimage_width,$g2heigth,array(30,10,5,$g2marginlow),"linlin");
	$graph2->xaxis->HideLabels();
	
	$b1plot = new BarPlot($derivy1,$derivx);
	$b2plot = new BarPlot($derivy2,$derivx);		
	
	$graph2->Add($b1plot);
	$graph2->Add($b2plot);
	
	$b1plot->SetFillGradient($domimage_color['team'][0][0],$domimage_color['team'][0][1],GRAD_HOR);
	$b1plot->SetColor($domimage_color['team'][0][0]);
	$b1plot->SetWidth(0.85);
	$b2plot->SetFillGradient($domimage_color['team'][1][0],$domimage_color['team'][1][1],GRAD_HOR);
	$b2plot->SetColor($domimage_color['team'][1][0]);	
	$b2plot->SetWidth(0.85);
	
	// If this is a cp plot, render the names
	if(strlen($cp) > 0) {			
		renderNamesCP($tempTableName,$cp,$graph2,$domimage_width,$g2heigth);	
	}
		
	// Combine both graphs into one
	$mgraph = new MGraph();
	$xpos1=0;$ypos1=0;
	$xpos2=0;$ypos2=$g1heigth;
	$mgraph->Add($graph1,$xpos1,$ypos1);
	$mgraph->Add($graph2,$xpos2,$ypos2);
	$mgraph->SetFillColor($domimage_color['background']);
		
	$gdImgHandler = $mgraph->Stroke(_IMG_HANDLER);
	 
	$fileName = $domimages_folder."/".$matchid."-".$appex.".png";
	$mgraph->Stroke($fileName);	
}

/*
Helper function to parse the data for the main renderData
*/
function parseData($query) {
	$q_result = mysql_query($query) or die(mysql_error()); 

	$prevx = 0;
	$prevy1 = 0;
	$prevy2 = 0;
	$smallcounter = 1/3;
	$counter = 1;
	
	while($data = mysql_fetch_array($q_result)) {	
			
		// Prep data for dom points over time
		// Only save the data each $smallcounter interval, to optimize rendering time
		if($data[0]>=$smallcounter) {
		
			$datax[] = $data[0];
			$datay1[] = $data[1];
			$datay2[] = $data[2];	
			
			$smallcounter+=1/3;
		}
			
		// Prep data for net change over time
		// Only save the data each integer interval, since we want the value per minute
		// Start at 0.5, to ensure proper alignment with graph above (cheap hack)
		if($data[0]>=$counter) {
			$derivx[] = $counter-1;
		
			$derivy1pre = $data[1]-$prevy1;
			$derivy2pre = $data[2]-$prevy2;
			
			$derivy1post = $derivy1pre-$derivy2pre;
			$derivy2post = $derivy2pre-$derivy1pre;
			
			$derivy1[] = $derivy1post>0 ? $derivy1post : 0;
			$derivy2[] = $derivy2post>0 ? $derivy2post : 0;
			
			$prevy1 = $data[1];
			$prevy2 = $data[2];		
			
			$counter++;
		}					
	}
		
	return array($datax,$datay1,$datay2,$derivx,$derivy1,$derivy2);
}

/*
Render the names of players that played the cp
*/
function renderNamesCP($tempTableName,$appex,$graph,$width,$height) {
	global $domimage_color;
	
	$appex = mysql_real_escape_string($appex);

	$q_namesPerCP = mysql_query("SELECT playername, COUNT( playername ) AS cplayer, MAX( teamid ) AS tid, AVG( realTimeEnd ) AS ati FROM  $tempTableName WHERE cp = '$appex' GROUP BY playername ORDER BY tid,ati") or die(mysql_error());
		
	$yTeam[0][0] = 45;
	$yTeam[0][1] = 35;
	$yTeam[1][0] = 20;
	$yTeam[1][1] = 10;
	$prevTeam = -1;
		
	while($r_namesPerCP = mysql_fetch_array($q_namesPerCP)) {
		$playerName = substr($r_namesPerCP[0],0,20);
		$timesTouched = $r_namesPerCP[1];
		$teamid = $r_namesPerCP[2];
		$avgTime = $r_namesPerCP[3];
	
		// Only plot the playername if he touched the cp at least 60 times
		if(strlen($playerName)>0 && $timesTouched > 60) {
		
			$txt = new Text($playerName);	 
			
			// Names per team can be plotted on 2 different heights to ensure it doesn't become a mess on one line
			// This bit of code ensures height is flipped each time
			if($teamid == $prevTeam) {			
				$ypos = $height-$yTeam[$teamid][1];
				$prevTeam = -1;					
			} else {
				$ypos = $height-$yTeam[$teamid][0];
				$prevTeam = $teamid;
			}
			
			$color = $domimage_color['team'][$teamid][0];
			
			// Plot name at average time, minus 25 (hack assuming most names are around 50 pixels)
			$txt->SetPos($width/20*$avgTime-25,$ypos);
			
			$txt->SetColor($color);
			$txt->SetFont(FF_VERDANA,FS_NORMAL,8);
			$graph->AddText($txt); 					
		}
		
		$prevAvgTime = $avgTime;
	}

}

/*
Helper function to generate the graph object
*/
function createGraph($title,$width,$height,$margin,$scale) {
	global $domimage_color;

	$graph = new Graph($width,$height);
	
	$graph->SetScale($scale);	 
	$graph->SetMargin($margin[0],$margin[1],$margin[2],$margin[3]);        
	$graph->SetMarginColor($domimage_color['background']); 
	$graph->SetColor($domimage_color['background']); 
	$graph->SetFrame(true,$domimage_color['background'],0);
	
	$graph->ygrid->SetFill(true,$domimage_color['band'][0],$domimage_color['band'][1]); 
	$graph->ygrid->SetColor($domimage_color['background']); 
	
	$graph->yaxis->SetColor($domimage_color['background'],$domimage_color['font']);
	$graph->xaxis->SetColor($domimage_color['background'],$domimage_color['font']);
	
	if(strlen($title)>0) {
		$graph->title->Set($title);
		$graph->title->SetFont(FF_VERDANA,FS_BOLD);
		$graph->title->SetColor($domimage_color['heading']);
	}
	return $graph;
}

/*
Helper function to generate the scatter plots
*/
function generateScatterLink($datax, $datay, $color,$callback) {
	$sp = new ScatterPlot($datay,$datax);

	$sp->link->Show();
	$sp->link->SetStyle('solid');
	$sp->link->SetWeight(3);
	$sp->link->SetColor($color);
	
	if(strlen($callback) > 0)
		$sp->mark->SetCallbackYX($callback);
	
	$sp->mark->SetType(MARK_FILLEDCIRCLE);
	
	return $sp;
}

/*
Format functions for scatterplot to format based on team and whether amp was taken
*/
function FCallbackRedAmp($bVal,$aVal) {
	return FCallbackAmp($aVal,0);
}

function FCallbackBlueAmp($bVal,$aVal) {
	return FCallbackAmp($aVal,1);
}

function FCallbackAmp($aVal, $team) {
	global $ampTimes;
	global $domimage_color;
	
	$size = 1;
	$color = $domimage_color['team'][$team][0];
		
	foreach ($ampTimes[$team] as $ampTaken) {
	
		if($aVal > $ampTaken[0] && $aVal < $ampTaken[1]) {
		
			$color = $domimage_color['amp'];
			$size = 3;
			break;
		}			
	}	
	
    return array($size,$color,$color);
}

/*
Helper function to sort array on key, based on solution from the interwebs
*/
function array_sort($array, $on) {
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        asort($sortable_array);            

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}
?>