Trackmania TMF KNOCKOUT PLUGIN help needed!

Discuss everything about Xymph's Aseco flavoured server control scripts for TM Forever / classic TMN and for TM² Canyon.

Moderators: Xymph, TM-Patrol

Post Reply
bajszatlan
highway camper
highway camper
Posts: 3
Joined: 10 Dec 2015 21:11
Owned TM-games: tmuf, + all tm2

Trackmania TMF KNOCKOUT PLUGIN help needed!

Post by bajszatlan » 21 May 2017 09:37

Hi!
I have a plugin that is 99% functional, but i have a problem with it.
in rounds mode, sometimes replaces the time of the finished players.
example: 1st place: 9.80, 2nd place: 9.85, 3rd place: 9.81, 4th place:9.90....

I hope someone can help with that.Maybe just a bad callback.

Here's a source of the plugin:

Code: Select all

<?php

namespace AsecoNC {
	abstract class Record {
		var $map,
			$score,
			$date,
			$checkpoints,
			$new,
			$position;
		/** @var Player */
		var $player;
	}
	abstract class Player {
		var $id,
			$pid,
			$login,
			$nickname,
			$language,
			$avatar,
			$clublink,
			$ip,
			$port,
			$downloadrate,
			$uploadrate,
			$is_official,
			$is_referee,
			$is_podium_ready,
			$is_using_stereoscopy,
			$is_managed_by_other_server,
			$is_server,
			$is_broadcasting,
			$has_joined_game,
			$has_player_slot,
			$is_spectator,
			$forced_spectator,
			$temporary_spectator,
			$pure_spectator,
			$target_autoselect,
			$target_spectating,
			$teamid,
			$allies,
			$ladderrank,
			$ladderscore,
			$lastmatchscore,
			$nbwins,
			$nbdraws,
			$nblosses,
			$client,
			$created,
			$zone_inscription,
			$zone,
			$continent,
			$nation,
			$visits,
			$wins,
			$newwins,
			$timeplayed,
			$data,
			$unlocked,
			$pmbuf,
			$mutelist,
			$mutebuf,
			$style,
			$panels,
			$panelbg,
			$speclogin,
			$dedirank,
			$maplist,
			$playerlist,
			$msgs;
	}
}
namespace NC {
	class Player implements \ArrayAccess {
		const PRIMARY_KEY = 'login';

		public $login, $nick, $score, $pos, $restarts;

		function __construct($arr){
			foreach ($arr as $k => $v){
				if (!property_exists($this, $k)){
					throw new \Exception("Unknown property $k in ".__METHOD__);
				}
				$this->{$k} = $v;
			}
		}

		public function offsetSet($offset, $value){
			if (!property_exists($this, $offset)){
				throw new \Exception("Unknown property $offset in ".__METHOD__);
			}

			$this->{$offset} = $value;
		}

		public function offsetExists($offset){
			return isset($this->{$offset});
		}

		public function offsetUnset($offset){
			unset($this->{$offset});
		}

		public function offsetGet($offset){
			return isset($this->{$offset}) ? $this->{$offset} : null;
		}

		/** @return string */
		public function __toString(){
			return $this->nick;
		}
	}
	class PlayerList implements \Countable, \Iterator, \ArrayAccess {
		private $primaryKey = Player::PRIMARY_KEY;
		/** @var \NC\Player[] */
		private $list = array();

		/**
		 * @param Player $player
		 */
		public function add(Player $player){
			$this->offsetSet($player->login, $player);
		}

		/**
		 * @param string|\NC\Player $key
		 */
		public function remove($key){
			if ($key instanceof Player)
				$key = $key->{Player::PRIMARY_KEY};
			if ($this->contains($key)){
				unset($this->list[$key]);
			}
		}

		/**
		 * @param string|\NC\Player $key
		 *
		 * @return bool
		 */
		public function contains($key){
			if ($key instanceof Player)
				$key = $key->{Player::PRIMARY_KEY};
			return $this->offsetExists($key);
		}

		public function keys(){
			return array_keys($this->list);
		}

		public function sort(callable $function){
			return uasort($this->list, $function);
		}

