필사즉생 구현

This commit is contained in:
2020-03-12 21:57:57 +09:00
parent 1bc468095c
commit 38bef005f7
15 changed files with 297 additions and 28 deletions
@@ -0,0 +1,66 @@
<?php
namespace sammo\Constraint;
use \sammo\JosaUtil;
use \sammo\DB;
class AllowDiplomacyStatus extends Constraint{
const REQ_VALUES = Constraint::REQ_ARRAY_ARG;
protected $nationID;
protected $allowStatus = [];
public function checkInputValues(bool $throwExeception=true):bool{
if(!parent::checkInputValues($throwExeception) && !$throwExeception){
return false;
}
if(count($this->arg) != 3){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require nationID, allowStatus, errMsg tuple");
}
[$this->nationID, $this->allowStatus, $this->errMsg] = $this->arg;
if(!is_int($this->nationID)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("nationID {$this->nationID} must be int");
}
if(!is_array($this->allowStatus)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("allowStatus {$this->allowStatus} must be array");
}
if(!is_string($this->errMsg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("allowStatus {$this->errMsg} must be string");
}
foreach($this->allowStatus as $dipCode){
if(!is_int($dipCode)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("dipCode $dipCode must be int");
}
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
$db = DB::db();
$state = $db->queryFirstField(
'SELECT state FROM diplomacy WHERE me = %i AND `state` IN %li LIMIT 1',
$this->nationID,
array_keys($this->allowStatus)
);
if($state !== null){
return true;
}
$this->reason = $this->errMsg;
return false;
}
}
+6 -2
View File
@@ -7,6 +7,10 @@ class ConstraintHelper{
static function AdhocCallback(callable $callback):array{
return [__FUNCTION__, $callback];
}
static function AllowDiplomacyStatus(int $nationID, array $disallowList, string $errMsg):array{
return [__FUNCTION__, [$nationID, $disallowList, $errMsg]];
}
static function AllowJoinAction():array{
return [__FUNCTION__];
@@ -212,8 +216,8 @@ class ConstraintHelper{
return [__FUNCTION__, $reqRice];
}
static function ReqNationValue($key, string $keyNick, string $comp, $reqVal):array{
return [__FUNCTION__, [$key, $keyNick, $comp, $reqVal]];
static function ReqNationValue($key, string $keyNick, string $comp, $reqVal, ?string $errMsg=null):array{
return [__FUNCTION__, [$key, $keyNick, $comp, $reqVal, $errMsg]];
}
static function ReqTroopMembers():array{
@@ -59,7 +59,7 @@ class DisallowDiplomacyStatus extends Constraint{
if($state === null){
return true;
}
$this->msg = $this->disallowStatus[$state];
$this->reason = $this->disallowStatus[$state];
return false;
}
}
+11 -5
View File
@@ -16,14 +16,15 @@ class ReqNationValue extends Constraint{
protected $keyNick;
protected $reqVal;
protected $comp;
protected $errMsg;
public function checkInputValues(bool $throwExeception=true):bool{
if(!parent::checkInputValues($throwExeception) && !$throwExeception){
return false;
}
if(count($this->arg) == 4){
[$this->key, $this->keyNick, $comp, $this->reqVal] = $this->arg;
if(count($this->arg) == 5){
[$this->key, $this->keyNick, $comp, $this->reqVal, $this->errMsg] = $this->arg;
if(!in_array($comp, ['>', '>=', '==', '<=', '<', '!=', '===', '!=='])){
if(!$throwExeception){return false; }
@@ -32,7 +33,7 @@ class ReqNationValue extends Constraint{
}
else{
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require key, keyNick, comp, reqVal");
throw new \InvalidArgumentException("require key, keyNick, comp, reqVal[, errMsg]");
}
$this->comp = $comp;
@@ -47,7 +48,7 @@ class ReqNationValue extends Constraint{
if(is_numeric($this->reqVal)){
$this->isPercent = false;
}
else if(is_str($this->reqVal)){
else if(is_string($this->reqVal)){
$this->reqVal = Util::convPercentStrToFloat($this->reqVal);
if($this->reqVal === null){
if(!$throwExeception){return false; }
@@ -121,7 +122,12 @@ class ReqNationValue extends Constraint{
}
$josaYi = JosaUtil::pick($keyNick, '이');
$this->reason = "{$keyNick}{$josaYi} {$result}";
if($this->errMsg){
$this->reason = $this->errMsg;
}
else{
$this->reason = "{$keyNick}{$josaYi} {$result}";
}
return false;
}