feat: 여러턴 예약을 ReserveBulkdCommand로 대체

This commit is contained in:
2022-03-24 00:34:05 +09:00
parent 79b9e1b22a
commit acafb15215
5 changed files with 114 additions and 35 deletions
@@ -0,0 +1,76 @@
<?php
namespace sammo\API\Command;
use sammo\Session;
use DateTimeInterface;
use sammo\GameConst;
use sammo\Util;
use sammo\Validator;
use function sammo\setGeneralCommand;
class ReserveBulkCommand extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
foreach ($this->args as $idx => $turn) {
$v = new Validator($turn);
$v->rule('required', [
'action',
'turnList'
])
->rule('lengthMin', 'action', 1)
->rule('integerArray', 'turnList');
if (!$v->validate()) {
return "{$idx}:{$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)
{
$briefList = [];
foreach ($this->args as $idx => $turn) {
$action = $turn['action'];
$turnList = $turn['turnList'];
$arg = $turn['arg'] ?? [];
if (!$turnList) {
return "{$idx}: 턴이 입력되지 않았습니다";
}
if (!in_array($action, Util::array_flatten(GameConst::$availableGeneralCommand))) {
return "{$idx}: 사용할 수 없는 커맨드입니다.";
}
if (!is_array($arg)) {
return "{$idx}: 올바른 arg 형태가 아닙니다.";
}
$partialResult = setGeneralCommand($session->generalID, $turnList, $action, $arg);
if(!$partialResult['result']){
return [
'result' => false,
'briefList' => $briefList,
'errorIdx' => $idx,
'reason' => $partialResult['reason']
];
}
$briefList[$idx] = $partialResult['brief'];
}
return [
'result' => true,
'briefList' => $briefList,
'reason' => 'success'
];
}
}
+22 -25
View File
@@ -319,12 +319,13 @@ import { parseTime } from "@util/parseTime";
import { parseYearMonth } from "@util/parseYearMonth";
import DragSelect from "@/components/DragSelect.vue";
import { SammoAPI, type InvalidResponse } from "./SammoAPI";
import type { CommandItem, ReserveCommandResponse } from "@/defs";
import type { CommandItem, ReserveBulkCommandResponse, ReserveCommandResponse } from "@/defs";
import CommandSelectForm from "@/components/CommandSelectForm.vue";
import { BButton, BButtonGroup, BDropdownItem, BDropdown, BDropdownText, BDropdownDivider } from "bootstrap-vue-3";
import { StoredActionsHelper } from "./util/StoredActionsHelper";
import type { TurnObj } from '@/defs';
import { unwrap } from "./util/unwrap";
import type { Args } from "./processing/args";
type TurnObjWithTime = TurnObj & {
time: string;
@@ -620,37 +621,33 @@ async function reloadCommandList() {
async function reserveCommandDirect(args: [number[], TurnObj][], reload = true): Promise<boolean> {
const waiterList: Promise<ReserveCommandResponse | InvalidResponse>[] = [];
const query: {
turnList: number[],
action: string,
arg: Args
}[] = [];
for (const [turnList, { action, arg }] of args) {
waiterList.push(
SammoAPI.Command.ReserveCommand<ReserveCommandResponse, InvalidResponse>({
turnList,
action,
arg,
}, true)
);
query.push({
turnList,
action,
arg
});
}
let success = true;
for (const [idx, waiter] of waiterList.entries()) {
try {
const response = await waiter;
if (!response.result) {
const message = `${args[idx][0].join(',')}을 예약하지 못했습니다: ${response.reason}`
console.error(message, args[idx][1]);
alert(message);
success = false;
}
}
catch (e) {
const message = `${args[idx][0].join(',')}을 예약하지 못했습니다: ${e}`
console.error(message, args[idx][1]);
success = false;
}
try {
await SammoAPI.Command.ReserveBulkCommand<ReserveBulkCommandResponse>(query);
releaseSelectedTurnList();
} catch (e) {
console.error(e);
alert(`실패했습니다: ${e}`);
return false;
}
if (reload) {
await reloadCommandList();
}
return success;
return true;
}
function getSelectedTurnList(): number[] {
+7 -6
View File
@@ -3,17 +3,17 @@ import { APIPathGen } from "./util/APIPathGen";
import { callSammoAPI, type ValidResponse } from "./util/callSammoAPI";
export type { ValidResponse, InvalidResponse };
async function done<ResultType extends ValidResponse>(args?: Record<string, unknown>): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse>(args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
async function done<ResultType extends ValidResponse>(args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: false): Promise<ResultType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: true): Promise<ResultType | ErrorType>;
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType> {
async function done<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(args?: Record<string, unknown> | Record<string, unknown>[], returnError = false): Promise<ResultType | ErrorType> {
console.error(`Can't directly call. ${args}, ${returnError}. Use auto-generated path API.`);
return callSammoAPI<ResultType, ErrorType>([], args, true);
}
const apiRealPath = {
Betting:{
Betting: {
Bet: done,
GetBettingDetail: done,
GetBettingList: done,
@@ -23,6 +23,7 @@ const apiRealPath = {
PushCommand: done,
RepeatCommand: done,
ReserveCommand: done,
ReserveBulkCommand: done,
},
General: {
Join: done,
@@ -54,7 +55,7 @@ const apiRealPath = {
} as const;
export const SammoAPI = APIPathGen<typeof done, typeof apiRealPath>(apiRealPath, (path: string[]) => {
return (args?: Record<string, unknown>, returnError?: boolean) => {
return (args?: Record<string, unknown> | Record<string, unknown>[], returnError?: boolean) => {
if (returnError) {
return callSammoAPI(path.join('/'), args, true);
}
+5
View File
@@ -40,6 +40,11 @@ export type ReserveCommandResponse = {
brief: string,
}
export type ReserveBulkCommandResponse = {
result: true,
briefList: string[],
}
export type NationLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
export const NationLevelText: Record<NationLevel, string> = {
0: '방랑군',
+4 -4
View File
@@ -6,11 +6,11 @@ export type ValidResponse = {
result: true
}
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown>): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: false): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args?: Record<string, unknown> | Record<string, unknown>[]): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse>(path: string | string[], args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: false): Promise<ResultType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args: Record<string, unknown> | Record<string, unknown>[] | undefined, returnError: true): Promise<ResultType | ErrorType>;
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown>, returnError = false): Promise<ResultType | ErrorType> {
export async function callSammoAPI<ResultType extends ValidResponse, ErrorType extends InvalidResponse>(path: string | string[], args?: Record<string, unknown> | Record<string, unknown>[], returnError = false): Promise<ResultType | ErrorType> {
if (isArray(path)) {
path = path.join('/');
}