		public function resetRestarts(){
			foreach ($this->list as $player){
				$player->restarts = 0;
			}
		}

		public function wipe(){
			$this->list = [];
		}

		public function last(){
			return array_slice($this->list, -1)[0];
		}

		public function isEmpty(){
			return empty($this->list);
		}

		public function count(){
			return count($this->list);
		}

		public function rewind(){
			reset($this->list);
		}

		public function current(){
			return current($this->list);
		}

		public function key(){
			return key($this->list);
		}

		public function next(){
			return next($this->list);
		}

		public function valid(){
			$key = $this->key();

			return ($key !== null && $key !== false);
		}

		public function offsetSet($offset, $value){
			if (is_null($offset)){
				throw new \Exception('Missing $offset for '.__METHOD__);
			}

			$this->list[$offset] = $value;
		}

		public function offsetExists($offset){
			return isset($this->list[$offset]);
		}

		public function offsetUnset($offset){
			unset($this->list[$offset]);
		}

		public function offsetGet($offset){
			return isset($this->list[$offset]) ? $this->list[$offset] : null;
		}
	}
	class FinishedPlayerList extends PlayerList {}
	class CustomPoints {
		/** @var array */
		private $pointArray;

		function __construct($array = null){
			$this->pointArray = array_fill(0, 49, 0);
			if (!empty($array)){
				foreach ($array as $i => $el){
					$this->pointArray[$i] = $el;
				}
			}
		}

		function toArray(){
			return $this->pointArray;
		}
	}
}
namespace {
	// ************************************************
	// 	KO plugin (ex GoalRace) 2016.09.24
	// ************************************************
	$events = array('Startup','PlayerFinish','EndRound','NewChallenge','BeginRound','PlayerConnect','PlayerGiveUp');
	foreach ($events as $event)
		Aseco::registerEvent("on$event", "pts_$event");
	Aseco::registerEvent('onPlayerManialinkPageAnswer', 'event_ko');
	Aseco::addChatCommand('ko', 'Goal Race control commands');
	Aseco::registerEvent('onPlayerManialinkPageAnswer', 'koButtonHandleClick');

	//rounds panel
	foreach(array('onBeginRound','onNewChallenge','onEndRace') as $event)
		Aseco::registerEvent($event, "pts_panel_off");

	/** @var $pts PointSystem */
	global $pts;
	global $ko_admins;

	$ko_admins = array("mobulla", "bakterhaz1982"); // ADMINS

	// Initialize
	function pts_Startup($aseco, $command){
		global $aseco;
		global $pts;

		$pts = new PointSystem($aseco);
	}
	function pts_PlayerFinish($aseco, $record){
		global $pts;
		$pts->PlayerFinish($record);
	}
	function pts_PlayerGiveUp($aseco, $login){
		global $pts;
		$pts->PlayerGiveUp($login);
	}
	function pts_NewChallenge($aseco, $challenge){
		global $pts;
		$pts->NewChallenge();
	}
	function pts_EndRound($aseco, $challenge){
		global $pts;
		$pts->EndRound();
	}
	function pts_PlayerConnect($aseco, $player){
		global $pts;
		$pts->PlayerConnect($player);
	}
	function pts_BeginRound($aseco, $challenge){
		global $pts;
		$pts->BeginRound();
	}

	function koButtonHandleClick($aseco, $command){
		global $pts;
		global $ko_admins;

		$login = $command[1];
		$action = strval($command[2]);
		if (!in_array($login, $ko_admins)){  // if not admin throw error and exit
			$pts->sendMessageTo($login, "Elnézést kérünk, de ez a parancs csak adminoknak működik.");

			return;
		}
		switch ($action) {
			case "97008503":
				playerList();
			break;
			case '97008505':
				addremovePlayers($aseco, $login);
			break;
			case '97008506':
				$pts->startGame();
			break;
			case '97008507':
				$pts->stopGame();
			break;
			case '97008508':
				buttons_off($login);
			break;
		}
	}

