feat: 새 부대편성 페이지 (#223)

Reviewed-on: https://storage.hided.net/gitea/devsam/core/pulls/223
This commit was merged in pull request #223.
This commit is contained in:
2022-07-27 18:01:41 +09:00
parent 308b1f2348
commit a0dee353b5
19 changed files with 1397 additions and 410 deletions
+65 -40
View File
@@ -2,6 +2,7 @@
namespace sammo\API\Nation;
use ArrayObject;
use sammo\DB;
use sammo\General;
use sammo\Session;
@@ -45,10 +46,11 @@ class GeneralList extends \sammo\BaseAPI
'connect' => 0,
'troop' => 0,
'city' => 0,
'con' => 1,
'specage' => 1,
'specage2' => 1,
'specage' => 0,
'specage2' => 0,
'leadership_exp' => 1,
'strength_exp' => 1,
'intel_exp' => 1,
@@ -58,7 +60,6 @@ class GeneralList extends \sammo\BaseAPI
'dex4' => 1,
'dex5' => 1,
'city' => 1,
'experience' => 1,
'dedication' => 1,
@@ -158,35 +159,59 @@ class GeneralList extends \sammo\BaseAPI
$rawGeneralList = Util::convertArrayToDict($db->query('SELECT %l from general WHERE nation = %i ORDER BY turntime ASC', Util::formatListOfBackticks($queryColumns), $nationID), 'no');
/** @var ArrayObject[] */
$troops = [];
foreach ($db->queryAllLists('SELECT troop_leader,name FROM troop WHERE nation = %i', $nationID) as [$troopLeaderID, $troopName]) {
if (!key_exists($troopLeaderID, $rawGeneralList)) {
continue;
}
$troopTurnTime = $rawGeneralList[$troopLeaderID]['turntime'];
$troops[$troopLeaderID] = new ArrayObject([
'id' => $troopLeaderID,
'name' => $troopName,
'turntime' => $troopTurnTime,
'reservedCommand' => [],
]);
}
$reservedCommand = [];
if ($this->permission >= 1) {
$nonNPCGeneralIDList = [];
foreach ($rawGeneralList as $rawGeneral) {
if ($rawGeneral['npc'] < 2) {
$nonNPCGeneralIDList[] = $rawGeneral['no'];
if ($this->permission >= 1 || count($troops)) {
$reservedCommandTargetGeneralIDList = [];
if($this->permission >= 1){
foreach ($rawGeneralList as $rawGeneral) {
if ($rawGeneral['npc'] < 2) {
$reservedCommandTargetGeneralIDList[$rawGeneral['no']] = $rawGeneral['no'];
}
}
}
foreach($troops as $troop){
$reservedCommandTargetGeneralIDList[$troop['id']] = $troop['id'];
}
$rawTurnList = $db->query(
'SELECT general_id, turn_idx, action, arg, brief FROM general_turn WHERE general_id IN %li AND turn_idx < 5 ORDER BY general_id asc, turn_idx asc',
$nonNPCGeneralIDList
);
foreach ($rawTurnList as $rawTurn) {
[
'general_id' => $generalID,
'action' => $action,
'arg' => $arg,
'brief' => $brief,
] = $rawTurn;
if (!key_exists($generalID, $reservedCommand)) {
$reservedCommand[$generalID] = [];
if($reservedCommandTargetGeneralIDList){
$rawTurnList = $db->query(
'SELECT general_id, turn_idx, action, arg, brief FROM general_turn WHERE general_id IN %li AND turn_idx < 5 ORDER BY general_id asc, turn_idx asc',
array_values($reservedCommandTargetGeneralIDList)
);
foreach ($rawTurnList as $rawTurn) {
[
'general_id' => $generalID,
'action' => $action,
'arg' => $arg,
'brief' => $brief,
] = $rawTurn;
if (!key_exists($generalID, $reservedCommand)) {
$reservedCommand[$generalID] = [];
}
$reservedCommand[$generalID][] = [
'action' => $action,
'arg' => $arg,
'brief' => $brief
];
}
$reservedCommand[$generalID][] = [
'action' => $action,
'arg' => $arg,
'brief' => $brief
];
}
}
@@ -252,6 +277,17 @@ class GeneralList extends \sammo\BaseAPI
$resultColumns[$column] = $column;
}
foreach ($troops as $troop){
$troopLeaderID = $troop['id'];
$troop['reservedCommand'] = array_map(function($turnObj){
$brief = $turnObj['brief'];
if($brief == '집합'){
return $brief;
}
return '-';
}, $specialViewFilter['reservedCommand']($rawGeneralList[$troopLeaderID]));
}
$generalList = [];
foreach ($rawGeneralList as $rawGeneral) {
//General 생성?
@@ -272,25 +308,14 @@ class GeneralList extends \sammo\BaseAPI
$generalList[] = $item;
}
$troops = [];
foreach ($db->queryAllLists('SELECT troop_leader,name FROM troop WHERE nation = %i', $nationID) as [$troopLeaderID, $troopName]) {
if (!key_exists($troopLeaderID, $rawGeneralList)) {
continue;
}
$troopTurnTime = $rawGeneralList[$troopLeaderID]['turntime'];
$troops[] = [
'id' => $troopLeaderID,
'name' => $troopName,
'turntime' => $troopTurnTime
];
}
$result = [
'result' => true,
'permission' => $this->permission,
'column' => array_keys($resultColumns),
'list' => $generalList,
'troops' => $troops,
'troops' => array_values($troops),
'env' => $env,
'myGeneralID' => $session->generalID,
];
return $result;
+1
View File
@@ -10,6 +10,7 @@ use sammo\Validator;
use function sammo\checkSecretPermission;
/** @deprecated */
class SetTroopName extends \sammo\BaseAPI
{
public function validateArgs(): ?string
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace sammo\API\Troop;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
class ExitTroop extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$generalID = $session->generalID;
$db = DB::db();
$troopID = $db->queryFirstField('SELECT troop FROM general WHERE no = %i', $generalID);
if($troopID == 0){
return '부대에 소속되어 있지 않습니다.';
}
if($generalID != $troopID){
$db->update('general', [
'troop' => 0,
], '`no` = %i', $generalID);
return null;
}
//부대장이다.
$db->update('general', [
'troop' => 0,
], '`troop` = %i', $troopID);
$db->delete('troop', 'troop_leader = %i', $troopID);
return null;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace sammo\API\Troop;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
use sammo\StringUtil;
use sammo\Validator;
class JoinTroop extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('required', [
'troopID',
])
->rule('integer', 'troopID');
if (!$v->validate()) {
return $v->errorStr();
}
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$userID = $session->userID;
$troopID = $this->args['troopID'];
$db = DB::db();
$me = $db->queryFirstRow('SELECT `no`,nation,`troop`,`officer_level`,permission,penalty FROM general WHERE `owner`=%i', $userID);
if ($me['troop'] != 0) {
return '이미 부대에 소속되어 있습니다.';
}
$nationID = $me['nation'];
if ($nationID == 0) {
return '국가에 소속되어 있지 않습니다.';
}
$troopExists = $db->queryFirstField('SELECT `troop_leader` FROM `troop` WHERE `troop_leader` = %i AND `nation` = %i', $troopID, $nationID);
if (!$troopExists) {
return '부대가 올바르지 않습니다.';
}
$generalID = $me['no'];
$db->update('general', [
'troop' => $troopID,
], '`no` = %i AND `troop` = %i', $generalID, 0);
return null;
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace sammo\API\Troop;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
use sammo\Validator;
class KickFromTroop extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('required', [
'troopID',
'generalID',
])
->rule('integer', 'troopID')
->rule('integer', 'generalID');
if (!$v->validate()) {
return $v->errorStr();
}
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$generalID = $this->args['generalID'];
$db = DB::db();
$troopID = $db->queryFirstField('SELECT troop FROM general WHERE no = %i', $generalID);
if($troopID == 0){
return '부대에 소속되어 있지 않습니다.';
}
if($troopID != $this->args['troopID']){
return '다른 부대에 소속되어 있습니다.';
}
if($troopID == $generalID){
return '부대장을 추방할 수 없습니다.';
}
$db->update('general', [
'troop' => 0,
], '`no` = %i AND `troop` = %i', $generalID, $troopID);
return null;
}
}
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace sammo\API\Troop;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
use sammo\StringUtil;
use sammo\Validator;
class NewTroop extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('required', [
'troopName',
])
->rule('stringWidthBetween', 'troopName', 1, 18);
if (!$v->validate()) {
return $v->errorStr();
}
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$userID = $session->userID;
$troopName = StringUtil::neutralize($this->args['troopName']);
if(!$troopName){
return '부대 이름이 없습니다.';
}
$db = DB::db();
$me = $db->queryFirstRow('SELECT `no`,nation,`troop`,`officer_level`,permission,penalty FROM general WHERE `owner`=%i', $userID);
if($me['troop'] != 0){
return '이미 부대에 소속되어 있습니다.';
}
$nationID = $me['nation'];
if($nationID == 0){
return '국가에 소속되어 있지 않습니다.';
}
$generalID = $me['no'];
$db->insert('troop', [
'name'=>$troopName,
'troop_leader'=>$generalID,
'nation'=>$nationID,
]);
if($db->affectedRows() == 0){
return '부대가 생성되지 않았습니다. 버그일 수 있습니다.';
}
$db->update('general', [
'troop'=>$generalID
], '`no` = %i', $generalID);
return null;
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace sammo\API\Troop;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
use sammo\StringUtil;
use sammo\Validator;
use function sammo\checkSecretPermission;
class SetTroopName extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('required', [
'troopID',
'troopName',
])
->rule('stringWidthBetween', 'troopName', 1, 18)
->rule('integer', 'troopID');
if (!$v->validate()) {
return $v->errorStr();
}
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN | static::REQ_READ_ONLY;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$userID = $session->userID;
$db = DB::db();
$me = $db->queryFirstRow('SELECT `no`,nation,`officer_level`,permission,penalty FROM general WHERE `owner`=%i', $userID);
$permission = checkSecretPermission($me, false);
$troopID = $this->args['troopID'];
$generalID = $me['no'];
if($generalID != $troopID && $permission < 4){
return "권한이 부족합니다.";
}
$troopName = StringUtil::neutralize($this->args['troopName']);
if(!$troopName){
return '부대 이름이 없습니다.';
}
$nationID = $me['nation'];
$db->update('troop', [
'name'=>$troopName
], 'troop_leader=%i AND `nation`=%i',$troopID, $nationID);
if($db->affectedRows() == 0){
return '부대가 없습니다.';
}
return null;
}
}