diff --git a/twe/sammo/Event/Action.php b/twe/sammo/Event/Action.php new file mode 100644 index 00000000..70a60a42 --- /dev/null +++ b/twe/sammo/Event/Action.php @@ -0,0 +1,22 @@ +newInstanceArgs($args); + } +} \ No newline at end of file diff --git a/twe/sammo/Event/Action/CreateAdminNPC.php b/twe/sammo/Event/Action/CreateAdminNPC.php new file mode 100644 index 00000000..cf511809 --- /dev/null +++ b/twe/sammo/Event/Action/CreateAdminNPC.php @@ -0,0 +1,14 @@ +newInstanceArgs($args); + } + + //array의 첫번째 값이 Condition이 아닌 경우에는 그냥 배열로 처리함. + return array_map(static::build, $conditionChain); + } + + protected static function _eval($arg, $env=null){ + if(is_bool($arg)){ + return [ + 'value'=>$arg, + 'chain'=>['boolean'] + ]; + } + if($arg instanceof Condition){ + return $arg->checkCondition($env); + } + throw new \InvalidArgumentException('평가 인자는 boolean이거나 Condition 클래스여야 합니다.'); + } +} \ No newline at end of file diff --git a/twe/sammo/Event/Condition/ConstBool.php b/twe/sammo/Event/Condition/ConstBool.php new file mode 100644 index 00000000..89f64930 --- /dev/null +++ b/twe/sammo/Event/Condition/ConstBool.php @@ -0,0 +1,17 @@ +fixedResult = $value; + } + + public function eval($env=null){ + return [ + 'value'=>$this->fixedResult, + 'chain'=>['ConstBool'] + ]; + } +} \ No newline at end of file diff --git a/twe/sammo/Event/Condition/Interval.php b/twe/sammo/Event/Condition/Interval.php new file mode 100644 index 00000000..b15c56ac --- /dev/null +++ b/twe/sammo/Event/Condition/Interval.php @@ -0,0 +1,16 @@ +false, + 'and'=>true, + 'or'=>true, + 'xor'=>true + ]; + + public function __construct(string $mode, ...$conditions){ + $mode = strtolower($mode); + if(!array_key_exists($mode, self::AVAILABLE_LOGIC_NAME)){ + throw new \InvalidArgumentException('첫번째 인자는 not, and, or, xor 중 하나여야 합니다.'); + } + + if(!self::AVAILABLE_LOGIC_NAME[$mode] && count($conditions)>1){ + throw new \InvalidArgumentException('조건을 하나만 받을 수 있습니다.'); + } + + $this->mode = $mode; + $this->conditions = $conditions; + } + + public function eval($env=null){ + switch($this->$mode){ + case 'not': + return $this->logicNot($env); + case 'and': + return $this->logicAnd($env); + case 'or': + return $this->logicOr($env); + case 'xor': + return $this->logicXor($env); + } + + throw new \InvalidArgumentException('올바르지 않은 mode.'); + } + + private function logicNot($env){ + $sub = self::_eval($this->conditions[0], $env); + $result['value'] = !$result['value']; + $result['chain'][] = 'not'; + return $result; + } + + private function logicAnd($env){ + $value = true; + $chain = []; + + foreach($this->conditions as $cond){ + $sub = self::_eval($cond, $env); + $chain[] = $sub['chain']; + if(!$sub['value']){ + $result['value'] = false; + break; + } + } + + return [ + 'value'=>$value, + 'chain'=>[$chain, 'and'] + ]; + } + + private function logicOr($env){ + $value = false; + $chain = []; + + foreach($this->conditions as $cond){ + $sub = self::_eval($cond, $env); + $chain[] = $sub['chain']; + if($sub['value']){ + $result['value'] = true; + break; + } + } + + return [ + 'value'=>$value, + 'chain'=>[$chain, 'or'] + ]; + } + + private function logicXor($env){ + $value = false; + $chain = []; + + foreach($this->conditions as $cond){ + $sub = self::_eval($cond, $env); + $chain[] = $sub['chain']; + $value ^= $sub['value']; + } + + return [ + 'value'=>$value, + 'chain'=>[$chain, 'xor'] + ]; + } + + +} \ No newline at end of file diff --git a/twe/sammo/Event/Engine.php b/twe/sammo/Event/Engine.php new file mode 100644 index 00000000..527b9292 --- /dev/null +++ b/twe/sammo/Event/Engine.php @@ -0,0 +1,11 @@ +condition = Condition::build($rawCondition); + foreach($rawActions as $rawActio){ + $this->condition = Action::build($rawAction); + } + + } + + public function tryRunEvent(array $env=null){ + $result = $this->condition->eval($env); + + if(!$result['value']){ + return $result; + } + + $resultAction = []; + foreach($this->actions as $action){ + $resultAction[] = $action->run(); + } + $result['action'] = $resultAction; + + return $result; + } +} \ No newline at end of file