	function buttons_off($login){
		global $pts;
		$pts->sendMessageTo($login, "Gombok kikapcsolva, a /ko on ismét bekapcsolja.");
		for ($i = 2; $i <= 5; $i++){
			$iinc = $i + 3;
			$button = <<<XML
	<manialink id="08154700001{$i}">
	<frame posn="0 0 0">
	<quad posn="0 0 0" sizen="0 0" halign="center" valign="center" action="9700850{$iinc}" /> 
	</frame>
	</manialink>
XML;
			$pts->maniaLinkPage($button, 0);
		}
	}

	function chat_ko($aseco, $command){
		global $pts;
		global $ko_admins;

		$player = $command['author'];
		$login = $player->login;
		$params = preg_split('/\s+/', $command['params']);

		if (!in_array($login, $ko_admins)){  // if not admin throw error and exit
			$pts->sendMessageTo($login, "Elnézést kérünk, de ez a parancs csak adminoknak működik.");

			return;
		}

		switch ($params[0]) {
			case "go":
				$pts->startGame();
			break;
			case "on":
				$texts = [
					'KO Játékosok',
					'KO Start',
					'KO Stop',
					'KO Ki',
				];
				for ($i = 2; $i <= 5; $i++){
					$iinc = $i + 3;
					$zeroi = $i - 2;
					$el = $texts[$zeroi];
					$pos = -58 + ($zeroi * 8);

					$button = <<<XML
<manialink id="081547000012{$i}">
<format style="TextCardInfoSmall" textsize="1" />
<frame posn="{$pos} -30 1">
<quad posn="0 0 0" sizen="8 2.5"  halign="center" valign="center" style="Bgs1InRace" substyle="NavButton"  />
<label posn="0 0.2 1" sizen="8 2" halign="center" valign="center" text="\$i\$s\$fff{$el}" action="9700850{$iinc}"/>
</frame>
</manialink>
XML;
					$pts->maniaLinkPageTo($login, $button, 0);
				}
				break;
			case "off":
				buttons_off($login);
				break;
			case "stop":
				$pts->stopGame();
				break;
			case "pl":
				addremovePlayers($aseco, $login);
				break;
			case "help":
				ko_help($aseco, $command);
				break;
			case "remove":
			case "add":
				if (!isset($params[1])){
					$pts->sendMessageTo($login, "Adj meg legaláb 1 játékos nevet!");

					return;
				}

				if ($pts->GameEnabled){
					$pts->sendMessage("Race is going, you can't {$params[0]} players now.");

					return true;
				}

				$method = "$params[0]Player";
				for ($x = 1; $x < count($params); $x++){ // if more than 1 player, add them all
					$pts->{$method}($params[$x]);
				}
				break;
			case "restart":
				if (!isset($params[1])){
					$pts->sendMessageTo($login, "Hiányzó paraméter!");

					return;
				}

				if ($pts->GameEnabled){
					$pts->sendMessageTo($login, "Race is going, you can't change mode now.");

					return true;
				}

				switch (trim($params[1])) {
					case "true":
					case "yes":
					case "igen":
						$pts->restartEnabled = true;
						break;
					case "false":
					case "no":
					case "nem":
						$pts->restartEnabled = false;
						break;
					default:
						$pts->sendMessageTo($login, "Érvénytelen paraméter {$params[1]}");

						return true;
				}

				$pts->sendMessage('Restart is now '.($pts->restartEnabled ? 'true' : 'false'));
				break;
		}
	}

	function addremovePlayers($aseco, $command){
		global $pts;

		/** @var $player \AsecoNC\Player */
		foreach ($aseco->server->players->player_list as $player){
			$player->playerlist[] = array('login' => $player->login);
			$pts->addPlayer($player->login);
		}
	}

	function event_ko($aseco, $answer){
		global $pts;

		list($uid, $login, $action) = $answer;
		// leave actions outside 40001 - 40200 to other handlers
		if ($action >= 40001 && $action <= 40200){
			// get player
			$player = $aseco->server->players->getPlayer($login);
			$target = $player->playerlist[$action - 40001]['login'];

			if (isset($pts->playerList[$target])){
				$pts->removePlayer($target);
				addremovePlayers($aseco, $login);
			}
			else {
				$pts->addPlayer($target);
				addremovePlayers($aseco, $login);
			}
		}
	}

