From 16fb782bbf3406b3f9c338cfb136139f7037b158 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Tue, 23 Aug 2022 00:37:58 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20LazyVarUpdater=EC=9D=98=20key=EB=A1=9C?= =?UTF-8?q?=20BackedEnum=20=ED=97=88=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/LazyVarUpdater.php | 57 ++++++++++++++++++++++++++++-------- hwe/sammo/WarUnitCity.php | 5 +++- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/hwe/sammo/LazyVarUpdater.php b/hwe/sammo/LazyVarUpdater.php index 8c460d42..b53891b6 100644 --- a/hwe/sammo/LazyVarUpdater.php +++ b/hwe/sammo/LazyVarUpdater.php @@ -15,15 +15,21 @@ trait LazyVarUpdater{ return $this->raw; } - function getVar(string $key){ + function getVar(string|\BackedEnum $key){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } return $this->raw[$key]; } - function getVars(string ...$keys){ + function getVars(string|\BackedEnum ...$keys){ return array_map([$this, 'getVar'], $keys); } - function touchVar(string $key):bool{ + function touchVar(string|\BackedEnum $key):bool{ + if($key instanceof \BackedEnum){ + $key = $key->value; + } if(key_exists($key, $this->raw)){ return false; } @@ -41,16 +47,25 @@ trait LazyVarUpdater{ } } - function setVar(string $key, $value){ + function setVar(string|\BackedEnum $key, $value){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } return $this->updateVar($key, $value); } - function getAuxVar(string $key){ + function getAuxVar(string|\BackedEnum $key){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } $this->unpackAux(); return $this->raw['auxVar'][$key]??null; } - function setAuxVar(string $key, $var){ + function setAuxVar(string|\BackedEnum $key, $var){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } $oldVar = $this->getAuxVar($key); if($oldVar === $var){ @@ -66,7 +81,10 @@ trait LazyVarUpdater{ $this->auxUpdated = true; } - function updateVar(string $key, $value){ + function updateVar(string|\BackedEnum $key, $value){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } if(($this->raw[$key]??null) === $value){ return; } @@ -76,7 +94,10 @@ trait LazyVarUpdater{ $this->raw[$key] = $value; } - function updateVarWithLimit(string $key, $value, $min = null, $max = null){ + function updateVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } if($min !== null && $value < $min){ $value = $min; } @@ -86,8 +107,11 @@ trait LazyVarUpdater{ $this->updateVar($key, $value); } - function increaseVar(string $key, $value) + function increaseVar(string|\BackedEnum $key, $value) { + if($key instanceof \BackedEnum){ + $key = $key->value; + } if($value === 0){ return; } @@ -95,7 +119,10 @@ trait LazyVarUpdater{ $this->updateVar($key, $targetValue); } - function increaseVarWithLimit(string $key, $value, $min = null, $max = null){ + function increaseVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } $targetValue = $this->raw[$key] + $value; if($min !== null && $targetValue < $min){ $targetValue = $min; @@ -106,8 +133,11 @@ trait LazyVarUpdater{ $this->updateVar($key, $targetValue); } - function multiplyVar(string $key, $value) + function multiplyVar(string|\BackedEnum $key, $value) { + if($key instanceof \BackedEnum){ + $key = $key->value; + } if($value === 1){ return; } @@ -115,7 +145,10 @@ trait LazyVarUpdater{ $this->updateVar($key, $targetValue); } - function multiplyVarWithLimit(string $key, $value, $min = null, $max = null){ + function multiplyVarWithLimit(string|\BackedEnum $key, $value, $min = null, $max = null){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } $targetValue = $this->raw[$key] * $value; if($min !== null && $targetValue < $min){ $targetValue = $min; diff --git a/hwe/sammo/WarUnitCity.php b/hwe/sammo/WarUnitCity.php index efe90307..a92f31b1 100644 --- a/hwe/sammo/WarUnitCity.php +++ b/hwe/sammo/WarUnitCity.php @@ -44,7 +44,10 @@ class WarUnitCity extends WarUnit{ return $this->getVar('name'); } - function getCityVar(string $key){ + function getCityVar(string|\BackedEnum $key){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } return $this->raw[$key]; }