%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /www/old2/_music/diplomka/diplomka/src/API/app/model/
Upload File :
Create Path :
Current File : /www/old2/_music/diplomka/diplomka/src/API/app/model/Stats.php

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Waritko
 * Date: 15.5.13
 * Time: 23:03
 * To change this template use File | Settings | File Templates.
 */

/**
 * Class representing model for retrieving statistics from DB
 */
class Stats extends Base
{
    /** Method for retrieving maximum level of players
     * @return array Array of objects containing details about player level and when he was active last
     */
    public function getPlayersMaxLevel()
    {
        return $this->db->query("SELECT [name], [level], [side], [when] as [lastActive] FROM (select distinct [actions].[who] AS [player], max([actions].[level]) AS [level], max([actions].[when]) as [when] from [actions] where ([actions].[action] = 'resonator_create') group by [actions].[who]) as [player_max_level] JOIN [players] ON [player_max_level].[player] = [players].[id] order by [level] desc, [name]")->fetchAll();
    }

    /** Method for retrieving AP stats for single player
     * @param $playerName In-game nick of player
     * @param $range Time range to retrieve stats for
     * @param $city City to retrieve stats for
     * @return playerApInfo Information about player AP for selected range
     */
    public function getPlayerAp($playerName, $range, $city)
    {
        $start = $this->getRangeStart($range);
        $city = $this->verifyCity($city);
        $out = new playerApInfo();

        // get player ID
        $res = $this->db->query("select [id] from [players] where [name] like %s", $playerName)->fetchSingle();
        if($res != false)
        {
            $pid = $res;
            $sum = 0;
            $res = $this->db->query("select sum(ap) as created from actions where action in ('resonator_create', 'link_create', 'capture', 'field_create') AND `when` >= %i AND city=%s AND who=%i", $start, $city, $pid);
            if($r = $res->fetchSingle())
            {
                $out->created = (int)$r;
                $sum += $r;
            }
            $res = $this->db->query("select sum(ap) as destroyed from actions where action in ('resonator_destroy', 'link_destroy', 'field_destroy') AND `when` >= %i AND city=%s AND who=%i", $start, $city, $pid);
            if($r = $res->fetchSingle())
            {
                $out->destroyed = (int)$r;
                $sum += $r;
            }
            $out->total = $sum;
            // Funny details :D
            $res = $this->db->query("SELECT DISTINCT `action`, sum(`ap`) AS `ap` FROM `actions` WHERE `who` = %i AND `when` > %i AND `city` = %s GROUP BY `action`", $pid, $start, $city);
            foreach($res as $r)
            {
                $out->details->{$r->action} = $r->ap;
            }
        }
        return $out;
    }

    /** Gets overal AP statistics for all players active in given range
     * @param $range Time range to retrieve stats for
     * @param $city City to retrieve stats for
     * @return array Array of individual players stats
     */
    public function getPlayerApOverall($range, $city)
    {
        $start = $this->getRangeStart($range);
        $city = $this->verifyCity($city);
        $out = array();

        $res = $this->db->query("SELECT `action`, `who`, `ap`, `name`, `side` FROM ( SELECT `action`, `who`, `ap` FROM ( SELECT DISTINCT `action`, `who`, sum(`ap`) AS `ap` FROM `actions` WHERE `when` > %i AND `city` = %s GROUP BY `who`, `action` ) AS `act` ORDER BY `who` ) AS `ap_details` JOIN `players` ON `players`.`id` = `ap_details`.`who`", $start, $city);
        $helper = array();
        foreach($res as $r)
        {
            if(!isset($helper[$r->who]))
            {
                $helper[$r->who] = new playerApInfo();
                $helper[$r->who]->name = $r->name;
                $helper[$r->who]->side = $r->side;
            }
            $helper[$r->who]->total += $r->ap;
            if($r->action == 'resonator_destroy' || $r->action == 'field_destroy' || $r->action == 'link_destroy')
            {
                $helper[$r->who]->destroyed += $r->ap;
            }
            if($r->action == 'resonator_create' || $r->action == 'field_create' || $r->action == 'link_create' || $r->action == 'capture')
            {
                $helper[$r->who]->created += $r->ap;
            }
            $helper[$r->who]->details->{$r->action} = $r->ap;
        }
        // Data needs to be re-put into the out array so it's not hash table
        foreach($helper as $h)
        {
            $out[] = $h;
        }
        return $out;
    }

    /**
     * @param $playerName Name of player to retrieve statistics for
     * @param $action In-game action, for which to retrieve stats (now should be only resonator_create and resonator_destroy)
     * @param $range Time range to retrieve stats for
     * @param $city City to retrieve stats for
     * @return playerResonatorInfo Level statistics for given action
     */
    public function getPlayerResonatorAction($playerName, $action, $range, $city)
    {
        $start = $this->getRangeStart($range);
        $city = $this->verifyCity($city);
        $play = new playerResonatorInfo();

        // get player ID
        $res = $this->db->query("select [id], [name], [side] from [players] where [name] like %s", $playerName)->fetch();
        if($res != false)
        {
            $pid = $res->id;
            $play->name = $res->name;
            $play->side = $res->side;
            $res = $this->db->query("select distinct `level`, count(`level`) as `count` from `actions` where `who`=%i and `action`=%s AND `city`=%s AND `when` > %i group by `level` order by `level` asc", $pid, $action, $city, $start);
            $sum = 0;
            foreach($res as $r)
            {
                $play->resonators[] = $r;
                $sum += $r->count;
            }
            $total = new stdClass();
            $total->level = 'Total';
            $total->count = $sum;
            $play->resonators[] = $total;
        }
        return $play;
    }

    /** Gets last action of given player
     * @param $playerName In-game nick of player
     * @param $range Time range to retrieve actions for
     * @param $city City to retrieve actions for
     * @return array Array of actions sorted by action time in descending order
     */
    public function getPlayerLastActions($playerName, $range, $city)
    {
        $start = $this->getRangeStart($range);
        $city = $this->verifyCity($city);

        // get player ID
        $res = $this->db->query("select [id] from [players] where [name] like %s", $playerName)->fetchSingle();
        if($res != false)
        {
            $pid = $res;
            $res = $this->db->query("select `players`.`name`, `action`, `when`, `level`, `ap`, `latitude`, `longitude` from `actions` join `players` on `players`.`id`=`actions`.`who` where `who`=%i AND `when` >= %i AND `city`=%s order by `when` desc", $pid, $start, $city);
            return $res->fetchAll();
        }
        return array();
    }



    /** Converts text representation of range into unix timestamp
     * @param $range Range to be converted
     * @return int Unix timestamo with range start time
     */
    protected function getRangeStart($range)
    {
        switch($range)
        {
            case 'day':
            {
                return time()-3600*24;
            }
            case '2day':
            {
                return time() - 3600 * 48;
            }
            case 'week':
            {
                return time() - 3600 * 24 * 7;
            }
            case 'month':
            {
                return time() - 3600 * 24 * 30;
            }
            case 'all':
            {
                return 0;
            }
            case 'hour':
            {
                return time() - 3600;
            }

            default:
            {
                return 0;
            }
        }
    }

    /** Verifies city name and if invalid, returns Brno
     * @param $city City name to be checked
     * @return string Verified city name
     */
    protected function verifyCity($city)
    {
        if($city == 'Brno' || $city == 'Kromeriz' || $city == 'Bratislava' || $city == "Ostrava")
            return $city;
        return 'Brno';
    }
}

Zerion Mini Shell 1.0