	function ko_help($aseco, $command){
		$player = $command['author'];
		$login = $player->login;
		$header = 'KNOCKOUT MODE parancsok';
		$help = array(
			array('{#server}/ko on'),
			array('{#black}kezelő gombok be',),
			array('{#server}/ko off'),
			array('{#black}kezelő gombok ki',),
			array('{#server}/ko pl'),
			array('{#black}játékosok kezelése'),
			array('{#server}/ko add {#highlite}login'),
			array('{#black}játékos hozzáadása'),
			array('{#server}/ko remove {#highlite}login'),
			array('{#black}játékos eltávolítása'),
			array('{#server}/ko restart {#highlite}true / false'),
			array('{#black}restart engedélyezése játékosoknak'),
			array('{#black}_____________________________________'),
			array('{#server}/ko go'),
			array('{#black}elindítja a versenyt'),
			array('{#server}/ko stop'),
			array('{#black}leállítja a versenyt'),
			array('{#black}_____________________________________'),
			array('{#black}Először adj hozzá játékosokat,  '),
			array('{#black}majd indítsd el a versenyt! '),
		);

		// display ManiaLink message
		display_manialink($login, $header, array('Icons64x64_1', 'TrackInfo', -0.01), $help, array(1.0, 1.0), 'OK');
	}

	function pts_panel_off($aseco){
		global $pts;

		$pts->maniaLinkPage('<manialinks><manialink id="40400"></manialink></manialinks>', 1, false);
	}

	function pts_panel_on($aseco){
		global $pts;

		$maniapos = -1;
		$linkmaniapos = -0.2;
		$linkmaniapos2 = -0;
		$xml = '<manialinks><manialink id="40400"><frame posn="42.2 11.5 0">';

		if (empty($pts->finishedPlayerList)){
			return true;
		}

		/**
		 * @var $a \NC\Player
		 * @var $b \NC\Player
		 */
		$pts->finishedPlayerList->sort(function ($a, $b){
			return $a->pos == $b->pos ? 0 : ($a->pos > $b->pos ? 1 : -1);
		});

		/** @var $player \NC\Player */
		foreach ($pts->finishedPlayerList as $player){
			$rpointinc++;
			$linkmaniapos = ($linkmaniapos - 2.8);
			$linkmaniapos2 = ($linkmaniapos2 - 2.8);
			$maniapos = ($maniapos - 2.8);

			if ($rpointinc <= 15){
				$formattedTime = formatTime($player->score);
				$xml .= <<<XML
<format textsize="1"/><label posn="-4 $linkmaniapos 1.5" sizen="30 3 2" scale="0.3" style="TextRaceChrono" halign="center" text="\$s\$0f0{$player->pos} "/>
<format textsize="1"/><label posn="-2 $linkmaniapos 1.5" sizen="30 3 2" scale="0.3" style="TextRaceChrono" halign="left" text="\$s\$fff$formattedTime "/>
<format textsize="1.7"/><label posn="4.5 $linkmaniapos 1.6" sizen="12 3 2" halign="left" text="$000{$player} "/>
<quad sizen="4 3" posn="-4.3 $maniapos 1"  valign="center" halign="center" style="Bgs1InRace" substyle="NavButton" />
<quad sizen="6.2 3" posn="0.6 $maniapos 1"  valign="center" halign="center" style="Bgs1InRace" substyle="NavButton" />
<quad sizen="17 3" posn="3.6 $maniapos 1.1"  valign="center" halign="left" style="Bgs1InRace" substyle="NavButton"/>
XML;
			}
		}

		$xml .= '</frame></manialink></manialinks>';
		$pts->maniaLinkPage($xml, 0, false);
	}

	// Class system
	class PointSystem {
		// <KONFIGURÁCIÓ> //
		const
			ROUNDS_PER_MAP = 2, // HÁNY KÖR LEGYEN 1 PÁLYÁN
			FIX_WARMUP_ROUNDS = 0; // BEMELEGÍTŐ KÖRÖK SZÁMA
		// </KONFIGURÁCIÓ> //

