초기 constraint 지정

This commit is contained in:
2018-09-02 13:17:26 +09:00
parent 16831880dd
commit df6b2bd0e8
6 changed files with 302 additions and 0 deletions
+157
View File
@@ -0,0 +1,157 @@
<?php
namespace sammo\Constraint;
abstract class Constraint{
private function __construct(){
}
const REQ_GENERAL = 0x10;
const REQ_CITY = 0x20;
const REQ_NATION = 0x40;
const REQ_ARG = 0x80;
const REQ_STRING_ARG = self::REQ_ARG | 0x100;
const REQ_INT_ARG = self::REQ_ARG | 0x200;
const REQ_NUMERIC_ARG = self::REQ_ARG | 0x400;
const REQ_ARRAY_ARG = self::REQ_ARG | 0x800;
const REQ_VALUES = 0;
protected $general = null;
protected $city = null;
protected $nation = null;
protected $arg = null;
protected $destGeneral = null;
protected $destCity = null;
protected $destNation = null;
protected $tested = false;
protected $reason = null;
abstract public function test():bool;
static public function requiredValueType():int{
return static::REQ_VALUES;
}
public function general(array $general){
$this->general = $general;
$this->tested = false;
$this->reason = null;
}
public function city(array $city){
$this->city = $city;
$this->tested = false;
$this->reason = null;
}
public function nation(array $nation){
$this->nation = $nation;
$this->tested = false;
$this->reason = null;
}
public function arg($arg){
$this->arg = $arg;
$this->tested = false;
$this->reason = null;
}
public function destGeneral(array $general){
$this->destGeneral = $general;
$this->tested = false;
$this->reason = null;
}
public function destCity(array $city){
$this->destCity = $city;
$this->tested = false;
$this->reason = null;
}
public function destNation(array $nation){
$this->destNation = $nation;
$this->tested = false;
$this->reason = null;
}
static public function build(array $input):this{
$self = new static();
foreach($input as $key=>$value){
switch($key){
case 'general': $self->general($value); break;
case 'city': $self->city($value); break;
case 'nation': $self->nation($value); break;
case 'arg': $self->arg($value); break;
case 'destGeneral': $self->destGeneral($value); break;
case 'destCity': $self->destCity($value); break;
case 'destNation': $self->destNation($value); break;
}
}
return $self;
}
public function checkInputValues(bool $throwExeception=true):bool{
$valueType = static::requiredValueType();
if(($valueType&REQ_GENERAL) && $this->general === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require general');
}
if(($valueType&REQ_CITY) && $this->city === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require city');
}
if(($valueType&REQ_NATION) && $this->nation === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require nation');
}
if (!($valueType&REQ_ARG)) {
return true;
}
if($valueType === null){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require arg');
}
if(($valueType&REQ_STRING_ARG) && !is_string($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require string arg');
}
if(($valueType&REQ_INT_ARG) && !is_int($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require int arg');
}
if(($valueType&REQ_NUMERIC_ARG) && !is_numeric($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require numeric arg');
}
if(!($valueType&REQ_ARRAY_ARG) && !is_array($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException('require array arg');
}
return true;
}
public function reason($withTest=true):?string{
if(!$this->tested === false){
if($withTest){
$this->test();
}
else{
throw new \RuntimeException('test가 실행되지 않음');
}
}
return $this->reason;
}
}
@@ -0,0 +1,47 @@
<?php
namespace sammo\Constraint;
use \sammo\JosaUtil;
class NoFullCityCapacity extends Constraint{
const REQ_VALUES = Constraint::REQ_CITY|Constraint::REQ_ARRAY_ARG;
protected $key;
protected $maxKey;
protected $keyNick;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
[$this->key, $this->keyNick] = $this->arg;
$this->maxKey = $this->key.'2';
if(!key_exists($this->key, $this->city)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require {$this->key} in city");
}
if(!key_exists($this->maxKey, $this->city)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require {$this->maxKey} in city");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->city[$this->key] < $this->city[$this->maxKey]){
return true;
}
$josaUn = JosaUtil::pick($keyNick, '은');
$this->reason = "{$keyNick}{$josaUn} 충분합니다.";
return false;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class NoOpeningPart extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('level', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require level in general");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->general['level'] != 0){
return true;
}
$this->reason = "재야입니다.";
return false;
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace sammo\Constraint;
class NoOpeningPart extends Constraint{
const REQ_VALUES = Constraint::REQ_INT_ARG;
protected $relYear;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
$this->relYear = $this->arg;
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($relYear >= GameConst::$openingPartYear){
return true;
}
$this->reason = "초반 제한 중에는 불가능합니다.";
return false;
}
}
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class NoOpeningPart extends Constraint{
const REQ_VALUES = Constraint::REQ_NATION;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('level', $this->nation)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require level in nation");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->nation['level'] != 0){
return true;
}
$this->reason = "방랑군은 불가능합니다.";
return false;
}
}
+3
View File
@@ -90,4 +90,7 @@ class GameConstBase
public static $maxTurn = 24;
public static $statGradeLevel = 5;
/** @var int 초반 제한 기간 */
public static $openingPartYear = 3;
}