refac: LazyVarAndAuxUpdater

- LazyVarUpdater에다가 aux var 대응 분리
This commit is contained in:
2023-08-01 16:28:29 +00:00
parent ad23398592
commit 8aeb4883ff
3 changed files with 76 additions and 51 deletions
+2
View File
@@ -12,6 +12,8 @@ use sammo\WarUnitTrigger as WarUnitTrigger;
class General extends GeneralBase implements iAction
{
use LazyVarAndAuxUpdater;
/** @var Map<RankColumn,int> */
protected Map $rankVarRead;
/** @var Map<RankColumn,int> */
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace sammo;
trait LazyVarAndAuxUpdater{
use LazyVarUpdater;
protected $auxVar = null;
protected $auxUpdated = false;
function getRaw(bool $extractAux=false):array{
if($extractAux){
$this->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']);
}
}
}
+1 -51
View File
@@ -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']);
}
}
}