<?php
// Single ObjectiveDebrief
class ObjectiveDebrief
{
    public $id = "";
    public $description = "";
    public float $score = 0.0;
    public float $weight = 0.0;
    public int $completed = 0;

    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function add (ObjectiveDebrief $otherDebrief)
    {
		if ($this->id == $otherDebrief->id)
        {
            // only add stats with same id
            $this->score += $otherDebrief->score;
            //$this->weight += $otherDebrief->weight; // weight should be the same if ids are the same
            if ($this->completed != 1)
                $this->completed = $otherDebrief->completed == 1 ? 1 : $this->completed;
        }
    }
}



// General GameSession debrief (multiple ObjectiveDebrief)
class UserInGameSessionResults
{
    public ObjectiveDebrief $civilian;
    public ObjectiveDebrief $time;
    public ObjectiveDebrief $enemy;
    public ObjectiveDebrief $health;
    public ObjectiveDebrief $precision;
    public ObjectiveDebrief $reactTime;
    public ObjectiveDebrief $ammoLimit;
    public ObjectiveDebrief $target;
    public ObjectiveDebrief $overall;


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public function __construct ()
	{
		$this->civilian = new ObjectiveDebrief();
		$this->time = new ObjectiveDebrief();
		$this->enemy = new ObjectiveDebrief();
		$this->health = new ObjectiveDebrief();
		$this->precision = new ObjectiveDebrief();
		$this->reactTime = new ObjectiveDebrief();
		$this->ammoLimit = new ObjectiveDebrief();
		$this->target = new ObjectiveDebrief();
		$this->overall = new ObjectiveDebrief();
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static function fromJsonString (string $jsonResults)
    {
		$instance = new self();
        
        if (!empty($jsonResults))
            $instance->createFromJsonArray( json_decode($jsonResults, true) );
        
        return $instance;
    }


	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private function createFromJsonArray ($missionDebriefData)
    {
        // Parse all objectiveDebrief JSON entries in data (civilian, time, enemy, etc.)
        foreach ($missionDebriefData as $objectiveDebriefKey => $objectiveDebriefValue)
        {
            // Create an ObjectiveDebrief variable for each objectiveDebrief JSON entry
            $debrief = new ObjectiveDebrief();
            foreach ($objectiveDebriefValue as $debriefKey => $debriefValue)
            {
                // Read all JSON properties and assign them to the ObjectiveDebrief variable
                $debrief->{$debriefKey} = $debriefValue;
            }

            // Assign ObjectiveDebrief variable to the corresponding property in UserInGameSessionResults
            $this->{$objectiveDebriefKey} = $debrief;
        }
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function add (UserInGameSessionResults $otherParticipationResults)
    {
        $this->civilian->add($otherParticipationResults->civilian);
		$this->time->add($otherParticipationResults->time);
		$this->enemy->add($otherParticipationResults->enemy);
		$this->health->add($otherParticipationResults->health);
		$this->precision->add($otherParticipationResults->precision);
		$this->reactTime->add($otherParticipationResults->reactTime);
		$this->ammoLimit->add($otherParticipationResults->ammoLimit);
		$this->target->add($otherParticipationResults->target);
		$this->overall->add($otherParticipationResults->overall);
    }

    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function calculateAverages ($nb)
    {
        $this->civilian->score /= $nb;
        $this->time->score /= $nb;
        $this->enemy->score /= $nb;
        $this->health->score /= $nb;
        $this->precision->score /= $nb;
        $this->reactTime->score /= $nb;
        $this->ammoLimit->score /= $nb;
        $this->target->score /= $nb;
        $this->overall->score /= $nb;
    }
}
?>