From a03eac6f2a8b76387dcd33b8e840c7848ac5c3fd Mon Sep 17 00:00:00 2001 From: hide_d Date: Wed, 28 Mar 2018 02:45:44 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=9C=EB=82=98=EB=A6=AC=EC=98=A4=EC=9A=A9?= =?UTF-8?q?=20EventEngine....=20=ED=8B=80=EB=A7=8C=20=EC=99=84=EC=84=B1!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- twe/sammo/Event/Action.php | 22 ++++ twe/sammo/Event/Action/CreateAdminNPC.php | 14 +++ twe/sammo/Event/Action/CreateManyNPC.php | 13 +++ twe/sammo/Event/Action/RaiseInvaderStrong.php | 13 +++ twe/sammo/Event/Action/RaiseInvaderWeak.php | 13 +++ twe/sammo/Event/Condition.php | 43 +++++++ twe/sammo/Event/Condition/ConstBool.php | 17 +++ twe/sammo/Event/Condition/Interval.php | 16 +++ twe/sammo/Event/Condition/Logic.php | 105 ++++++++++++++++++ twe/sammo/Event/Engine.php | 11 ++ twe/sammo/Event/EventHandler.php | 32 ++++++ 11 files changed, 299 insertions(+) create mode 100644 twe/sammo/Event/Action.php create mode 100644 twe/sammo/Event/Action/CreateAdminNPC.php create mode 100644 twe/sammo/Event/Action/CreateManyNPC.php create mode 100644 twe/sammo/Event/Action/RaiseInvaderStrong.php create mode 100644 twe/sammo/Event/Action/RaiseInvaderWeak.php create mode 100644 twe/sammo/Event/Condition.php create mode 100644 twe/sammo/Event/Condition/ConstBool.php create mode 100644 twe/sammo/Event/Condition/Interval.php create mode 100644 twe/sammo/Event/Condition/Logic.php create mode 100644 twe/sammo/Event/Engine.php create mode 100644 twe/sammo/Event/EventHandler.php 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