		private $Aseco;
		/** @var \NC\PlayerList */
		public $playerList;
		/** @var bool */
		public
			$GameEnabled,
			$restartEnabled = true,
			$SwitchingMap = false;
		/** @var int */
		public
			$CurrentRound = 0,
			$FixRounds,
			$CurrentWarmupRound = 0;
		/** @var \NC\FinishedPlayerList */
		public $finishedPlayerList;
		const
			SZIN_ZOLD = '$z$s$0e1',
			SZIN_SARGA = '$z$s$ff0',
			SZIN_PIROS = '$z$s$f00',
			KO_ZOLD = '$z$0e1$s[KNOCKOUT MODE] - ',
			KO_ZOLD_SARGA = '$z$0e1$s[KNOCKOUT MODE] - \$fa0',
			KO_PIROS = '$z$e10$s[KNOCKOUT MODE] - ',
			KO_SARGA = '$z$ff0$s[KNOCKOUT MODE] - ';

		public function __construct($aseco){
			$this->Aseco = $aseco;
			$this->playerList = new \NC\PlayerList();
			$this->finishedPlayerList = new \NC\FinishedPlayerList();
		}

		function playerList(&$command, &$wildcard){
			$content = array();
			$header = "<line height='.02'><cell width='0.94'></cell></line>
               <line height='0.04'>
               <cell width='0.47' bgcolor='888E'><text halign='right'>Login</text></cell>
               <cell width='0.47' bgcolor='888E'><text>  NickName</text></cell></line>";

			$detail = "<line height='0.04'>
               <cell width='0.47'><text textcolor='EEEE' halign='right'>{LOGIN}</text></cell>
               <cell width='0.47'><text textcolor='EEEE'>  {NICKNAME}</text></cell></line>";
			$msg = '';
			/** @var $player \AsecoNC\Player */
			$player = $command['author'];
			$counter1 = 0;
			foreach ($this->Aseco->server->players->player_list as $pl){
				if (strlen($command['params'][0]) == 0 ||
					stripos(stripFormatting($pl->nickname), $wildcard) > 0 ||
					stripos($pl->login, $wildcard) > 0
				){
					$s = str_replace('{LOGIN}', $pl->login, $detail);
					$s = str_replace('{NICKNAME}', htmlspecialchars($pl->nickname), $s);
					$counter1++;
					$msg .= $s;
					if ($counter1 > 20){
						$counter1 = 0;
						$content[] = $header.$msg;
						$msg = '';
					}
				}
			}

			if (strlen($msg) > 0){
				$counter1 = 0;
				$content[] = $header.$msg;
			}

			if (empty($content)){
				$this->sendMessageTo($command['author']->login, 'No matches found');

				return;
			}

			$window = $this->createWindowToPlayerFramework('Player List', .94, 1.15, $player);
			$window->setContent($content);
			$window->addCallback('close', 'onCloseWin');
			$player->data['w_players'] = $window;
		}

		function startGame(){
			$playerCount = count($this->playerList);
			if (!count($this->playerList))
				return;

			$this->FixRounds = $playerCount;
			$this->CurrentRound = 0;
			$this->GameEnabled = true;
			$this->sendMessage('The challenge starts with '.self::SZIN_SARGA.$this->FixRounds.self::SZIN_ZOLD.' round(s)!');
			$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773123499">
<frame posn="0 15 1">
<format textsize="8"/>
<label posn="0 -6 1" sizen="98 2" halign="center" valign="center" text="\$e10\$w\$oLIVE! LIVE! LIVE!"/>
</frame></manialink>
XML;
			$this->maniaLinkPage($xml, 4500, false);

			foreach ($this->playerList as $player){
				$this->forceSpectatorState($player->login, self::FORCE_PLAYER);
				$player->restarts = 0;
			}
			$this->Aseco->client->query('NextChallenge');
		}

