public function show_Main()
 {
     $error = "";
     if ($this->get(1) == 'ok') {
         Framework::TPL()->assign('ok', true);
     }
     if (isset($_POST["change"])) {
         $auto = isset($_POST["automatic"]) && $_POST["automatic"] == 1 ? 1 : 0;
         $this->myPremium->auto = $auto;
         R::store($this->myPremium);
         Framework::redir('game/premium/main');
     }
     if (isset($_POST["activate"])) {
         if ($_SESSION['secHash'] != $_POST['hash']) {
             Framework::TPL()->assign('pError', 'invalid hash');
             return;
         }
         $length = array(1 => array(3, 150), 2 => array(7, 300), 3 => array(14, 400), 4 => array(30, 500));
         $l = @$_POST['length'];
         if (!isset($length[$l])) {
             Framework::TPL()->assign('pError', 'invalid length');
             return;
         }
         $details = $length[$l];
         if ($details[1] > $this->myPremium->points) {
             $error = "Du hast nicht genügend Premium-Punkte.";
         } else {
             $this->myPremium->points -= $details[1];
             if ($this->myPremium->until < time()) {
                 $this->myPremium->until = time() + $details[0] * 24 * 3600;
             } else {
                 $this->myPremium->until += $details[0] * 24 * 3600;
             }
             R::store($this->myPremium);
             $_SESSION['secHash'] = md5(mt_rand(1000, 10000) . microtime(true));
             RCached::clear();
             // clear cache files
             Framework::redir('game/premium/main/ok');
         }
     }
     Framework::TPL()->assign('pError', $error);
 }
 /**
  * check if given user has premium account
  */
 public function hasPremium($nocache = false)
 {
     if (isset(self::$hasPremiumCache[$this->id]) && !$nocache) {
         return self::$hasPremiumCache[$this->id];
     }
     if ($nocache) {
         $premium = R::findOne('user_premium', ' user_id = ?', array($this->id), date("d.m.Y - H"));
     } else {
         $premium = RCached::findOne('user_premium', ' user_id = ?', array($this->id), date("d.m.Y - H"));
     }
     if (!$premium) {
         self::$hasPremiumCache[$this->id] = false;
         return false;
     }
     if ($premium->until > time()) {
         self::$hasPremiumCache[$this->id] = true;
     } else {
         self::$hasPremiumCache[$this->id] = false;
     }
     return self::$hasPremiumCache[$this->id];
 }
    public function show_Update()
    {
        $maxPX = 4;
        $userID = $this->user->getID();
        // check if new x/y pos is a valid position
        if (!is_numeric($_POST['x']) || !is_numeric($_POST['y'])) {
            $this->error('Invalid position');
        }
        // new pos
        $x = $_POST['x'] < 0 ? 0 : $_POST['x'];
        $y = $_POST['y'] < 0 ? 0 : $_POST['y'];
        // old pos
        $om = $this->mapPosition;
        // you cant move to much at once
        $deltaX = abs($om->x - $x);
        $deltaY = abs($om->y - $y);
        // skip if player did not move
        if ($deltaX != 0 || $deltaY != 0) {
            if ($deltaX > $maxPX || $deltaY > $maxPX) {
                $this->output('player_position', array($om->x, $om->y));
                $this->error('moved_to_far');
            }
            // update to new pos
            $om->x = $x;
            $om->y = $y;
            foreach ($this->teleportMap[$om->map] as $teleportPoint) {
                if ($om->x == $teleportPoint["x"] && $om->y == $teleportPoint["y"]) {
                    // check if key is needed
                    if (isset($teleportPoint["keys"])) {
                        $array = $teleportPoint["keys"];
                        $ct = R::getCell('SELECT
											COUNT(inventory.id)
									FROM
										inventory, item
									WHERE
										item.type IN (' . R::genSlots($teleportPoint["keys"]) . ')
									AND
										inventory.user_id = ' . $this->user->getID(), $teleportPoint["keys"]);
                        if ($ct == 0) {
                            continue;
                        }
                    }
                    $om->map = $teleportPoint["target"]["map"];
                    $om->x = $teleportPoint["target"]["x"];
                    $om->y = $teleportPoint["target"]["y"];
                }
            }
            //R::store($om);
            R::exec('UPDATE map_position SET x = ?, y = ?, map = ? WHERE user_id = ?', array($om->x, $om->y, $om->map, $this->user->getID()));
        }
        $this->output('player_position', array($om->x, $om->y));
        $this->output('map', $om->map);
        // get surrounding players
        /*$players = R::find('map_position', ' map = ? AND user_id != ?', array(
        			$om->map,
        			$this->user->getID()
        		));*/
        // get surrounding npcs, cached
        $npcs = RCached::find('map_npc', ' map = ?', array($om->map), date('d.m.Y'));
        $p = array();
        $players = R::getAll('SELECT
			map_position.x as x,
			map_position.y as y,

			user.id as userid,
			user.username as username,
			user.characterImage as ucharacter,

			COUNT(session.id) as isOnline

		FROM

			map_position, user, session

		WHERE

			map_position.map = ? AND
			map_position.user_id != ? AND
			user.id = map_position.user_id AND
			session.user_id = user.id AND
			session.expires > ?', array($om->map, $this->user->getId(), time()));
        foreach ($players as $playerPos) {
            if ($playerPos['x'] == null) {
                continue;
            }
            $p['p' . $playerPos['userid']] = array('name' => $playerPos['username'], 'id' => $playerPos['userid'], 'x' => $playerPos['x'], 'y' => $playerPos['y'], 'character' => $playerPos['ucharacter'], 'is_npc' => false, 'look_direction' => 2, 'npc_type' => '');
        }
        /*
        foreach ($players as $playerPos) {
        	if (!$playerPos->user->isOnline()) {
        		continue;
        	}
        
        	$p['p'.$playerPos->user->id] = array('name' => $playerPos->user->username,
        										 'x' => $playerPos->x,
        										 'y' => $playerPos->y,
        										 'id' => $playerPos->user->id,
        										 'character' => $playerPos->user->characterImage,
        										 'is_npc' => false,
        									     'look_direction' => 2,
        									     'npc_type' => "");
        }
        */
        foreach ($npcs as $npc) {
            $p['n' . $npc->id] = array('name' => $npc->name, 'x' => $npc->x, 'y' => $npc->y, 'id' => $npc->id, 'character' => $npc->characterImage, 'is_npc' => true, 'look_direction' => $npc->lookDirection, 'npc_type' => $npc->type);
        }
        $this->output('players', $p);
    }