시나리오용 EventEngine.... 틀만 완성!
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event;
|
||||||
|
|
||||||
|
abstract class Action{
|
||||||
|
public abstract function __construct($args=null);
|
||||||
|
public abstract function run($env=null);
|
||||||
|
|
||||||
|
public static function build($actionArgs){
|
||||||
|
if(!is_array($actionArgs)){
|
||||||
|
throw new \InvalidArgumentException('action을 입력해야 합니다.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$className = ___NAMESPACE__.'\\Action\\'.$actionArgs[0];
|
||||||
|
if(!class_exists($className)){
|
||||||
|
throw new \InvalidArgumentException('존재하지 않는 Action입니다 :'.$actionArgs[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$args = array_slice($actionArgs, 1);
|
||||||
|
$ref = new \ReflectionClass($className);
|
||||||
|
return $ref->newInstanceArgs($args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Action;
|
||||||
|
|
||||||
|
//기존 event_4.php
|
||||||
|
class CreateAdminNPC extends sammo\Event\Action{
|
||||||
|
public function __construct(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run($env=null){
|
||||||
|
return [__CLASS__, 'NYI'];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Action;
|
||||||
|
|
||||||
|
//기존 event_3.php
|
||||||
|
class CreateManyNPC extends sammo\Event\Action{
|
||||||
|
public function __construct($npcCount = 200){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run($env=null){
|
||||||
|
return [__CLASS__, 'NYI'];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Action;
|
||||||
|
|
||||||
|
//기존 event_1.php
|
||||||
|
class RaiseInavderStrong extends sammo\Event\Action{
|
||||||
|
public function __construct($npcCount = 200){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run($env=null){
|
||||||
|
return [__CLASS__, 'NYI'];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Action;
|
||||||
|
|
||||||
|
//기존 event_2.php
|
||||||
|
class RaiseInavderWeak extends sammo\Event\Action{
|
||||||
|
public function __construct($npcCount = 200){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run($env=null){
|
||||||
|
return [__CLASS__, 'NYI'];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event;
|
||||||
|
|
||||||
|
abstract class Condition{
|
||||||
|
public abstract function __construct($args=null);
|
||||||
|
public abstract function eval($env=null);
|
||||||
|
|
||||||
|
public static function build($conditionChain){
|
||||||
|
if(!is_array($conditionChain)){
|
||||||
|
return $conditionChain;
|
||||||
|
}
|
||||||
|
|
||||||
|
$className = 'sammo\\Event\\Condition\\'.$conditionChain[0];
|
||||||
|
if(class_exists($className)){
|
||||||
|
$args = [];
|
||||||
|
|
||||||
|
reset($conditionChain);
|
||||||
|
while (next($conditionChain) !== FALSE)
|
||||||
|
{
|
||||||
|
$args[] = static::build(current($conditionChain));
|
||||||
|
}
|
||||||
|
|
||||||
|
$ref = new \ReflectionClass($className);
|
||||||
|
return $ref->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 클래스여야 합니다.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Condition;
|
||||||
|
|
||||||
|
class ConstBool extends sammo\Event\Condition{
|
||||||
|
private $fixedResult = true;
|
||||||
|
|
||||||
|
public function __construct(bool $value){
|
||||||
|
$this->fixedResult = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function eval($env=null){
|
||||||
|
return [
|
||||||
|
'value'=>$this->fixedResult,
|
||||||
|
'chain'=>['ConstBool']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Condition;
|
||||||
|
|
||||||
|
class Interval extends sammo\Event\Condition{
|
||||||
|
//TODO:구현
|
||||||
|
public function __construct(...$args){
|
||||||
|
throw new \BadMethodCallException('Not Yet Implmented.');
|
||||||
|
|
||||||
|
//from Year, from Month, Interval, to Year, to Month
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function eval($env=null){
|
||||||
|
throw new \BadMethodCallException('Not Yet Implmented.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event\Condition;
|
||||||
|
|
||||||
|
class Logic extends sammo\Event\Condition{
|
||||||
|
private $mode = 'and';
|
||||||
|
private $conditions = [];
|
||||||
|
const AVAILABLE_LOGIC_NAME = [
|
||||||
|
'not'=>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']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 게임 내에서 매 월마다 EventHandler들을 점검하고, 이벤트를 일으키는 이벤트 엔진.
|
||||||
|
* 조건 방식이 '무식'하므로, 게임 내부에서 사용하지는 않고, 특수 시나리오를 구동하는데 사용.
|
||||||
|
* (예: 칠종칠금?)
|
||||||
|
*/
|
||||||
|
class Engine{
|
||||||
|
//TODO: 구현하라. 미래의 누군가. =P (메롱!)
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace sammo\Event;
|
||||||
|
|
||||||
|
class EventHandler{
|
||||||
|
|
||||||
|
private $condition = null;
|
||||||
|
private $actions = [];
|
||||||
|
|
||||||
|
public function __construct($rawCondition, $rawActions){
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user