		function stopGame(){
			$this->GameEnabled = false;
			$message2 = self::KO_ZOLD_SARGA."The challenge is over!";

			$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773443499">
<frame posn="0 15 1">
<format textsize="2"/>
<label posn="-61 -61.5 1" sizen="98 2" halign="left" valign="center" text="$message2"/>
</frame></manialink>
XML;
			$this->maniaLinkPage($xml, 20000, false);

			if (!$this->finishedPlayerList->isEmpty()){
				/**
				 * @var $a \NC\Player
				 * @var $b \NC\Player
				 */
				$this->finishedPlayerList->sort(function($a, $b){
					return $a->score == $b->score ? 0 : ($a->score < $b->score ? 1 : -1);
				});
				$gyoztes = $this->finishedPlayerList->last();
				$this->sendMessage("A győztes: $gyoztes", self::KO_ZOLD_SARGA);
				$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773123499">
<frame posn="0 15 1">
<format textsize="8"/>
<label posn="0 -6 1" sizen="98 2" halign="center" valign="center" text="\$f80\$w\$oA győztes!"/>
<label posn="0 0 1" sizen="98 2" halign="center" valign="center" text="$gyoztes"/>
</frame></manialink>
XML;
				$this->maniaLinkPage($xml, 20000, false);
			}
			else $this->sendMessage("A kihívás nyertes nélkül befejeződött.", self::KO_ZOLD_SARGA);

			foreach ($this->playerList as $player){
				$this->sendMessageTo($player->login, "Mostantól újra versenyezhetsz!", self::KO_ZOLD_SARGA);
				$this->forceSpectatorState($player->login, self::FORCE_PLAYER);
			}

			$this->finishedPlayerList->wipe();
			$this->playerList->wipe();
			$this->CurrentRound = 0;
			$this->CurrentWarmupRound = 0;
			unset($this->FixRounds);

			sleep(5);
			$this->Aseco->client->query('ForceEndRound');
			$this->Aseco->client->query('NextChallenge');
		}

		/** @param \AsecoNC\Player $player */
		function PlayerConnect($player){
			if (!$this->playerList->contains($player->login)){
				$this->forceSpectatorState($player->login);
				$this->sendMessage("You are in Waiting list {$player->nickname}");
			}
		}

		function NewChallenge(){
			$this->customPoints(new \NC\CustomPoints());
			$this->sendMessage("Új pálya!");
			$this->CurrentWarmupRound = 0;
			$this->FixRounds = count($this->playerList);
			foreach ($this->playerList as $player){
				$player->score = 0;
				unset($player->pos);
			}
		}

		function BeginRound(){
			$this->playerList->resetRestarts();
			if (!$this->GameEnabled)
				return true;

			$playerCount = count($this->playerList);
			if ($playerCount > 1 && $this->CurrentWarmupRound < self::FIX_WARMUP_ROUNDS){
				$this->CurrentWarmupRound++;
				$this->sendMessage('Kezdődik a'.(
						self::FIX_WARMUP_ROUNDS > 1
						? "(z) {$this->CurrentWarmupRound}."
						: ''
					).' bemelegítő kör!');
				return;
			}

			if ($playerCount > 1){
				if ($playerCount < 2)
					$this->customPoints(new \NC\CustomPoints(array(200, 100)));
				else $this->customPoints(new \NC\CustomPoints(array_fill(0, 12, 1)));

				$this->CurrentRound++;
				if ($this->CurrentRound === 1){
					$this->sendMessage("A kihívás most kezdődik! Sok szerencsét és jó szórakozást!");
				}
				else if ($this->CurrentRound % self::ROUNDS_PER_MAP === 0){
					// Fix körök száma elérve, pályaváltás
					$this->sendMessage("Pályaváltás következik! Sok sikert és jó szórakozást!");
					$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773123499">
<frame posn="0 15 1">
<format textsize="8"/>
<label posn="0 -6 1" sizen="98 2" halign="center" valign="center" text="\$f80\$w\$oPályaváltás következik!"/>
</frame></manialink>
XML;
					$this->maniaLinkPage($xml, 3000, false);

					$this->SwitchingMap = true;
					$this->Aseco->client->query('NextChallenge');
					$this->NewChallenge();
					$this->SwitchingMap = false;
					return;
				}

				$message = self::SZIN_SARGA.'['.$this->CurrentWarmupRound.'/'.$this->FixRounds.'/'.$this->CurrentRound.']'.self::SZIN_PIROS.' rounds, '.self::SZIN_SARGA.count($this->playerList).self::SZIN_PIROS.' players in race.';
				$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773443499">
<frame posn="0 15 1">
<format textsize="2"/>
<label posn="-61 -61.5 1" sizen="98 2" halign="left" valign="center" text="{$message}"/>
</frame></manialink>
XML;
				$this->maniaLinkPage($xml, 0);
			}

			if ($playerCount == 1){
				echo " ///// gyoztes /////";
				foreach ($this->playerList as $player)
					$this->forceSpectatorState($player->login, self::FORCE_PLAYER);
				$this->stopGame();
			}
		}

