<?php
include_once '../objects/db_user.php';
include_once '../objects/db_session.php';

class UserStatsInSessionRow
{
	public GameSession $session;
	public User $user;

	public int $sessionId = -1;
	public int $userId = -1;

	public int $nbFiredShotsByUser = 0;
	public int $nbEnemyHitsByUser = 0;
	public int $nbCivilHitsByUser = 0;
	public int $nbPoliceHitsByUser = 0;
	public int $nbObjectHitsByUser = 0;
	public int $nbMissedShotsByUser = 0;
	public int $nbDeadBodyHitsByUser = 0;

	public float $averagePrecision = 0.0;
	public float $averageReactionTime = 0.0;

	public int $nbReceivedHitsFromEnemyIA = 0;
	public int $nbReceivedHitsFromEnemyUser = 0;
	public int $nbReceivedHitsFromPoliceUser = 0;

	public int $totalEnemyKilled = 0;
	public int $totalCivilKilled = 0;
	public int $totalPoliceKilled = 0;

	

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function __construct ()
	{
		$this->session = new GameSession(null);
		$this->sessionId = -1;
		$this->user = new User(null);
		$this->userId = -1;
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static function withRow ($db, array $row)
	{
		$instance = new self();
		$instance->createSession($db, (int)$row['SessionId']);
		$instance->createUser($db, (int)$row['UserId']);
		//$this->readRow($row);
		return $instance;
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function createSessionAndUser ($db, int $sessionId=-1, int $userId=-1)
	{
		if ($sessionId > -1)
			$this->createSession($db, $sessionId);

		if ($userId > 0)
			$this->createUser($db, $userId);
	}

	
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private function createSession ($db, int $id)
	{
		$this->session = new GameSession($db);
		$this->session->id = $id;
		$this->sessionId = $id;	// keeping this field for easier access in UE
		$this->session->load();
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	private function createUser ($db, int $id)
	{
		$this->user = new User($db);
		$this->user->id = $id;
		$this->userId = $id;	// keeping this field for easier access in UE
		$this->user->load();
	}


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function readRow (array $row)
	{
        $this->sessionId = $row['SessionId'];
		//$this->sessionTypeId = $row['SessionTypeId'];
		//$this->sessionType = $row['SessionType'];
		//$this->sessionName = $row['SessionName'];
		//$this->sessionDate = $row['SessionDate'];
		//$this->mapName = $row['MapName'];
		//$this->scenarioName = $row['ScenarioName'];
		//$this->sessionSuccessful = $row['SessionSuccessful'] == 1;
		//$this->sessionDuration = (float)$row['SessionDuration'];
		//$this->triggerTypeId = (int)$row['TriggerTypeId'];
		//$this->triggerType = $row['TriggerType'];
		//$this->shooterId = (int)$row['ShooterId'];
		//$this->shooterName = $row['ShooterName'];
		//$this->shooterRoleId = (int)$row['ShooterRoleId'];
		//$this->shooterRole = $row['ShooterRole'];
		//$this->shotIndex = (int)$row['ShotIndex'];
		//$this->reactId = (int)$row['ReactId'];
		//$this->reactModeId = (int)$row['ReactModeId'];
		//$this->reactMode = $row['ReactMode'];
		//$this->reactTypeId = (int)$row['ReactTypeId'];
		//$this->reactType = $row['ReactType'];
		//$this->targetUserId = (int)$row['TargetUserId'];
		//$this->targetUserName = $row['TargetUserName'];
		//$this->targetRoleId = (int)$row['TargetRoleId'];
		//$this->targetRole = $row['TargetRole'];
		//$this->targetName = $row['TargetName'];
		//$this->targetBoneName = $row['TargetBoneName'];
		//$this->targetKilled = $row['TargetKilled'] == 1;
		//$this->hitLocationX = (float)$row['HitLocationX'];
		//$this->hitLocationY = (float)$row['HitLocationY'];
		//$this->hitLocationTag = $row['HitLocationTag'];
		//$this->hitPrecision = (float)$row['HitPrecision'];
		//$this->hitTargetDistance = (float)$row['HitTargetDistance'];
		//$this->reactTime = (float)$row['ReactionTime'];
		//$this->timeStamp = (float)$row['TimeStamp'];
		//$this->nbHit = (int)$row['NbHit'];
		//$this->nbKilled = (int)$row['NbKilled'];
        $this->userId = $row['UserId'];
    }
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function setFiredShots (array $row)
	{
		$this->nbFiredShotsByUser = (int)$row['NbShotsFired'];
		$this->nbMissedShotsByUser = (int)$row['NbMissedShots'];
		$this->nbEnemyHitsByUser = (int)$row['NbEnemyHits'];
		$this->nbCivilHitsByUser = (int)$row['NbCivilHits'];
		$this->nbPoliceHitsByUser = (int)$row['NbPoliceHits'];
		$this->nbObjectHitsByUser = (int)$row['NbObjectHits'];
		$this->nbDeadBodyHitsByUser = (int)$row['NbDeadBodyHits'];
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function setPrecisionAndReactionTime (array $row)
	{
		$this->averagePrecision = (float)$row['AveragePrecision'];
		$this->averageReactionTime = (float)$row['AverageReactionTime'];
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function setReceivedHits (array $row)
	{
		$this->nbReceivedHitsFromEnemyIA = (int)$row['NbEnemyShotsIA'];
		$this->nbReceivedHitsFromEnemyUser = (int)$row['NbEnemyShotsUser'];
		$this->nbReceivedHitsFromPoliceUser = (int)$row['NbPoliceHits'];
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function setTotalKills (array $row)
	{
		$this->totalEnemyKilled = (int)$row['NbEnemyKilled'];
		$this->totalCivilKilled = (int)$row['NbCivilKilled'];
		$this->totalPoliceKilled = (int)$row['NbPoliceKilled'];
	}

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function toArray () : array
	{
		return array (
			"session" => $this->session->toArray() ?? "",
			"user" => $this->user->toArray() ?? "",
			"userId" => (int)$this->userId ?? -1,
			"sessionId" => (int)$this->sessionId ?? -1,
			"userId" => (int)$this->userId ?? -1,
			"nbFiredShotsByUser" => (int)$this->nbFiredShotsByUser ?? 0,
			"nbEnemyHitsByUser" => (int)$this->nbEnemyHitsByUser ?? 0,
			"nbCivilHitsByUser" => (int)$this->nbCivilHitsByUser ?? 0,
			"nbPoliceHitsByUser" => (int)$this->nbPoliceHitsByUser ?? 0,
			"nbObjectHitsByUser" => (int)$this->nbObjectHitsByUser ?? 0,
			"nbDeadBodyHitsHitsByUser" => (int)$this->nbDeadBodyHitsByUser ?? 0,
			"nbMissedShotsByUser" => (int)$this->nbMissedShotsByUser ?? 0,
			"averagePrecision" => (float)$this->averagePrecision ?? 0.0,
			"averageReactionTime" => (float)$this->averageReactionTime ?? 0.0,
			"nbReceivedHitsFromEnemyIA" => (int)$this->nbReceivedHitsFromEnemyIA ?? 0,
			"nbReceivedHitsFromEnemyUser" => (int)$this->nbReceivedHitsFromEnemyUser ?? 0,
			"nbReceivedHitsFromPoliceUser" => (int)$this->nbReceivedHitsFromPoliceUser ?? 0,
			"totalEnemyKilled" => (int)$this->totalEnemyKilled ?? 0,
			"totalCivilKilled" => (int)$this->totalCivilKilled ?? 0,
			"totalPoliceKilled" => (int)$this->totalPoliceKilled ?? 0
		);
	}
	
	
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function isForUserInSession ($userIdToCheck, $sessionIdToCheck)
	{
		return $this->userId == $userIdToCheck && $this->sessionId == $sessionIdToCheck;
	}
}









class Stats_ShotsPerUserPerSession_Row
{
	public int $sessionId = -1;
	public int $userId = -1;
	public $username = "";
	public int $nbShotsFired = 0;
	public int $nbEnemyHits = 0;
	public int $nbCivilsHits = 0;
	public int $nbObjectHits = 0;
	public int $nbDeadHits = 0;
	
	public function __construct($row)
	{
		$this->sessionId = $row['sessionId'];
		$this->userId = $row['userId'];
		$this->username = $row['username'];
		$this->nbShotsFired = $row['nbShotsFired'];
		$this->nbEnemyHits = $row['nbEnemyHits'];
		$this->nbCivilsHits = $row['nbCivilsHits'];
		$this->nbObjectHits = $row['nbObjectHits'];
		$this->nbDeadHits = $row['nbDeadHits'];
	}

	public function toArray () : array
	{
		return array (
			"sessionId" => (int)$this->sessionId ?? -1,
			"userId" => (int)$this->userId ?? -1,
			"username" => $this->username ?? "",
			"nbShotsFired" => (int)$this->nbShotsFired ?? 0,
			"nbEnemyHits" => (int)$this->nbEnemyHits ?? 0,
			"nbCivilsHits" => (int)$this->nbCivilsHits ?? 0,
			"nbObjectHits" => (int)$this->nbObjectHits ?? 0,
			"nbDeadHits" => (int)$this->nbDeadHits ?? 0
		);
	}
}
?>