diff options
author | Matthijs Kuiper <info@matthijskuiper.nl> | 2018-01-08 23:47:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-08 23:47:23 +0100 |
commit | e861eafbeae0560add7956530d76de6cc285be20 (patch) | |
tree | c32e746977fde711cecd716570989014bf2d3592 /html/includes/renderer-dom.php | |
parent | 66b162b5f01c996fd8a5cd4e570e31f8483cdffa (diff) | |
parent | 730c20dd04b5753cb372a289923e39516d1f8b87 (diff) |
Merge pull request #5 from randommonk/develop4.3.0
Bumped version to 4.3. Added jpgraph stats developed by killerEye
Diffstat (limited to 'html/includes/renderer-dom.php')
-rw-r--r-- | html/includes/renderer-dom.php | 396 |
1 files changed, 396 insertions, 0 deletions
diff --git a/html/includes/renderer-dom.php b/html/includes/renderer-dom.php new file mode 100644 index 0000000..d481695 --- /dev/null +++ b/html/includes/renderer-dom.php @@ -0,0 +1,396 @@ +<?php + +require_once 'renderer-general-import.php'; + +/* +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; + + $uid = mysql_real_escape_string($uid); + $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') AND col0>".mysql_real_escape_string($time_gamestart)." 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 = mysql_real_escape_string($r_logdom['col2']); + $r_time = mysql_real_escape_string($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 = mysql_real_escape_string($r_logdom['col1']); + $r_pid = mysql_real_escape_string($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(); + + $uid = mysql_real_escape_string($uid); + + // 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 $renderer_color; + + $uid = mysql_real_escape_string($uid); + + // Iterate over amp runs + for($i=0;$i<2;$i++) { + foreach ($ampTimes[$i] as $ampTaken) { + $ampStart = mysql_real_escape_string($ampTaken[0]); + $ampEnd = mysql_real_escape_string($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,18)." (".$timeLabel.")"; + $ampRun['points'] = round($netPoints,0); + $ampRun['team'] = $i; + + $ampRuns[] = $ampRun; + } + } + } + + if(count($ampRuns)>0) { + + // Sort ampruns not on teams, but on start time, to make sure they are plotted chronocologicstic + $ampRuns = array_sort($ampRuns,'start'); + + $data = array(); + $labels = array("Net Points"); + $categories = array(); + + // Process the ampruns to map it to bar plots + foreach ($ampRuns as $ampRun) { + $categories[] = $ampRun['label']; + $data[] = array("y" => $ampRun['points'], "color" => $renderer_color['team'][$ampRun['team']][0]); + } + + // Save team score over team for teams + mysql_query("INSERT INTO uts_chartdata (mid,chartid,data,labels,categories) VALUES (".$matchid.", ".RENDERER_CHART_ITEMS_AMPRUNS.", + '".mysql_real_escape_string(gzencode(serialize(array($data))))."', + '".mysql_real_escape_string(gzencode(serialize($labels)))."', + '".mysql_real_escape_string(gzencode(serialize($categories)))."')") or die(mysql_error()); + + } + + +} + +/* +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 = mysql_real_escape_string($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_cps[0],$r_cps[0]); + + } +} + +/* +Render dom points over time & net rate of change per minute +*/ +function renderData($uid,$tempTableName,$query,$appex,$title,$cp) { + global $matchid; + + // Use helper function to parse the data into different datasets for charts + list($dompoints,$derivdompoints) = parseData($query); + $title = "Total"; + + if(count($dompoints) > 0 && count($dompoints[0]) > 0 && count($dompoints[1]) > 0) { + + + // If this is a cp plot, use player names as label. Otherwise simply teamname + if(strlen($cp) > 0) { + $labels = renderNamesCP($tempTableName,$cp); + $title = $cp; + } else { + $labels = generateTeamLabels(); + + } + + // Deriv Dom points over time per team + mysql_query("INSERT INTO uts_chartdata (mid,chartid,title,data,labels) VALUES (".$matchid.", ".RENDERER_CHART_DOM_SCOREDERIV.", + '".mysql_real_escape_string($title)."', + '".mysql_real_escape_string(gzencode(serialize(array($derivdompoints,$dompoints))))."', + '".mysql_real_escape_string(gzencode(serialize($labels)))."')") or die(mysql_error()); + + } +} + +/* +Helper function to parse the data for the main renderData +*/ +function parseData($query) { + $q_result = mysql_query($query) or die(mysql_error()); + + $smallcounter = 1/3; + $counter = 1; + $prevpoints = array(0,0); + $prevtime = 0; + + $run = 0; + $numRows = mysql_num_rows($q_result); + + while($data = mysql_fetch_array($q_result)) { + $r_time = round($data[0],2); + if($run==($numRows-1)) + $r_time = $prevtime; + + $points[0] = round($data[1],0); + $points[1] = round($data[2],0); + + // Prep data for dom points over time + // Only save the data each $smallcounter interval, to optimize rendering time + if($r_time >= $smallcounter || $run==($numRows-1)) { + + for($i=0;$i<2;$i++) { + $dompoints[$i][] = array($r_time,$points[$i]); + } + + $smallcounter+=1/3; + } + + // Prep data for net change over time + // Only save the data each integer interval, since we want the value per minute + if($r_time >= $counter || ($run==($numRows-1) && ($counter-$r_time)<(1/3))) { + + // Iterate over teams to get points last min + for($i=0;$i<2;$i++) { + $derivypre[$i] = $points[$i]-$prevpoints[$i]; + } + + // Iterate over teams again to determine net points + for($i=0;$i<2;$i++) { + $derivypost[$i] = $derivypre[$i]-$derivypre[1-$i]; + $derivdompoints[$i][] = $derivypost[$i]>0 ? $derivypost[$i] : 0; + } + + $prevpoints = $points; + + $counter++; + } + + $prevtime = $r_time; + $run++; + } + + + return array($dompoints,$derivdompoints); +} + +/* +Render the names of players that played the cp +*/ +function renderNamesCP($tempTableName,$cp) { + $labels = array(); + + $q_namesPerCP = mysql_query("SELECT playername, COUNT( playername ) AS cplayer, MAX( teamid ) AS tid, AVG( realTimeEnd ) AS ati FROM $tempTableName WHERE cp = '".mysql_real_escape_string($cp)."' GROUP BY playername ORDER BY tid,ati") or die(mysql_error()); + + while($r_namesPerCP = mysql_fetch_array($q_namesPerCP)) { + + $playerName = substr($r_namesPerCP[0],0,18); + $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(isset($playerName) && !empty($playerName) && strlen($playerName)>0 && $timesTouched > 75) { + if(strlen($labels[$teamid])>0) + $labels[$teamid] .= " + "; + + $labels[$teamid] .= $playerName; + } + + } + + // Default value if empty + if(!isset($labels[0]) || !strlen($labels[0])>1) + $labels[0] = 'Red Team'; + if(!isset($labels[1]) || !strlen($labels[1])>1) + $labels[1] = 'Blue Team'; + + return $labels; +} + + +?>
\ No newline at end of file |