		function knockOut(\NC\Player $player, $reason){
			$login = $player->login;
			$this->forceSpectatorState($login);
			$this->playerList->remove($login);
			$this->finishedPlayerList->remove($login);
			$this->sendMessage($player.self::SZIN_PIROS." kisestt. Ok: $reason.", self::KO_PIROS);
			$this->sendMessageTo($login, "Amíg a kihívás véget nem ér, néző módban figyelheted tovább a játékot.", self::KO_ZOLD_SARGA);

			echo " ///// kiesett /////";
			var_dump($login);
			var_dump($this->playerList[$login]['nick']);
			$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<manialink id="773123499">
<frame posn="0 15 1">
<format textsize="8"/>
<label posn="0 -6 1" sizen="98 2" halign="center" valign="center" text="\$f00\$w\$oYou have been Knocked out!"/>
<label posn="0 0 1" sizen="98 2" halign="center" valign="center" text="$player"/>
</frame></manialink>
XML;

			$this->maniaLinkPageTo($login, $xml);
			$this->playerList->remove($login);
		}

		function EndRound(){
			if (!$this->GameEnabled || $this->SwitchingMap)
				return true;

			if ($this->CurrentWarmupRound < self::FIX_WARMUP_ROUNDS){
				$this->sendMessage(
					self::FIX_WARMUP_ROUNDS - $this->CurrentWarmupRound === 0
					? (
						self::FIX_WARMUP_ROUNDS > 1
						? "{$this->CurrentWarmupRound}. b"
						: 'B'
					).'emelegítő kör vége!'
					: (
						self::FIX_WARMUP_ROUNDS > 1
						?'Utolsó b'
						:'B'
					).'emelegítő kör következik!'
				);
				return;
			}
			else if ($this->CurrentWarmupRound === self::FIX_WARMUP_ROUNDS){
				$this->sendMessage('Vége az utolsó bemelegítő körnek!');
				$this->CurrentWarmupRound++;
				return;
			}

			$playerCount = count($this->playerList);
			if ($playerCount > 1 && $this->CurrentRound < $this->FixRounds){
				$diff = array_diff($this->finishedPlayerList->keys(), $this->playerList->keys());

				$toKnockOut = array();
				foreach ($this->playerList as $player){
					if ($this->finishedPlayerList->contains($player)){
						continue;
					}

					$toKnockOut[] = $player;
				}

				if (count($toKnockOut)){
					$key = array_rand($toKnockOut, 1);
					$this->knockOut($toKnockOut[$key], 'DNF/AFK');
				}
				else {
					$finishedCount = count($this->finishedPlayerList);
					if ($finishedCount > 1){
						foreach ($this->finishedPlayerList as $player){
							if ($finishedCount - $player->pos === 0){
								$this->knockOut($player, 'Last place');
								break;
							}
						}
					}
				}
			}
			else {
				// utolsó körben eredmény
				foreach ($this->finishedPlayerList as &$player){
					$pos = $player->pos;
					$points = $playerCount - ($pos - 1);
					$player->score += $points;
					echo "########################";
					var_dump($pos);
					var_dump($player['login']);
					var_dump($player['score']);
					var_dump($points);
				}

				$this->stopGame();
				return;
			}

			$this->playerList->resetRestarts();
		}

