- <?php
- /**
- * Class HeatMap API to generate/add/update/delete content for HeatMaps.
- */
- class HeatMap
- {
- public $version = "0.5";
- private $db;
- /**
- * Stuff we want to fire when we start.
- */
- public function __construct()
- {
- global $argv;
- $this->db = new DB(DB_HOST, DB_USER, DB_PASS, DB_NAME);
- $this->mapInfo();
- $this->parseArguments($argv);
- }
- /**
- * Function that builds the maplist from db elements.
- */
- public function mapInfo()
- {
- $query = "
- SELECT
- g.code,
- hc.code,
- hc.map,
- hc.xoffset,
- hc.yoffset,
- hc.flipx,
- hc.flipy,
- hc.rotate,
- hc.days,
- hc.brush,
- hc.scale,
- hc.font,
- hc.thumbw,
- hc.thumbh,
- hc.cropx1,
- hc.cropx2,
- hc.cropy1,
- hc.cropy2
- FROM hlstats_codes AS g
- INNER JOIN hlstats_Heatmap_Config AS hc
- ON hc.code = g.realcode
- WHERE 1=1
- ORDER BY code ASC, code ASC, map ASC";
- $result = $this->db->doQuery($query);
- $mapInfo = [];
- if ($this->db->numRows($result)) {
- while ($row = $this->db->getAssoc($result)) {
- foreach ($row as $key => $val) {
- $mapInfo[$row['code']][$row['map']][$key] = $val;
- }
- }
- Env::set('mapinfo', $mapInfo);
- }
- }
- /**
- * Print heat dot
- *
- * @param $dst_im
- * @param $src_im
- * @param $dst_x
- * @param $dst_y
- * @param $src_x
- * @param $src_y
- * @param $src_w
- * @param $src_h
- * @param $pct
- * @return bool
- */
- private function printHeatDot($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
- {
- return false;
- }
- $pct /= 100;
- // Get image width and height
- // Turn alpha blending off
- // Find the most opaque pixel in the image (the one with the smallest alpha value)
- $minAlpha = 127;
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- if ($alpha < $minAlpha) {
- $minAlpha = $alpha;
- }
- }
- }
- //loop through image pixels and modify alpha for each
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- //get current alpha value (represents the TANSPARENCY!)
- $alpha = ($colorXY >> 24) & 0xFF;
- //calculate new alpha
- if($minAlpha !== 127) {
- $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minAlpha);
- } else {
- $alpha += 127 * $pct;
- }
- //get the color index with new alpha
- $alphaColorXY = imagecolorallocatealpha($src_im, ($colorXY >> 16) & 0xFF, ($colorXY >> 8) & 0xFF, $colorXY & 0xFF, $alpha);
- //set pixel with the new color + opacity
- return false;
- }
- }
- }
- // The image copy
- }
- /**
- * Generate HeatMap
- *
- * @param $code
- * @param $map
- * @param $mode
- * @return bool
- */
- public function generate($code, $map, $mode)
- {
- // generatemap($map, $code, "Total Kills", "HlstatsX:CE")
- $this->buildQuery($code, $map);
- $mapinfo = Env::get('mapinfo');
- $map_query = Env::get('map_query');
- $disable_cache = Env::get('disable_cache');
- // See if we need to rotate the map or not.
- $rotate = $mapinfo[$code][$map]['rotate'];
- // Fix the last part of your path.
- $path = HLXCE_WEB . "/hlstatsimg/codes/" . $mapinfo[$code][$map]['code'] . "/heatmaps";
- Show::Event("PATH", $path, 3);
- // Does the source image exists? else there is no idea to spend resources on it.
- if (!file_exists(dirname(__FILE__) . "/src/" . $mapinfo[$code][$map]['code'] . "/" . $map . ".jpg")) {
- Show::Event("FILE", dirname(__FILE__) . "/src/" . $mapinfo[$code][$map]['code'] . "/" . $map . ".jpg doesn't exists", 3);
- return false;
- }
- // Check that the dir exists, else try to create it.
- Show::Event("PREPARE", "Couln't create outputfolder: $path", 1);
- }
- }
- // Check if we have cached info, then we should work from that instead.
- if ($file != "." && $file != ".." && preg_match(str_replace("\$","\\\$","/${map}_(\d+).png/i"), $file, $matches)) {
- $cache_file = CACHE_DIR . "/$code/$file";
- $oldTimestamp = $matches[1];
- // unless it's over 30 days old cache file, then we delete it and go from 0 again.
- $obsolite_cache = true;
- }
- // If we called with --disable-cache we want to clean up and then regen from our start.
- $disable_cache = true;
- }
- } else {
- Show::Event("CACHE","Found cached file ($file), we will use timestamp $oldTimestamp instead", 1);
- $find = '/.*AND hef.eventTime >= FROM_UNIXTIME\([0-9]+\).*/i';
- $replace = ' AND hef.eventTime > FROM_UNIXTIME(' . $oldTimestamp . ')';
- }
- }
- }
- }
- } else {
- Show::Event("CACHE", "Can't create cache_dir: " . CACHE_DIR . "/$code", 1);
- }
- }
- $result = $this->db->doQuery($map_query);
- $num_kills = $this->db->numRows($result);
- if (!$num_kills) {
- Show::Event("IGNORE", "code: $code, Map: $map, Kills: $num_kills, (to few kills)", 1);
- return false;
- }
- } else {
- }
- $brushSize = ($mapinfo[$code][$map]['brush'] == "large") ? 33 : 17;
- $num_kills = ($num_kills) ? $num_kills : 1;
- Show::Event("CREATE", "code: $code, Map: $map, Kills: $num_kills", 1);
- if ($opacity > 40) {
- $opacity = 40;
- }
- if ($opacity < 1) {
- $opacity = 2;
- }
- $max_red = 0;
- $i = 0;
- while ($row = $this->db->getAssoc($result)) {
- if ($row['eventTime'] < $firstData) {
- $firstData = $row['eventTime'];
- }
- if ($mapinfo[$code][$map]['flipx']) {
- $row['pos_x'] = $row['pos_x'] * -1;
- }
- if ($mapinfo[$code][$map]['flipy']) {
- $row['pos_y'] = $row['pos_y'] * -1;
- }
- $x = ($row['pos_x'] + $mapinfo[$code][$map]['xoffset']) / $mapinfo[$code][$map]['scale'];
- $y = ($row['pos_y'] + $mapinfo[$code][$map]['yoffset']) / $mapinfo[$code][$map]['scale'];
- if ($colors['red'] > $max_red) $max_red = $colors['red'];
- if ($colors['red'] <= 200) {
- // Rotate the image
- if ($rotate) {
- $this->printHeatDot($overlay, $brush, $y - ($brushSize / 2), $x - ($brushSize / 2), 0, 0, $brushSize, $brushSize, $opacity);
- } else {
- $this->printHeatDot($overlay, $brush, $x - ($brushSize / 2), $y - ($brushSize / 2), 0, 0, $brushSize, $brushSize, $opacity);
- }
- }
- }
- for ($line = 0; $line < 128; ++$line) {
- $colorArr[$line] = $colors;
- }
- for ($line = 128; $line < 255; ++$line) {
- $colorArr[$line] = $colors;
- }
- $color = imagecolorallocatealpha($img, $colorArr[$rgb['red']][0], $colorArr[$rgb['red']][1], $colorArr[$rgb['red']][2], 127 - ($rgb['red'] / 2));
- echo ".";
- }
- }
- }
- if ($mapinfo[$code][$map]['cropy2'] > 0 && $mapinfo[$code][$map]['cropy2'] > 0) {
- imagecopy($temp, $img, 0, 0, $mapinfo[$code][$map]['cropx1'], $mapinfo[$code][$map]['cropy1'], $mapinfo[$code][$map]['cropx2'], $mapinfo[$code][$map]['cropy2']);
- }
- if ($mapinfo[$code][$map]['thumbw'] > 0 && $mapinfo[$code][$map]['thumbh'] > 0) {
- $thumb = imagecreatetruecolor(imagesx($img) * $mapinfo[$code][$map]['thumbw'], imagesy($img) * $mapinfo[$code][$map]['thumbh']);
- imagecopyresampled($thumb, $img, 0, 0, 0, 0, imagesx($thumb), imagesy($thumb), imagesx($img), imagesy($img));
- }
- $img = self::drawHud($img, $map, "HLX:CE", "Total Kills", $num_kills, $firstData);
- $return = false;
- $return = true;
- }
- $return = true;
- }
- // Clean upc cache file
- }
- return $return;
- }
- /**
- * Build map SQL query
- *
- * @param $code
- * @param $map
- */
- public function buildQuery($code, $map)
- {
- $mapInfo = Env::get('mapinfo');
- Env::set('code', $code);
- $ignore_infected = Env::get('ignore_infected');
- $map_query = "
- SELECT
- 'frag' AS killtype,
- hef.id,
- hef.map,
- hs.code,
- hef.eventTime,
- hef.pos_x,
- hef.pos_y
- FROM
- hlstats_Events_Frags as hef,
- hlstats_Servers as hs
- WHERE 1=1
- AND hef.map = {$map}
- AND hs.serverId = hef.serverId
- AND hs.code = {$code}
- AND hef.pos_x IS NOT NULL
- AND hef.pos_y IS NOT NULL
- AND hef.eventTime >= FROM_UNIXTIME(' . $timeScope . ')";
- if ($ignore_infected) {
- $map_query.= " AND hef.victimRole != 'infected'";
- }
- $map_query.= " LIMIT 1000
- UNION ALL
- SELECT
- 'teamkill' AS killtype,
- hef.id,
- hef.map,
- hs.code,
- hef.eventTime,
- hef.pos_x,
- hef.pos_y
- FROM
- hlstats_Events_Teamkills as hef,
- hlstats_Servers as hs
- WHERE 1=1
- AND hef.map = {$map}
- AND hs.serverId = hef.serverId
- AND hs.code = {$code}
- AND hef.pos_x IS NOT NULL
- AND hef.pos_y IS NOT NULL
- AND hef.eventTime >= FROM_UNIXTIME({$timeScope})
- LIMIT 1000";
- Env::set('map_query', $map_query);
- Show::Event("SQL", $map_query, 3);
- }
- /**
- * Draw HUD
- *
- * @param $img
- * @param $map
- * @param $heatmapname
- * @param $method
- * @param $num_kills
- * @param $firstdata
- * @return false|resource
- */
- private function drawHud($img, $map, $heatmapname, $method, $num_kills, $firstdata)
- {
- $mapInfo = Env::get('mapinfo');
- $code = Env::get('code');
- // Resize the image according to your settings
- $img = $this->resize($img);
- $hudText = [
- HUD_URL
- ];
- Show::Event("HUD", "Creating Overlay HUD", 2);
- $hud = imagecreatetruecolor(imagesx($img), imagesy($img)) or die ('Cannot Initialize new GD image stream');
- $font = "./DejaVuSans.ttf";
- // Copy the hud to the top of the image.
- //array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
- $i = 1;
- foreach ($hudText as $text) {
- $mapInfo[$code][$map]['font'],
- 0,
- 10,
- $font,
- $text
- );
- $i++;
- }
- Show::Event("HUD", "Done...", 2);
- return $img;
- }
- /**
- * Resize Image
- *
- * @param $img
- * @return false|resource
- */
- private function resize($img)
- {
- switch (OUTPUT_SIZE) {
- case "small":
- $newWidth = 800;
- $newHeight = 600;
- break;
- case "medium":
- $newWidth = 1024;
- $newHeight = 768;
- break;
- case "large":
- $newWidth = 1280;
- $newHeight = 1024;
- // As for now we don't do anything since this is default size
- return $img;
- break;
- default:
- $newWidth = 1024;
- $newHeight = 768;
- }
- Show::Event("RESIZE", "Adjusting Heatmap to current setting: " . OUTPUT_SIZE, 2);
- Show::Event("RESIZE", "Done...", 2);
- return $resized;
- }
- /**
- * Command line arguments
- *
- * @param $argv
- * @return array
- */
- private function arguments($argv)
- {
- $_ARG = [];
- foreach ($argv as $arg) {
- $arg = '';
- $arg .= $str[$i];
- }
- $_ARG[$key] = $arg;
- $_ARG[$arg] = 'true';
- }
- }
- return $_ARG;
- }
- /**
- * Parse command line arguments
- *
- * @param $argv
- */
- private function parseArguments($argv)
- {
- $mapInfo = Env::get('mapinfo');
- $cache = false;
- $args = $this->arguments($argv);
- Show::Event("ERROR", "code: " . $args['code'] . " doesn't exists, escaping", 1);
- exit;
- }
- Show::Event("ERROR", "code: " . $args['code'] . " Map: " . $args['map'] . " doesn't exists, escaping", 1);
- exit;
- }
- $tmp[$args['code']][$args['map']] = $mapInfo[$args['code']][$args['map']];
- Show::Event("ARGS", "--code=" . $args['code'], 2);
- Show::Event("ARGS", "--map=" . $args['map'], 2);
- } else {
- $tmp[$args['code']] = $mapInfo[$args['code']];
- Show::Event("ARGS", "--code=" . $args['code'], 2);
- }
- } else {
- $visible = '';
- $query = "SELECT code FROM ${DB_PREFIX}_codes WHERE hidden='0'";
- $result = $this->db->doQuery($query);
- if ($this->db->numRows($result)) {
- while ($row = $this->db->getAssoc($result)) {
- foreach ($row as $key => $val) {
- $visible .= "$val, ";
- $tmp[$val] = $mapInfo[$val];
- }
- }
- }
- }
- }
- $mapInfo = $tmp;
- }
- $cache = true;
- Show::Event("ARGS", "--disable-cache=true", 2);
- } else {
- $cache = false;
- Show::Event("ARGS", "--disable-cache=false", 2);
- }
- $ignore_infected = true;
- Show::Event("ARGS", "--ignore-infected=true", 2);
- } else {
- $ignore_infected = false;
- Show::Event("ARGS", "--ignore-infected=false", 2);
- }
- Env::set('mapinfo', $mapInfo);
- Env::set('disable_cache', $cache);
- Env::set('ignore_infected', $ignore_infected);
- }
- }
- /**
- * Class DB
- */
- class DB
- {
- private $link;
- public function __construct($host, $user, $pass, $dbName)
- {
- Show::Event("DB", "Connected to " . DB_NAME . " as " . DB_USER . "@" . DB_HOST, 1);
- }
- public function doQuery($query)
- {
- }
- public function getAssoc($result)
- {
- }
- public function numRows($result)
- {
- }
- }
- /**
- * Class Env holds Environmental vars that can be used anywhere in the code
- * For example the database object to perform mysql stuff
- */
- class Env
- {
- private static $data = [];
- /**
- * Add the specified key=>value to the environmental data array
- *
- * @param string $key this is the identifier of the value you are adding
- * @param string|array $value The value to add into the array
- */
- public static function set($key, $value)
- {
- self::$data[$key] = $value;
- }
- /**
- * Gets the current value from the data array
- *
- * @param string $key The key to lookup in the array
- * @return mixed null if the key cannot be found, or the value that was stored in the array
- */
- public static function get($key)
- {
- }
- }
- /**
- * Class Show for console output
- */
- class Show
- {
- public static function Event ($type, $text, $runLevel)
- {
- if ($runLevel <= DEBUG) {
- }
- }
- }
Recent Pastes