Skip to content

Commit

Permalink
Add support for non-Hero units
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Jan 27, 2020
1 parent 74c4c19 commit 88dd660
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/Commands/Simulate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace App\Commands;

use App\Units\Hero;
use App\Units\Unit;
use App\Units\Heroes\Samuro;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
Expand Down Expand Up @@ -30,6 +31,8 @@ public function run(array $params = [])
// Fetch our combatants
$samuro = new Samuro(20, $talents);
$raynor = new Hero('raynor', 20);

$unit = new Unit('MercDefenderSiegeGiant');

$samuro->schedule()->timelimit = 20;

Expand Down
3 changes: 2 additions & 1 deletion app/Controllers/Simulate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace App\Controllers;

use App\Units\Hero;
use App\Units\Unit;
use App\Units\Heroes\Samuro;

class Simulate extends BaseController
Expand Down Expand Up @@ -29,7 +30,7 @@ public function results()
// WIP - needs to check for implemented hero class \App\Units\Heroes\{$hero}
$samuro = new Samuro($level, $post);

$unit = new Hero($target);
$unit = $target == 'Raynor' ? new Hero($target) : new Unit($target);

// Pre-cast abilities in the desired order
$samuro->setCrit(0);
Expand Down
1 change: 0 additions & 1 deletion app/Units/Hero.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public function name(): string

/**
* Add each talent to the list of selected talents.
* WIP - probably needs to check for talent removal or something
*
* @param string $nameId nameId of the target talent
*
Expand Down
87 changes: 87 additions & 0 deletions app/Units/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php namespace App\Units;

/**
* Class Unit
*
* A non-hero unit, relevant for PvE.
*/
class Unit extends BaseUnit
{
/**
* Master set of all unitdata.
*
* @var object
*/
protected static $master;

/**
* Unique unit identifier.
*
* @var string
*/
protected $cUnitId;

/**
* Create the unit with an intial set of values.
*
* @param string $cUnitId The ID of the unit to load
*/
public function __construct(string $cUnitId)
{
$this->cUnitId = $cUnitId;
}

/**
* Lazy load data from the source.
*/
protected function ensureData(): void
{
// If the data is there then all is well
if ($this->data)
{
return;
}

// Make sure the data is loaded from file
if (self::$master === null)
{
$path = $this->getPath('unitdata_*.json');
$json = file_get_contents($path);
self::$master = json_decode($json);
}

// Clone the master data to the current set
/*
* WIP - `clone` doesn't dereference sub-objects, hack it with serializing for now
* $this->data = clone self::$master->{$this->cHeroId};
*/
$this->data = unserialize(serialize(self::$master->{$this->cUnitId}));
}

/**
* Resets data to their defaults and clears all statuses.
*
* @return $this
*/
public function reset()
{
$this->data = unserialize(serialize(self::$master->{$this->cUnitId}));

foreach ($this->statuses as $statusId => $status)
{
$this->removeStatus($statusId);
}

return $this;
}

/**
* Returns this unit's cUnitId.
*
* @return string
*/
public function name(): string
{
return $this->cUnitId;
}
}
5 changes: 3 additions & 2 deletions app/Views/simulate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

<div class="form-group">
<label for="target">Target</label>
<select class="custom-select" name="target" disabled>
<option selected>Raynor</option>
<select class="custom-select" name="target">
<option>Raynor</option>
<option>MercDefenderSiegeGiant</option>
</select>
</select>
</div>
Expand Down

0 comments on commit 88dd660

Please sign in to comment.