		/**
		 * @param \AsecoNC\Record $record
		 *
		 * @return void|bool
		 */
		function PlayerFinish($record){
			$login = $record->player->login;
			$score = $record->score;
			// get back if game is not on or challenge is not yet started
			if (!$this->GameEnabled || $this->CurrentRound == 0)
				return true;

			if (empty($this->playerList))
				return;

			if ($this->playerList->contains($login)){ // if player is at race continue
				if ($record->score != 0 && !$this->finishedPlayerList->contains($login)){  // if player gets to goal
					if ($this->restartEnabled || (!$this->restartEnabled && $this->playerList[$login]->restarts == 1)){ // if restartEnabled
						$player = $this->playerList[$login];
						$player->score = $record->score;
						$player->pos = count($this->finishedPlayerList) + 1;
						$this->finishedPlayerList->add($player);

						$this->sendMessage($player.self::SZIN_SARGA." finished at {$player->pos}. place !",self::KO_SARGA);
						pts_panel_on($this->Aseco);
					}
					$this->playerList[$login]->restarts++;
				}
			}
		}

		function PlayerGiveUp($login){
			if ($this->CurrentWarmupRound < self::FIX_WARMUP_ROUNDS)
				return;

			$this->sendMessage($this->playerList[$login]." feladta");
			$this->playerList->remove($login);
		}

		function addPlayer($login){
			if ($this->playerList->contains($login)){
				$this->sendMessage("A játékos már versenyben van $login");

				return true;
			}

			/** @var $origPlayer \AsecoNC\Player */
			foreach ($this->Aseco->server->players->player_list as $origPlayer){
				if ($origPlayer->login === $login){
					$this->Aseco->client->query('GetPlayerInfo', $login);
					$info = $this->Aseco->client->getResponse();

					$player = new \NC\Player(array(
						'login' => $login,
						'nick' => $info['NickName'],
						'score' => $score,
					));
					$this->playerList->add($player);
					$this->sendMessage($player.self::SZIN_ZOLD." is in the Knockout challenge!");

					return;
				}
			}

			$this->sendMessage("Helytelen játékos login $login");
		}

		function removePlayer($login){
			if (!$this->playerList->contains($login))
				$this->sendMessage("Nem lehet eltávolítani a játékost - helytelen login $login");

			$player = $this->playerList[$login];
			$this->sendMessage(self::KO_ZOLD."Játékos eltávolítva $player");
			$this->playerList->remove($player);
		}

		function sendMessage($message, $color = self::KO_ZOLD){
			$this->Aseco->client->query('ChatSendServerMessage', $color.$message);
		}
		function sendMessageTo($login, $message, $color = self::KO_ZOLD){
			$this->Aseco->client->query('ChatSendServerMessageToLogin', $color.$message, $login);
		}
		const PERMANENT = 0;
		function maniaLinkPage($xml, $autohideIn = 20000, $hideOnClick = false){
			$this->Aseco->client->query("SendDisplayManialinkPage", $xml, $autohideIn, $hideOnClick);
		}
		function maniaLinkPageTo($login, $xml, $autohideIn = 20000, $hideOnClick = false){
			$this->Aseco->client->addCall('SendDisplayManialinkPageToLogin', $login, $xml, $autohideIn, $hideOnClick);
		}
		function customPoints(\NC\CustomPoints $points, $boolean = true){
			$this->Aseco->client->query('SetRoundCustomPoints', $points->toArray(), $boolean);
		}

		const
			USER_SELECT = 0,
			FORCE_SPECTATOR = 1,
			FORCE_PLAYER = 2;

		function forceSpectatorState($login, $state = self::FORCE_SPECTATOR){
			$this->Aseco->client->query('ForceSpectator', $login, $state);
		}
	}

	function pts_formatTime($MwTime, $hsec = true){
		if ($MwTime == -1)
			return '???';

		$hseconds = (($MwTime - (floor($MwTime / 1000) * 1000)) / 10);
		$MwTime = floor($MwTime / 1000);
		$hours = floor($MwTime / 3600);
		$MwTime = $MwTime - ($hours * 3600);
		$minutes = floor($MwTime / 60);
		$MwTime = $MwTime - ($minutes * 60);
		$seconds = floor($MwTime);

		return $hsec
			? (
				$hours
				? sprintf('%d:%02d:%02d.%02d', $hours, $minutes, $seconds, $hseconds)
				: sprintf('%d:%02d.%02d', $minutes, $seconds, $hseconds)
			)
			: (
				$hours
				? sprintf('%d:%02d:%02d', $hours, $minutes, $seconds)
				: sprintf('%d:%02d', $minutes, $seconds)
			);
	}
}

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests