Skip to content

Commit

Permalink
Training units - process training
Browse files Browse the repository at this point in the history
  • Loading branch information
Spamercz committed Feb 14, 2016
1 parent 8e6b6b4 commit 1ffbc95
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions App/Config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ services:
- App\GameModule\Model\Units\UnitModel
- App\GameModule\Model\Units\UnitService(%speed%)
- App\GameModule\Model\Units\TrainingModel
- App\GameModule\Model\Event\ProcessTraining

# commands
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<p>
<input n:name="submit" type="image" id="btn_train" class="dynamic_img" value="ok" src="{$baseUrl}/img/x.gif" alt="train"/>
</p>
<table cellpadding="1" cellspacing="1" class="under_progress">
<table n:if="$queue" cellpadding="1" cellspacing="1" class="under_progress">
<thead>
<tr>
<td>Troops in training</td>
Expand Down
7 changes: 7 additions & 0 deletions App/GameModule/Model/Event/ProcessEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class ProcessEvents
* @var App\FrontModule\Model\VData\VDataModel
*/
private $VDataModel;
/**
* @var ProcessTraining
*/
private $processTraining;


public function __construct(
Expand All @@ -35,12 +39,14 @@ public function __construct(
App\FrontModule\Model\VData\VillageService $villageService,
Kdyby\Clock\IDateTimeProvider $dateTimeProvider,
App\FrontModule\Model\VData\VDataModel $VDataModel
, ProcessTraining $processTraining
){
$this->buildingService = $buildingService;
$this->productionService = $productionService;
$this->villageService = $villageService;
$this->dateTimeProvider = $dateTimeProvider;
$this->VDataModel = $VDataModel;
$this->processTraining = $processTraining;
}


Expand All @@ -54,6 +60,7 @@ public function process()
$this->productionService->processProduction($village, $time);
}
$this->buildingService->processBuildings($time);
$this->processTraining->process($time);

$this->releaseLock();
}
Expand Down
64 changes: 64 additions & 0 deletions App/GameModule/Model/Event/ProcessTraining.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\GameModule\Model\Event;

use App;
use Kdyby;

class ProcessTraining
{
/**
* @var App\GameModule\Model\Units\TrainingModel
*/
private $trainingModel;
/**
* @var App\GameModule\Model\Units\UnitsModel
*/
private $unitsModel;


public function __construct(
App\GameModule\Model\Units\TrainingModel $trainingModel
, App\GameModule\Model\Units\UnitsModel $unitsModel
) {
$this->unitsModel = $unitsModel;
$this->trainingModel = $trainingModel;
}


public function process($time)
{
$trainData = $this->trainingModel->getProcessData($time);
foreach ($trainData as $single) {
if ($single->timestamp2 < ($time + $single->eachtime)) {
$diff = $time - $single->timestamp2;
$amount = floor($diff / $single->eachtime);
if ($amount > $single->amt) {
$amount = $single->amt;
}
$remaining = $single->amt - $amount;
if ($remaining < 1) {
$this->trainingModel->delete($single->id);
} else {
$this->trainingModel->update($single->id, [
'amt' => $remaining,
'timestamp2' => $single->timestamp2 + ($single->eachtime * $amount)
]);
}
/** @var \stdClass $units */
$units = $this->unitsModel->get($single->vref);
$unitName = 'u' . $single->unit;
if ($units) {
$unitAmount = $units['u' . $single->unit] + $amount;
$this->unitsModel->update($units['vref'], [
$unitName => $unitAmount,
]);
} else {
$this->unitsModel->add([
$unitName => $amount,
]);
}
}
}
}
}
8 changes: 8 additions & 0 deletions App/GameModule/Model/Units/TrainingModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public function getByBuilding($village, $building)

return $query->fetchAll();
}


public function getProcessData($time)
{
return $this->database->select('*')->from($this->table)
->where('timestamp < %i', $time)
->fetchAll();
}
}
8 changes: 8 additions & 0 deletions App/GameModule/Model/Units/UnitsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public function get($id)
->toArray();
}


public function update($id, $data)
{
return $this->database->update($this->table, $data)
->where('vref = %i', $id)
->execute();
}

}

0 comments on commit 1ffbc95

Please sign in to comment.