범용성을 목적으로 LazyVarUpdater를 분리

This commit is contained in:
2018-09-02 17:25:04 +09:00
parent f412b7e734
commit 78955a1540
4 changed files with 83 additions and 81 deletions
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace sammo;
trait LazyVarUpdater{
protected $raw = [];
protected $updatedVar = [];
function getRaw():array{
return $this->raw;
}
function updateVar(string $key, $value){
if($this->raw[$key] === $value){
return;
}
if(!key_exists($key, $this->updatedVar)){
$this->updatedVar[$key] = $this->raw[$key];
}
$this->raw[$key] = $value;
}
function updateVarWithLimit(string $key, $value, $min = null, $max = null){
if($min !== null && $value < $min){
$value = $min;
}
if($max !== null && $value > $max){
$value = $max;
}
$this->updateVar($key, $value);
}
function increaseVar(string $key, $value)
{
if($value === 0){
return;
}
$targetValue = $this->raw[$key] + $value;
$this->updateVar($key, $targetValue);
}
function increaseVarWithLimit(string $key, $value, $min = null, $max = null){
$targetValue = $this->raw[$key] + $value;
if($min !== null && $targetValue < $min){
$targetValue = $min;
}
if($max !== null && $targetValue > $max){
$targetValue = $max;
}
$this->updateVar($key, $targetValue);
}
function multiplyVar(string $key, $value)
{
if($value === 1){
return;
}
$targetValue = $this->raw[$key] * $value;
$this->updateVar($key, $targetValue);
}
function multiplyVarWithLimit(string $key, $value, $min = null, $max = null){
$targetValue = $this->raw[$key] * $value;
if($min !== null && $targetValue < $min){
$targetValue = $min;
}
if($max !== null && $targetValue > $max){
$targetValue = $max;
}
$this->updateVar($key, $targetValue);
}
function getUpdatedValues():array {
$updateVals = [];
foreach(array_keys($this->updatedVar) as $key){
$updateVals[$key] = $this->raw[$key];
}
return $updateVals;
}
}
+2 -73
View File
@@ -2,6 +2,8 @@
namespace sammo;
class WarUnit{
use LazyVarUpdater;
protected $raw;
protected $rawNation;
@@ -54,76 +56,10 @@ class WarUnit{
return $this->logActivatedSkill;
}
function getRaw():array{
return $this->raw;
}
function getRawNation():array{
return $this->rawNation;
}
function getVar(string $key){
return $this->raw[$key];
}
function updateVar(string $key, $value){
if($this->raw[$key] === $value){
return;
}
$this->raw[$key] = $value;
$this->updatedVar[$key] = true;
}
function updateVarWithLimit(string $key, $value, $min = null, $max = null){
if($min !== null && $value < $min){
$value = $min;
}
if($max !== null && $value > $max){
$value = $max;
}
$this->updateVar($key, $value);
}
function increaseVar(string $key, $value)
{
if($value === 0){
return;
}
$this->raw[$key] += $value;
$this->updatedVar[$key] = true;
}
function increaseVarWithLimit(string $key, $value, $min = null, $max = null){
$targetValue = $this->raw[$key] + $value;
if($min !== null && $targetValue < $min){
$targetValue = $min;
}
if($max !== null && $targetValue > $max){
$targetValue = $max;
}
$this->updateVar($key, $targetValue);
}
function multiplyVar(string $key, $value)
{
if($value === 1){
return;
}
$this->raw[$key] *= $value;
$this->updatedVar[$key] = true;
}
function multiplyVarWithLimit(string $key, $value, $min = null, $max = null){
$targetValue = $this->raw[$key] * $value;
if($min !== null && $targetValue < $min){
$targetValue = $min;
}
if($max !== null && $targetValue > $max){
$targetValue = $max;
}
$this->updateVar($key, $targetValue);
}
function getNationVar(string $key){
return $this->rawNation[$key];
}
@@ -415,15 +351,8 @@ class WarUnit{
$this->getLogger()->pushBattleResultTemplate($this, $this->getOppose());
}
function applyDB($db):bool{
throw new NotInheritedMethodException();
}
function criticalDamage():float{
//전특, 병종에 따라 필살 데미지가 달라질지도 모르므로 static 함수는 아닌 것으로
return Util::randRange(1.3, 2.0);
}
}
+1 -4
View File
@@ -137,10 +137,7 @@ class WarUnitCity extends WarUnit{
function applyDB($db):bool{
$updateVals = [];
foreach(array_keys($this->updatedVar) as $key){
$updateVals[$key] = $this->raw[$key];
}
$updateVals = $this->getUpdatedValues();
if(!$updateVals){
return false;
+1 -4
View File
@@ -971,10 +971,7 @@ class WarUnitGeneral extends WarUnit{
* @param \MeekroDB $db
*/
function applyDB($db):bool{
$updateVals = [];
foreach(array_keys($this->updatedVar) as $key){
$updateVals[$key] = $this->raw[$key];
}
$updateVals = $this->getUpdatedValues();
if(!$updateVals){
return false;