diff --git a/hwe/sammo/General.php b/hwe/sammo/General.php index 31a38b10..0cf70ebb 100644 --- a/hwe/sammo/General.php +++ b/hwe/sammo/General.php @@ -12,6 +12,8 @@ use sammo\WarUnitTrigger as WarUnitTrigger; class General extends GeneralBase implements iAction { + use LazyVarAndAuxUpdater; + /** @var Map */ protected Map $rankVarRead; /** @var Map */ diff --git a/hwe/sammo/LazyVarAndAuxUpdater.php b/hwe/sammo/LazyVarAndAuxUpdater.php new file mode 100644 index 00000000..497698e7 --- /dev/null +++ b/hwe/sammo/LazyVarAndAuxUpdater.php @@ -0,0 +1,73 @@ +getAuxVar(''); + + } + return $this->raw; + } + + function unpackAux(){ + if(!key_exists('auxVar', $this->raw)){ + if(!key_exists('aux', $this->raw)){ + throw new \RuntimeException('aux is not set'); + } + $this->raw['auxVar'] = Json::decode($this->raw['aux']??'{}'); + } + } + + function getAuxVar(string|\BackedEnum $key){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } + $this->unpackAux(); + return $this->raw['auxVar'][$key]??null; + } + + function setAuxVar(string|\BackedEnum $key, $var){ + if($key instanceof \BackedEnum){ + $key = $key->value; + } + $oldVar = $this->getAuxVar($key); + + if($oldVar === $var){ + return; + } + + if($var === null){ + unset($this->raw['auxVar'][$key]); + $this->auxUpdated = true; + return; + } + $this->raw['auxVar'][$key] = $var; + $this->auxUpdated = true; + } + + function getUpdatedValues():array { + if($this->auxUpdated){ + $this->setVar('aux', Json::encode($this->raw['auxVar'])); + $this->auxUpdated = false; + } + $updateVals = []; + foreach(array_keys($this->updatedVar) as $key){ + $updateVals[$key] = $this->raw[$key]; + } + return $updateVals; + } + + function flushUpdateValues():void { + $this->updatedVar = []; + if(key_exists('auxVar', $this->raw)){ + $this->auxUpdated = false; + unset($this->raw['auxVar']); + } + } +} \ No newline at end of file diff --git a/hwe/sammo/LazyVarUpdater.php b/hwe/sammo/LazyVarUpdater.php index b53891b6..988d20a9 100644 --- a/hwe/sammo/LazyVarUpdater.php +++ b/hwe/sammo/LazyVarUpdater.php @@ -4,14 +4,8 @@ namespace sammo; trait LazyVarUpdater{ protected $raw = []; protected $updatedVar = []; - protected $auxVar = null; - protected $auxUpdated = false; - function getRaw(bool $extractAux=false):array{ - if($extractAux){ - $this->getAuxVar(''); - - } + function getRaw():array{ return $this->raw; } @@ -38,15 +32,6 @@ trait LazyVarUpdater{ return true; } - function unpackAux(){ - if(!key_exists('auxVar', $this->raw)){ - if(!key_exists('aux', $this->raw)){ - throw new \RuntimeException('aux is not set'); - } - $this->raw['auxVar'] = Json::decode($this->raw['aux']??'{}'); - } - } - function setVar(string|\BackedEnum $key, $value){ if($key instanceof \BackedEnum){ $key = $key->value; @@ -54,33 +39,6 @@ trait LazyVarUpdater{ return $this->updateVar($key, $value); } - function getAuxVar(string|\BackedEnum $key){ - if($key instanceof \BackedEnum){ - $key = $key->value; - } - $this->unpackAux(); - return $this->raw['auxVar'][$key]??null; - } - - function setAuxVar(string|\BackedEnum $key, $var){ - if($key instanceof \BackedEnum){ - $key = $key->value; - } - $oldVar = $this->getAuxVar($key); - - if($oldVar === $var){ - return; - } - - if($var === null){ - unset($this->raw['auxVar'][$key]); - $this->auxUpdated = true; - return; - } - $this->raw['auxVar'][$key] = $var; - $this->auxUpdated = true; - } - function updateVar(string|\BackedEnum $key, $value){ if($key instanceof \BackedEnum){ $key = $key->value; @@ -160,10 +118,6 @@ trait LazyVarUpdater{ } function getUpdatedValues():array { - if($this->auxUpdated){ - $this->setVar('aux', Json::encode($this->raw['auxVar'])); - $this->auxUpdated = false; - } $updateVals = []; foreach(array_keys($this->updatedVar) as $key){ $updateVals[$key] = $this->raw[$key]; @@ -173,9 +127,5 @@ trait LazyVarUpdater{ function flushUpdateValues():void { $this->updatedVar = []; - if(key_exists('auxVar', $this->raw)){ - $this->auxUpdated = false; - unset($this->raw['auxVar']); - } } } \ No newline at end of file