Author SHA1 Message Date
Hide_D 81e101ffef 갱신 행의 ID를 보존하고 예약턴의 불필요한 시퀀스를 제거한다 2026-11-14 18:44:22 +09:00
Hide_D bc54b157c1 고빈도 저장 ID를 64비트로 확장하고 랭킹 갱신의 시퀀스 낭비를 막는다 2026-11-14 15:34:21 +09:00
Hide_D 087517821e 운영 안정화 변경을 최신 main과 통합한다 2026-11-14 15:13:53 +09:00
Hide_D f36bc2c8d7 VM 복귀 후 예약턴 잠금 복구와 Windows 부팅 식별자 확인을 추가한다 2026-11-12 09:16:04 +09:00
Hide_D fd8145d6eb 감사 유지보수 중 새 기수 예약과 identity 변경 대응을 문서화한다 2026-11-09 21:54:57 +09:00
Hide_D 79de6d484c 감사 정리 CLI가 게임 데몬 실행 역할을 상속하지 않게 한다 2026-11-09 20:49:20 +09:00
Hide_D 078b9aa205 감사 상세를 무손실 압축하고 이전 기수 정리 처리량을 개선한다 2026-11-09 20:43:05 +09:00
Hide_D c81d304073 feat: 카카오 계정의 본인 복구와 인증 후 연결 교체를 추가한다 2026-11-07 21:31:16 +09:00
Hide_D d1cdf8a85f feat: 관리자가 사유를 남겨 탈퇴 예약을 취소하도록 추가한다 2026-11-07 20:48:19 +09:00
Hide_D dcecb32ef4 feat: 관리자 행동별 모든 서버 선택과 권한 묶음 표시를 추가한다 2026-11-07 00:39:25 +09:00
Hide_D 257a2bea10 feat: 탈퇴 취소와 이관 계정 복구 예외를 추가한다 2026-11-07 00:24:57 +09:00
Hide_D f16d33e34c fix: 세력도시 기본 목록을 지역과 규모순으로 정렬한다 2026-11-02 10:39:12 +09:00
Hide_D da79013226 fix: 국호와 예약 인자의 불필요한 앰퍼샌드 escape를 제거한다 2026-10-29 23:35:17 +09:00
Hide_D 6a23e4ca08 fix: 턴 저장 트랜잭션의 연결 획득 대기를 보완한다 2026-10-26 17:57:58 +09:00
Hide_D 8c877b4a6f fix: 장수일람 특기 설명 줄바꿈을 공통 처리한다 2026-10-23 09:55:51 +09:00
Hide_D b3f7a419a9 운영 보안을 보존하여 빌드 개선 배포본 통합 2026-10-23 01:33:42 +09:00
Hide_D af51968782 빌드 선언 생성 병목과 Vue 중복 타입 검사를 개선 2026-10-23 01:33:34 +09:00
Hide_D b377250fb9 fix: 첫 빌드 전 닫힌 서버도 패치 정책으로 오픈 요청을 받는다 2026-10-23 00:42:11 +09:00
Hide_D d4a87c981f fix: 오픈 요청 화면의 복사 전용 안내를 실제 등록 흐름에 맞춘다 2026-10-23 00:35:54 +09:00
Hide_D e42eaa4e58 fix: 오픈 요청 관계의 갱신 규칙을 migration 제약과 맞춘다 2026-10-23 00:23:33 +09:00
Hide_D 2662091a6a merge: 최신 main의 NPC 정책과 운영 보완을 오픈 요청에 통합한다 2026-10-23 00:18:45 +09:00
Hide_D 236f1bc4ed feat: 정식 사용자의 오픈 요청과 동의 기반 예약을 영속화한다 2026-10-23 00:18:45 +09:00
Hide_D 8e5bd94cb8 NPC 사령턴에서 비활성 발령 절차를 건너뛰는지 검증한다 2026-10-23 00:01:51 +09:00
Hide_D e5a39cb714 최신 main의 수비 설정 수정을 NPC 정책 작업에 통합한다 2026-10-22 23:50:35 +09:00
Hide_D 039c5d2a59 NPC 정책 적용을 검증하고 자국 변경 이력 조회를 제공한다 2026-10-22 23:50:35 +09:00
Hide_D 856187b315 수비끔 전환에서 전투 훈련·사기 상한을 보존한다 2026-10-22 23:11:52 +09:00
Hide_D b67924a71d chore: Node.js 24.22.1 보안 릴리스로 개발 버전을 고정한다 2026-10-22 17:18:48 +09:00
Hide_D 4e668b9fb6 fix: 공개 API와 토너먼트의 사용자·장수 권한 경계를 강화한다 2026-10-10 01:56:31 +09:00
Hide_D 5771701007 이미지 업로드와 WebPush 응답의 자원 경계를 제한한다 2026-10-09 07:09:23 +09:00
Hide_D c5716cf144 SSE 연결과 미완료 준비 작업의 자원 상한을 보강한다 2026-10-09 06:30:04 +09:00
Hide_D 005f88d7ca 게임 토큰 회수를 Redis에 유지하고 재시작과 알림 누락을 방어한다 2026-10-09 05:59:28 +09:00
Hide_D eeda225429 게임 세션의 원자적 발급과 추적 상한 및 회수를 보강한다 2026-10-09 05:10:05 +09:00
Hide_D 6755f09ee7 원격 릴리스 빌드에 토큰 파일 인증과 응답 제한을 적용한다 2026-10-09 04:31:44 +09:00
Hide_D 3d9f7392df 온라인 인증 시도와 처리 비용 및 Kakao 응답 경계를 제한한다 2026-10-09 03:43:59 +09:00
Hide_D 9dceaf8e4e SSE 헤더 인증과 연결 수명 검증 및 HTTP 오류 정보 보호 2026-10-09 02:35:45 +09:00
Hide_D 87f810a878 취약 의존성을 갱신하고 PM2 파일 감시를 비활성화한다 2026-10-09 01:35:28 +09:00
Hide_D e378936051 OAuth state 중복 소비와 Web Push 내부망 요청을 차단한다 2026-10-09 00:59:06 +09:00
Hide_D df6c9269fa 밸런스 실험에 모집 경제와 조건부 효과 대조를 보강한다 2026-09-29 11:26:00 +00:00
Hide_D 4b00c1052d 내부 밸런스 비교 CLI와 조건별 대조 실험을 추가한다 2026-09-29 10:55:43 +00:00
Hide_D b060bc83bf 도시 관직 요청 화면의 공통 글자 크기 통합 검증을 추가한다 2026-09-29 09:52:11 +00:00
Hide_D 7c65cf60f3 프로필 UI 글자 크기를 네 단계와 공통 축소 규칙으로 통일한다 2026-09-29 09:41:39 +00:00
Hide_D 5bafdfc1e7 인사부 도시 관직 자원과 기본 처리 및 수뇌 알림 추가 2026-09-29 09:41:01 +00:00
Hide_D cd6ded4970 중복 문서를 통합하고 완료된 구현 계획을 정리한다 2026-09-29 09:08:28 +00:00
Hide_D 08c3d0e936 독자 수준별 구조 문서와 게이머 입문·전술 안내를 재정비한다 2026-09-29 08:53:30 +00:00
Hide_D 71155186b7 짧은 UI 텍스트의 세로 정렬과 두 축 정렬 원칙을 적용한다 2026-09-29 08:51:05 +00:00
Hide_D 295ecb39a3 fix: 전용 장수 아이콘 뒤 기본 이미지 중첩 제거 2026-09-29 08:43:13 +00:00
Hide_D aea5056834 NPC 감사에서 최근 기록 월을 기본 조회하고 실제 시나리오로 검증한다 2026-09-26 17:37:32 +00:00
Hide_D 6b65633324 감사 수집과 저장 장애를 게임 진행에서 분리한다 2026-09-26 16:26:02 +00:00
Hide_D daf7a87799 감사 정책 head 유실을 복구하고 데몬 준비 상태로 배포를 검증한다 2026-09-26 15:21:17 +00:00
Hide_D 11d0a4afdd fix: 턴 상세 입력 확인 영역을 콘텐츠 높이에 맞춰 배치 2026-09-26 14:30:27 +00:00
Hide_D 51d0317a69 플레이 감사에 장수 수치 정렬과 시점별 도시 지도를 추가한다 2026-09-26 04:52:18 +00:00
Hide_D e35679335c 플레이 감사를 3단 대시보드와 국가 추이 그래프로 개선한다 2026-09-26 04:36:31 +00:00
Hide_D 2b2edeaeff 왕조 연감에 해당 기수의 지도 레이아웃을 적용한다 2026-09-24 16:03:51 +00:00
Hide_D 8823434610 계정 아이콘 등록과 목록 내리기 제한을 조정한다 2026-09-24 15:46:11 +00:00
Hide_D 4bf812b601 예약 초기화에서 구버전 릴리스 빌드 호환성을 확인한다 2026-09-24 05:47:46 +00:00
Hide_DandClaude Opus 5.5 3c75de16e9 사령부 명령 목록을 자기 수뇌 열과 클릭한 턴에 맞춘다
1000px 사령부 desktop에서 compact 명령 목록이 `position: fixed`로 첫째 줄 첫째 열에
고정되어, 다른 수뇌 자리에서 입력해도 군주 열 위에 떠 있던 문제를 고친다.
Ref ChiefReservedCommand처럼 목록을 자기 수뇌 열 안에 두고, 턴을 누르면 그 행
바로 아래에, 고급 모드의 턴 미지정 목록은 턴 표 하단 기준으로 위로 펼친다.
수뇌 12·6·9·5 위치별 Chromium geometry 회귀 테스트를 추가한다.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 16:49:45 +00:00
Hide_D 08be92b6fa 조건부 통합 테스트 등록과 시계 fixture를 바로잡는다 2026-09-23 16:21:52 +00:00
Hide_DandClaude Opus 5.5 7e1783d651 장비매매 불가 표시·숙련전환 전후 미리보기·건국 성향표를 Ref처럼 복원
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 15:45:25 +00:00
Hide_DandClaude Opus 5.5 f74c3322b7 국가 대상 명령 입력에 Ref 권유문·원조 한도·피장파장 전략 대기 복원
- 임관·장수대상임관: 정제된 임관 권유문 목록(크게/작게 보기, 940px 기준 2열)과
  임관 대상 국가별 가능 여부(AllowJoinDestNation)를 명령표에 싣는다.
- 물자원조: 작위별 원조 한도표를 보이고 현재 작위를 굵은 밑줄로 표시한다.
- 피장파장: 전략 선택지를 원본 key 대신 전략 이름으로 보이고, 재사용 대기 중인
  전략은 `(불가, N턴)` 붉은색으로 표시한다. 선포·전쟁 국가만 대상 가능으로 둔다.
- 이호경식·급습: Ref 외교 조건으로 대상 국가 가능 여부를 계산한다.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 15:24:31 +00:00
Hide_DandClaude Opus 5.5 4a50c354fe 등용·장수대상임관 대상의 타국 장수 정보를 Ref 공개 범위로 제한하고 국가색 묶음 복원
- 명령표의 등용·장수대상임관·공통 장수 fallback에서 타국·재야 장수의 금·쌀·병력·훈련·사기·부대·위치 도시를 제외
- Ref exportJSVars 범위(이름·국가·NPC·통/무/지)만 싣고 국가 묶음용 nationId 추가
- 재야 actor에게는 같은 국가 명령 후보를 보내지 않음
- 등용·장수대상임관 후보를 Ref SelectGeneral groupByNation처럼 국가색 머리와 optgroup으로 묶음
- 증여·포상·몰수·부대탈퇴지시·선양 후보에 Ref의 통/무/지 표시

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 14:55:39 +00:00
Hide_DandClaude Opus 5.5 0a9c47eddf 도시 대상 명령 입력에 Ref 거리별 도시 목록 복원
Ref ProcessCity의 `N칸 떨어진 도시:` 목록(CitiesBasedOnDistance)을 전체화면 인자 입력에
되살린다. 이동·출병 1칸, 강행·첩보·화계·탈취·파괴·선동 3칸을 map layout path의 BFS로
Ref searchDistance와 같은 순서로 나열하고, 거리별 magenta/orange/yellow 밑줄 도시를 누르면
대상 도시를 고른다. che 지도 94개 도시 결과를 Ref PHP 원문 실행 fixture와 비교하는 unit test와
1200/390px e2e를 추가한다.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 14:35:40 +00:00
Hide_DandClaude Opus 5.5 b90dd4499d 전체화면 명령 입력의 제출 버튼을 primary로 구분
하단 `입력`이 막대 위 글자로만 보여 제출인지 알기 어려웠고 녹색 상단 버튼이 제출처럼
보였다. Ref처럼 녹색은 이동(명령 취소)에만 두고, 제출은 명령명을 붙인 primary 버튼
(`화계 입력`)으로, `명령 다시 선택`은 secondary로 낮춘다. e2e locator와 버튼 색·
geometry assertion을 함께 갱신한다.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 14:04:49 +00:00
Hide_DandClaude Opus 5.5 a373ed327d 예약 명령 인자 입력을 전체화면 overlay로 전환
Ref v_processing처럼 인자가 필요한 장수·사령턴 명령은 작은 popup 대신
body 위 전체화면 dialog에서 입력한다. 명령 목록 단계는 기존 popup을 유지한다.

- 상단 막대: 명령 취소 · 명령 다시 선택 · 명령명과 대상 턴 · 검색 켜짐/꺼짐
- 검색 토글 상태를 useCommandTargetSearchEnabled()로 폼과 공유
- URL은 유지하고 같은 URL의 history entry로 Back이 overlay만 닫게 함
- 1120px 이상에서는 원본 700px 지도와 대상 목록을 두 열로 배치
- 징병·모병 sticky 기준을 공용 header 높이 변수로 연결
- e2e를 전체화면 계약에 맞게 갱신하고 overlay·Back·geometry 테스트 추가

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 13:39:02 +00:00
Hide_D 9d0a1a4add add symlink CLAUDE.md 2026-09-23 12:40:58 +00:00
494 changed files with 30209 additions and 5501 deletions

No files matched your search

+16
View File
@@ -38,6 +38,15 @@ GATEWAY_INTERNAL_API_URL=http://127.0.0.1:13000
ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS=30000 ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS=30000
WEB_PUSH_OUTBOX_POLL_MS=1000 WEB_PUSH_OUTBOX_POLL_MS=1000
GATEWAY_PUBLIC_URL=http://localhost:13000 GATEWAY_PUBLIC_URL=http://localhost:13000
# Browser origins default to GATEWAY_PUBLIC_URL (Game API also accepts GAME_PUBLIC_URL).
# Add exact HTTP(S) origins for a separate frontend; wildcards/null/path entries are rejected.
# Requests with a different Origin fail with 403 before mutation. No-Origin clients still need auth.
# API_ALLOWED_ORIGINS=http://localhost:5173,http://localhost:5174
# GAME_PUBLIC_URL=http://localhost:14000
# Gateway authentication protection shares atomic 60s counters under this prefix.
# Defaults: 10 login attempts/account, 600 auth requests/socket peer, 1200 globally;
# expensive auth work is capped at 4/process. Forwarded headers are not trusted.
# These are security defaults in auth/attemptBudget.ts, not disable-able env toggles.
GATEWAY_REDIS_PREFIX=sammo:gateway GATEWAY_REDIS_PREFIX=sammo:gateway
GATEWAY_DB_SCHEMA=public GATEWAY_DB_SCHEMA=public
GATEWAY_WORKSPACE_ROOT=/path/to/core2026 GATEWAY_WORKSPACE_ROOT=/path/to/core2026
@@ -82,6 +91,9 @@ DAEMON_REQUEST_TIMEOUT_MS=5000
# realtime은 벽시계 경과를 game tick으로 투영합니다. manual은 daemon이 # realtime은 벽시계 경과를 game tick으로 투영합니다. manual은 daemon이
# 다음 월 snapshot을 대기 없이 실행하며 테스트/장기 시뮬레이션에 사용합니다. # 다음 월 snapshot을 대기 없이 실행하며 테스트/장기 시뮬레이션에 사용합니다.
GAME_CLOCK_MODE=realtime GAME_CLOCK_MODE=realtime
# Optional Windows host boot identity. Key is a private file, never a VITE_* value.
# TURN_HOST_BOOT_URL=http://windows-private-address:17781/v1/boot-id
# TURN_HOST_BOOT_KEY_FILE=/run/secrets/host_boot_key
TRPC_PATH=/trpc TRPC_PATH=/trpc
# Frontend public URLs # Frontend public URLs
@@ -103,3 +115,7 @@ VITE_CASUAL_CHAT_URL=
# 수동 turn-daemon 기동 시 실행 산출물의 확정된 전체 commit SHA. 미확인이면 비워 둔다. # 수동 turn-daemon 기동 시 실행 산출물의 확정된 전체 commit SHA. 미확인이면 비워 둔다.
# Gateway 관리 daemon은 프로필 buildCommitSha를 자동 전달하므로 수동 설정하지 않는다. # Gateway 관리 daemon은 프로필 buildCommitSha를 자동 전달하므로 수동 설정하지 않는다.
TURN_BUILD_COMMIT_SHA= TURN_BUILD_COMMIT_SHA=
# Set both only when builds use the dedicated remote builder; never expose token to VITE_*.
RELEASE_BUILDER_URL=
RELEASE_BUILDER_TOKEN_FILE=
+3
View File
@@ -16,6 +16,9 @@ GAME_API_HOST=0.0.0.0
GAME_API_PORT=14000 GAME_API_PORT=14000
PROFILE=hwe PROFILE=hwe
SCENARIO=default SCENARIO=default
# Optional Windows observation; unavailable or unconfigured service never blocks gameplay.
# TURN_HOST_BOOT_URL=http://windows-private-address:17781/v1/boot-id
# TURN_HOST_BOOT_KEY_FILE=/run/sammo-host-recovery/host_boot.key
KAKAO_REST_KEY=your-kakao-rest-key KAKAO_REST_KEY=your-kakao-rest-key
KAKAO_ADMIN_KEY=your-kakao-admin-key KAKAO_ADMIN_KEY=your-kakao-admin-key
KAKAO_REDIRECT_URI=https://example.com/oauth/kakao/callback KAKAO_REDIRECT_URI=https://example.com/oauth/kakao/callback
+1
View File
@@ -89,6 +89,7 @@ web_modules/
.env.local .env.local
.env.ci .env.ci
.env.ops .env.ops
tools/host-recovery/*.key
tools/legacy-db-migration/migration-plan.json tools/legacy-db-migration/migration-plan.json
tools/legacy-db-migration/secrets/ tools/legacy-db-migration/secrets/
+1 -1
View File
@@ -1 +1 @@
24.21.0 24.22.1
+8 -1
View File
@@ -126,6 +126,10 @@ UI의 공통 보존·Chromium·artifact 요구는 [상위 UI 지침](../AGENTS.m
분석 편의를 위해 배포하는 산출물입니다. 보안상 비공개가 필요하다는 별도 요구와 분석 편의를 위해 배포하는 산출물입니다. 보안상 비공개가 필요하다는 별도 요구와
검토가 없는 한 번들 크기, build 시간 또는 일반적인 "최적화" 지시만으로 검토가 없는 한 번들 크기, build 시간 또는 일반적인 "최적화" 지시만으로
`build.sourcemap`을 끄거나 `.map` 파일을 배포 산출물에서 제외하지 말아 주세요. `build.sourcemap`을 끄거나 `.map` 파일을 배포 산출물에서 제외하지 말아 주세요.
- 짧은 제목·상태·버튼·정보 셀은 가로와 세로 정렬을 각각 결정합니다. 가로 가운데만
지정하고 끝내지 말고, 왼쪽/오른쪽 정렬 값도 세로 가운데가 적절한지 확인합니다.
여러 줄 본문·로그는 읽기 시작점을 보존하고, 좁은 화면의 줄바꿈과 overflow를
검증합니다. 구현 방법과 예외는 [두 축 텍스트 정렬](docs/frontend-css-architecture.md#text-alignment-on-both-axes-2026-09-29)을 따릅니다.
- 공통화는 동일한 렌더링 계약이 확인된 token/shell에 한합니다. `.error`, - 공통화는 동일한 렌더링 계약이 확인된 token/shell에 한합니다. `.error`,
`.stack`처럼 이름만 같은 page selector를 전역화하지 말아 주세요. `.stack`처럼 이름만 같은 page selector를 전역화하지 말아 주세요.
- `v-html`은 입력 source와 sanitization/allowlist를 확인해 주세요. 기존 warning을 - `v-html`은 입력 source와 sanitization/allowlist를 확인해 주세요. 기존 warning을
@@ -200,10 +204,13 @@ HTTPS 검증과 구분합니다.
## 코드 스타일 ## 코드 스타일
- Vite, Vue/Volar와 compiler API 소비자는 workspace 전체에서 정확히 - Vite, ESLint와 일반 compiler API 소비자는 workspace 전체에서 정확히
TypeScript `6.0.3`을 사용해 주세요. CLI project build/typecheck는 root의 TypeScript `6.0.3`을 사용해 주세요. CLI project build/typecheck는 root의
`@typescript/native` alias로 고정한 TypeScript 7 `tsc`를 사용하며 package-local `@typescript/native` alias로 고정한 TypeScript 7 `tsc`를 사용하며 package-local
다른 compiler version이나 직접 `tsc` 호출을 추가하지 말아 주세요. 다른 compiler version이나 직접 `tsc` 호출을 추가하지 말아 주세요.
Vue CLI만 `tools/vue-typecheck.mjs`로 고정한
`typescript-native-bridge@6.0.3-bridge.18.tsgo.7.0.2`를 사용합니다. 이 예외는
Vue 타입 검사에 한하며 workspace `typescript` override를 교체하지 않습니다.
- TypeScript/JSON/Vue SFC는 기존 4-space 스타일을 유지해 주세요. - TypeScript/JSON/Vue SFC는 기존 4-space 스타일을 유지해 주세요.
- public API는 명시적 타입을 사용하고 `any`, 불필요하게 넓은 `unknown`, - public API는 명시적 타입을 사용하고 `any`, 불필요하게 넓은 `unknown`,
`as unknown as` 우회를 피해 주세요. `as unknown as` 우회를 피해 주세요.
Symlink
+1
View File
@@ -0,0 +1 @@
AGENTS.md
+18 -4
View File
@@ -5,6 +5,13 @@
비교하고, 명시적인 신규 기능·밸런스·UX는 Core 계약으로 검증합니다. 비교하고, 명시적인 신규 기능·밸런스·UX는 Core 계약으로 검증합니다.
작업 규칙과 분야별 지침의 시작점은 [AGENTS.md](AGENTS.md)입니다. 작업 규칙과 분야별 지침의 시작점은 [AGENTS.md](AGENTS.md)입니다.
## 독자별 시작점
- 게임이 처음인 플레이어: [삼국지 모의전투 첫 안내](docs/user/index.md)
- 프로그래밍 기초만 아는 독자: [화면·서버·저장부터 배우기](docs/developer/first-steps.md)
- 개발 경험이 있는 독자: [요청·실행·운영의 구조 읽기](docs/developer/system-walkthrough.md)
- 전체 문서 탐색: [핸드북](docs/index.md), [게시판 용어 사전](docs/user/glossary.md)
## 저장소 구성 ## 저장소 구성
| 경로 | 책임 | | 경로 | 책임 |
@@ -51,8 +58,13 @@ Gateway 자체 릴리스는 Gateway 프로세스 밖의 `release-controller`가
Kakao 계정은 OAuth callback과 일반 비밀번호 로그인 모두에서 Kakao 고유 ID와 Kakao 계정은 OAuth callback과 일반 비밀번호 로그인 모두에서 Kakao 고유 ID와
현재 인증 이메일을 다시 확인합니다. 로컬 고유 ID 연결이 없지만 영구 보존된 현재 인증 이메일을 다시 확인합니다. 로컬 고유 ID 연결이 없지만 영구 보존된
이메일 계정이 있으면 자동 로그인하지 않고 그 계정에 연결할지 묻습니다. 기존 이메일 계정이 있으면 자동 로그인하지 않고 그 계정에 연결할지 묻습니다. 기존
계정 연결을 확인하면 이전 Kakao ID와 과거 talk proof를 교체하고 새 계정 연결은 새 “나와의 채팅” 숫자 코드 인증까지 끝난 뒤에만 저장하며, 그 전에는
“나와의 채팅” 숫자 코드로 다시 증명한 뒤에만 Gateway session을 발급합니다. 이전 연결과 비밀번호를 유지합니다. 일반 계정은 카카오가 인증한 동일 이메일로
관리자 승인 없이 복구하고, 기존 서비스 비밀번호로 확인하면 이메일이 달라도
새 카카오에 연결할 수 있습니다. 관리자 계정 교체에는 승인을 유지하며 다른 계정과의
연결·이메일 충돌은 자동 처리하지 않습니다. 새 교체부터 이전 Kakao ID는 원래 계정의
복구 이력으로 보존하여 같은 계정에만 다시 연결할 수 있습니다. 기존에 영구 폐기된
ID는 계속 차단합니다.
Kakao가 `already registered`를 반환했지만 보존 이메일 계정도 없으면 재가입 Kakao가 `already registered`를 반환했지만 보존 이메일 계정도 없으면 재가입
확인 뒤 신규 가입 form을 엽니다. 이미 로컬에 연결된 stable ID의 변경 이메일이 확인 뒤 신규 가입 form을 엽니다. 이미 로컬에 연결된 stable ID의 변경 이메일이
다른 계정에 있으면 기존처럼 충돌을 거부합니다. 다른 계정에 있으면 기존처럼 충돌을 거부합니다.
@@ -80,8 +92,9 @@ input-event 결과를 transaction으로 반영합니다. Redis pub/sub과 SSE는
## 도구 체인 ## 도구 체인
- Node.js `24.21.0`, pnpm `12.5.1`, Turbo - Node.js `24.22.1`, pnpm `12.5.1`, Turbo
- TypeScript `6.0.3` compiler API와 TypeScript `7.0.2` native `tsc` - TypeScript `6.0.3` compiler API와 TypeScript `7.0.2` native `tsc`
- Vue CLI 전용 `typescript-native-bridge` (`6.0.3-bridge.18.tsgo.7.0.2`)
- Fastify, tRPC, zod - Fastify, tRPC, zod
- Vue 3, Pinia, Vue Router, Vite - Vue 3, Pinia, Vue Router, Vite
- PostgreSQL, Prisma, Redis - PostgreSQL, Prisma, Redis
@@ -92,7 +105,8 @@ Node.js는 `.nvmrc`에서 24.x로 고정합니다. 의존성 설치와 검증에
`package.json`과 `pnpm-lock.yaml`을 함께 사용해 주세요. 모든 workspace package는 `package.json`과 `pnpm-lock.yaml`을 함께 사용해 주세요. 모든 workspace package는
내부 전용(`private`)이므로 manifest의 `0.0.0`은 배포 버전이 아닙니다. 배포 source는 내부 전용(`private`)이므로 manifest의 `0.0.0`은 배포 버전이 아닙니다. 배포 source는
full Git commit으로 고정하고, 실험 릴리스 같은 milestone은 annotated Git tag로 full Git commit으로 고정하고, 실험 릴리스 같은 milestone은 annotated Git tag로
식별합니다. Profile frontend는 같은 commit의 정적 버전 문서를 고정 URL과 ETag로 식별합니다. 선언 파일 생성·Vue 검사 범위와 캐시 계약은 [빌드 도구](docs/build-performance.md)를
확인해 주세요. Profile frontend는 같은 commit의 정적 버전 문서를 고정 URL과 ETag로
주기적으로 재검증하고, 현재 열린 bundle과 달라졌을 때만 강제 reload 없이 공용 주기적으로 재검증하고, 현재 열린 bundle과 달라졌을 때만 강제 reload 없이 공용
toast로 한 번 안내합니다. toast로 한 번 안내합니다.
+3 -3
View File
@@ -40,10 +40,10 @@
"@trpc/server": "^11.8.1", "@trpc/server": "^11.8.1",
"date-fns": "^4.1.0", "date-fns": "^4.1.0",
"es-toolkit": "^1.43.0", "es-toolkit": "^1.43.0",
"fastify": "^5.6.2", "fastify": "^5.12.5",
"redis": "^5.10.0", "redis": "^5.10.0",
"sanitize-html": "2.17.6", "sanitize-html": "2.17.7",
"sharp": "^0.35.0", "sharp": "^0.35.5",
"zod": "^4.3.5" "zod": "^4.3.5"
} }
} }
+65 -1
View File
@@ -1,3 +1,10 @@
import {
buildSessionRevocationBaselineKey,
buildUserSessionRevocationKey,
parseSessionRevocationWatermark,
SessionRevocationCommandBudget,
} from '@sammo-ts/common/auth/sessionRevocation';
export interface GatewayUserFlushEvent { export interface GatewayUserFlushEvent {
userId: string; userId: string;
flushedAt: string; flushedAt: string;
@@ -8,7 +15,7 @@ export interface GatewayUserFlushEvent {
} }
export interface FlushStore { export interface FlushStore {
getFlushedAt(userId: string): Date | null; getFlushedAt(userId: string): Date | null | Promise<Date | null>;
applyFlush(event: GatewayUserFlushEvent): void; applyFlush(event: GatewayUserFlushEvent): void;
} }
@@ -31,6 +38,63 @@ export class InMemoryFlushStore implements FlushStore {
} }
} }
interface RevocationRedisClient {
readonly isReady?: boolean;
mGet(keys: string[]): Promise<(string | null)[]>;
withAbortSignal?(signal: AbortSignal): RevocationRedisClient;
}
/** Pub/sub accelerates closure; Redis is checked again at every authorization boundary. */
export class RedisFlushStore implements FlushStore {
private readonly local = new Map<string, Date>();
private readonly commands = new SessionRevocationCommandBudget(64);
constructor(
private readonly client: RevocationRedisClient,
private readonly channel: string
) {}
private commandClient(): RevocationRedisClient {
if (this.client.isReady === false) throw new Error('Session revocation storage is unavailable.');
return this.client.withAbortSignal?.(AbortSignal.timeout(2000)) ?? this.client;
}
async assertInitialized(): Promise<void> {
const values = await this.commands.run(() =>
this.commandClient().mGet([buildSessionRevocationBaselineKey(this.channel)])
);
if (values.length !== 1 || !parseSessionRevocationWatermark(values[0]!))
throw new Error('Gateway session revocation must be initialized before game API startup.');
}
async getFlushedAt(userId: string): Promise<Date | null> {
const values = await this.commands.run(() =>
this.commandClient().mGet([
buildSessionRevocationBaselineKey(this.channel),
buildUserSessionRevocationKey(this.channel, userId),
])
);
if (values.length !== 2) throw new Error('Session revocation storage is unavailable.');
const baseline = parseSessionRevocationWatermark(values[0]!);
if (!baseline) throw new Error('Session revocation storage is unavailable.');
const persisted = parseSessionRevocationWatermark(values[1]!);
return new Date(
Math.max(baseline.getTime(), persisted?.getTime() ?? 0, this.local.get(userId)?.getTime() ?? 0)
);
}
applyFlush(event: GatewayUserFlushEvent): void {
const parsed = new Date(event.flushedAt);
if (Number.isNaN(parsed.getTime())) return;
const prior = this.local.get(event.userId);
if (!prior || parsed > prior) {
this.local.delete(event.userId);
this.local.set(event.userId, parsed);
}
// 이 캐시는 재시작/eviction 뒤에도 Redis watermark를 확인하므로 권한 근거가 아니다.
if (this.local.size > 4096) this.local.delete(this.local.keys().next().value!);
}
}
export class RedisGatewayFlushSubscriber { export class RedisGatewayFlushSubscriber {
private readonly client: { private readonly client: {
subscribe: (channel: string, listener: (message: string) => void) => Promise<void>; subscribe: (channel: string, listener: (message: string) => void) => Promise<void>;
+6 -1
View File
@@ -1,4 +1,4 @@
import { parseNumberWithFallback } from '@sammo-ts/common'; import { parseNumberWithFallback, resolveApiAllowedOrigins } from '@sammo-ts/common';
const parseReconcileInterval = (value: string | undefined): number => { const parseReconcileInterval = (value: string | undefined): number => {
const parsed = parseNumberWithFallback(value, 30_000, 'ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS'); const parsed = parseNumberWithFallback(value, 30_000, 'ACCOUNT_ICON_RESET_RECONCILE_INTERVAL_MS');
@@ -10,6 +10,7 @@ const parseReconcileInterval = (value: string | undefined): number => {
export interface GameApiConfig { export interface GameApiConfig {
host: string; host: string;
corsAllowedOrigins: string[];
port: number; port: number;
trpcPath: string; trpcPath: string;
eventsPath: string; eventsPath: string;
@@ -48,6 +49,10 @@ export const resolveGameApiConfigFromEnv = (env: NodeJS.ProcessEnv = process.env
return { return {
host: env.GAME_API_HOST ?? '0.0.0.0', host: env.GAME_API_HOST ?? '0.0.0.0',
corsAllowedOrigins: resolveApiAllowedOrigins(env.API_ALLOWED_ORIGINS, [
env.GATEWAY_PUBLIC_URL,
env.GAME_PUBLIC_URL,
]),
port: parseNumberWithFallback(env.GAME_API_PORT, 14000, 'GAME_API_PORT'), port: parseNumberWithFallback(env.GAME_API_PORT, 14000, 'GAME_API_PORT'),
trpcPath: env.GAME_TRPC_PATH ?? env.TRPC_PATH ?? '/trpc', trpcPath: env.GAME_TRPC_PATH ?? env.TRPC_PATH ?? '/trpc',
eventsPath: env.GAME_API_EVENTS_PATH ?? '/events', eventsPath: env.GAME_API_EVENTS_PATH ?? '/events',
+9 -2
View File
@@ -66,8 +66,10 @@ const resolveMapName = async (
} }
}; };
export const loadMapLayout = async (scenario: string, options: MapLayoutLoaderOptions = {}): Promise<MapLayout> => { export const loadMapLayoutByName = async (
const mapName = await resolveMapName(scenario, options.loadScenario ?? loadScenarioDefinitionById); mapName: string,
options: MapLayoutLoaderOptions = {}
): Promise<MapLayout> => {
const useCache = !options.loadScenario && !options.loadMap && !options.loadRegionMap; const useCache = !options.loadScenario && !options.loadMap && !options.loadRegionMap;
const cached = useCache ? layoutCache.get(mapName) : undefined; const cached = useCache ? layoutCache.get(mapName) : undefined;
if (cached) { if (cached) {
@@ -98,3 +100,8 @@ export const loadMapLayout = async (scenario: string, options: MapLayoutLoaderOp
} }
return layout; return layout;
}; };
export const loadMapLayout = async (scenario: string, options: MapLayoutLoaderOptions = {}): Promise<MapLayout> => {
const mapName = await resolveMapName(scenario, options.loadScenario ?? loadScenarioDefinitionById);
return loadMapLayoutByName(mapName, options);
};
+31 -17
View File
@@ -1,5 +1,10 @@
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { persistAuditDiplomacyEvents, type AuditDiplomacyEventDraft, type GamePrisma } from '@sammo-ts/infra'; import {
withPlayAuditSavepoint,
persistAuditDiplomacyEvents,
type AuditDiplomacyEventDraft,
type GamePrisma,
} from '@sammo-ts/infra';
import type { ApiInputExecutionContext } from '../inputEventBoundary.js'; import type { ApiInputExecutionContext } from '../inputEventBoundary.js';
import { asRecord, JosaUtil } from '@sammo-ts/common'; import { asRecord, JosaUtil } from '@sammo-ts/common';
@@ -104,6 +109,7 @@ const persistEffects = async (
} }
): Promise<void> => { ): Promise<void> => {
const changes: AuditDiplomacyEventDraft[] = []; const changes: AuditDiplomacyEventDraft[] = [];
let collectionFailed = false;
const states = new Map(audit?.before.map((row) => [`${row.srcNationId}:${row.destNationId}`, row])); const states = new Map(audit?.before.map((row) => [`${row.srcNationId}:${row.destNationId}`, row]));
const project = (row: GamePrisma.DiplomacyGetPayload<Record<string, never>>) => ({ const project = (row: GamePrisma.DiplomacyGetPayload<Record<string, never>>) => ({
state: row.stateCode, state: row.stateCode,
@@ -129,22 +135,26 @@ const persistEffects = async (
}, },
}); });
if (audit) { if (audit) {
const key = `${effect.srcNationId}:${effect.destNationId}`; try {
const previous = states.get(key); const key = `${effect.srcNationId}:${effect.destNationId}`;
if (!previous) throw new Error('Missing locked diplomacy audit state'); const previous = states.get(key);
const before = project(previous); if (!previous) throw new Error('Missing locked diplomacy audit state');
const after = project(updated); const before = project(previous);
if (JSON.stringify(before) !== JSON.stringify(after)) { const after = project(updated);
changes.push({ if (JSON.stringify(before) !== JSON.stringify(after)) {
...audit.base, changes.push({
srcNationId: effect.srcNationId, ...audit.base,
destNationId: effect.destNationId, srcNationId: effect.srcNationId,
ordinal: changes.length + 1, destNationId: effect.destNationId,
before, ordinal: changes.length + 1,
after, before,
}); after,
});
}
states.set(key, updated);
} catch {
collectionFailed = true;
} }
states.set(key, updated);
} }
} else if (effect.type === 'nation:patch' && effect.targetId !== undefined) { } else if (effect.type === 'nation:patch' && effect.targetId !== undefined) {
const patch = effect.patch; const patch = effect.patch;
@@ -159,7 +169,11 @@ const persistEffects = async (
} }
} }
await persistLogs(db, orderLegacyActionLoggerFlush(logs), year, month, at, serverId); await persistLogs(db, orderLegacyActionLoggerFlush(logs), year, month, at, serverId);
await persistAuditDiplomacyEvents(db, changes); if (changes.length || collectionFailed)
await withPlayAuditSavepoint(db, async (auditDb) => {
if (collectionFailed) throw new Error('Audit relation collection failed');
await persistAuditDiplomacyEvents(auditDb, changes);
});
}; };
const refreshFrontStates = async (db: DatabaseClient, mapName: string, nationIds: number[]): Promise<number[]> => { const refreshFrontStates = async (db: DatabaseClient, mapName: string, nationIds: number[]): Promise<number[]> => {
@@ -0,0 +1,90 @@
import type { ServerResponse } from 'node:http';
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
const MAX_PENDING_FRAMES = 32;
const MAX_BUFFER_BYTES = 64 * 1024;
/** Revalidate before work and before writing its result; never retain unbounded frames. */
export class AuthenticatedRealtimeConnection {
private queue = Promise.resolve();
private pending = 0;
private ended = false;
private readonly expiryTimer: ReturnType<typeof setTimeout>;
private cleanup = new Set<() => void>();
constructor(
readonly auth: GameSessionTokenPayload,
private readonly response: ServerResponse,
private readonly resolveAuth: () => Promise<GameSessionTokenPayload | null>
) {
// AccessTokenStore requires a full remaining TTL second on every lookup.
this.expiryTimer = setTimeout(() => this.close(), Math.max(0, Date.parse(auth.expiresAt) - Date.now() - 999));
this.expiryTimer.unref();
response.once('close', this.close);
response.once('error', this.close);
}
get closed(): boolean {
return this.ended;
}
whenIdle(): Promise<void> {
return this.queue;
}
onClose(cleanup: () => void): void {
if (this.ended) cleanup();
else this.cleanup.add(cleanup);
}
private async currentAuth(): Promise<GameSessionTokenPayload | null> {
if (this.ended) return null;
const current = await this.resolveAuth();
if (
!current ||
this.ended ||
current.sessionId !== this.auth.sessionId ||
current.user.id !== this.auth.user.id ||
current.profile !== this.auth.profile
) {
this.close();
return null;
}
return current;
}
enqueue(work: (auth: GameSessionTokenPayload) => Promise<string | null>): void {
if (this.ended) return;
if (this.pending >= MAX_PENDING_FRAMES || this.response.writableLength > MAX_BUFFER_BYTES) {
this.close();
return;
}
this.pending += 1;
this.queue = this.queue
.then(async () => {
const current = await this.currentAuth();
if (!current) return;
const frame = await work(current);
if (!frame || !(await this.currentAuth())) return;
if (Buffer.byteLength(frame) > MAX_BUFFER_BYTES || !this.response.write(frame)) this.close();
})
.catch(() => {
// Redis/auth failure is fail closed. Reconnect must authenticate again.
this.close();
})
.finally(() => {
this.pending -= 1;
});
}
close = (): void => {
if (this.ended) return;
this.ended = true;
clearTimeout(this.expiryTimer);
this.response.off('close', this.close);
this.response.off('error', this.close);
for (const cleanup of this.cleanup) cleanup();
this.cleanup.clear();
if (!this.response.destroyed && !this.response.writableEnded) this.response.end();
};
}
@@ -0,0 +1,93 @@
const DEFAULT_LIMITS = { connections: 512, perUser: 8, opening: 32 } as const;
const OPENING_DEADLINE_MS = 5000;
export interface RealtimeConnectionReservation {
canContinue(): boolean;
reserveUser(userId: string): boolean;
startStreaming(): boolean;
cancelOpening(): void;
release(): void;
}
/** One game API process owns both pending setup and live streams; no waiting queue. */
export class RealtimeConnectionAdmission {
private readonly limits: { connections: number; perUser: number; opening: number };
private total = 0;
private opening = 0;
private closed = false;
private readonly byUser = new Map<string, number>();
private readonly pending = new Set<() => void>();
constructor(limits: Partial<{ connections: number; perUser: number; opening: number }> = {}) {
this.limits = { ...DEFAULT_LIMITS, ...limits };
if (Object.values(this.limits).some((value) => !Number.isSafeInteger(value) || value < 1))
throw new Error('Invalid realtime connection limits.');
}
begin(onTimeout: () => void): RealtimeConnectionReservation | null {
if (this.closed || this.total >= this.limits.connections || this.opening >= this.limits.opening) return null;
this.total += 1;
this.opening += 1;
let userId: string | null = null;
let streaming = false;
let cancelled = false;
let released = false;
const deadline = Date.now() + OPENING_DEADLINE_MS;
const expire = () => {
if (released || streaming || cancelled) return;
cancelled = true;
clearTimeout(timer);
onTimeout();
};
const timer = setTimeout(expire, OPENING_DEADLINE_MS);
timer.unref();
this.pending.add(expire);
const canContinue = () => {
if (!released && !streaming && !cancelled && Date.now() >= deadline) expire();
return !released && !streaming && !cancelled;
};
return {
canContinue,
reserveUser: (id) => {
if (!canContinue() || userId !== null) return false;
const count = this.byUser.get(id) ?? 0;
if (count >= this.limits.perUser) return false;
this.byUser.set(id, count + 1);
userId = id;
return true;
},
startStreaming: () => {
if (!canContinue() || userId === null) return false;
streaming = true;
this.opening -= 1;
clearTimeout(timer);
this.pending.delete(expire);
return true;
},
cancelOpening: () => {
if (released || streaming) return;
cancelled = true;
clearTimeout(timer);
// Disconnect/timeout does not prove the awaited Redis/SQL operation has settled.
},
release: () => {
if (released) return;
released = true;
this.total -= 1;
if (!streaming) this.opening -= 1;
clearTimeout(timer);
this.pending.delete(expire);
if (userId !== null) {
const count = (this.byUser.get(userId) ?? 1) - 1;
if (count === 0) this.byUser.delete(userId);
else this.byUser.set(userId, count);
}
},
};
}
close(): void {
this.closed = true;
for (const expire of this.pending) expire();
}
}
+12 -6
View File
@@ -76,7 +76,7 @@ export class ReadModelOutboxWorker implements ReadModelOutboxWakeup {
this.leaseMs = normalizePositiveInteger(options.leaseMs, 30_000); this.leaseMs = normalizePositiveInteger(options.leaseMs, 30_000);
this.retentionMs = normalizePositiveInteger(options.retentionMs, 24 * 60 * 60 * 1_000); this.retentionMs = normalizePositiveInteger(options.retentionMs, 24 * 60 * 60 * 1_000);
this.pruneIntervalMs = normalizePositiveInteger(options.pruneIntervalMs, 60_000); this.pruneIntervalMs = normalizePositiveInteger(options.pruneIntervalMs, 60_000);
this.pruneLimit = normalizePositiveInteger(options.pruneLimit, 100); this.pruneLimit = Math.min(500, normalizePositiveInteger(options.pruneLimit, 500));
this.owner = options.owner ?? `game-api:${profileName}:${process.pid}:${randomUUID()}`; this.owner = options.owner ?? `game-api:${profileName}:${process.pid}:${randomUUID()}`;
this.now = options.now ?? (() => new Date()); this.now = options.now ?? (() => new Date());
this.onError = options.onError ?? (() => undefined); this.onError = options.onError ?? (() => undefined);
@@ -109,11 +109,17 @@ export class ReadModelOutboxWorker implements ReadModelOutboxWakeup {
const now = this.now(); const now = this.now();
if (now.getTime() >= this.nextPruneAt) { if (now.getTime() >= this.nextPruneAt) {
this.nextPruneAt = now.getTime() + this.pruneIntervalMs; // backlog가 있으면 다음 dispatch에서도 이어서 정리한다. 평상시에는 낮은 빈도로 확인한다.
await pruneDeliveredReadModelOutbox(this.db, { // 한 호출은 최대 10 batch/50ms이며 각 SQL도 최대 500행이다.
deliveredBefore: new Date(now.getTime() - this.retentionMs), const started = performance.now();
limit: this.pruneLimit, for (let batch = 0; batch < 10; batch += 1) {
}); const deleted = await pruneDeliveredReadModelOutbox(this.db, {
deliveredBefore: new Date(now.getTime() - this.retentionMs),
limit: this.pruneLimit,
});
this.nextPruneAt = deleted < this.pruneLimit ? now.getTime() + this.pruneIntervalMs : 0;
if (deleted < this.pruneLimit || performance.now() - started >= 50) break;
}
} }
return result; return result;
} }
+14 -4
View File
@@ -1,3 +1,4 @@
import { toSafeInteger } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
@@ -281,7 +282,7 @@ export const auctionRouter = router({
callerAlias, callerAlias,
remainPoint: point?.value ?? 0, remainPoint: point?.value ?? 0,
recentLogs: recentLogs.map((log) => ({ recentLogs: recentLogs.map((log) => ({
id: log.id, id: toSafeInteger(log.id),
text: log.text, text: log.text,
createdAt: log.createdAt.toISOString(), createdAt: log.createdAt.toISOString(),
})), })),
@@ -444,7 +445,10 @@ export const auctionRouter = router({
const timerKeys = buildAuctionTimerKeys(ctx.profile.name); const timerKeys = buildAuctionTimerKeys(ctx.profile.name);
const nextCloseAt = new Date(result.closeAt); const nextCloseAt = new Date(result.closeAt);
await ctx.redis.zAdd(timerKeys.timerKey, [ await ctx.redis.zAdd(timerKeys.timerKey, [
{ score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)), value: String(auction.id) }, {
score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)),
value: String(auction.id),
},
]); ]);
return { ok: true }; return { ok: true };
@@ -521,7 +525,10 @@ export const auctionRouter = router({
const timerKeys = buildAuctionTimerKeys(ctx.profile.name); const timerKeys = buildAuctionTimerKeys(ctx.profile.name);
const nextCloseAt = new Date(result.closeAt); const nextCloseAt = new Date(result.closeAt);
await ctx.redis.zAdd(timerKeys.timerKey, [ await ctx.redis.zAdd(timerKeys.timerKey, [
{ score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)), value: String(auction.id) }, {
score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)),
value: String(auction.id),
},
]); ]);
return { ok: true }; return { ok: true };
@@ -659,7 +666,10 @@ export const auctionRouter = router({
const timerKeys = buildAuctionTimerKeys(ctx.profile.name); const timerKeys = buildAuctionTimerKeys(ctx.profile.name);
const nextCloseAt = new Date(result.closeAt); const nextCloseAt = new Date(result.closeAt);
await ctx.redis.zAdd(timerKeys.timerKey, [ await ctx.redis.zAdd(timerKeys.timerKey, [
{ score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)), value: String(auction.id) }, {
score: resolveAuctionTimerScore(gameTime, nextCloseAt, BigInt(result.closeTick)),
value: String(auction.id),
},
]); ]);
return { ok: true }; return { ok: true };
+9 -1
View File
@@ -66,7 +66,15 @@ export const authRouter = router({
message: 'Game access is restricted for this account.', message: 'Game access is restricted for this account.',
}); });
} }
const flushedAt = ctx.flushStore.getFlushedAt(payload.user.id); let flushedAt: Date | null;
try {
flushedAt = await ctx.flushStore.getFlushedAt(payload.user.id);
} catch {
throw new TRPCError({
code: 'SERVICE_UNAVAILABLE',
message: 'Authentication protection is unavailable.',
});
}
if (flushedAt && new Date(payload.issuedAt) <= flushedAt) { if (flushedAt && new Date(payload.issuedAt) <= flushedAt) {
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
+3 -2
View File
@@ -1,3 +1,4 @@
import { upsertWithoutSequence } from '@sammo-ts/infra';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { CLOCK_OPERATION_PERSISTENCE_LOCK, GamePrisma, acquireGameSchemaAdvisoryXactLock } from '@sammo-ts/infra'; import { CLOCK_OPERATION_PERSISTENCE_LOCK, GamePrisma, acquireGameSchemaAdvisoryXactLock } from '@sammo-ts/infra';
import { z } from 'zod'; import { z } from 'zod';
@@ -223,7 +224,7 @@ export const bettingRouter = router({
world.currentMonth, world.currentMonth,
`${input.amount} 포인트를 베팅에 사용` `${input.amount} 포인트를 베팅에 사용`
); );
await ctx.db.rankData.upsert({ await upsertWithoutSequence(ctx.db.rankData, {
where: { where: {
generalId_type: { generalId_type: {
generalId: general.id, generalId: general.id,
@@ -245,7 +246,7 @@ export const bettingRouter = router({
}); });
} }
await ctx.db.nationBet.upsert({ await upsertWithoutSequence(ctx.db.nationBet, {
where: { where: {
bettingId_userId_selectionKey: { bettingId_userId_selectionKey: {
bettingId: input.bettingId, bettingId: input.bettingId,
+128 -63
View File
@@ -1,3 +1,9 @@
import {
ImageWorkBudget,
ImageWorkBusyError,
ImageUploadError,
isRasterImage,
} from '@sammo-ts/common/images/imageUpload';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import { randomBytes } from 'crypto'; import { randomBytes } from 'crypto';
@@ -7,6 +13,9 @@ import { accessAuthedInputProcedure, authedProcedure, router } from '../../trpc.
import { getMyGeneral } from '../shared/general.js'; import { getMyGeneral } from '../shared/general.js';
import { resolveSecretPermission } from '../shared/secretPermission.js'; import { resolveSecretPermission } from '../shared/secretPermission.js';
const boardImageBudget = new ImageWorkBudget();
const MAX_IMAGE_PIXELS = 16 * 1024 * 1024;
const MAX_UPLOAD_BYTES = 1024 * 1024; const MAX_UPLOAD_BYTES = 1024 * 1024;
const MAX_LONG_EDGE = 2048; const MAX_LONG_EDGE = 2048;
const WEBP_QUALITY = 80; const WEBP_QUALITY = 80;
@@ -57,7 +66,7 @@ const buildWebpBuffer = async (
buffer: Buffer, buffer: Buffer,
{ animated, resize }: { animated: boolean; resize: boolean } { animated, resize }: { animated: boolean; resize: boolean }
): Promise<Buffer> => { ): Promise<Buffer> => {
let pipeline = sharp(buffer, { animated: true }); let pipeline = sharp(buffer, { animated: true, limitInputPixels: MAX_IMAGE_PIXELS }).timeout({ seconds: 10 });
if (resize) { if (resize) {
pipeline = pipeline.resize({ pipeline = pipeline.resize({
width: MAX_LONG_EDGE, width: MAX_LONG_EDGE,
@@ -74,11 +83,16 @@ const buildWebpBuffer = async (
...(animated ? { animated: true } : {}), ...(animated ? { animated: true } : {}),
}; };
return pipeline.webp(webpOptions).toBuffer(); return pipeline
.webp(webpOptions)
.toBuffer()
.catch(() => {
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지를 변환할 수 없습니다.' });
});
}; };
const buildAvifBuffer = async (buffer: Buffer, resize: boolean): Promise<Buffer> => { const buildAvifBuffer = async (buffer: Buffer, resize: boolean): Promise<Buffer> => {
let pipeline = sharp(buffer, { animated: true }); let pipeline = sharp(buffer, { animated: true, limitInputPixels: MAX_IMAGE_PIXELS }).timeout({ seconds: 10 });
if (resize) { if (resize) {
pipeline = pipeline.resize({ pipeline = pipeline.resize({
width: MAX_LONG_EDGE, width: MAX_LONG_EDGE,
@@ -87,7 +101,12 @@ const buildAvifBuffer = async (buffer: Buffer, resize: boolean): Promise<Buffer>
withoutEnlargement: true, withoutEnlargement: true,
}); });
} }
return pipeline.avif({ quality: 60, effort: 4 }).toBuffer(); return pipeline
.avif({ quality: 60, effort: 4 })
.toBuffer()
.catch(() => {
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지를 변환할 수 없습니다.' });
});
}; };
export const boardRouter = router({ export const boardRouter = router({
@@ -213,70 +232,116 @@ export const boardRouter = router({
return { id: comment.id }; return { id: comment.id };
}), }),
uploadImage: authedProcedure.input(z.object({ dataUrl: z.string().min(1) })).mutation(async ({ ctx, input }) => { uploadImage: authedProcedure
const { permission } = await getBoardActor(ctx); .input(
assertBoardAccess(permission, true); z.object({
dataUrl: z
.string()
.min(1)
.max(Math.ceil(MAX_UPLOAD_BYTES / 3) * 4 + 128),
})
)
.mutation(async ({ ctx, input }) => {
const { permission } = await getBoardActor(ctx);
assertBoardAccess(permission, true);
const buffer = parseDataUrl(input.dataUrl); try {
if (buffer.length > MAX_UPLOAD_BYTES) { return await boardImageBudget.run(async () => {
throw new TRPCError({ code: 'PAYLOAD_TOO_LARGE', message: '이미지 용량 제한(1MB)을 초과했습니다.' }); const buffer = parseDataUrl(input.dataUrl);
} if (buffer.length > MAX_UPLOAD_BYTES) {
throw new TRPCError({
code: 'PAYLOAD_TOO_LARGE',
message: '이미지 용량 제한(1MB)을 초과했습니다.',
});
}
const metadata = await sharp(buffer, { animated: true }).metadata(); if (!isRasterImage(buffer))
if (!metadata.format || !metadata.width || !metadata.height) { throw new TRPCError({ code: 'BAD_REQUEST', message: '지원하지 않는 이미지 형식입니다.' });
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지 정보를 확인할 수 없습니다.' }); const metadata = await sharp(buffer, { animated: true, limitInputPixels: MAX_IMAGE_PIXELS })
} .timeout({ seconds: 10 })
.metadata()
.catch(() => {
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지 정보를 확인할 수 없습니다.' });
});
if ((metadata.pages ?? 1) > 256)
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지 프레임 제한을 초과했습니다.' });
if (!metadata.format || !metadata.width || !metadata.height) {
throw new TRPCError({ code: 'BAD_REQUEST', message: '이미지 정보를 확인할 수 없습니다.' });
}
const format = metadata.format; const format = metadata.format;
const isAvif = metadata.mediaType === 'image/avif'; const isAvif = metadata.mediaType === 'image/avif';
const isAnimated = (metadata.pages ?? 1) > 1; const isAnimated = (metadata.pages ?? 1) > 1;
const needsResize = Math.max(metadata.width, metadata.height) > MAX_LONG_EDGE; const needsResize = Math.max(metadata.width, metadata.height) > MAX_LONG_EDGE;
const allowed = new Set(['png', 'jpeg', 'jpg', 'gif', 'webp', 'avif', 'heif', 'tiff', 'bmp']); const allowed = new Set(['png', 'jpeg', 'jpg', 'gif', 'webp', 'avif', 'heif', 'tiff', 'bmp']);
if (!allowed.has(format)) { if (!allowed.has(format)) {
throw new TRPCError({ code: 'BAD_REQUEST', message: '지원하지 않는 이미지 형식입니다.' }); throw new TRPCError({ code: 'BAD_REQUEST', message: '지원하지 않는 이미지 형식입니다.' });
} }
let outputBuffer = buffer; let outputBuffer = buffer;
let outputFormat = isAvif ? 'avif' : 'webp'; let outputFormat = isAvif ? 'avif' : 'webp';
if (isAvif) { if (isAvif) {
if (needsResize) { if (needsResize) {
outputBuffer = await buildAvifBuffer(buffer, true); outputBuffer = await buildAvifBuffer(buffer, true);
}
} else {
const webpBuffer = await buildWebpBuffer(buffer, {
animated: isAnimated || format === 'gif',
resize: needsResize,
});
if (
format === 'webp' &&
!needsResize &&
webpBuffer.length >= buffer.length * WEBP_MIN_SAVING_RATIO
) {
outputBuffer = buffer;
outputFormat = 'webp';
} else {
outputBuffer = webpBuffer;
outputFormat = 'webp';
}
}
if (!ctx.contentImageUpload) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: '이미지 저장소가 설정되지 않았습니다.',
});
}
const filename = `${randomBytes(16).toString('hex')}.${outputFormat}`;
const uploaded = await ctx.contentImageUpload.upload({
filename,
contentType: outputFormat === 'avif' ? 'image/avif' : 'image/webp',
body: outputBuffer,
});
const outputMeta = await sharp(outputBuffer, { animated: true, limitInputPixels: MAX_IMAGE_PIXELS })
.timeout({ seconds: 10 })
.metadata();
return {
url: uploaded.publicUrl,
width: outputMeta.width ?? metadata.width,
height: outputMeta.height ?? metadata.height,
format: outputFormat,
animated: isAnimated,
size: outputBuffer.length,
};
});
} catch (error) {
if (error instanceof ImageWorkBusyError)
throw new TRPCError({
code: 'TOO_MANY_REQUESTS',
message: '이미지 처리 중입니다. 잠시 후 다시 시도해주세요.',
});
if (error instanceof ImageUploadError)
throw new TRPCError({
code: 'SERVICE_UNAVAILABLE',
message: '이미지 저장소에 연결할 수 없습니다. 잠시 후 다시 시도해주세요.',
});
throw error;
} }
} else { }),
const webpBuffer = await buildWebpBuffer(buffer, {
animated: isAnimated || format === 'gif',
resize: needsResize,
});
if (format === 'webp' && !needsResize && webpBuffer.length >= buffer.length * WEBP_MIN_SAVING_RATIO) {
outputBuffer = buffer;
outputFormat = 'webp';
} else {
outputBuffer = webpBuffer;
outputFormat = 'webp';
}
}
if (!ctx.contentImageUpload) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: '이미지 저장소가 설정되지 않았습니다.' });
}
const filename = `${randomBytes(16).toString('hex')}.${outputFormat}`;
const uploaded = await ctx.contentImageUpload.upload({
filename,
contentType: outputFormat === 'avif' ? 'image/avif' : 'image/webp',
body: outputBuffer,
});
const outputMeta = await sharp(outputBuffer, { animated: true }).metadata();
return {
url: uploaded.publicUrl,
width: outputMeta.width ?? metadata.width,
height: outputMeta.height ?? metadata.height,
format: outputFormat,
animated: isAnimated,
size: outputBuffer.length,
};
}),
}); });
+29 -10
View File
@@ -2,7 +2,7 @@ import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import { getBillByLevel, LogCategory, LogScope } from '@sammo-ts/logic'; import { getBillByLevel, LogCategory, LogScope } from '@sammo-ts/logic';
import { asRecord, type RankDataType } from '@sammo-ts/common'; import { asRecord, readOfficeRequest, toSafeInteger, withSafeIntegerId, type RankDataType } from '@sammo-ts/common';
import type { GameApiContext } from '../../context.js'; import type { GameApiContext } from '../../context.js';
import { import {
@@ -588,10 +588,7 @@ export const getGeneralContext = async (ctx: GameApiContext) => {
recentWar: general.recentWarTime?.toISOString() ?? null, recentWar: general.recentWarTime?.toISOString() ?? null,
defenceTrain: settings.defence_train, defenceTrain: settings.defence_train,
killTurn: readNumber(metaRecord.killturn ?? metaRecord.killTurn, 0), killTurn: readNumber(metaRecord.killturn ?? metaRecord.killTurn, 0),
remainingMinutes: resolveRemainingMinutes( remainingMinutes: resolveRemainingMinutes(general.turnTime, gameTime.now),
general.turnTime,
gameTime.now
),
crewTypeId: general.crewTypeId, crewTypeId: general.crewTypeId,
crewTypeName: crewTypeDetails.get(general.crewTypeId)?.name ?? '-', crewTypeName: crewTypeDetails.get(general.crewTypeId)?.name ?? '-',
crewTypeInfo: crewTypeDetails.get(general.crewTypeId) ?? null, crewTypeInfo: crewTypeDetails.get(general.crewTypeId) ?? null,
@@ -916,7 +913,7 @@ export const generalRouter = router({
return { return {
type: input.type, type: input.type,
logs: logs.map((entry) => ({ logs: logs.map((entry) => ({
id: entry.id, id: toSafeInteger(entry.id),
text: entry.text, text: entry.text,
})), })),
}; };
@@ -966,9 +963,9 @@ export const generalRouter = router({
]); ]);
return { return {
global: trimRecentRecords(global, input.lastGeneralRecordId), global: trimRecentRecords(global.map(withSafeIntegerId), input.lastGeneralRecordId),
general: trimRecentRecords(general, input.lastGeneralRecordId), general: trimRecentRecords(general.map(withSafeIntegerId), input.lastGeneralRecordId),
history: trimRecentRecords(history, input.lastWorldHistoryId), history: trimRecentRecords(history.map(withSafeIntegerId), input.lastWorldHistoryId),
}; };
}), }),
// 메인 화면은 SSE invalidation, 탭 복귀와 직접 갱신이 같은 read model을 // 메인 화면은 SSE invalidation, 탭 복귀와 직접 갱신이 같은 read model을
@@ -989,7 +986,7 @@ export const generalRouter = router({
const now = new Date(); const now = new Date();
const { scoreStartedAt } = resolveAccessWindows(now, worldState.tickSeconds, worldState.meta); const { scoreStartedAt } = resolveAccessWindows(now, worldState.tickSeconds, worldState.meta);
const [onlineAccess, ownNation, openPolls, gameTime] = await Promise.all([ const [onlineAccess, ownNation, openPolls, gameTime, officeRequestRows] = await Promise.all([
ctx.db.generalAccessLog.findMany({ ctx.db.generalAccessLog.findMany({
where: { where: {
lastActionAt: { lastActionAt: {
@@ -1020,6 +1017,15 @@ export const generalRouter = router({
}, },
}), }),
loadCurrentGameTime(ctx.db, now), loadCurrentGameTime(ctx.db, now),
me.nationId > 0 && me.officerLevel >= 5 && !asRecord(me.penalty).noChief
? ctx.db.general.findMany({
where: {
nationId: me.nationId,
meta: { path: ['cityOfficeRequest', 'status'], equals: 'pending' },
},
select: { id: true, userId: true, meta: true },
})
: Promise.resolve([]),
]); ]);
// vote_poll timestamps are logical game-wall values. During PREOPEN the // vote_poll timestamps are logical game-wall values. During PREOPEN the
// logical clock advances through negative ticks before the opening anchor, // logical clock advances through negative ticks before the opening anchor,
@@ -1088,6 +1094,19 @@ export const generalRouter = router({
return { return {
serverId, serverId,
cityOfficeRequests: {
generalId: me.id,
nationId: me.nationId,
ids: officeRequestRows.flatMap((row) => {
const request = readOfficeRequest(row.meta);
return request?.status === 'pending' &&
request.generalId === row.id &&
request.nationId === me.nationId &&
request.userId === row.userId
? [request.id]
: [];
}),
},
onlineUserCount: onlineGenerals.length, onlineUserCount: onlineGenerals.length,
onlineNations, onlineNations,
onlineGenerals: myOnlineGenerals, onlineGenerals: myOnlineGenerals,
+3 -3
View File
@@ -1,3 +1,4 @@
import { withSafeIntegerId } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
@@ -38,7 +39,6 @@ const BUFF_KEYS: InheritBuffType[] = [
const UNIQUE_ITEM_SLOT_ORDER: readonly ItemSlot[] = ['horse', 'weapon', 'book', 'item']; const UNIQUE_ITEM_SLOT_ORDER: readonly ItemSlot[] = ['horse', 'weapon', 'book', 'item'];
const POSTGRES_INTEGER_MAX = 2_147_483_647;
const MIN_INHERIT_BUFF_LEVEL = 1; const MIN_INHERIT_BUFF_LEVEL = 1;
const MAX_INHERIT_BUFF_LEVEL = 5; const MAX_INHERIT_BUFF_LEVEL = 5;
@@ -283,7 +283,7 @@ export const inheritRouter = router({
getLogs: authedProcedure getLogs: authedProcedure
.input( .input(
z.object({ z.object({
lastId: z.number().int().min(1).max(POSTGRES_INTEGER_MAX).optional(), lastId: z.number().int().min(1).max(Number.MAX_SAFE_INTEGER).optional(),
}) })
) )
.query(async ({ ctx, input }) => { .query(async ({ ctx, input }) => {
@@ -300,7 +300,7 @@ export const inheritRouter = router({
take: 30, take: 30,
select: { id: true, year: true, month: true, text: true, createdAt: true }, select: { id: true, year: true, month: true, text: true, createdAt: true },
}); });
return logs; return logs.map(withSafeIntegerId);
}), }),
buyHiddenBuff: engineAuthedProcedure buyHiddenBuff: engineAuthedProcedure
.input( .input(
@@ -0,0 +1,173 @@
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import {
asRecord,
GAME_TICKS_PER_TURN,
officeRequestQuarter,
readOfficeRequest,
type CityOfficeLevel,
} from '@sammo-ts/common';
import { resolveOfficeRequestEligibility, type OfficeCandidate } from '@sammo-ts/logic';
import { accessAuthedProcedure, engineAuthedProcedure } from '../../../trpc.js';
import { getAuthenticatedUserId, getMyGeneral } from '../../shared/general.js';
import { throwIfCommandRejected } from '../../shared/turnDaemon.js';
import { assertNationAccess, resolveChiefStatMin } from '../shared.js';
import { loadCurrentGameTime } from '../../../services/gameClock.js';
export const cityOfficeRequest = engineAuthedProcedure
.input(
z.discriminatedUnion('action', [
z.object({
action: z.literal('request'),
officerLevel: z.union([z.literal(2), z.literal(3), z.literal(4)]),
}),
z.object({
action: z.enum(['approve', 'reject', 'withdraw']),
targetGeneralId: z.number().int().positive(),
officeRequestId: z.string().min(1),
}),
])
)
.mutation(async ({ ctx, input }) => {
const general = await getMyGeneral(ctx);
const result = await ctx.turnDaemon.requestCommand({
...input,
type: 'cityOfficeRequest',
userId: getAuthenticatedUserId(ctx),
generalId: general.id,
...(ctx.requestId
? { requestId: `${ctx.requestId}:nation.cityOfficeRequest:engine:0:cityOfficeRequest` }
: {}),
});
throwIfCommandRejected(result);
if (!result || result.type !== 'cityOfficeRequest')
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Unexpected response' });
if (!result.ok) throw new TRPCError({ code: 'BAD_REQUEST', message: result.reason });
return { ok: true };
});
export const getCityOfficeRequests = accessAuthedProcedure.query(async ({ ctx }) => {
const me = await getMyGeneral(ctx);
assertNationAccess(me);
const [nation, cities, rows, state, clock] = await Promise.all([
ctx.db.nation.findUnique({ where: { id: me.nationId }, select: { meta: true } }),
ctx.db.city.findMany({
where: { nationId: me.nationId },
select: { id: true, nationId: true, name: true, meta: true },
}),
ctx.db.general.findMany({
where: { nationId: me.nationId },
select: {
id: true,
userId: true,
name: true,
nationId: true,
cityId: true,
officerLevel: true,
npcState: true,
strength: true,
intel: true,
meta: true,
penalty: true,
},
}),
ctx.db.worldState.findFirst(),
loadCurrentGameTime(ctx.db),
]);
if (!nation || !state)
throw new TRPCError({ code: 'PRECONDITION_FAILED', message: '국가 또는 게임 상태를 찾을 수 없습니다.' });
const candidates = rows.map((row) => ({ ...row, intelligence: row.intel }));
const applicant: OfficeCandidate = { ...me, intelligence: me.intel };
const city = cities.find((entry) => entry.id === me.cityId) ?? null;
const quarter = officeRequestQuarter(state.currentYear, state.currentMonth);
const own = readOfficeRequest(me.meta);
const requests = candidates.flatMap((general) => {
const request = readOfficeRequest(general.meta);
if (
!request ||
request.generalId !== general.id ||
request.nationId !== me.nationId ||
request.userId !== general.userId
)
return [];
return [{ general, request }];
});
const evaluate = (general: OfficeCandidate, cityId: number, level: CityOfficeLevel) =>
resolveOfficeRequestEligibility({
applicant: general,
city: cities.find((entry) => entry.id === cityId) ?? null,
level,
incumbent:
candidates.find(
(entry) =>
entry.officerLevel === level &&
Number(asRecord(entry.meta).officerCity ?? asRecord(entry.meta).officer_city ?? 0) === cityId
) ?? null,
nationMeta: nation.meta,
chiefStatMin: resolveChiefStatMin(state),
});
const canManage = me.officerLevel >= 5 && !asRecord(me.penalty).noChief;
const nowTick = clock.tick;
const options = ([4, 3, 2] as const).map((level) => {
const check = evaluate(applicant, me.cityId, level);
const occupied = requests.some(
({ request }) =>
request.status === 'pending' && request.cityId === me.cityId && request.officerLevel === level
);
const reason =
check.reason ??
(own?.status === 'pending'
? '요청 대기 중'
: own?.quarter === quarter
? '이번 분기 자원 완료'
: occupied
? '다른 장수 자원 중'
: nowTick === null
? '게임 시계 준비 중'
: null);
return { level, ...check, allowed: !reason, reason };
});
return {
canManage,
generalId: me.id,
nationId: me.nationId,
serverId: String(asRecord(state.meta).serverId ?? ctx.profile?.name ?? 'game'),
city: city ? { id: city.id, name: city.name } : null,
options,
running: clock.running,
tickSeconds: state.tickSeconds,
requests: requests
.filter(({ general, request }) => general.id === me.id || (canManage && request.status === 'pending'))
.map(({ general, request }) => {
const check = evaluate(general, request.cityId, request.officerLevel);
const defaultDecision =
request.defaultDecision === 'reject' || check.defaultDecision === 'reject'
? ('reject' as const)
: ('approve' as const);
const months = Math.max(
0,
Math.ceil((request.dueTick - Number(state.lastTurnTick ?? 0)) / GAME_TICKS_PER_TURN)
);
const monthIndex = state.currentYear * 12 + state.currentMonth - 1 + months;
return {
id: request.id,
generalId: general.id,
generalName: general.name,
cityId: request.cityId,
cityName: cities.find((entry) => entry.id === request.cityId)?.name ?? '-',
officerLevel: request.officerLevel,
status: request.status,
defaultDecision,
defaultReason: check.defaultReason === 'secret' ? ('secret' as const) : request.defaultReason,
resultReason: request.resultReason ?? null,
dueYear: Math.floor(monthIndex / 12),
dueMonth: (monthIndex % 12) + 1,
remainingSeconds:
nowTick === null
? null
: Math.max(0, ((request.dueTick - nowTick) / GAME_TICKS_PER_TURN) * state.tickSeconds),
invalidReason: request.status === 'pending' ? check.reason : null,
};
}),
};
});
@@ -1,3 +1,4 @@
import { toSafeInteger } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
@@ -77,7 +78,7 @@ export const getGeneralLog = accessLimitAuthedInputProcedure(
type: input.type, type: input.type,
generalId: target.id, generalId: target.id,
logs: logs.map((entry) => ({ logs: logs.map((entry) => ({
id: entry.id, id: toSafeInteger(entry.id),
text: entry.text, text: entry.text,
year: entry.year, year: entry.year,
month: entry.month, month: entry.month,
@@ -1,3 +1,4 @@
import { withSafeIntegerId } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { asRecord } from '@sammo-ts/common'; import { asRecord } from '@sammo-ts/common';
@@ -129,6 +130,6 @@ export const getNationInfo = authedProcedure.query(async ({ ctx }) => {
name: city.name, name: city.name,
capital: city.id === nation.capitalCityId, capital: city.id === nation.capitalCityId,
})), })),
history, history: history.map(withSafeIntegerId),
}; };
}); });
+3
View File
@@ -1,3 +1,4 @@
import { cityOfficeRequest, getCityOfficeRequests } from './endpoints/cityOfficeRequests.js';
import { router } from '../../trpc.js'; import { router } from '../../trpc.js';
import { appoint } from './endpoints/appoint.js'; import { appoint } from './endpoints/appoint.js';
import { changePermission } from './endpoints/changePermission.js'; import { changePermission } from './endpoints/changePermission.js';
@@ -25,6 +26,8 @@ export const nationRouter = router({
getSecretGeneralList, getSecretGeneralList,
getCityOverview, getCityOverview,
getPersonnelInfo, getPersonnelInfo,
getCityOfficeRequests,
cityOfficeRequest,
getStratFinan, getStratFinan,
setNotice, setNotice,
setScoutMsg, setScoutMsg,
+91
View File
@@ -0,0 +1,91 @@
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import type { GamePrisma } from '@sammo-ts/infra';
import type { GameApiContext } from '../../context.js';
import { authedProcedure } from '../../trpc.js';
import { getAuthenticatedUserId } from '../shared/general.js';
import { resolveSecretPermission } from '../shared/secretPermission.js';
import { projectPolicyConfiguration, projectPolicySummary } from '../playAudit/policies.js';
import { readAudit, readAuditWorld } from '../playAudit/shared.js';
const area = z.enum(['NPC_VALUES', 'NPC_NATION_PRIORITY', 'NPC_GENERAL_PRIORITY']);
const summarySelect = {
id: true,
schemaVersion: true,
nationId: true,
area: true,
revision: true,
previousId: true,
source: true,
year: true,
month: true,
actor: true,
createdAt: true,
} satisfies GamePrisma.PlayAuditPolicySelect;
// 소속·기밀 권한과 이력을 같은 snapshot에서 읽어 이적/권한 변경 경합을 막는다.
const readNationId = async (ctx: GameApiContext, tx: GamePrisma.TransactionClient): Promise<number> => {
const general = await tx.general.findFirst({ where: { userId: getAuthenticatedUserId(ctx) } });
if (!general) throw new TRPCError({ code: 'NOT_FOUND', message: '장수 정보를 찾을 수 없습니다.' });
if (general.nationId <= 0)
throw new TRPCError({ code: 'PRECONDITION_FAILED', message: '국가에 소속되어있지 않습니다.' });
const nation = await tx.nation.findUnique({ where: { id: general.nationId }, select: { meta: true } });
if (!nation) throw new TRPCError({ code: 'NOT_FOUND', message: '국가 정보를 찾을 수 없습니다.' });
if (resolveSecretPermission(general, nation.meta) < 1)
throw new TRPCError({ code: 'FORBIDDEN', message: '권한이 부족합니다.' });
return general.nationId;
};
export const policyHistory = authedProcedure
.input(
z
.object({
area,
cursor: z.number().int().positive().optional(),
limit: z.number().int().min(1).max(50).default(20),
})
.strict()
)
.query(({ ctx, input }) =>
readAudit(ctx, async (tx) => {
const nationId = await readNationId(ctx, tx);
const world = await readAuditWorld(tx);
const rows = world.serverId
? await tx.playAuditPolicy.findMany({
where: {
serverId: world.serverId,
nationId,
area: input.area,
revision: input.cursor === undefined ? undefined : { lt: input.cursor },
},
orderBy: { revision: 'desc' },
take: input.limit + 1,
select: summarySelect,
})
: [];
return {
coverage: world.serverId ? ('RECORDED_VERSIONS_ONLY' as const) : ('IDENTITY_MISSING' as const),
historyGap: world.historyGap,
items: rows.slice(0, input.limit).map(projectPolicySummary),
nextCursor: rows.length > input.limit ? rows[input.limit - 1]!.revision : null,
};
})
);
export const policyVersion = authedProcedure
.input(z.object({ id: z.string().regex(/^[a-f0-9]{64}$/) }).strict())
.query(({ ctx, input }) =>
readAudit(ctx, async (tx) => {
const nationId = await readNationId(ctx, tx);
const world = await readAuditWorld(tx);
const row = world.serverId
? await tx.playAuditPolicy.findFirst({
where: { id: input.id, serverId: world.serverId, nationId, area: { in: area.options } },
select: { ...summarySelect, before: true, after: true },
})
: null;
if (!row)
throw new TRPCError({ code: 'NOT_FOUND', message: '자국의 현재 기수 정책 기록을 찾을 수 없습니다.' });
return projectPolicyConfiguration(row);
})
);
+3
View File
@@ -15,6 +15,7 @@ import { loadUnitSetDefinitionByName } from '@sammo-ts/game-engine/scenario/unit
import type { GameApiContext } from '../../context.js'; import type { GameApiContext } from '../../context.js';
import { getMyGeneral } from '../shared/general.js'; import { getMyGeneral } from '../shared/general.js';
import { resolveSecretPermission } from '../shared/secretPermission.js'; import { resolveSecretPermission } from '../shared/secretPermission.js';
import { policyHistory, policyVersion } from './history.js';
type SetterInfo = { type SetterInfo = {
setter: string | null; setter: string | null;
@@ -356,6 +357,8 @@ const requestNpcPolicyMutation = async (
}; };
export const npcRouter = router({ export const npcRouter = router({
policyHistory,
policyVersion,
getPolicy: authedProcedure.query(async ({ ctx }) => { getPolicy: authedProcedure.query(async ({ ctx }) => {
const general = await getMyGeneral(ctx); const general = await getMyGeneral(ctx);
if (general.nationId <= 0) { if (general.nationId <= 0) {
@@ -0,0 +1,93 @@
import { TRPCError } from '@trpc/server';
import { asRecord } from '@sammo-ts/common';
import { z } from 'zod';
import { loadMapLayout, loadMapLayoutByName } from '../../maps/mapLayout.js';
import { auditProcedure, readAudit, readAuditWorld, findAuditMonth, zAuditMonth } from './shared.js';
import { citySelect, projectCurrentCity, zAuditCityData } from './projection.js';
/** 도시 표본 전체를 한 지도 단위로 읽는다. 장수/로그/trace는 읽지 않는다. */
export const cityMap = auditProcedure.input(z.object({ at: zAuditMonth.optional() }).strict()).query(({ ctx, input }) =>
readAudit(ctx, async (tx) => {
const world = await readAuditWorld(tx);
const sample = input.at ? await findAuditMonth(tx, world, input.at) : null;
if (input.at && !sample)
return {
...world,
collected: false,
sample,
map: null,
layout: null,
unmappedCityIds: [],
nextCursor: null,
};
const state = await tx.worldState.findFirst({ orderBy: { id: 'asc' }, select: { config: true } });
const environment = asRecord(asRecord(state?.config).environment);
const layout =
typeof environment.mapName === 'string' && environment.mapName.trim()
? await loadMapLayoutByName(environment.mapName)
: await loadMapLayout(ctx.profile.scenario);
const cities = sample
? (
await tx.playAuditCity.findMany({
where: { sampleId: sample.id },
select: { data: true },
take: 1025,
})
).map((row) => zAuditCityData.parse(row.data))
: (await tx.city.findMany({ select: citySelect, take: 1025 })).map(projectCurrentCity);
if (cities.length > 1024)
throw new TRPCError({
code: 'BAD_REQUEST',
message: '지도 조회 범위를 초과했습니다. 도시 목록으로 조회해 주세요.',
});
const nationIds = [...new Set(cities.map((city) => city.nationId))];
const identity = z.object({ id: z.number(), name: z.string(), color: z.string() });
const nations = sample
? (
await tx.playAuditNation.findMany({
where: { sampleId: sample.id, nationId: { in: nationIds } },
select: { data: true },
})
).map((row) => ({ ...identity.parse(row.data), capitalCityId: 0 }))
: await tx.nation.findMany({
where: { id: { in: nationIds } },
select: { id: true, name: true, color: true, capitalCityId: true },
});
const byCity = new Map(cities.map((city) => [city.id, city]));
const byNation = new Map(nations.map((nation) => [nation.id, nation]));
const layoutIds = new Set(layout.cityList.map((city) => city.id));
// 없는 표본을 무주 도시로 그리지 않는다. 당시 이름과 소유만 사용한다.
const visibleLayout = {
...layout,
cityList: layout.cityList
.filter((city) => byCity.has(city.id))
.map((city) => ({ ...city, name: byCity.get(city.id)!.name })),
};
return {
...world,
collected: true,
sample,
layout: visibleLayout,
nextCursor: null,
unmappedCityIds: cities.filter((city) => !layoutIds.has(city.id)).map((city) => city.id),
map: {
year: input.at?.year ?? world.year,
month: input.at?.month ?? world.month,
startYear: world.startYear,
cityList: visibleLayout.cityList.map((layoutCity): [number, number, number, number, number, number] => {
const city = byCity.get(layoutCity.id)!;
return [city.id, city.level, city.state, city.nationId, layoutCity.region, city.supplyState];
}),
nationList: nationIds.map((id): [number, string, string, number] => {
const nation = byNation.get(id);
return [
id,
nation?.name ?? (id === 0 ? '무주' : `국가 #${id} (미수집)`),
nation?.color ?? '#888888',
nation?.capitalCityId ?? 0,
];
}),
},
};
})
);
+33 -3
View File
@@ -1,6 +1,6 @@
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import type { GamePrisma } from '@sammo-ts/infra'; import { decodeAuditDecisionSteps, type GamePrisma } from '@sammo-ts/infra';
import { auditProcedure, monthOrdinal, readAudit, readAuditWorld, zAuditMonth } from './shared.js'; import { auditProcedure, monthOrdinal, readAudit, readAuditWorld, zAuditMonth } from './shared.js';
const zEffectivePolicy = z.object({ const zEffectivePolicy = z.object({
@@ -173,7 +173,35 @@ export const decisionHistory = auditProcedure
.query(({ ctx, input }) => .query(({ ctx, input }) =>
readAudit(ctx, async (tx) => { readAudit(ctx, async (tx) => {
const world = await readAuditWorld(tx); const world = await readAuditWorld(tx);
const month = input.month ?? { year: world.year, month: world.month }; // 장수 턴은 월 경계와 동시에 실행되지 않는다. 현재 상태에서는 마지막으로
// 실제 결정이 저장된 월을 열고, 명시한 과거 월은 빈 월이어도 그대로 보존한다.
const latest =
!input.month && world.serverId
? await tx.playAuditDecision.findFirst({
where: {
serverId: world.serverId,
generalId: input.generalId,
phase: input.phase,
AND: [
{
OR: [
{ year: { gt: world.startYear } },
{ year: world.startYear, month: { gte: world.startMonth } },
],
},
{
OR: [
{ year: { lt: world.year } },
{ year: world.year, month: { lte: world.month } },
],
},
],
},
orderBy: [{ year: 'desc' }, { month: 'desc' }, { tick: 'desc' }, { id: 'desc' }],
select: { year: true, month: true },
})
: null;
const month = input.month ?? latest ?? { year: world.year, month: world.month };
const ordinal = monthOrdinal(month.year, month.month); const ordinal = monthOrdinal(month.year, month.month);
if ( if (
ordinal < monthOrdinal(world.startYear, world.startMonth) || ordinal < monthOrdinal(world.startYear, world.startMonth) ||
@@ -206,6 +234,8 @@ export const decisionHistory = auditProcedure
return { return {
...world, ...world,
month, month,
currentMonth: { year: world.year, month: world.month },
selection: input.month ? ('MONTH' as const) : ('LATEST' as const),
coverage: world.serverId ? ('PROCEDURES_ONLY' as const) : ('IDENTITY_MISSING' as const), coverage: world.serverId ? ('PROCEDURES_ONLY' as const) : ('IDENTITY_MISSING' as const),
items: rows.slice(0, input.limit).map(project), items: rows.slice(0, input.limit).map(project),
nextCursor: rows.length > input.limit && last ? { tick: last.tick.toString(), id: last.id } : null, nextCursor: rows.length > input.limit && last ? { tick: last.tick.toString(), id: last.id } : null,
@@ -245,7 +275,7 @@ export const decisionDetail = auditProcedure
decision: project(row), decision: project(row),
chunks: chunks.slice(0, input.limit).map((chunk) => ({ chunks: chunks.slice(0, input.limit).map((chunk) => ({
ordinal: chunk.ordinal, ordinal: chunk.ordinal,
steps: z.array(zStep).max(128).parse(chunk.steps), steps: z.array(zStep).max(128).parse(decodeAuditDecisionSteps(chunk.steps)),
})), })),
nextCursor: nextCursor:
chunks.length && chunks.at(-1)!.ordinal + 1 < Math.ceil(row.stepCount / 128) chunks.length && chunks.at(-1)!.ordinal + 1 < Math.ceil(row.stepCount / 128)
@@ -0,0 +1,108 @@
import { TRPCError } from '@trpc/server';
import { GamePrisma } from '@sammo-ts/infra';
import { z } from 'zod';
import { generalSelect, projectCurrentGeneral, zAuditGeneralData } from './projection.js';
export const zGeneralSort = z.enum([
'id',
'gold',
'rice',
'crew',
'train',
'atmos',
'leadership',
'strength',
'intelligence',
'experience',
'dedication',
'dex1',
'dex2',
'dex3',
'dex4',
'dex5',
]);
export const zGeneralCursor = z.object({ id: z.number().int().nonnegative(), value: z.number().nullable() }).strict();
const columns: Record<z.infer<typeof zGeneralSort>, string> = {
id: 'id',
gold: 'gold',
rice: 'rice',
crew: 'crew',
train: 'train',
atmos: 'atmos',
leadership: 'leadership',
strength: 'strength',
intelligence: 'intel',
experience: 'experience',
dedication: 'dedication',
dex1: 'dex1',
dex2: 'dex2',
dex3: 'dex3',
dex4: 'dex4',
dex5: 'dex5',
};
export const readSortedGenerals = async (
tx: GamePrisma.TransactionClient,
input: {
sort: z.infer<typeof zGeneralSort>;
order: 'asc' | 'desc';
limit: number;
cursor?: number | z.infer<typeof zGeneralCursor>;
nationId?: number;
cityId?: number;
population?: 'human' | 'npc' | 'troopNpc';
name?: string;
},
sampleId?: string
) => {
if (typeof input.cursor === 'number')
throw new TRPCError({ code: 'BAD_REQUEST', message: '정렬 조건에 맞는 다음 페이지를 선택해 주세요.' });
// SQL 식별자/JSON 경로는 서버 allowlist에서만 조립한다. 사용자 문자열은 parameter다.
const id = GamePrisma.raw(sampleId ? 'general_id' : 'id');
const path = ['leadership', 'strength', 'intelligence'].includes(input.sort)
? `stats,${input.sort}`
: input.sort.startsWith('dex')
? `dex,${input.sort}`
: input.sort;
const metric = sampleId
? GamePrisma.raw(`(data #>> '{${path}}')::double precision`)
: input.sort.startsWith('dex')
? GamePrisma.raw(
`CASE WHEN jsonb_typeof(meta->'${input.sort}') = 'number' THEN (meta->>'${input.sort}')::double precision ELSE 0 END`
)
: GamePrisma.raw(columns[input.sort]);
const filters: GamePrisma.Sql[] = [];
if (sampleId) filters.push(GamePrisma.sql`sample_id = ${sampleId}`);
if (input.nationId !== undefined) filters.push(GamePrisma.sql`nation_id = ${input.nationId}`);
if (input.cityId !== undefined) filters.push(GamePrisma.sql`city_id = ${input.cityId}`);
if (input.population === 'human') filters.push(GamePrisma.sql`npc_state < 2`);
if (input.population === 'npc') filters.push(GamePrisma.sql`npc_state >= 2 AND npc_state <> 5`);
if (input.population === 'troopNpc') filters.push(GamePrisma.sql`npc_state = 5`);
if (input.name)
filters.push(
GamePrisma.sql`${GamePrisma.raw(sampleId ? "data->>'name'" : 'name')} LIKE ${`%${input.name.replace(/[\\%_]/g, '\\$&')}%`}`
);
if (input.cursor) {
const { value, id: cursorId } = input.cursor;
filters.push(
value === null
? GamePrisma.sql`(${metric} IS NULL AND ${id} > ${cursorId})`
: GamePrisma.sql`(${metric} ${GamePrisma.raw(input.order === 'asc' ? '>' : '<')} ${value} OR (${metric} = ${value} AND ${id} > ${cursorId}) OR ${metric} IS NULL)`
);
}
const rows = await tx.$queryRaw<{ id: number; value: number | null; data: unknown }[]>(GamePrisma.sql`
SELECT ${id} AS id, ${metric} AS value, ${sampleId ? GamePrisma.raw('data') : GamePrisma.sql`NULL`} AS data
FROM ${GamePrisma.raw(sampleId ? 'play_audit_general' : 'general')}
WHERE ${filters.length ? GamePrisma.join(filters, ' AND ') : GamePrisma.sql`TRUE`}
ORDER BY ${metric} ${GamePrisma.raw(input.order.toUpperCase())} NULLS LAST, ${id} ASC LIMIT ${input.limit + 1}
`);
const page = rows.slice(0, input.limit);
const current = sampleId
? []
: await tx.general.findMany({ where: { id: { in: page.map((row) => row.id) } }, select: generalSelect });
const byId = new Map(current.map((row) => [row.id, projectCurrentGeneral(row)]));
return {
items: page.map((row) => (sampleId ? zAuditGeneralData.parse(row.data) : byId.get(row.id)!)),
nextCursor:
rows.length > input.limit ? { id: page[page.length - 1]!.id, value: page[page.length - 1]!.value } : null,
};
};
@@ -1,3 +1,6 @@
import { cityMap } from './cityMap.js';
import { TRPCError } from '@trpc/server';
import { readSortedGenerals, zGeneralSort, zGeneralCursor } from './generalSort.js';
import { requestState } from './requests.js'; import { requestState } from './requests.js';
import { decisionHistory, decisionDetail } from './decisions.js'; import { decisionHistory, decisionDetail } from './decisions.js';
import { diplomacyHistory, diplomacyEvent } from './diplomacy.js'; import { diplomacyHistory, diplomacyEvent } from './diplomacy.js';
@@ -27,6 +30,7 @@ import {
} from './projection.js'; } from './projection.js';
export const playAuditRouter = router({ export const playAuditRouter = router({
cityMap,
requestState, requestState,
decisionHistory, decisionHistory,
decisionDetail, decisionDetail,
@@ -161,11 +165,20 @@ export const playAuditRouter = router({
population: z.enum(['human', 'npc', 'troopNpc']).optional(), population: z.enum(['human', 'npc', 'troopNpc']).optional(),
name: z.string().trim().max(64).optional(), name: z.string().trim().max(64).optional(),
order: z.enum(['asc', 'desc']).default('asc'), order: z.enum(['asc', 'desc']).default('asc'),
sort: zGeneralSort.default('id'),
cursor: z.union([z.number().int().nonnegative(), zGeneralCursor]).optional(),
}) })
) )
.query(({ ctx, input }) => .query(({ ctx, input }) =>
readAudit(ctx, async (tx) => { readAudit(ctx, async (tx) => {
const world = await readAuditWorld(tx); const world = await readAuditWorld(tx);
if (input.sort !== 'id') {
const sample = input.at ? await findAuditMonth(tx, world, input.at) : null;
if (input.at && !sample) return { ...world, sample, collected: false, items: [], nextCursor: null };
return { ...world, sample, collected: true, ...(await readSortedGenerals(tx, input, sample?.id)) };
}
if (typeof input.cursor === 'object')
throw new TRPCError({ code: 'BAD_REQUEST', message: '장수 번호 정렬의 다음 페이지가 아닙니다.' });
const npcState = const npcState =
input.population === 'human' input.population === 'human'
? { lt: 2 } ? { lt: 2 }
+3 -2
View File
@@ -1,3 +1,4 @@
import { toSafeInteger, withSafeIntegerId } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import { auditProcedure, monthOrdinal, readAudit, readAuditWorld } from './shared.js'; import { auditProcedure, monthOrdinal, readAudit, readAuditWorld } from './shared.js';
@@ -53,8 +54,8 @@ export const generalLogs = auditProcedure
...world, ...world,
type: input.type, type: input.type,
coverage: world.serverId ? ('IDENTIFIED_LOGS_ONLY' as const) : ('IDENTITY_MISSING' as const), coverage: world.serverId ? ('IDENTIFIED_LOGS_ONLY' as const) : ('IDENTITY_MISSING' as const),
items: rows.slice(0, input.limit), items: rows.slice(0, input.limit).map(withSafeIntegerId),
nextCursor: rows.length > input.limit ? rows[input.limit - 1]!.id : null, nextCursor: rows.length > input.limit ? toSafeInteger(rows[input.limit - 1]!.id) : null,
}; };
}) })
); );
@@ -1,3 +1,5 @@
import { GamePrisma } from '@sammo-ts/infra';
import { asRecord } from '@sammo-ts/common';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import { auditProcedure, monthOrdinal, readAudit, readAuditWorld, zAuditMonth } from './shared.js'; import { auditProcedure, monthOrdinal, readAudit, readAuditWorld, zAuditMonth } from './shared.js';
@@ -5,6 +7,7 @@ import { auditProcedure, monthOrdinal, readAudit, readAuditWorld, zAuditMonth }
const zDex = z.object({ dex1: z.number(), dex2: z.number(), dex3: z.number(), dex4: z.number(), dex5: z.number() }); const zDex = z.object({ dex1: z.number(), dex2: z.number(), dex3: z.number(), dex4: z.number(), dex5: z.number() });
const zPopulation = z.object({ const zPopulation = z.object({
count: z.number().int().nonnegative(), count: z.number().int().nonnegative(),
crew: z.number().nonnegative().nullable().default(null),
gold: z.number(), gold: z.number(),
rice: z.number(), rice: z.number(),
dex: zDex, dex: zDex,
@@ -92,6 +95,34 @@ export const summarizeNationPeriod = (start: number, width: number, months: Nati
}; };
}; };
/** 같은 transaction에서 commit된 당월 정산만 반환한다. 미관측을 0으로 만들지 않는다. */
export const projectCurrentSettlement = (meta: unknown, year: number, month: number, nationId: number) => {
const flows = asRecord(asRecord(meta).playAuditFlows);
const matches = flows.year === year && flows.month === month;
const entries = asRecord(flows.entries);
const resource = (key: 'gold' | 'rice') => {
const row = asRecord(entries[`${nationId}:${key}`]);
if (
!matches ||
row.nationId !== nationId ||
row.resource !== key ||
typeof row.income !== 'number' ||
!Number.isFinite(row.income) ||
typeof row.paid !== 'number' ||
!Number.isFinite(row.paid)
)
return null;
return { income: row.income, paid: row.paid };
};
return {
year,
month,
gold: resource('gold'),
rice: resource('rice'),
complete: matches && flows.complete === true,
};
};
const zCalendarMonth = zAuditMonth.omit({ kind: true }); const zCalendarMonth = zAuditMonth.omit({ kind: true });
export const nationSeries = auditProcedure export const nationSeries = auditProcedure
.input( .input(
@@ -151,6 +182,35 @@ export const nationSeries = auditProcedure
}) })
: []; : [];
const dataBySample = new Map(nations.map((row) => [row.sampleId, zAuditNation.parse(row.data)])); const dataBySample = new Map(nations.map((row) => [row.sampleId, zAuditNation.parse(row.data)]));
// 기존 집계는 같은 월의 장수 표본을 DB에서 합산한다. 원문은 전송하지 않는다.
const legacySamples = [...dataBySample]
.filter(([, nation]) => Object.values(nation.populations).some((group) => group.crew === null))
.map(([id]) => id);
if (legacySamples.length) {
const troops = await tx.$queryRaw<
{ sampleId: string; population: 'human' | 'npc' | 'troopNpc'; count: number; crew: number | null }[]
>(GamePrisma.sql`
SELECT sample_id AS "sampleId",
CASE WHEN npc_state = 5 THEN 'troopNpc' WHEN npc_state < 2 THEN 'human' ELSE 'npc' END AS population,
count(*)::int AS count,
CASE WHEN bool_and(jsonb_typeof(data->'crew') = 'number' AND data->'crew' IS NOT NULL)
THEN sum(CASE WHEN jsonb_typeof(data->'crew') = 'number' THEN (data->>'crew')::double precision END) ELSE NULL END AS crew
FROM play_audit_general
WHERE nation_id = ${input.nationId} AND sample_id IN (${GamePrisma.join(legacySamples)})
GROUP BY sample_id, population
`);
for (const sampleId of legacySamples) {
const nation = dataBySample.get(sampleId)!;
for (const key of ['human', 'npc', 'troopNpc'] as const) {
const group = nation.populations[key];
if (group.crew !== null) continue;
const row = troops.find((item) => item.sampleId === sampleId && item.population === key);
// 저장 인원수와 원본 표본 수가 같을 때만 확정한다.
if (group.count === 0 && !row) group.crew = 0;
else if (row?.count === group.count) group.crew = row.crew;
}
}
}
const sampleByMonth = new Map(samples.map((sample) => [monthOrdinal(sample.year, sample.month), sample])); const sampleByMonth = new Map(samples.map((sample) => [monthOrdinal(sample.year, sample.month), sample]));
const items: ReturnType<typeof summarizeNationPeriod>[] = []; const items: ReturnType<typeof summarizeNationPeriod>[] = [];
for (let period = start; period <= end; period += width) { for (let period = start; period <= end; period += width) {
@@ -166,6 +226,17 @@ export const nationSeries = auditProcedure
} }
items.push(summarizeNationPeriod(period, width, months)); items.push(summarizeNationPeriod(period, width, months));
} }
return { ...world, items, nextCursor: end < to ? dateOf(start + input.limit * width) : null }; const currentState =
world.serverId && end === current
? await tx.worldState.findFirst({ orderBy: { id: 'asc' }, select: { meta: true } })
: null;
return {
...world,
items,
currentSettlement: currentState
? projectCurrentSettlement(currentState.meta, world.year, world.month, input.nationId)
: null,
nextCursor: end < to ? dateOf(start + input.limit * width) : null,
};
}) })
); );
@@ -117,6 +117,12 @@ export const readAuditWorld = async (tx: GamePrisma.TransactionClient) => {
startYear, startYear,
startMonth, startMonth,
collectionStart, collectionStart,
historyGap: (() => {
const gap = asRecord(meta.playAuditGap);
return gap.serverId === serverId && Number.isInteger(gap.firstYear) && Number.isInteger(gap.firstMonth)
? { firstYear: Number(gap.firstYear), firstMonth: Number(gap.firstMonth) }
: null;
})(),
tick: world.lastTurnTick?.toString() ?? null, tick: world.lastTurnTick?.toString() ?? null,
asOf: new Date().toISOString(), asOf: new Date().toISOString(),
}; };
+2 -2
View File
@@ -1,5 +1,5 @@
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { asNumber, asRecord } from '@sammo-ts/common'; import { asNumber, asRecord, withSafeIntegerId } from '@sammo-ts/common';
import { LEGACY_DEFAULT_MAX_LEVEL, LogCategory, LogScope } from '@sammo-ts/logic'; import { LEGACY_DEFAULT_MAX_LEVEL, LogCategory, LogScope } from '@sammo-ts/logic';
import { z } from 'zod'; import { z } from 'zod';
@@ -306,7 +306,7 @@ export const publicRouter = router({
} }
const snapshot = { const snapshot = {
...map, ...map,
history, history: history.map(withSafeIntegerId),
}; };
await ctx.redis.set(cacheKey, JSON.stringify(snapshot), { EX: PUBLIC_CACHE_TTL_SECONDS }); await ctx.redis.set(cacheKey, JSON.stringify(snapshot), { EX: PUBLIC_CACHE_TTL_SECONDS });
return snapshot; return snapshot;
+55 -70
View File
@@ -1,7 +1,7 @@
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { z } from 'zod'; import { z } from 'zod';
import { asRecord } from '@sammo-ts/common'; import { asRecord, type TurnDaemonCommandResult } from '@sammo-ts/common';
import type { TournamentType } from '@sammo-ts/logic'; import type { TournamentType } from '@sammo-ts/logic';
import type { TournamentState } from '../../tournament/types.js'; import type { TournamentState } from '../../tournament/types.js';
@@ -18,6 +18,16 @@ import {
} from '../../services/redisClockFence.js'; } from '../../services/redisClockFence.js';
import { loadClockAdminStatus } from '../../services/clockReadiness.js'; import { loadClockAdminStatus } from '../../services/clockReadiness.js';
const assertTournamentAdjustment = (result: TurnDaemonCommandResult | null): void => {
if (result?.type === 'commandRejected') {
throw new TRPCError({ code: 'FORBIDDEN', message: result.reason });
}
if (!result || result.type !== 'tournamentAdjustGeneral') {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Unexpected tournament response' });
}
if (!result.ok) throw new TRPCError({ code: result.code, message: result.reason });
};
const hasAdminRole = (roles: string[], profileName: string): boolean => { const hasAdminRole = (roles: string[], profileName: string): boolean => {
if (roles.includes('superuser') || roles.includes('admin') || roles.includes('admin.superuser')) { if (roles.includes('superuser') || roles.includes('admin') || roles.includes('admin.superuser')) {
return true; return true;
@@ -197,6 +207,7 @@ const zMatch = z.object({
const zBetEntry = z.object({ const zBetEntry = z.object({
generalId: z.number().int().positive(), generalId: z.number().int().positive(),
userId: z.string().min(1).nullable().optional(),
targetId: z.number().int().positive(), targetId: z.number().int().positive(),
amount: z.number().int().positive(), amount: z.number().int().positive(),
}); });
@@ -458,7 +469,7 @@ export const tournamentRouter = router({
}); });
}), }),
getBettingSummary: authedProcedure.query(async ({ ctx }) => { getBettingSummary: authedProcedure.query(async ({ ctx }) => {
const general = await getMyGeneral(ctx); await getMyGeneral(ctx);
const store = new TournamentStore(ctx.redis, buildTournamentKeys(ctx.profile.name)); const store = new TournamentStore(ctx.redis, buildTournamentKeys(ctx.profile.name));
const [state, entries, matches] = await Promise.all([ const [state, entries, matches] = await Promise.all([
store.getState(), store.getState(),
@@ -488,7 +499,7 @@ export const tournamentRouter = router({
} }
totals[entry.targetId] = (totals[entry.targetId] ?? 0) + entry.amount; totals[entry.targetId] = (totals[entry.targetId] ?? 0) + entry.amount;
totalAmount += entry.amount; totalAmount += entry.amount;
if (entry.generalId === general.id) { if (entry.userId === ctx.auth!.user.id) {
myTotals[entry.targetId] = (myTotals[entry.targetId] ?? 0) + entry.amount; myTotals[entry.targetId] = (myTotals[entry.targetId] ?? 0) + entry.amount;
myAmount += entry.amount; myAmount += entry.amount;
} }
@@ -523,20 +534,15 @@ export const tournamentRouter = router({
const develCost = resolveCurrentDevelCost(worldState); const develCost = resolveCurrentDevelCost(worldState);
const feeResult = await ctx.turnDaemon.requestCommand({ const feeResult = await ctx.turnDaemon.requestCommand({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
requestId: tournamentJoinCommandRequestId(ctx.requestId, 'resources'), requestId: tournamentJoinCommandRequestId(ctx.requestId, 'resources'),
reason: 'tournamentJoin', reason: 'tournamentJoin',
adjustments: [{ generalId: general.id, goldDelta: -develCost, minGoldAfter: 0 }], userId: ctx.auth!.user.id,
generalId: general.id,
goldDelta: -develCost,
minGoldAfter: 0,
}); });
if (!feeResult || feeResult.type !== 'adjustGeneralResources') { assertTournamentAdjustment(feeResult);
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Unexpected response' });
}
if (!feeResult.ok || feeResult.processed !== 1) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: feeResult.ok ? '금이 부족합니다.' : feeResult.reason,
});
}
const meta = asRecord(general.meta); const meta = asRecord(general.meta);
const level = typeof meta.explevel === 'number' ? meta.explevel : 0; const level = typeof meta.explevel === 'number' ? meta.explevel : 0;
@@ -558,12 +564,17 @@ export const tournamentRouter = router({
try { try {
await store.setParticipants(next); await store.setParticipants(next);
} catch (error) { } catch (error) {
await ctx.turnDaemon.requestCommand({ assertTournamentAdjustment(
type: 'adjustGeneralResources', await ctx.turnDaemon.requestCommand({
requestId: tournamentJoinCommandRequestId(ctx.requestId, 'projection-rollback-resources'), type: 'tournamentAdjustGeneral',
reason: 'tournamentJoinRollback', requestId: tournamentJoinCommandRequestId(ctx.requestId, 'projection-rollback-resources'),
adjustments: [{ generalId: general.id, goldDelta: develCost }], reason: 'tournamentJoinRollback',
}); userId: ctx.auth!.user.id,
generalId: general.id,
goldDelta: develCost,
minGoldAfter: 0,
})
);
throw error; throw error;
} }
return { ok: true, count: next.length }; return { ok: true, count: next.length };
@@ -663,8 +674,13 @@ export const tournamentRouter = router({
throw new TRPCError({ code: 'BAD_REQUEST', message: '올바르지 않은 베팅 대상입니다.' }); throw new TRPCError({ code: 'BAD_REQUEST', message: '올바르지 않은 베팅 대상입니다.' });
} }
// 구형 row의 당시 actor는 현재 장수 소유자로 추정할 수 없다.
// 운영 전환은 베팅 종료 뒤 하거나 검증한 actor 정보를 관리자가 복원해야 한다.
if (entries.some((entry) => entry.userId === undefined)) {
throw new TRPCError({ code: 'BAD_REQUEST', message: '이전 베팅의 사용자 확인이 필요합니다.' });
}
const previousBetAmount = entries const previousBetAmount = entries
.filter((entry) => entry.generalId === general.id) .filter((entry) => entry.userId === ctx.auth!.user.id)
.reduce((sum, entry) => sum + entry.amount, 0); .reduce((sum, entry) => sum + entry.amount, 0);
if (previousBetAmount + input.amount > 1_000) { if (previousBetAmount + input.amount > 1_000) {
throw new TRPCError({ throw new TRPCError({
@@ -674,68 +690,37 @@ export const tournamentRouter = router({
} }
const adjustResult = await ctx.turnDaemon.requestCommand({ const adjustResult = await ctx.turnDaemon.requestCommand({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
requestId: tournamentBetCommandRequestId(ctx.requestId, 'resources'), requestId: tournamentBetCommandRequestId(ctx.requestId, 'resources'),
reason: 'tournamentBet', reason: 'tournamentBet',
adjustments: [{ generalId: general.id, goldDelta: -input.amount, minGoldAfter: 500 }], userId: ctx.auth!.user.id,
generalId: general.id,
goldDelta: -input.amount,
betGoldDelta: input.amount,
minGoldAfter: 500,
}); });
if (!adjustResult || adjustResult.type !== 'adjustGeneralResources') { assertTournamentAdjustment(adjustResult);
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Unexpected response' });
}
if (!adjustResult.ok || adjustResult.processed !== 1) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: adjustResult.ok ? '금이 부족합니다.' : adjustResult.reason,
});
}
const rankResult = await ctx.turnDaemon.requestCommand({
type: 'adjustGeneralMeta',
requestId: tournamentBetCommandRequestId(ctx.requestId, 'rank'),
reason: 'tournamentBet',
adjustments: [
{
generalId: general.id,
metaDelta: { betgold: input.amount },
},
],
});
if (!rankResult || rankResult.type !== 'adjustGeneralMeta' || !rankResult.ok) {
await ctx.turnDaemon.requestCommand({
type: 'adjustGeneralResources',
requestId: tournamentBetCommandRequestId(ctx.requestId, 'rank-rollback-resources'),
reason: 'tournamentBetRollback',
adjustments: [{ generalId: general.id, goldDelta: input.amount }],
});
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: '베팅 기록을 저장하지 못했습니다.' });
}
try { try {
await store.appendBettingEntry({ await store.appendBettingEntry({
generalId: general.id, generalId: general.id,
userId: ctx.auth!.user.id,
targetId: input.targetId, targetId: input.targetId,
amount: input.amount, amount: input.amount,
}); });
} catch (error) { } catch (error) {
await Promise.all([ assertTournamentAdjustment(
ctx.turnDaemon.requestCommand({ await ctx.turnDaemon.requestCommand({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
requestId: tournamentBetCommandRequestId(ctx.requestId, 'projection-rollback-resources'), requestId: tournamentBetCommandRequestId(ctx.requestId, 'projection-rollback-resources'),
reason: 'tournamentBetRollback', reason: 'tournamentBetRollback',
adjustments: [{ generalId: general.id, goldDelta: input.amount }], userId: ctx.auth!.user.id,
}), generalId: general.id,
ctx.turnDaemon.requestCommand({ goldDelta: input.amount,
type: 'adjustGeneralMeta', betGoldDelta: -input.amount,
requestId: tournamentBetCommandRequestId(ctx.requestId, 'projection-rollback-rank'), minGoldAfter: 0,
reason: 'tournamentBetRollback', })
adjustments: [ );
{
generalId: general.id,
metaDelta: { betgold: -input.amount },
},
],
}),
]);
throw error; throw error;
} }
return { ok: true }; return { ok: true };
+22
View File
@@ -11,6 +11,7 @@ import {
buildRecruitmentCommandInfo, buildRecruitmentCommandInfo,
buildTurnCommandTable, buildTurnCommandTable,
evaluateReservedTurnPermission, evaluateReservedTurnPermission,
resolveCommandJoinEnv,
} from '../../turns/commandTable.js'; } from '../../turns/commandTable.js';
import { loadMapDefinitionByName } from '../../maps/mapDefinition.js'; import { loadMapDefinitionByName } from '../../maps/mapDefinition.js';
import { import {
@@ -45,7 +46,10 @@ import {
buildRefAmountPresets, buildRefAmountPresets,
buildRefGeneralTargetOptions, buildRefGeneralTargetOptions,
buildRefNationTargetOptions, buildRefNationTargetOptions,
COUNTER_STRATEGY_COMMAND_KEYS,
} from '../../turns/commandTargets.js'; } from '../../turns/commandTargets.js';
import { resolveImpossibleStrategicCommands } from '../../services/mainNationProjection.js';
import { resolveNationBlockScout, resolveNationScoutMessage } from '../nation/shared.js';
const zPushAmount = z const zPushAmount = z
.number() .number()
@@ -357,10 +361,20 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
if (adjacentNationId && adjacentNationId !== general.nationId) adjacentNationIds.add(adjacentNationId); if (adjacentNationId && adjacentNationId !== general.nationId) adjacentNationIds.add(adjacentNationId);
} }
} }
// Ref che_피장파장 availableCommandTypeList: 아국 nation_env next_execute_<전략명> 기준 남은 턴.
const strategyCooldowns = Object.fromEntries(
resolveImpossibleStrategicCommands(nation?.meta, worldState.currentYear, worldState.currentMonth).map(
(entry) => [`che_${entry.name}`, entry.remainingTurns]
)
);
const nationTargetOptions = buildRefNationTargetOptions({ const nationTargetOptions = buildRefNationTargetOptions({
actorNationId: general.nationId, actorNationId: general.nationId,
join: { actorNpcState: general.npcState, ...resolveCommandJoinEnv(worldState) },
strategyAvailable: COUNTER_STRATEGY_COMMAND_KEYS.some((key) => strategyCooldowns[key] === undefined),
nations: nations.map((entry) => { nations: nations.map((entry) => {
const relation = diplomacyByNation.get(entry.id); const relation = diplomacyByNation.get(entry.id);
const meta = asRecord(entry.meta);
const gennum = readGeneralMetaNumber(meta, 'gennum');
return { return {
id: entry.id, id: entry.id,
name: entry.name, name: entry.name,
@@ -374,6 +388,9 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
diplomacyTerm: relation?.term ?? 0, diplomacyTerm: relation?.term ?? 0,
adjacent: adjacentNationIds.has(entry.id), adjacent: adjacentNationIds.has(entry.id),
diplomacyRestricted: (readGeneralMetaNumber(entry.meta, 'surlimit') ?? 0) !== 0, diplomacyRestricted: (readGeneralMetaNumber(entry.meta, 'surlimit') ?? 0) !== 0,
...(gennum === null ? {} : { gennum }),
blockScout: resolveNationBlockScout(meta),
scoutMessage: resolveNationScoutMessage(meta),
}; };
}), }),
}); });
@@ -398,6 +415,7 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
item: general.itemCode, item: general.itemCode,
}, },
}); });
const dexterityByArmType: Record<string, number> = {};
const inputOptions: TurnCommandInputOptions = { const inputOptions: TurnCommandInputOptions = {
cities: cities.map((entry) => ({ cities: cities.map((entry) => ({
value: entry.id, value: entry.id,
@@ -406,6 +424,8 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
})), })),
nations: nationTargetOptions.nations, nations: nationTargetOptions.nations,
nationTargets: nationTargetOptions.nationTargets, nationTargets: nationTargetOptions.nationTargets,
nationScoutMessages: nationTargetOptions.nationScoutMessages,
strategyCooldowns,
generals: generalTargetOptions.generals, generals: generalTargetOptions.generals,
generalTargets: generalTargetOptions.generalTargets, generalTargets: generalTargetOptions.generalTargets,
crewTypes: (environment.unitSet.crewTypes ?? []) crewTypes: (environment.unitSet.crewTypes ?? [])
@@ -413,6 +433,7 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
.map((entry) => ({ value: entry.id, label: entry.name })), .map((entry) => ({ value: entry.id, label: entry.name })),
armTypes: Object.entries(environment.unitSet.armTypes ?? {}).map(([value, label]) => { armTypes: Object.entries(environment.unitSet.armTypes ?? {}).map(([value, label]) => {
const dexterity = readGeneralMetaNumber(general.meta, `dex${value}`); const dexterity = readGeneralMetaNumber(general.meta, `dex${value}`);
if (dexterity !== null) dexterityByArmType[value] = dexterity;
return { return {
value: Number(value), value: Number(value),
label, label,
@@ -449,6 +470,7 @@ export const getTurnCommandTable = async (ctx: GameApiContext, generalId: number
actorRice: general.rice, actorRice: general.rice,
...(city ? { citySecurity: city.security } : {}), ...(city ? { citySecurity: city.security } : {}),
...(nation ? { nationGold: nation.gold, nationRice: nation.rice, nationLevel: nation.level } : {}), ...(nation ? { nationGold: nation.gold, nationRice: nation.rice, nationLevel: nation.level } : {}),
dexterity: dexterityByArmType,
}, },
}; };
+13
View File
@@ -6,6 +6,7 @@ import { asRecord, isRecord } from '@sammo-ts/common';
import { LogCategory, LogScope } from '@sammo-ts/logic'; import { LogCategory, LogScope } from '@sammo-ts/logic';
import type { GameApiContext } from '../../context.js'; import type { GameApiContext } from '../../context.js';
import { loadMapLayout, loadMapLayoutByName } from '../../maps/mapLayout.js';
import { loadPublicMap, type BaseMapResult } from '../../maps/worldMap.js'; import { loadPublicMap, type BaseMapResult } from '../../maps/worldMap.js';
import { import {
formatGeneralAccessLimitMessage, formatGeneralAccessLimitMessage,
@@ -339,11 +340,23 @@ export const yearbookRouter = router({
const firstYearMonth = firstRow ? joinYearMonth(firstRow.year, firstRow.month) : fallbackYearMonth; const firstYearMonth = firstRow ? joinYearMonth(firstRow.year, firstRow.month) : fallbackYearMonth;
const lastYearMonth = lastRow ? joinYearMonth(lastRow.year, lastRow.month) : fallbackYearMonth; const lastYearMonth = lastRow ? joinYearMonth(lastRow.year, lastRow.month) : fallbackYearMonth;
const selectedYearMonth = target.isCurrentProfile ? currentYearMonth : lastYearMonth; const selectedYearMonth = target.isCurrentProfile ? currentYearMonth : lastYearMonth;
// Ref v_history.php resolves an archived generation's map from ng_games.map.
const mapLayout = target.isCurrentProfile
? await loadMapLayout(ctx.profile.scenario)
: await loadMapLayoutByName(
(
await ctx.db.gameHistory.findUnique({
where: { serverId: target.archiveKey },
select: { map: true },
})
)?.map || 'che'
);
return { return {
firstYearMonth, firstYearMonth,
lastYearMonth, lastYearMonth,
currentYearMonth: selectedYearMonth, currentYearMonth: selectedYearMonth,
mapLayout,
}; };
}), }),
getHistory: authedProcedure getHistory: authedProcedure
+188 -113
View File
@@ -1,8 +1,10 @@
import { safeHttpLoggerOptions, safeHttpErrorHandler, createApiOriginGuard } from '@sammo-ts/common';
import fastify, { type FastifyRequest } from 'fastify'; import fastify, { type FastifyRequest } from 'fastify';
import cors from '@fastify/cors'; import cors from '@fastify/cors';
import fastifyStatic from '@fastify/static'; import fastifyStatic from '@fastify/static';
import path from 'path'; import path from 'path';
import fs from 'node:fs/promises'; import fs from 'node:fs/promises';
import { TRPCError } from '@trpc/server';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify'; import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import { import {
buildGameEventChannel, buildGameEventChannel,
@@ -21,7 +23,7 @@ import {
import { resolveGameApiConfigFromEnv } from './config.js'; import { resolveGameApiConfigFromEnv } from './config.js';
import { createGameApiContext, type DatabaseClient as _DatabaseClient } from './context.js'; import { createGameApiContext, type DatabaseClient as _DatabaseClient } from './context.js';
import { DatabaseTurnDaemonTransport } from './daemon/databaseTransport.js'; import { DatabaseTurnDaemonTransport } from './daemon/databaseTransport.js';
import { InMemoryFlushStore, RedisGatewayFlushSubscriber, type FlushStore } from './auth/flushStore.js'; import { RedisFlushStore, RedisGatewayFlushSubscriber, type FlushStore } from './auth/flushStore.js';
import { RedisAccessTokenStore } from './auth/accessTokenStore.js'; import { RedisAccessTokenStore } from './auth/accessTokenStore.js';
import { import {
consumeRealtimeAccessGrantHeader, consumeRealtimeAccessGrantHeader,
@@ -32,6 +34,9 @@ import { appRouter } from './router.js';
import { buildBattleSimQueueKeys } from './battleSim/keys.js'; import { buildBattleSimQueueKeys } from './battleSim/keys.js';
import { RedisBattleSimTransport } from './battleSim/redisTransport.js'; import { RedisBattleSimTransport } from './battleSim/redisTransport.js';
import { RedisRealtimeEventHub } from './realtime/eventHub.js'; import { RedisRealtimeEventHub } from './realtime/eventHub.js';
import { isGameAccessBlocked } from '@sammo-ts/common/auth/sanctions';
import { AuthenticatedRealtimeConnection } from './realtime/authenticatedConnection.js';
import { RealtimeConnectionAdmission } from './realtime/connectionAdmission.js';
import { formatSseFrame } from './realtime/sse.js'; import { formatSseFrame } from './realtime/sse.js';
import { import {
shouldForwardRealtimeEvent, shouldForwardRealtimeEvent,
@@ -51,7 +56,7 @@ import { ReadModelOutboxWorker } from './realtime/outboxWorker.js';
import { DeferredGeneralAccessWorker } from './services/deferredGeneralAccess.js'; import { DeferredGeneralAccessWorker } from './services/deferredGeneralAccess.js';
import { WebPushOutboxWorker } from './services/webPushOutboxWorker.js'; import { WebPushOutboxWorker } from './services/webPushOutboxWorker.js';
import { scopeHttpIdempotencyKey } from './requestId.js'; import { scopeHttpIdempotencyKey } from './requestId.js';
import { loadClockReadiness } from './services/clockReadiness.js'; import { loadProfileReadiness } from './services/clockReadiness.js';
const extractBearerToken = (value: string | string[] | undefined): string | null => { const extractBearerToken = (value: string | string[] | undefined): string | null => {
if (!value) { if (!value) {
@@ -71,16 +76,22 @@ const extractBearerToken = (value: string | string[] | undefined): string | null
const resolveAuthFromToken = async ( const resolveAuthFromToken = async (
token: string | null, token: string | null,
accessTokenStore: RedisAccessTokenStore, accessTokenStore: RedisAccessTokenStore,
flushStore: FlushStore flushStore: FlushStore,
profileName: string
): Promise<GameSessionTokenPayload | null> => { ): Promise<GameSessionTokenPayload | null> => {
if (!token) { if (!token) {
return null; return null;
} }
const stored = await accessTokenStore.get(token); const stored = await accessTokenStore.get(token);
if (!stored) { if (!stored || stored.profile !== profileName) {
return null; return null;
} }
const flushedAt = flushStore.getFlushedAt(stored.user.id); let flushedAt: Date | null;
try {
flushedAt = await flushStore.getFlushedAt(stored.user.id);
} catch {
throw new TRPCError({ code: 'SERVICE_UNAVAILABLE', message: 'Authentication protection is unavailable.' });
}
if (flushedAt && new Date(stored.issuedAt) <= flushedAt) { if (flushedAt && new Date(stored.issuedAt) <= flushedAt) {
return null; return null;
} }
@@ -99,11 +110,18 @@ export const createGameApiServer = async () => {
imageUploadSecret imageUploadSecret
); );
const app = fastify({ const app = fastify({
logger: true, logger: safeHttpLoggerOptions,
routerOptions: { routerOptions: {
maxParamLength: 2048, maxParamLength: 2048,
}, },
}); });
app.setErrorHandler(safeHttpErrorHandler);
const realtimeConnections = new Set<AuthenticatedRealtimeConnection>();
const realtimeAdmission = new RealtimeConnectionAdmission();
app.addHook('preClose', async () => {
realtimeAdmission.close();
for (const connection of realtimeConnections) connection.close();
});
const postgres = createGamePostgresConnector(resolvePostgresConfigFromEnv({ schema: config.profile })); const postgres = createGamePostgresConnector(resolvePostgresConfigFromEnv({ schema: config.profile }));
const redis = createRedisConnector(resolveRedisConfigFromEnv()); const redis = createRedisConnector(resolveRedisConfigFromEnv());
@@ -134,7 +152,18 @@ export const createGameApiServer = async () => {
requestTimeoutMs: config.battleSimRequestTimeoutMs, requestTimeoutMs: config.battleSimRequestTimeoutMs,
resultTtlSeconds: config.battleSimResultTtlSeconds, resultTtlSeconds: config.battleSimResultTtlSeconds,
}); });
const flushStore = new InMemoryFlushStore(); const flushStore = new RedisFlushStore(redis.client, config.flushChannel);
try {
await flushStore.assertInitialized();
} catch {
await Promise.allSettled([
Promise.resolve().then(() => {
if (redis.client.isOpen) redis.client.destroy();
}),
postgres.disconnect(),
]);
throw new Error('Gateway session revocation is unavailable; game API startup refused.');
}
const flushSubscriberClient = redis.client.duplicate(); const flushSubscriberClient = redis.client.duplicate();
try { try {
await flushSubscriberClient.connect(); await flushSubscriberClient.connect();
@@ -150,6 +179,12 @@ export const createGameApiServer = async () => {
config.flushChannel, config.flushChannel,
flushStore, flushStore,
async (event) => { async (event) => {
const flushedAt = Date.parse(event.flushedAt);
for (const connection of realtimeConnections) {
if (connection.auth.user.id === event.userId && Date.parse(connection.auth.issuedAt) <= flushedAt) {
connection.close();
}
}
await Promise.all([iconFlushHandler(event), identityFlushHandler(event)]); await Promise.all([iconFlushHandler(event), identityFlushHandler(event)]);
}, },
(error, event) => { (error, event) => {
@@ -239,8 +274,9 @@ export const createGameApiServer = async () => {
app.addHook('onClose', closeResources); app.addHook('onClose', closeResources);
app.addHook('onRequest', createApiOriginGuard(config.corsAllowedOrigins));
await app.register(cors, { await app.register(cors, {
origin: true, origin: config.corsAllowedOrigins,
credentials: true, credentials: true,
}); });
@@ -256,7 +292,7 @@ export const createGameApiServer = async () => {
...trpcJsonBodyHttpServerOptions, ...trpcJsonBodyHttpServerOptions,
createContext: async ({ req }: { req: FastifyRequest }) => { createContext: async ({ req }: { req: FastifyRequest }) => {
const token = extractBearerToken(req.headers.authorization); const token = extractBearerToken(req.headers.authorization);
const auth = await resolveAuthFromToken(token, accessTokenStore, flushStore); const auth = await resolveAuthFromToken(token, accessTokenStore, flushStore, config.profileName);
const rawIdempotencyKey = Array.isArray(req.headers['idempotency-key']) const rawIdempotencyKey = Array.isArray(req.headers['idempotency-key'])
? req.headers['idempotency-key'][0] ? req.headers['idempotency-key'][0]
: req.headers['idempotency-key']; : req.headers['idempotency-key'];
@@ -299,80 +335,124 @@ export const createGameApiServer = async () => {
}, },
}); });
const resolveRealtimeAuth = async (token: string | null): Promise<GameSessionTokenPayload | null> => {
const auth = await resolveAuthFromToken(token, accessTokenStore, flushStore, config.profileName);
if (
!auth ||
auth.profile !== config.profileName ||
isGameAccessBlocked(auth.sanctions, [config.profileName, config.profile])
)
return null;
return auth;
};
app.get(config.eventsPath, async (request, reply) => { app.get(config.eventsPath, async (request, reply) => {
const query = request.query as { token?: string; scope?: string }; const query = request.query as { token?: string; scope?: string };
const subscriptionScope = query.scope === 'tournament' ? 'tournament' : 'dashboard'; const subscriptionScope = query.scope === 'tournament' ? 'tournament' : 'dashboard';
const tokenFromHeader = extractBearerToken(request.headers.authorization); const tokenFromHeader = extractBearerToken(request.headers.authorization);
const tokenFromQuery = typeof query.token === 'string' ? query.token : null; if (query.token !== undefined || !tokenFromHeader) {
const auth = await resolveAuthFromToken(tokenFromHeader ?? tokenFromQuery, accessTokenStore, flushStore);
if (!auth) {
await reply.status(401).send({ ok: false, error: 'unauthorized' }); await reply.status(401).send({ ok: false, error: 'unauthorized' });
return; return;
} }
let expired = false;
const loadViewerIdentity = async (): Promise<RealtimeViewerIdentity> => { let disconnected = false;
const general = await postgres.prisma.general.findFirst({ const opening = realtimeAdmission.begin(() => {
where: { userId: auth.user.id, npcState: 0 }, expired = true;
select: { id: true, cityId: true, nationId: true, officerLevel: true, meta: true, penalty: true }, if (!reply.sent && !reply.raw.destroyed) {
}); reply.header('Retry-After', '15');
if (!general) { void reply.status(503).send({ ok: false, error: 'realtime_unavailable' });
return { generalId: null, cityId: null, nationId: null, canReadDiplomacy: false };
} }
const nation = });
general.nationId > 0 if (!opening) {
? await postgres.prisma.nation.findUnique({ await reply.header('Retry-After', '15').status(503).send({ ok: false, error: 'realtime_unavailable' });
where: { id: general.nationId }, return;
select: { meta: true },
})
: null;
return {
generalId: general.id,
cityId: general.cityId,
nationId: general.nationId,
canReadDiplomacy: Boolean(nation && resolveNationPermission(general, nation.meta, false) >= 3),
};
};
let viewerIdentity = await loadViewerIdentity();
reply.hijack();
const requestOrigin = request.headers.origin;
if (typeof requestOrigin === 'string' && requestOrigin.length > 0) {
// Hijacked SSE responses bypass Fastify's normal CORS response hook.
reply.raw.setHeader('Access-Control-Allow-Origin', requestOrigin);
reply.raw.setHeader('Access-Control-Allow-Credentials', 'true');
reply.raw.setHeader('Vary', 'Origin');
} }
reply.raw.setHeader('Content-Type', 'text/event-stream'); const cancelOpening = () => {
reply.raw.setHeader('Cache-Control', 'no-cache'); disconnected = true;
reply.raw.setHeader('Connection', 'keep-alive'); opening.cancelOpening();
reply.raw.setHeader('X-Accel-Buffering', 'no'); };
request.raw.setTimeout(0); request.raw.once('aborted', cancelOpening);
reply.raw.setTimeout?.(0); reply.raw.once('close', cancelOpening);
reply.raw.flushHeaders?.(); let transferred = false;
try {
const sendFrame = (payload: string) => { let auth: GameSessionTokenPayload | null = null;
try { try {
reply.raw.write(payload); auth = await resolveRealtimeAuth(tokenFromHeader);
} catch { } catch {
if (opening.canContinue() && !reply.raw.destroyed)
await reply.status(503).send({ ok: false, error: 'authentication_unavailable' });
return; return;
} }
}; if (!opening.canContinue() || reply.raw.destroyed) return;
sendFrame( if (!auth || auth.profile !== config.profileName) {
formatSseFrame({ await reply.status(401).send({ ok: false, error: 'unauthorized' });
event: 'ready', return;
data: '{}', }
}) if (!opening.reserveUser(auth.user.id)) {
); if (opening.canContinue())
await reply
.header('Retry-After', '15')
.status(429)
.send({ ok: false, error: 'realtime_connection_limit' });
return;
}
let closed = false; const loadViewerIdentity = async (): Promise<RealtimeViewerIdentity> => {
let eventQueue = Promise.resolve(); const general = await postgres.prisma.general.findFirst({
const unsubscribe = realtimeHub.subscribe((event) => { where: { userId: auth.user.id, npcState: 0 },
if (!shouldForwardRealtimeEvent(event, subscriptionScope)) return; select: { id: true, cityId: true, nationId: true, officerLevel: true, meta: true, penalty: true },
eventQueue = eventQueue });
.then(async () => { if (!general) {
if (closed) return; return { generalId: null, cityId: null, nationId: null, canReadDiplomacy: false };
}
const nation =
general.nationId > 0
? await postgres.prisma.nation.findUnique({
where: { id: general.nationId },
select: { meta: true },
})
: null;
return {
generalId: general.id,
cityId: general.cityId,
nationId: general.nationId,
canReadDiplomacy: Boolean(nation && resolveNationPermission(general, nation.meta, false) >= 3),
};
};
let viewerIdentity = await loadViewerIdentity();
if (!opening.canContinue() || reply.raw.destroyed || !opening.startStreaming()) return;
reply.hijack();
const requestOrigin = request.headers.origin;
if (typeof requestOrigin === 'string' && requestOrigin.length > 0) {
// Hijacked SSE responses bypass Fastify's normal CORS response hook.
reply.raw.setHeader('Access-Control-Allow-Origin', requestOrigin);
reply.raw.setHeader('Access-Control-Allow-Credentials', 'true');
reply.raw.setHeader('Vary', 'Origin');
}
reply.raw.setHeader('Content-Type', 'text/event-stream');
reply.raw.setHeader('Cache-Control', 'no-cache');
reply.raw.setHeader('Connection', 'keep-alive');
reply.raw.setHeader('X-Accel-Buffering', 'no');
request.raw.setTimeout(0);
reply.raw.setTimeout?.(0);
reply.raw.flushHeaders?.();
const connection = new AuthenticatedRealtimeConnection(auth, reply.raw, () =>
resolveRealtimeAuth(tokenFromHeader)
);
realtimeConnections.add(connection);
connection.onClose(() => {
realtimeConnections.delete(connection);
// A disconnected socket can still own an in-flight Redis/SQL operation.
void connection.whenIdle().then(opening.release, opening.release);
});
transferred = true;
connection.enqueue(async () => formatSseFrame({ event: 'ready', data: '{}' }));
const unsubscribe = realtimeHub.subscribe((event) => {
if (!shouldForwardRealtimeEvent(event, subscriptionScope)) return;
connection.enqueue(async (currentAuth) => {
const identities = [viewerIdentity]; const identities = [viewerIdentity];
if (shouldReloadRealtimeViewerIdentity(event, viewerIdentity)) { if (shouldReloadRealtimeViewerIdentity(event, viewerIdentity)) {
const nextIdentity = await loadViewerIdentity(); const nextIdentity = await loadViewerIdentity();
@@ -381,69 +461,64 @@ export const createGameApiServer = async () => {
} }
let refreshGrant: string | undefined; let refreshGrant: string | undefined;
const publicEvent = toPublicRealtimeEvent(event, identities, () => { const publicEvent = toPublicRealtimeEvent(event, identities, () => {
refreshGrant ??= createRealtimeAccessGrant(auth, config.profileName, config.gameTokenSecret); refreshGrant ??= createRealtimeAccessGrant(
currentAuth,
config.profileName,
config.gameTokenSecret
);
return refreshGrant; return refreshGrant;
}); });
if (!publicEvent || closed) return; if (!publicEvent || connection.closed) return null;
if (refreshGrant) { if (refreshGrant) {
try { try {
await registerRealtimeAccessGrant(redis.client, refreshGrant, config.profileName); await registerRealtimeAccessGrant(redis.client, refreshGrant, config.profileName);
} catch { } catch {
// Preserve the invalidation. An unregistered grant safely falls back // An unregistered grant falls back to the scored refresh path.
// to the normal scored refresh path.
} }
} }
sendFrame( return formatSseFrame({ event: publicEvent.type, data: JSON.stringify(publicEvent) });
formatSseFrame({
event: publicEvent.type,
data: JSON.stringify(publicEvent),
})
);
})
.catch(() => {
// A best-effort notification must not affect committed game state.
}); });
}); });
connection.onClose(unsubscribe);
let heartbeatPending = false; let heartbeatPending = false;
const heartbeat = setInterval(() => { const heartbeat = setInterval(() => {
if (heartbeatPending) return; if (heartbeatPending || connection.closed) return;
heartbeatPending = true; heartbeatPending = true;
void turnEngineStatus connection.enqueue(async () => {
.get() try {
.then((turnEngineRunning) => { return formatSseFrame({
if (closed) return;
sendFrame(
formatSseFrame({
event: 'ping', event: 'ping',
data: JSON.stringify({ turnEngineRunning }), data: JSON.stringify({ turnEngineRunning: await turnEngineStatus.get() }),
}) });
); } finally {
}) heartbeatPending = false;
.finally(() => { }
heartbeatPending = false;
}); });
}, 15000); }, 15_000);
connection.onClose(() => {
const close = () => { clearInterval(heartbeat);
closed = true; });
clearInterval(heartbeat); request.raw.once('aborted', connection.close);
unsubscribe(); connection.onClose(() => {
}; request.raw.off('aborted', connection.close);
});
request.raw.on('close', close); } catch (error) {
request.raw.on('aborted', close); if (!expired && !disconnected) throw error;
} finally {
request.raw.off('aborted', cancelOpening);
reply.raw.off('close', cancelOpening);
if (!transferred) opening.release();
}
}); });
app.get('/healthz', async (_request, reply) => { app.get('/healthz', async (_request, reply) => {
const clock = await loadClockReadiness(postgres.prisma); const readiness = await loadProfileReadiness(postgres.prisma, config.profileName);
if (!clock.reconciliationComplete) reply.code(503); if (!readiness.ok) reply.code(503);
return { return {
ok: clock.reconciliationComplete, ...readiness,
profile: config.profileName, profile: config.profileName,
postgresPool: postgres.getPoolStats(), postgresPool: postgres.getPoolStats(),
accountIconReconciliation: accountIconResetReconciler.getHealth(), accountIconReconciliation: accountIconResetReconciler.getHealth(),
clock,
}; };
}); });
@@ -1,4 +1,5 @@
import { parseGameClockPhase } from '@sammo-ts/common'; import { parseGameClockPhase } from '@sammo-ts/common';
import { GamePrisma } from '@sammo-ts/infra';
import type { DatabaseClient } from '../context.js'; import type { DatabaseClient } from '../context.js';
@@ -90,3 +91,20 @@ export const loadClockAdminStatus = async (db: DatabaseClient) => {
}, },
}; };
}; };
/** API 기동과 별개로 데몬의 초기 감사 저장 및 clock 복구가 끝나야 배포 준비 완료다. */
export const loadProfileReadiness = async (db: DatabaseClient, profileName: string) => {
const [clock, leases] = await Promise.all([
loadClockReadiness(db),
db.$queryRaw<Array<{ ready: boolean }>>(GamePrisma.sql`
SELECT EXISTS (
SELECT 1 FROM turn_daemon_lease
WHERE profile = ${profileName}
AND clock_ready = TRUE
AND lease_until > (clock_timestamp() AT TIME ZONE 'UTC')
) AS ready
`),
]);
const daemonReady = leases[0]?.ready === true;
return { ok: clock.reconciliationComplete && daemonReady, clock, daemonReady };
};
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { createHash, randomUUID } from 'node:crypto'; import { createHash, randomUUID } from 'node:crypto';
import { asRecord, resolveAccessLimitLevel, resolveAccessRefreshLimit } from '@sammo-ts/common'; import { asRecord, resolveAccessLimitLevel, resolveAccessRefreshLimit } from '@sammo-ts/common';
@@ -94,7 +95,13 @@ export const enqueueDeferredGeneralAccess = async (
} }
await redis.eval(ENQUEUE_SCRIPT, { await redis.eval(ENQUEUE_SCRIPT, {
keys: [activeKey(profileName)], keys: [activeKey(profileName)],
arguments: [String(generalId), auth.user.id, String(weight), String(now.getTime()), String(ACTIVE_BATCH_TTL_MS)], arguments: [
String(generalId),
auth.user.id,
String(weight),
String(now.getTime()),
String(ACTIVE_BATCH_TTL_MS),
],
}); });
return true; return true;
}; };
@@ -258,9 +265,10 @@ export const flushDeferredGeneralAccessBatch = async (
), ),
missing_periods AS ( missing_periods AS (
INSERT INTO "traffic_period" ( INSERT INTO "traffic_period" (
"world_state_id", "year", "month", "started_at", "last_refresh", "refresh", "online" "id", "world_state_id", "year", "month", "started_at", "last_refresh", "refresh", "online"
) )
SELECT SELECT
${existingRowIdOrNext('traffic_period', GamePrisma.sql`stable_id.world_state_id = ${worldState.id} AND stable_id.year = (missing_key / 12)::INTEGER AND stable_id.month = (missing_key % 12 + 1)::INTEGER`)},
${worldState.id}, ${worldState.id},
(missing_key / 12)::INTEGER, (missing_key / 12)::INTEGER,
(missing_key % 12 + 1)::INTEGER, (missing_key % 12 + 1)::INTEGER,
@@ -281,9 +289,10 @@ export const flushDeferredGeneralAccessBatch = async (
), ),
period_upsert AS ( period_upsert AS (
INSERT INTO "traffic_period" ( INSERT INTO "traffic_period" (
"world_state_id", "year", "month", "started_at", "last_refresh", "refresh", "online" "id", "world_state_id", "year", "month", "started_at", "last_refresh", "refresh", "online"
) )
SELECT SELECT
${existingRowIdOrNext('traffic_period', GamePrisma.sql`stable_id.world_state_id = ${worldState.id} AND stable_id.year = ${worldState.currentYear} AND stable_id.month = ${worldState.currentMonth}`)},
${worldState.id}, ${worldState.id},
${worldState.currentYear}, ${worldState.currentYear},
${worldState.currentMonth}, ${worldState.currentMonth},
@@ -343,10 +352,11 @@ export const flushDeferredGeneralAccessBatch = async (
), ),
access_updates AS ( access_updates AS (
INSERT INTO "general_access_log" ( INSERT INTO "general_access_log" (
"general_id", "user_id", "last_refresh", "refresh", "refresh_total", "id", "general_id", "user_id", "last_refresh", "refresh", "refresh_total",
"refresh_score", "refresh_score_total" "refresh_score", "refresh_score_total"
) )
SELECT SELECT
${existingRowIdOrNext('general_access_log', GamePrisma.sql`stable_id.general_id = resolved."general_id"`)},
resolved."general_id", resolved."general_id",
resolved."user_id", resolved."user_id",
resolved."last_refresh", resolved."last_refresh",
@@ -471,11 +481,9 @@ export class DeferredGeneralAccessWorker {
await this.redis.del(key); await this.redis.del(key);
return; return;
} }
await this.redis.set( await this.redis.set(key, JSON.stringify({ nextAccessAt: state.nextAccessAt.toISOString() }), {
key, PX: Math.max(1, state.nextAccessAt.getTime() - now.getTime()),
JSON.stringify({ nextAccessAt: state.nextAccessAt.toISOString() }), });
{ PX: Math.max(1, state.nextAccessAt.getTime() - now.getTime()) }
);
}) })
); );
} }
+6 -1
View File
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { asRecord, resolveAccessLimitLevel, resolveAccessRefreshLimit, type AccessLimitLevel } from '@sammo-ts/common'; import { asRecord, resolveAccessLimitLevel, resolveAccessRefreshLimit, type AccessLimitLevel } from '@sammo-ts/common';
import { acquireGameSchemaAdvisoryXactLock, GENERAL_ACCESS_PERSISTENCE_LOCK, GamePrisma } from '@sammo-ts/infra'; import { acquireGameSchemaAdvisoryXactLock, GENERAL_ACCESS_PERSISTENCE_LOCK, GamePrisma } from '@sammo-ts/infra';
@@ -230,7 +231,7 @@ export const upsertGeneralAccess = async (
await db.$transaction(async (transaction) => { await db.$transaction(async (transaction) => {
await acquireGameSchemaAdvisoryXactLock(transaction, GENERAL_ACCESS_PERSISTENCE_LOCK); await acquireGameSchemaAdvisoryXactLock(transaction, GENERAL_ACCESS_PERSISTENCE_LOCK);
const periodKey = input.year * 12 + input.month - 1; const periodKey = input.year * 12 + input.month - 1;
const periodRows = await transaction.$queryRaw<Array<{ id: number }>>( const periodRows = await transaction.$queryRaw<Array<{ id: bigint }>>(
GamePrisma.sql` GamePrisma.sql`
WITH latest_period AS ( WITH latest_period AS (
SELECT MAX(year * 12 + month - 1)::INTEGER AS period_key SELECT MAX(year * 12 + month - 1)::INTEGER AS period_key
@@ -269,6 +270,7 @@ export const upsertGeneralAccess = async (
RETURNING id RETURNING id
) )
INSERT INTO traffic_period ( INSERT INTO traffic_period (
id,
world_state_id, world_state_id,
year, year,
month, month,
@@ -277,6 +279,7 @@ export const upsertGeneralAccess = async (
refresh refresh
) )
VALUES ( VALUES (
${existingRowIdOrNext('traffic_period', GamePrisma.sql`stable_id.world_state_id = ${input.worldStateId} AND stable_id.year = ${input.year} AND stable_id.month = ${input.month}`)},
${input.worldStateId}, ${input.worldStateId},
${input.year}, ${input.year},
${input.month}, ${input.month},
@@ -341,6 +344,7 @@ export const upsertGeneralAccess = async (
await transaction.$executeRaw( await transaction.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO general_access_log ( INSERT INTO general_access_log (
id,
general_id, general_id,
user_id, user_id,
last_refresh, last_refresh,
@@ -350,6 +354,7 @@ export const upsertGeneralAccess = async (
refresh_score_total refresh_score_total
) )
VALUES ( VALUES (
${existingRowIdOrNext('general_access_log', GamePrisma.sql`stable_id.general_id = ${input.generalId}`)},
${input.generalId}, ${input.generalId},
${input.userId}, ${input.userId},
${input.now}, ${input.now},
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { GamePrisma } from '@sammo-ts/infra'; import { GamePrisma } from '@sammo-ts/infra';
import type { GameApiContext } from '../context.js'; import type { GameApiContext } from '../context.js';
@@ -20,11 +21,13 @@ export const recordGeneralActivity = async (
const written = await ctx.db.$executeRaw( const written = await ctx.db.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO general_access_log ( INSERT INTO general_access_log (
id,
general_id, general_id,
user_id, user_id,
last_action_at last_action_at
) )
SELECT SELECT
${existingRowIdOrNext('general_access_log', GamePrisma.sql`stable_id.general_id = "general".id`)},
id, id,
${user.id}, ${user.id},
${now} ${now}
+2 -1
View File
@@ -1,3 +1,4 @@
import { upsertWithoutSequence } from '@sammo-ts/infra';
import { asNumber, asRecord } from '@sammo-ts/common'; import { asNumber, asRecord } from '@sammo-ts/common';
import { GamePrisma } from '@sammo-ts/infra'; import { GamePrisma } from '@sammo-ts/infra';
import { import {
@@ -132,7 +133,7 @@ export const setInheritancePoint = async (
key: InheritPointKey, key: InheritPointKey,
value: number value: number
): Promise<void> => { ): Promise<void> => {
await db.inheritancePoint.upsert({ await upsertWithoutSequence(db.inheritancePoint, {
where: { where: {
userId_key: { userId_key: {
userId, userId,
+45 -36
View File
@@ -3,6 +3,7 @@ import {
GamePrisma, GamePrisma,
hashAuditDiplomacyDocument, hashAuditDiplomacyDocument,
persistAuditDiplomacyEvents, persistAuditDiplomacyEvents,
withPlayAuditSavepoint,
projectAuditDocumentState, projectAuditDocumentState,
readTurnRuntimeReady, readTurnRuntimeReady,
type AuditDiplomacyEventDraft, type AuditDiplomacyEventDraft,
@@ -83,6 +84,7 @@ const readCoordinate = async (ctx: GameApiContext) => {
}; };
export const createDiplomacyDocumentAudit = (ctx: GameApiContext, actor: GeneralRow, permission: number) => { export const createDiplomacyDocumentAudit = (ctx: GameApiContext, actor: GeneralRow, permission: number) => {
let collectionFailed = false;
const changes: { const changes: {
letter: Letter; letter: Letter;
before: Record<string, unknown> | null; before: Record<string, unknown> | null;
@@ -92,48 +94,55 @@ export const createDiplomacyDocumentAudit = (ctx: GameApiContext, actor: General
return { return {
record: (letter: Letter, before: Record<string, unknown> | null, eventType: DocumentAction) => { record: (letter: Letter, before: Record<string, unknown> | null, eventType: DocumentAction) => {
if (!ctx.auditInput) return; if (!ctx.auditInput) return;
changes.push({ letter, before, after: projectAuditDocumentState(letter), eventType }); try {
changes.push({ letter, before, after: projectAuditDocumentState(letter), eventType });
} catch {
collectionFailed = true;
}
}, },
flush: async (): Promise<void> => { flush: async (): Promise<void> => {
// 무transaction legacy unit fixture에는 입력 원장 identity를 꾸며 넣지 않는다. // 무transaction legacy unit fixture에는 입력 원장 identity를 꾸며 넣지 않는다.
const input = ctx.auditInput; const input = ctx.auditInput;
if (!input || !changes.length) return; if (!input || (!changes.length && !collectionFailed)) return;
if (input.actorUserId !== actor.userId || input.actorUserId !== ctx.auth?.user.id) if (input.actorUserId !== actor.userId || input.actorUserId !== ctx.auth?.user.id)
throw new Error('Play audit diplomacy actor mismatch'); throw new Error('Play audit diplomacy actor mismatch');
const coordinate = await readCoordinate(ctx); await withPlayAuditSavepoint(ctx.db, async (auditDb) => {
if (!coordinate) return; if (collectionFailed) throw new Error('Audit document collection failed');
const events: AuditDiplomacyEventDraft[] = changes.map((change, index) => ({ const coordinate = await readCoordinate({ ...ctx, db: auditDb });
schemaVersion: 1, if (!coordinate) return;
serverId: coordinate.serverId, const events: AuditDiplomacyEventDraft[] = changes.map((change, index) => ({
srcNationId: change.letter.srcNationId, schemaVersion: 1,
destNationId: change.letter.destNationId, serverId: coordinate.serverId,
category: 'DOCUMENT', srcNationId: change.letter.srcNationId,
source: 'API', destNationId: change.letter.destNationId,
eventType: change.eventType, category: 'DOCUMENT',
documentId: change.letter.id, source: 'API',
documentHash: hashAuditDiplomacyDocument(change.letter), eventType: change.eventType,
previousDocumentId: change.letter.prevId, documentId: change.letter.id,
year: coordinate.year, documentHash: hashAuditDiplomacyDocument(change.letter),
month: coordinate.month, previousDocumentId: change.letter.prevId,
tick: coordinate.tick, year: coordinate.year,
clockRevision: coordinate.clockRevision, month: coordinate.month,
executionId: `api:${input.requestId}`, tick: coordinate.tick,
ordinal: index + 1, clockRevision: coordinate.clockRevision,
requestId: input.requestId, executionId: `api:${input.requestId}`,
inputSequence: input.sequence, ordinal: index + 1,
actor: { requestId: input.requestId,
userId: actor.userId, inputSequence: input.sequence,
generalId: actor.id, actor: {
name: actor.name, userId: actor.userId,
nationId: actor.nationId, generalId: actor.id,
officerLevel: actor.officerLevel, name: actor.name,
npcState: actor.npcState, nationId: actor.nationId,
permission, officerLevel: actor.officerLevel,
}, npcState: actor.npcState,
before: change.before, permission,
after: change.after, },
})); before: change.before,
await persistAuditDiplomacyEvents(ctx.db, events); after: change.after,
}));
await persistAuditDiplomacyEvents(auditDb, events);
});
}, },
}; };
}; };
@@ -1,4 +1,4 @@
import { createHash, createHmac, randomUUID } from 'node:crypto'; import { performSignedImageUpload } from '@sammo-ts/common/images/imageUpload';
export interface ContentImageUploadResult { export interface ContentImageUploadResult {
publicUrl: string; publicUrl: string;
@@ -21,32 +21,16 @@ export class RemoteContentImageStore implements ContentImageUploadStore {
if (!/^[a-f0-9]{32}\.(?:avif|webp|jpg|png|gif)$/.test(input.filename)) { if (!/^[a-f0-9]{32}\.(?:avif|webp|jpg|png|gif)$/.test(input.filename)) {
throw new Error('Invalid content image filename.'); throw new Error('Invalid content image filename.');
} }
const pathname = `/v1/uploads/content/core2026/${input.filename}`; await performSignedImageUpload(
const expires = String(Math.floor(this.now() / 1000) + 60); {
const requestId = randomUUID(); ...input,
const digest = createHash('sha256').update(input.body).digest('hex'); kind: 'content',
const signature = createHmac('sha256', this.secret) baseUrl: this.baseUrl,
.update(`${expires}.${requestId}.${pathname}.${input.contentType}.${digest}`) secret: this.secret,
.digest('hex');
const response = await this.fetchImpl(`${this.baseUrl.replace(/\/$/, '')}${pathname}`, {
method: 'PUT',
headers: {
'content-type': input.contentType,
'x-image-client': 'core2026',
'x-image-expires': expires,
'x-image-request-id': requestId,
'x-image-signature': signature,
}, },
body: new Uint8Array(input.body), this.fetchImpl,
}); this.now
if (!response.ok) { );
throw new Error(`Image repository upload failed with HTTP ${response.status}.`);
}
const expectedPath = `uploads/core2026/${input.filename}`;
const payload: unknown = await response.json();
if (!payload || typeof payload !== 'object' || !('path' in payload) || payload.path !== expectedPath) {
throw new Error('Image repository returned an unexpected content path.');
}
return { publicUrl: `${this.publicBaseUrl.replace(/\/$/, '')}/${input.filename}` }; return { publicUrl: `${this.publicBaseUrl.replace(/\/$/, '')}/${input.filename}` };
} }
} }
+5 -1
View File
@@ -31,6 +31,7 @@ export class CorruptTournamentProjectionError extends Error {
const zTournamentBet = z const zTournamentBet = z
.object({ .object({
generalId: z.number().int(), generalId: z.number().int(),
userId: z.string().min(1).nullable().optional(),
targetId: z.number().int(), targetId: z.number().int(),
amount: z.number(), amount: z.number(),
}) })
@@ -286,7 +287,10 @@ export class TournamentStore {
async appendBettingEntry(entry: TournamentBetEntry): Promise<TournamentBetEntry[]> { async appendBettingEntry(entry: TournamentBetEntry): Promise<TournamentBetEntry[]> {
const entries = await this.getBettingEntries(); const entries = await this.getBettingEntries();
const existing = entries.find( const existing = entries.find(
(candidate) => candidate.generalId === entry.generalId && candidate.targetId === entry.targetId (candidate) =>
candidate.generalId === entry.generalId &&
candidate.targetId === entry.targetId &&
candidate.userId === entry.userId
); );
if (existing) { if (existing) {
existing.amount += entry.amount; existing.amount += entry.amount;
+2
View File
@@ -74,6 +74,8 @@ export interface TournamentMatchEntry {
export interface TournamentBetEntry { export interface TournamentBetEntry {
generalId: number; generalId: number;
// 로그인 actor는 장수 빙의와 독립적이다. null은 서버가 만든 NPC, 생략은 구형 row다.
userId?: string | null;
targetId: number; targetId: number;
amount: number; amount: number;
} }
+18 -13
View File
@@ -760,7 +760,7 @@ const buildNpcBettingPlan = async (options: {
for (const npc of npcBetList) { for (const npc of npcBetList) {
const targetId = rng.choice(candidateIds); const targetId = rng.choice(candidateIds);
if (!existingBettors.has(npc.id as number)) { if (!existingBettors.has(npc.id as number)) {
entries.push({ generalId: npc.id as number, targetId, amount: betGold }); entries.push({ generalId: npc.id as number, userId: null, targetId, amount: betGold });
} }
} }
return entries; return entries;
@@ -789,19 +789,24 @@ export const seedNpcBets = async (options: {
} }
const requestPrefix = `tournament:${state.bettingId ?? `${state.openYear}:${state.openMonth}:${state.type}`}:npc-bet`; const requestPrefix = `tournament:${state.bettingId ?? `${state.openYear}:${state.openMonth}:${state.type}`}:npc-bet`;
await daemonTransport.sendCommand({ // Wait for one durable ENGINE result before publishing entries or opening stage 6.
type: 'adjustGeneralResources', // The engine skips NPCs whose owner changed after this deterministic plan was stored.
requestId: `${requestPrefix}:resources`, const result = await daemonTransport.requestCommand({
reason: 'tournamentNpcBet', type: 'tournamentSeedNpcBets',
adjustments: plan.map((entry) => ({ generalId: entry.generalId, goldDelta: -entry.amount })), requestId: `${requestPrefix}:atomic`,
}); bets: plan.map((entry) => ({ generalId: entry.generalId, amount: entry.amount })),
await daemonTransport.sendCommand({
type: 'adjustGeneralMeta',
requestId: `${requestPrefix}:meta`,
reason: 'tournamentNpcBet',
adjustments: plan.map((entry) => ({ generalId: entry.generalId, metaDelta: { betgold: entry.amount } })),
}); });
if (!result || result.type !== 'tournamentSeedNpcBets' || !result.ok) {
throw new Error('NPC tournament betting did not commit.');
}
const processed = new Set(result.processedGeneralIds);
const existing = await store.getBettingEntries(); const existing = await store.getBettingEntries();
const existingBettors = new Set(existing.map((entry) => entry.generalId)); const existingBettors = new Set(existing.map((entry) => entry.generalId));
await store.setBettingEntries(existing.concat(plan.filter((entry) => !existingBettors.has(entry.generalId)))); await store.setBettingEntries(
existing.concat(
plan
.filter((entry) => processed.has(entry.generalId) && !existingBettors.has(entry.generalId))
.map((entry) => ({ ...entry, userId: null }))
)
);
}; };
+3 -2
View File
@@ -1,3 +1,4 @@
import { safeTrpcErrorFormatter } from '@sammo-ts/common';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { initTRPC, TRPCError } from '@trpc/server'; import { initTRPC, TRPCError } from '@trpc/server';
import { middlewareMarker } from '@trpc/server/unstable-core-do-not-import'; import { middlewareMarker } from '@trpc/server/unstable-core-do-not-import';
@@ -20,10 +21,10 @@ import {
import { getDeferredGeneralAccessLimit } from './services/deferredGeneralAccess.js'; import { getDeferredGeneralAccessLimit } from './services/deferredGeneralAccess.js';
import { recordGeneralActivity } from './services/generalActivity.js'; import { recordGeneralActivity } from './services/generalActivity.js';
const t = initTRPC.context<GameApiContext>().create(); const t = initTRPC.context<GameApiContext>().create({ errorFormatter: safeTrpcErrorFormatter });
const requireAuthMiddleware = t.middleware(({ ctx, next }) => { const requireAuthMiddleware = t.middleware(({ ctx, next }) => {
if (!ctx.auth) { if (!ctx.auth || ctx.auth.profile !== ctx.profile.name) {
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
message: 'Unauthorized', message: 'Unauthorized',
+34 -8
View File
@@ -36,6 +36,8 @@ export interface TurnCommandOption {
crew?: number; crew?: number;
troopId?: number; troopId?: number;
npcState?: number; npcState?: number;
/** 등용·장수대상임관처럼 여러 국가 장수를 고르는 목록의 국가별 묶음 기준. */
nationId?: number;
} }
export interface TurnCommandAmountPreset { export interface TurnCommandAmountPreset {
@@ -97,10 +99,21 @@ export interface TurnCommandInputField {
tupleLabels?: string[]; tupleLabels?: string[];
} }
/** 임관 권유문. message는 서버에서 purifyNationHtml로 정제한 HTML이다. */
export interface TurnCommandNationScoutMessage {
nationId: number;
name: string;
color: string;
message: string;
}
export interface TurnCommandInputOptions { export interface TurnCommandInputOptions {
cities: TurnCommandOption[]; cities: TurnCommandOption[];
nations: TurnCommandOption[]; nations: TurnCommandOption[];
nationTargets?: Record<string, TurnCommandOption[]>; nationTargets?: Record<string, TurnCommandOption[]>;
nationScoutMessages?: TurnCommandNationScoutMessage[];
/** 아국 전략 명령별 남은 재사용 대기 턴. 대기 중인 전략만 담는다. */
strategyCooldowns?: Record<string, number>;
generals: TurnCommandOption[]; generals: TurnCommandOption[];
generalTargets?: Record<string, TurnCommandOption[]>; generalTargets?: Record<string, TurnCommandOption[]>;
crewTypes: TurnCommandOption[]; crewTypes: TurnCommandOption[];
@@ -117,6 +130,8 @@ export interface TurnCommandInputOptions {
nationGold?: number; nationGold?: number;
nationRice?: number; nationRice?: number;
nationLevel?: number; nationLevel?: number;
/** 숙련전환 전/후 미리보기용 본인 병과별 숙련(`dex<armType>`). */
dexterity?: Record<string, number>;
}; };
} }
@@ -151,6 +166,8 @@ export const buildEquipmentTradeItemOptions = (options: {
{ {
value: 'None', value: 'None',
label: ownedItem ? `${ownedItem.name} 판매` : `${slotName} 판매`, label: ownedItem ? `${ownedItem.name} 판매` : `${slotName} 판매`,
// Ref che_장비매매.vue: 소유하지 않은 종류의 판매는 붉은색 `(불가)`로 표시한다.
...(ownedItem ? {} : { availableNow: false }),
description: ownedItem description: ownedItem
? `소유 물품 판매 · 판매가 ${Math.floor((ownedItem.cost ?? 0) / 2).toLocaleString()}금` ? `소유 물품 판매 · 판매가 ${Math.floor((ownedItem.cost ?? 0) / 2).toLocaleString()}금`
: ownedCode && ownedCode !== 'None' : ownedCode && ownedCode !== 'None'
@@ -177,6 +194,7 @@ export const buildEquipmentTradeItemOptions = (options: {
items[item.slot].push({ items[item.slot].push({
value: item.key, value: item.key,
label: item.name, label: item.name,
availableNow: options.currentSecurity >= item.reqSecu && options.generalGold >= cost,
description: `${availability} · 가격 ${cost.toLocaleString()} · ${plainLegacyInfo(item.info)}`, description: `${availability} · 가격 ${cost.toLocaleString()} · ${plainLegacyInfo(item.info)}`,
}); });
} }
@@ -229,8 +247,9 @@ const FIELD_LABELS: Record<string, string> = {
nationName: '국가명', nationName: '국가명',
nationType: '국가 성향', nationType: '국가 성향',
colorType: '국기 색상', colorType: '국기 색상',
srcArmType: '기존 병과', // 숙련전환 전용. Ref che_숙련전환.vue의 '감소 대상 숙련 :'·'전환 대상 숙련 :'.
destArmType: '변경 병과', srcArmType: '감소 대상 숙련',
destArmType: '전환 대상 숙련',
itemType: '장비 종류', itemType: '장비 종류',
itemCode: '장비', itemCode: '장비',
crewType: '병종', crewType: '병종',
@@ -240,7 +259,8 @@ const FIELD_LABELS: Record<string, string> = {
optionText: '행동', optionText: '행동',
year: '기간(년)', year: '기간(년)',
month: '기간(월)', month: '기간(월)',
commandType: '대응 명령', // 피장파장 전용. Ref che_피장파장.vue의 '전략 :'.
commandType: '전략',
amountList: '지원 물자', amountList: '지원 물자',
}; };
@@ -266,10 +286,14 @@ const STATIC_LABELS: Record<string, Record<string, string>> = {
item: '도구', item: '도구',
}, },
commandType: { commandType: {
che_선전포고: '선전포고', // 피장파장 대상 전략. Ref availableCommandTypeList의 name(getName()).
che_불가침제의: '불가침 제의', che_필사즉생: '필사즉생',
che_불가침파기제의: '불가침 파기 제의', che_백성동원: '백성동원',
che_종전제의: '종전 제의', che_수몰: '수몰',
che_허보: '허보',
che_의병모집: '의병모집',
che_이호경식: '이호경식',
che_급습: '급습',
}, },
}; };
@@ -448,8 +472,10 @@ const sanitizeLegacyTurnArgString = (value: string): string => {
if (stripped === '' || stripped === '0') { if (stripped === '' || stripped === '0') {
return ''; return '';
} }
// Core의 Vue text와 formatLegacyLogHtml이 출력 시 &를 escape한다.
// Ref의 입력 단계 & 변환까지 적용하면 국호/예약 인자에 entity가 저장된다.
// <, > 중화는 로그의 legacy markup과 사용자 입력을 구분하기 위해 유지한다.
return stripped return stripped
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;') .replaceAll('<', '&lt;')
.replaceAll('>', '&gt;') .replaceAll('>', '&gt;')
.replace(/^[\p{Z}\p{C}]+|[\p{Z}\p{C}]+$/gu, ''); .replace(/^[\p{Z}\p{C}]+|[\p{Z}\p{C}]+$/gu, '');
+13
View File
@@ -479,6 +479,19 @@ const mapCityRow = (row: CityRow): City => {
}; };
}; };
/** 명령표의 임관 대상 표시에 쓰는 Ref AllowJoinDestNation 시점 값. */
export const resolveCommandJoinEnv = (
worldState: WorldStateRow
): { relYear: number; openingPartYear: number; initialNationGenLimit: number } => {
const constValues = asRecord(asRecord(worldState.config).const);
const env = buildConstraintEnv(worldState);
return {
relYear: Number(env.relYear),
openingPartYear: Number(env.openingPartYear),
initialNationGenLimit: resolveNumber(constValues, ['initialNationGenLimit'], 0),
};
};
const mapNationRow = (row: NationRow): Nation => ({ const mapNationRow = (row: NationRow): Nation => ({
id: row.id, id: row.id,
name: row.name, name: row.name,
+155 -34
View File
@@ -1,6 +1,6 @@
import { DIPLOMACY_STATE } from '@sammo-ts/logic'; import { DIPLOMACY_STATE } from '@sammo-ts/logic';
import type { TurnCommandAmountPreset, TurnCommandOption } from './commandInput.js'; import type { TurnCommandAmountPreset, TurnCommandNationScoutMessage, TurnCommandOption } from './commandInput.js';
export interface GeneralTargetSource { export interface GeneralTargetSource {
id: number; id: number;
@@ -33,6 +33,20 @@ export interface NationTargetSource {
diplomacyTerm: number; diplomacyTerm: number;
adjacent: boolean; adjacent: boolean;
diplomacyRestricted?: boolean; diplomacyRestricted?: boolean;
/** Ref nation.gennum. 없으면 generalCount를 쓴다. */
gennum?: number;
/** 임관 금지(nation.scout). */
blockScout?: boolean;
/** 서버에서 purifyNationHtml로 정제한 임관 권유문. */
scoutMessage?: string;
}
/** Ref AllowJoinDestNation이 쓰는 actor·시점 정보. */
export interface NationJoinContext {
actorNpcState: number;
relYear: number;
openingPartYear: number;
initialNationGenLimit: number;
} }
export interface RefGeneralTargetOptions { export interface RefGeneralTargetOptions {
@@ -43,11 +57,21 @@ export interface RefGeneralTargetOptions {
export interface RefNationTargetOptions { export interface RefNationTargetOptions {
nations: TurnCommandOption[]; nations: TurnCommandOption[];
nationTargets: Record<string, TurnCommandOption[]>; nationTargets: Record<string, TurnCommandOption[]>;
nationScoutMessages: TurnCommandNationScoutMessage[];
} }
const SAME_NATION_GENERAL_COMMANDS = ['che_증여'] as const; const SAME_NATION_GENERAL_COMMANDS = ['che_증여'] as const;
const SAME_NATION_NATION_COMMANDS = ['che_발령', 'che_포상', 'che_몰수', 'che_부대탈퇴지시'] as const; const SAME_NATION_NATION_COMMANDS = ['che_발령', 'che_포상', 'che_몰수', 'che_부대탈퇴지시'] as const;
const statsText = (entry: GeneralTargetSource): string =>
[
entry.leadership === undefined ? null : `통솔 ${entry.leadership.toLocaleString()}`,
entry.strength === undefined ? null : `무력 ${entry.strength.toLocaleString()}`,
entry.intel === undefined ? null : `지력 ${entry.intel.toLocaleString()}`,
]
.filter(Boolean)
.join(' · ');
/** Ref 각 처리 화면의 SELECT 조건을 공통 command table의 명령별 option으로 투영한다. */ /** Ref 각 처리 화면의 SELECT 조건을 공통 command table의 명령별 option으로 투영한다. */
export const buildRefGeneralTargetOptions = (options: { export const buildRefGeneralTargetOptions = (options: {
actorId: number; actorId: number;
@@ -64,29 +88,30 @@ export const buildRefGeneralTargetOptions = (options: {
const isTroopExit = action === 'che_부대탈퇴지시'; const isTroopExit = action === 'che_부대탈퇴지시';
const availableNow = isTroopExit ? isTroopMember && entry.id !== options.actorId : undefined; const availableNow = isTroopExit ? isTroopMember && entry.id !== options.actorId : undefined;
const label = (() => { const label = (() => {
if (action === 'che_발령') return `${entry.name} (${cityName})`; // 같은 국가 후보만 있는 명령은 국명을 반복하지 않는다.
if (action === 'che_포상' || action === 'che_몰수') return `${entry.name} (${cityName})`; if (['che_발령', 'che_포상', 'che_몰수', 'che_선양'].includes(action ?? '')) {
return `${entry.name} (${cityName})`;
}
return `${entry.name} (${options.nationNames.get(entry.nationId) ?? '무소속'} · ${cityName})`; return `${entry.name} (${options.nationNames.get(entry.nationId) ?? '무소속'} · ${cityName})`;
})(); })();
const details = [ // Ref che_선양.vue는 후보에 (통/무/지)만 보인다.
entry.gold === undefined ? null : `금 ${entry.gold.toLocaleString()}`, const details = (
entry.rice === undefined ? null : `쌀 ${entry.rice.toLocaleString()}`, action === 'che_선양'
entry.crew === undefined ? null : `병력 ${entry.crew.toLocaleString()}`, ? []
entry.train === undefined ? null : `훈련 ${entry.train.toLocaleString()}`, : [
entry.atmos === undefined ? null : `사기 ${entry.atmos.toLocaleString()}`, entry.gold === undefined ? null : `금 ${entry.gold.toLocaleString()}`,
].filter((value): value is string => Boolean(value)); entry.rice === undefined ? null : `쌀 ${entry.rice.toLocaleString()}`,
entry.crew === undefined ? null : `병력 ${entry.crew.toLocaleString()}`,
entry.train === undefined ? null : `훈련 ${entry.train.toLocaleString()}`,
entry.atmos === undefined ? null : `사기 ${entry.atmos.toLocaleString()}`,
]
).filter((value): value is string => Boolean(value));
// 발령 후보는 Ref처럼 능력치와 병력 준비 상태를 함께 비교한다. // 발령 후보는 Ref처럼 능력치와 병력 준비 상태를 함께 비교한다.
// 예약 요약에 쓰는 label은 그대로 두고, 같은 국가 후보의 상세 정보만 보강한다. // 예약 요약에 쓰는 label은 그대로 두고, 같은 국가 후보의 상세 정보만 보강한다.
const assignmentDetails = const assignmentDetails =
action === 'che_발령' action === 'che_발령'
? [ ? [
[ statsText(entry),
entry.leadership === undefined ? null : `통솔 ${entry.leadership.toLocaleString()}`,
entry.strength === undefined ? null : `무력 ${entry.strength.toLocaleString()}`,
entry.intel === undefined ? null : `지력 ${entry.intel.toLocaleString()}`,
]
.filter(Boolean)
.join(' · '),
[ [
entry.crew === undefined ? null : `병력 ${entry.crew.toLocaleString()}`, entry.crew === undefined ? null : `병력 ${entry.crew.toLocaleString()}`,
entry.train === undefined ? null : `훈련 ${entry.train.toLocaleString()}`, entry.train === undefined ? null : `훈련 ${entry.train.toLocaleString()}`,
@@ -104,6 +129,9 @@ export const buildRefGeneralTargetOptions = (options: {
.filter(Boolean) .filter(Boolean)
.join('\n') .join('\n')
: undefined; : undefined;
// Ref ProcessGeneralAmount·ProcessGeneral은 같은 국가 후보에 (통/무/지)를 함께 보인다.
const stats = statsText(entry);
if (stats && action !== 'che_발령') details.push(stats);
if (isTroopExit) { if (isTroopExit) {
details.unshift(availableNow ? '현재 탈퇴 지시 가능' : '현재 탈퇴 지시 불가'); details.unshift(availableNow ? '현재 탈퇴 지시 가능' : '현재 탈퇴 지시 불가');
} }
@@ -125,33 +153,48 @@ export const buildRefGeneralTargetOptions = (options: {
npcState: entry.npcState, npcState: entry.npcState,
}; };
}; };
const project = (predicate: (entry: GeneralTargetSource) => boolean): TurnCommandOption[] => // Ref che_등용·che_장수대상임관 exportJSVars()는 타국·재야 장수의
options.generals.filter(predicate).map((entry) => toOption(entry)); // no,name,nation,officer_level,npc,leadership,strength,intel만 내보낸다.
// 명령표는 모든 장수에게 전달되므로 금·쌀·병력·훈련·사기·부대·위치 도시를 싣지 않는다.
const toPublicOption = (entry: GeneralTargetSource): TurnCommandOption => {
const nationName = options.nationNames.get(entry.nationId) ?? null;
return {
value: entry.id,
label: `${entry.name} (${nationName ?? '재야'})`,
targetNames: { name: entry.name, nationName },
description: statsText(entry),
npcState: entry.npcState,
nationId: entry.nationId,
};
};
const projectPublic = (predicate: (entry: GeneralTargetSource) => boolean): TurnCommandOption[] =>
options.generals.filter(predicate).map(toPublicOption);
// 재야는 국가가 아니므로 같은 국가 명령 후보가 없다. 해당 명령도 NotBeNeutral로 막힌다.
const isSameNation = (entry: GeneralTargetSource): boolean =>
options.actorNationId !== 0 && entry.nationId === options.actorNationId;
const generalTargets: Record<string, TurnCommandOption[]> = {}; const generalTargets: Record<string, TurnCommandOption[]> = {};
for (const action of SAME_NATION_GENERAL_COMMANDS) { for (const action of SAME_NATION_GENERAL_COMMANDS) {
generalTargets[action] = options.generals generalTargets[action] = options.generals.filter(isSameNation).map((entry) => toOption(entry, action));
.filter((entry) => entry.nationId === options.actorNationId)
.map((entry) => toOption(entry, action));
} }
for (const action of SAME_NATION_NATION_COMMANDS) { for (const action of SAME_NATION_NATION_COMMANDS) {
generalTargets[action] = options.generals generalTargets[action] = options.generals
.filter((entry) => entry.nationId === options.actorNationId) .filter(isSameNation)
.map((entry) => toOption(entry, action)) .map((entry) => toOption(entry, action))
.sort((left, right) => Number(right.availableNow) - Number(left.availableNow)); .sort((left, right) => Number(right.availableNow) - Number(left.availableNow));
} }
generalTargets.che_선양 = project( generalTargets.che_선양 = options.generals
(entry) => entry.nationId !== 0 && entry.nationId === options.actorNationId && entry.id !== options.actorId .filter((entry) => isSameNation(entry) && entry.id !== options.actorId)
); .map((entry) => toOption(entry, 'che_선양'));
generalTargets.che_등용 = project( generalTargets.che_등용 = projectPublic(
(entry) => entry.npcState < 2 && entry.officerLevel !== 12 && entry.id !== options.actorId (entry) => entry.npcState < 2 && entry.officerLevel !== 12 && entry.id !== options.actorId
); );
generalTargets.che_장수대상임관 = project((entry) => entry.id !== options.actorId); generalTargets.che_장수대상임관 = projectPublic((entry) => entry.id !== options.actorId);
return { return {
// 기존 profile의 공통 fallback은 유저장 목록을 유지한다. // 기존 profile의 공통 fallback은 유저장 목록을 유지한다. 타국 장수가 섞이므로 공개 필드만 둔다.
generals: project((entry) => entry.npcState < 2), generals: projectPublic((entry) => entry.npcState < 2),
generalTargets, generalTargets,
}; };
}; };
@@ -169,14 +212,63 @@ const NATION_TARGET_COMMANDS = [
'che_선전포고', 'che_선전포고',
'che_종전제의', 'che_종전제의',
'che_불가침파기제의', 'che_불가침파기제의',
'che_피장파장',
'che_이호경식',
'che_급습',
] as const; ] as const;
const nationAvailability = ( /** 피장파장으로 지정할 수 있는 전략(GameConst availableChiefCommand['전략']에서 자신 제외). */
action: (typeof NATION_TARGET_COMMANDS)[number], export const COUNTER_STRATEGY_COMMAND_KEYS = [
'che_필사즉생',
'che_백성동원',
'che_수몰',
'che_허보',
'che_의병모집',
'che_이호경식',
'che_급습',
] as const;
/** Ref che_임관 exportJSVars()의 notAvailable: AllowJoinDestNation의 대상 국가 조건. */
const joinAvailability = (
join: NationJoinContext,
actorNationId: number, actorNationId: number,
target: NationTargetSource target: NationTargetSource
): { available: boolean; reason: string } => { ): { available: boolean; reason: string } => {
if (target.id === actorNationId) return { available: false, reason: '아국은 대상이 아닙니다.' }; if (target.id === actorNationId) return { available: false, reason: '아국은 대상이 아닙니다.' };
const gennum = target.gennum ?? target.generalCount;
if (join.relYear < join.openingPartYear && join.initialNationGenLimit > 0 && gennum >= join.initialNationGenLimit) {
return { available: false, reason: '임관이 제한되고 있습니다.' };
}
if (target.blockScout) return { available: false, reason: '임관이 금지되어 있습니다.' };
if (join.actorNpcState < 2 && target.name.startsWith('ⓤ')) {
return { available: false, reason: '유저장은 태수국에 임관할 수 없습니다.' };
}
if (join.actorNpcState !== 9 && target.name.startsWith('ⓞ')) {
return { available: false, reason: '이민족 국가에 임관할 수 없습니다.' };
}
return { available: true, reason: '현재 임관 가능' };
};
const nationAvailability = (
action: (typeof NATION_TARGET_COMMANDS)[number],
actorNationId: number,
target: NationTargetSource,
strategyAvailable: boolean
): { available: boolean; reason: string } => {
if (target.id === actorNationId) return { available: false, reason: '아국은 대상이 아닙니다.' };
// Ref 피장파장은 쓸 수 있는 전략이 하나도 없으면 모든 국가를 불가로 표시한다.
if (action === 'che_피장파장' && !strategyAvailable) {
return { available: false, reason: '사용할 수 있는 전략이 없습니다.' };
}
if (action === 'che_피장파장' || action === 'che_이호경식') {
const available = [DIPLOMACY_STATE.WAR, DIPLOMACY_STATE.DECLARATION].includes(target.diplomacyState as 0 | 1);
return { available, reason: available ? '현재 발동 가능' : '선포, 전쟁중인 상대국에게만 가능합니다.' };
}
if (action === 'che_급습') {
// Ref AllowDiplomacyWithTerm(1, 12): 선포 상태이고 남은 기간이 12개월 이상.
const available = target.diplomacyState === DIPLOMACY_STATE.DECLARATION && target.diplomacyTerm >= 12;
return { available, reason: available ? '현재 발동 가능' : '선포 12개월 이상인 상대국에만 가능합니다.' };
}
if (action === 'che_물자원조') { if (action === 'che_물자원조') {
return target.diplomacyRestricted return target.diplomacyRestricted
? { available: false, reason: '상대국이 외교제한 중입니다.' } ? { available: false, reason: '상대국이 외교제한 중입니다.' }
@@ -205,7 +297,12 @@ const nationAvailability = (
export const buildRefNationTargetOptions = (options: { export const buildRefNationTargetOptions = (options: {
actorNationId: number; actorNationId: number;
nations: readonly NationTargetSource[]; nations: readonly NationTargetSource[];
/** 없으면 임관 대상 가능 여부를 계산하지 않고 기본 국가 목록을 쓴다. */
join?: NationJoinContext;
/** 피장파장으로 지정할 수 있는 전략이 하나라도 있는지. 기본값 true. */
strategyAvailable?: boolean;
}): RefNationTargetOptions => { }): RefNationTargetOptions => {
const strategyAvailable = options.strategyAvailable ?? true;
const baseOptions = options.nations.map<TurnCommandOption>((entry) => ({ const baseOptions = options.nations.map<TurnCommandOption>((entry) => ({
value: entry.id, value: entry.id,
label: entry.name, label: entry.name,
@@ -217,7 +314,7 @@ export const buildRefNationTargetOptions = (options: {
for (const action of NATION_TARGET_COMMANDS) { for (const action of NATION_TARGET_COMMANDS) {
nationTargets[action] = options.nations nationTargets[action] = options.nations
.map((entry) => { .map((entry) => {
const availability = nationAvailability(action, options.actorNationId, entry); const availability = nationAvailability(action, options.actorNationId, entry, strategyAvailable);
const relation = DIPLOMACY_LABELS[entry.diplomacyState] ?? `관계 ${entry.diplomacyState}`; const relation = DIPLOMACY_LABELS[entry.diplomacyState] ?? `관계 ${entry.diplomacyState}`;
const term = entry.diplomacyTerm > 0 ? ` ${entry.diplomacyTerm}턴` : ''; const term = entry.diplomacyTerm > 0 ? ` ${entry.diplomacyTerm}턴` : '';
return { return {
@@ -238,7 +335,31 @@ export const buildRefNationTargetOptions = (options: {
) )
.map(({ power: _power, ...entry }) => entry); .map(({ power: _power, ...entry }) => entry);
} }
return { nations: baseOptions, nationTargets }; const join = options.join;
if (join) {
// Ref 임관 목록은 nation 표 순서 그대로이며 불가 국가도 남긴다. 재야(0)는 국가가 아니다.
nationTargets.che_임관 = options.nations.flatMap((entry, index) => {
if (entry.id <= 0) return [];
const availability = joinAvailability(join, options.actorNationId, entry);
return [
{
...baseOptions[index]!,
availableNow: availability.available,
description: `${availability.reason} · ${baseOptions[index]!.description}`,
},
];
});
}
// Ref che_임관·che_장수대상임관은 모든 국가의 권유문(scout_msg)을 함께 내보낸다.
const nationScoutMessages = options.nations
.filter((entry) => entry.id > 0)
.map((entry) => ({
nationId: entry.id,
name: entry.name,
color: entry.color,
message: entry.scoutMessage ?? '',
}));
return { nations: baseOptions, nationTargets, nationScoutMessages };
}; };
const RESOURCE_ACTION_GUIDE = [ const RESOURCE_ACTION_GUIDE = [
@@ -5,6 +5,7 @@ import { SystemClock } from '@sammo-ts/common';
import { import {
createDatabaseTurnHooks, createDatabaseTurnHooks,
DatabaseTurnDaemonCommandQueue, DatabaseTurnDaemonCommandQueue,
DatabaseTurnDaemonLease,
EngineStateManager, EngineStateManager,
InMemoryTurnStateStore, InMemoryTurnStateStore,
InMemoryTurnWorld, InMemoryTurnWorld,
@@ -695,6 +696,10 @@ liveDescribe('auction worker durable recovery', () => {
const redisConnector = createRedisConnector(resolveRedisConfigFromEnv()); const redisConnector = createRedisConnector(resolveRedisConfigFromEnv());
await redisConnector.connect(); await redisConnector.connect();
// 독립 worker는 활성 daemon의 clock_ready fence가 있을 때만 GAME_TIME을 진행한다.
const lease = await DatabaseTurnDaemonLease.connect(databaseUrl!, { profile: profileName });
await lease.acquire();
await lease.markClockReady();
const keys = buildAuctionTimerKeys(profileName); const keys = buildAuctionTimerKeys(profileName);
const abortController = new AbortController(); const abortController = new AbortController();
const worker = runAuctionWorker({ signal: abortController.signal }); const worker = runAuctionWorker({ signal: abortController.signal });
@@ -725,6 +730,8 @@ liveDescribe('auction worker durable recovery', () => {
} finally { } finally {
abortController.abort(); abortController.abort();
await worker; await worker;
await lease.close();
await connector.prisma.turnDaemonLease.delete({ where: { profile: profileName } });
await redisConnector.client.del([keys.timerKey, keys.historyKey]); await redisConnector.client.del([keys.timerKey, keys.historyKey]);
await redisConnector.disconnect(); await redisConnector.disconnect();
await connector.prisma.errorLog.deleteMany({ await connector.prisma.errorLog.deleteMany({
@@ -0,0 +1,95 @@
import { PassThrough } from 'node:stream';
import type { ServerResponse } from 'node:http';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
import { AuthenticatedRealtimeConnection } from '../src/realtime/authenticatedConnection.js';
const payload = (): GameSessionTokenPayload => ({
version: 1,
sessionId: 'test-session',
profile: 'che:default',
issuedAt: new Date().toISOString(),
expiresAt: new Date(Date.now() + 60_000).toISOString(),
user: {
id: 'test-user',
username: 'test',
displayName: 'test',
roles: ['user'],
createdAt: new Date().toISOString(),
},
sanctions: {},
});
const fixture = (resolve: () => Promise<GameSessionTokenPayload | null>, auth = payload()) => {
const response = new PassThrough();
const frames: string[] = [];
response.on('data', (chunk) => frames.push(String(chunk)));
const connection = new AuthenticatedRealtimeConnection(auth, response as unknown as ServerResponse, resolve);
return { response, frames, connection };
};
afterEach(() => vi.useRealTimers());
describe('authenticated realtime connection', () => {
it('does not forward an in-flight result after the token is revoked during its work', async () => {
const auth = payload();
let current: GameSessionTokenPayload | null = auth;
const { connection, frames } = fixture(async () => current, auth);
connection.enqueue(async () => {
current = null;
return 'private invalidation';
});
await vi.waitFor(() => expect(connection.closed).toBe(true));
expect(frames).toEqual([]);
});
it.each(['sessionId', 'profile'] as const)('rejects a replacement %s even for the same user', async (field) => {
const auth = payload();
const { connection, frames } = fixture(async () => ({ ...auth, [field]: 'replacement' }), auth);
connection.enqueue(async () => 'private invalidation');
await vi.waitFor(() => expect(connection.closed).toBe(true));
expect(frames).toEqual([]);
});
it('fails closed when authentication storage fails', async () => {
const { connection, frames } = fixture(async () => {
throw new Error('redis unavailable');
});
connection.enqueue(async () => 'private invalidation');
await vi.waitFor(() => expect(connection.closed).toBe(true));
expect(frames).toEqual([]);
});
it('ends an idle expired stream and releases subscriptions only once', async () => {
vi.useFakeTimers();
const auth = payload();
const { connection } = fixture(async () => auth, auth);
const cleanup = vi.fn();
connection.onClose(cleanup);
await vi.advanceTimersByTimeAsync(60_001);
expect(connection.closed).toBe(true);
expect(cleanup).toHaveBeenCalledTimes(1);
connection.close();
expect(cleanup).toHaveBeenCalledTimes(1);
});
it('bounds queued work while an earlier event is blocked', async () => {
const auth = payload();
const { connection, frames } = fixture(async () => auth, auth);
let release!: () => void;
const waiting = new Promise<void>((resolve) => {
release = resolve;
});
connection.enqueue(async () => {
await waiting;
return 'private invalidation';
});
for (let i = 0; i < 100; i += 1) connection.enqueue(async () => 'private invalidation');
expect(connection.closed).toBe(true);
release();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(frames).toEqual([]);
});
it('ends a slow socket instead of retaining further frames', async () => {
const auth = payload();
const { connection, response } = fixture(async () => auth, auth);
response.write = vi.fn(() => false);
connection.enqueue(async () => 'event: ping\ndata: {}\n\n');
await vi.waitFor(() => expect(connection.closed).toBe(true));
expect(response.writableEnded).toBe(true);
});
});
+75
View File
@@ -288,6 +288,81 @@ describe('board router actor, nation, and secret permissions', () => {
expect(result).toMatchObject({ width: 64, height: 48, format: 'webp', animated: false }); expect(result).toMatchObject({ width: 64, height: 48, format: 'webp', animated: false });
}); });
it('rejects highly compressed images over the decoded pixel budget before upload', async () => {
const upload = vi.fn(async () => ({ publicUrl: 'https://image.example/image.webp' }));
const fixture = buildContext({ me: buildGeneral({ officerLevel: 5 }), contentImageUpload: { upload } });
const png = await sharp({ create: { width: 4096, height: 4097, channels: 3, background: '#224466' } })
.png()
.toBuffer();
expect(png.length).toBeLessThan(1024 * 1024);
await expect(
appRouter.createCaller(fixture.context).board.uploadImage({
dataUrl: `data:image/png;base64,${png.toString('base64')}`,
})
).rejects.toMatchObject({ code: 'BAD_REQUEST' });
expect(upload).not.toHaveBeenCalled();
});
it('rejects excess concurrent image work and recovers when uploads settle', async () => {
const pending: Array<() => void> = [];
const upload = vi.fn(
() =>
new Promise<{ publicUrl: string }>((resolve) => {
pending.push(() => resolve({ publicUrl: 'https://image.example/image.webp' }));
})
);
const fixture = buildContext({ me: buildGeneral({ officerLevel: 5 }), contentImageUpload: { upload } });
const png = await sharp({ create: { width: 64, height: 48, channels: 3, background: '#224466' } })
.png()
.toBuffer();
const caller = appRouter.createCaller(fixture.context);
const input = { dataUrl: `data:image/png;base64,${png.toString('base64')}` };
const admitted = Array.from({ length: 4 }, () => caller.board.uploadImage(input));
try {
await vi.waitFor(() => expect(upload).toHaveBeenCalledTimes(4));
await expect(caller.board.uploadImage(input)).rejects.toMatchObject({ code: 'TOO_MANY_REQUESTS' });
} finally {
pending.forEach((done) => done());
await Promise.all(admitted);
}
upload.mockImplementation(async () => ({ publicUrl: 'https://image.example/image.webp' }));
await expect(caller.board.uploadImage(input)).resolves.toMatchObject({ format: 'webp' });
});
it('rejects SVG before a native image parser or upload is used', async () => {
const upload = vi.fn();
const fixture = buildContext({ me: buildGeneral({ officerLevel: 5 }), contentImageUpload: { upload } });
await expect(
appRouter.createCaller(fixture.context).board.uploadImage({
dataUrl: Buffer.from('<svg width="64" height="64"><script>alert(1)</script></svg>').toString('base64'),
})
).rejects.toMatchObject({ code: 'BAD_REQUEST' });
expect(upload).not.toHaveBeenCalled();
});
it('rejects excessive animation frames while preserving ordinary GIF animation', async () => {
const upload = vi.fn(async () => ({ publicUrl: 'https://image.example/image.webp' }));
const fixture = buildContext({ me: buildGeneral({ officerLevel: 5 }), contentImageUpload: { upload } });
const caller = appRouter.createCaller(fixture.context);
const makeGif = (pages: number) =>
sharp(Buffer.alloc(pages * 4, 255), {
raw: { width: 1, height: pages, channels: 4, pageHeight: 1 },
})
.gif({ keepDuplicateFrames: true, delay: Array(pages).fill(10) })
.toBuffer();
const excessive = await makeGif(257);
expect((await sharp(excessive, { animated: true }).metadata()).pages).toBe(257);
await expect(caller.board.uploadImage({ dataUrl: excessive.toString('base64') })).rejects.toMatchObject({
code: 'BAD_REQUEST',
});
expect(upload).not.toHaveBeenCalled();
const ordinary = await makeGif(3);
await expect(caller.board.uploadImage({ dataUrl: ordinary.toString('base64') })).resolves.toMatchObject({
animated: true,
format: 'webp',
});
});
it('preserves an AVIF editor image when resizing is unnecessary', async () => { it('preserves an AVIF editor image when resizing is unnecessary', async () => {
const upload = vi.fn(async ({ filename }: { filename: string }) => ({ const upload = vi.fn(async ({ filename }: { filename: string }) => ({
publicUrl: `https://sam-image.hided.net/uploads/core2026/${filename}`, publicUrl: `https://sam-image.hided.net/uploads/core2026/${filename}`,
+16 -1
View File
@@ -1,9 +1,24 @@
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import type { DatabaseClient } from '../src/context.js'; import type { DatabaseClient } from '../src/context.js';
import { loadClockAdminStatus, loadClockReadiness } from '../src/services/clockReadiness.js'; import { loadClockAdminStatus, loadClockReadiness, loadProfileReadiness } from '../src/services/clockReadiness.js';
describe('clock reconciliation readiness', () => { describe('clock reconciliation readiness', () => {
it.each([false, true])('requires the profile daemon lease to be ready: %s', async (ready) => {
const db = {
worldState: {
findFirst: vi.fn(async () => ({ clockPhase: 'PREOPEN', clockRevision: 1n, deadlineGeneration: 0n })),
},
clockProjectionOutbox: { count: vi.fn(async () => 0) },
$queryRaw: vi.fn(async () => [{ ready }]),
} as unknown as DatabaseClient;
await expect(loadProfileReadiness(db, 'che:default')).resolves.toMatchObject({
ok: ready,
daemonReady: ready,
clock: { reconciliationComplete: true, gameplayEnabled: false },
});
});
it('fails closed when the reconciliation schema is not available', async () => { it('fails closed when the reconciliation schema is not available', async () => {
const db = {} as DatabaseClient; const db = {} as DatabaseClient;
await expect(loadClockReadiness(db)).resolves.toEqual({ await expect(loadClockReadiness(db)).resolves.toEqual({
+50 -1
View File
@@ -53,6 +53,29 @@ describe('turn command argument input', () => {
{ key: 'destNationId', kind: 'select', optionSource: 'nations' }, { key: 'destNationId', kind: 'select', optionSource: 'nations' },
{ key: 'amountList', kind: 'numberTuple' }, { key: 'amountList', kind: 'numberTuple' },
]); ]);
// Ref che_피장파장.vue: '전략 :' select에 자신을 뺀 전략 이름을 보인다.
expect(fields.find((entry) => entry.key === 'che_피장파장')?.fields).toMatchObject([
{ key: 'destNationId', kind: 'select', optionSource: 'nations' },
{
key: 'commandType',
label: '전략',
kind: 'select',
options: [
{ value: 'che_필사즉생', label: '필사즉생' },
{ value: 'che_백성동원', label: '백성동원' },
{ value: 'che_수몰', label: '수몰' },
{ value: 'che_허보', label: '허보' },
{ value: 'che_의병모집', label: '의병모집' },
{ value: 'che_이호경식', label: '이호경식' },
{ value: 'che_급습', label: '급습' },
],
},
]);
// Ref che_숙련전환.vue: '감소 대상 숙련 :', '전환 대상 숙련 :'.
expect(fields.find((entry) => entry.key === 'che_숙련전환')?.fields).toMatchObject([
{ key: 'srcArmType', label: '감소 대상 숙련', kind: 'select', optionSource: 'armTypes' },
{ key: 'destArmType', label: '전환 대상 숙련', kind: 'select', optionSource: 'armTypes' },
]);
expect( expect(
nationFields nationFields
@@ -129,7 +152,29 @@ describe('turn command argument input', () => {
); );
}); });
it('recursively sanitizes reserved command strings like Ref before command parsing', async () => { it('preserves ampersands through reservation parsing and repeated sanitization', async () => {
const args = { nationName: '풍년F&B', nested: ['A&B', '&amp;', '&lt;img&gt;'] };
expect(sanitizeReservedTurnArgs(args)).toEqual(args);
expect(sanitizeReservedTurnArgs(sanitizeReservedTurnArgs(args))).toEqual(args);
await expect(parseReservedTurnArgs('nation', 'che_국호변경', args)).resolves.toEqual({
nationName: '풍년F&B',
});
await expect(
parseReservedTurnArgs('general', 'che_건국', {
nationName: '풍년F&B',
nationType: 'che_도적',
colorType: 0,
})
).resolves.toMatchObject({ nationName: '풍년F&B' });
// 반각 18자 판정에 &amp;의 저장 길이가 끼어들지 않는다.
const maxWidthName = '가나다라마바사아&B';
expect(() => assertReservedTurnArgsPassLegacyBasicValidation({ nationName: maxWidthName })).not.toThrow();
await expect(parseReservedTurnArgs('nation', 'che_국호변경', { nationName: maxWidthName })).resolves.toEqual({
nationName: maxWidthName,
});
});
it('retains Ref character removal and angle-bracket neutralization before command parsing', async () => {
expect( expect(
sanitizeReservedTurnArgs({ sanitizeReservedTurnArgs({
nationName: ' <신-국># ', nationName: ' <신-국># ',
@@ -321,6 +366,9 @@ describe('turn command argument input', () => {
'che_계략_향낭', 'che_계략_향낭',
]); ]);
expect(items.item[1]?.description).toBe('현재 구입 가능 · 가격 100 · 환약 · 설명'); expect(items.item[1]?.description).toBe('현재 구입 가능 · 가격 100 · 환약 · 설명');
expect(items.item[1]?.availableNow).toBe(true);
// Ref che_장비매매.vue: 소유하지 않은 종류의 판매는 불가로 표시한다.
expect(items.item[0]?.availableNow).toBe(false);
}); });
it('shows only zero-count buyable items selected by an explicit scenario pool', () => { it('shows only zero-count buyable items selected by an explicit scenario pool', () => {
@@ -341,5 +389,6 @@ describe('turn command argument input', () => {
expect(items.item.map((item) => item.value)).toEqual(['None', 'event_전투특기_격노']); expect(items.item.map((item) => item.value)).toEqual(['None', 'event_전투특기_격노']);
expect(items.item[1]?.description).toContain('현재 구입 불가: 치안 3,000 필요'); expect(items.item[1]?.description).toContain('현재 구입 불가: 치안 3,000 필요');
expect(items.item[1]?.availableNow).toBe(false);
}); });
}); });
+190 -3
View File
@@ -5,6 +5,7 @@ import {
buildRefGeneralTargetOptions, buildRefGeneralTargetOptions,
buildRefNationTargetOptions, buildRefNationTargetOptions,
type GeneralTargetSource, type GeneralTargetSource,
type NationTargetSource,
} from '../src/turns/commandTargets.js'; } from '../src/turns/commandTargets.js';
const general = (overrides: Partial<GeneralTargetSource>): GeneralTargetSource => ({ const general = (overrides: Partial<GeneralTargetSource>): GeneralTargetSource => ({
@@ -49,8 +50,9 @@ describe('Ref command general targets', () => {
it('indexes genuine names even when named 없음, and excludes missing-name placeholders', () => { it('indexes genuine names even when named 없음, and excludes missing-name placeholders', () => {
const options = buildRefGeneralTargetOptions({ const options = buildRefGeneralTargetOptions({
actorId: 1, actorId: 1,
actorNationId: 0, // 이름·국가·도시·부대 원본이 없는 경우. 재야 actor는 같은 국가 후보가 없으므로 국가 ID만 둔다.
generals: [general({ name: '없음', nationId: 0, cityId: 0, troopId: 99 })], actorNationId: 5,
generals: [general({ name: '없음', nationId: 5, cityId: 0, troopId: 99 })],
nationNames: new Map(), nationNames: new Map(),
cityNames: new Map(), cityNames: new Map(),
troopNames: new Map(), troopNames: new Map(),
@@ -118,7 +120,10 @@ describe('Ref command general targets', () => {
); );
expect(detailed.generalTargets.che_발령?.[0]?.description).toBe('병력 1,000\n금 5,000 · 쌀 4,000'); expect(detailed.generalTargets.che_발령?.[0]?.description).toBe('병력 1,000\n금 5,000 · 쌀 4,000');
expect(detailed.generalTargets.che_발령?.[2]?.targetNames?.troopName).toBe('청룡대'); expect(detailed.generalTargets.che_발령?.[2]?.targetNames?.troopName).toBe('청룡대');
expect(detailed.generalTargets.che_증여?.[1]?.description).not.toContain('통솔'); // Ref ProcessGeneralAmount.vue처럼 증여 후보도 (통/무/지)를 함께 보인다. 발령의 줄바꿈 형식은 쓰지 않는다.
expect(detailed.generalTargets.che_증여?.[1]?.description).toBe(
'금 100 · 쌀 200 · 병력 900 · 훈련 80 · 사기 70 · 통솔 100 · 무력 95 · 지력 0'
);
expect(detailed.generalTargets.che_증여?.map((entry) => entry.targetNames)).toEqual([ expect(detailed.generalTargets.che_증여?.map((entry) => entry.targetNames)).toEqual([
{ name: '본인', nationName: '아국', cityName: '업', troopName: null }, { name: '본인', nationName: '아국', cityName: '업', troopName: null },
{ name: '부대원', nationName: '아국', cityName: '업', troopName: '청룡대' }, { name: '부대원', nationName: '아국', cityName: '업', troopName: '청룡대' },
@@ -132,6 +137,106 @@ describe('Ref command general targets', () => {
expect(detailed.generalTargets.che_포상?.[0]?.description).toBe('금 5,000 · 쌀 4,000 · 병력 1,000'); expect(detailed.generalTargets.che_포상?.[0]?.description).toBe('금 5,000 · 쌀 4,000 · 병력 1,000');
expect(detailed.generalTargets.che_몰수?.[1]?.description).not.toContain('탑승 부대'); expect(detailed.generalTargets.che_몰수?.[1]?.description).not.toContain('탑승 부대');
}); });
it('limits cross-nation recruitment and joining targets to the fields Ref exports', () => {
const foreign = general({
id: 7,
name: '타국장수',
nationId: 2,
cityId: 20,
officerLevel: 1,
gold: 9000,
rice: 8000,
crew: 7000,
train: 90,
atmos: 80,
troopId: 7,
leadership: 75,
strength: 60,
intel: 45,
});
const wanderer = general({ id: 8, name: '재야장수', nationId: 0, cityId: 30, gold: 500, rice: 400 });
const options = buildRefGeneralTargetOptions({
actorId: 1,
actorNationId: 1,
generals: [general({}), foreign, wanderer],
nationNames: new Map([
[1, '아국'],
[2, '타국'],
]),
cityNames: new Map([
[10, '업'],
[20, '허창'],
[30, '단양'],
]),
troopNames: new Map([[7, '비밀부대']]),
});
for (const list of [
options.generalTargets.che_등용,
options.generalTargets.che_장수대상임관,
options.generals,
]) {
const target = list?.find((entry) => entry.value === 7);
// Ref: no,name,nation,officer_level,npc,leadership,strength,intel
expect(target).toEqual({
value: 7,
label: '타국장수 (타국)',
targetNames: { name: '타국장수', nationName: '타국' },
description: '통솔 75 · 무력 60 · 지력 45',
npcState: 0,
nationId: 2,
});
const serialized = JSON.stringify(list);
for (const hidden of ['9,000', '9000', '8000', '7000', '허창', '단양', '비밀부대', '훈련', '사기']) {
expect(serialized).not.toContain(hidden);
}
expect(list?.find((entry) => entry.value === 8)).toMatchObject({
label: '재야장수 (재야)',
targetNames: { name: '재야장수', nationName: null },
nationId: 0,
});
}
});
it('gives a wandering actor no same-nation general candidates', () => {
const options = buildRefGeneralTargetOptions({
actorId: 1,
actorNationId: 0,
generals: [general({ nationId: 0 }), general({ id: 2, name: '다른재야', nationId: 0, gold: 900 })],
nationNames: new Map(),
cityNames: new Map([[10, '업']]),
});
for (const action of ['che_증여', 'che_선양', 'che_발령', 'che_포상', 'che_몰수', 'che_부대탈퇴지시']) {
expect(options.generalTargets[action]).toEqual([]);
}
expect(options.generalTargets.che_장수대상임관?.map((entry) => entry.value)).toEqual([2]);
expect(JSON.stringify(options.generalTargets.che_장수대상임관)).not.toContain('900');
});
it('shows only city and Ref stats for abdication candidates', () => {
const options = buildRefGeneralTargetOptions({
actorId: 1,
actorNationId: 1,
generals: [
general({}),
general({
id: 2,
name: '후계',
gold: 100,
rice: 200,
crew: 300,
leadership: 80,
strength: 70,
intel: 60,
}),
],
nationNames: new Map([[1, '아국']]),
cityNames: new Map([[10, '업']]),
});
expect(options.generalTargets.che_선양).toMatchObject([
{ value: 2, label: '후계 (업)', description: '통솔 80 · 무력 70 · 지력 60' },
]);
});
}); });
describe('Ref nation target guidance', () => { describe('Ref nation target guidance', () => {
@@ -219,6 +324,88 @@ describe('Ref nation target guidance', () => {
expect(result.nationTargets.che_불가침파기제의?.[0]?.description).toContain('불가침 12턴'); expect(result.nationTargets.che_불가침파기제의?.[0]?.description).toContain('불가침 12턴');
expect(result.nationTargets.che_불가침파기제의?.at(-1)).toMatchObject({ value: 4, availableNow: false }); expect(result.nationTargets.che_불가침파기제의?.at(-1)).toMatchObject({ value: 4, availableNow: false });
}); });
it('marks counter-strategy targets like Ref exportJSVars notAvailable', () => {
const withDeclaration = [
...nations,
{ ...nations[3]!, id: 5, name: '선포국', diplomacyState: 1, diplomacyTerm: 12 },
{ ...nations[3]!, id: 6, name: '신규선포국', diplomacyState: 1, diplomacyTerm: 11 },
];
const result = buildRefNationTargetOptions({ actorNationId: 1, nations: withDeclaration });
const available = (action: string) =>
result.nationTargets[action]?.filter((entry) => entry.availableNow).map((entry) => entry.value);
// 피장파장·이호경식: AllowDiplomacyBetweenStatus([0, 1])
expect(available('che_피장파장')).toEqual([4, 5, 6]);
expect(available('che_이호경식')).toEqual([4, 5, 6]);
// 급습: AllowDiplomacyWithTerm(1, 12)
expect(available('che_급습')).toEqual([5]);
expect(result.nationTargets.che_급습?.find((entry) => entry.value === 6)?.description).toContain(
'선포 12개월 이상인 상대국에만 가능합니다.'
);
expect(result.nationTargets.che_피장파장?.find((entry) => entry.value === 1)).toMatchObject({
availableNow: false,
});
const exhausted = buildRefNationTargetOptions({
actorNationId: 1,
nations: withDeclaration,
strategyAvailable: false,
});
expect(exhausted.nationTargets.che_피장파장?.every((entry) => entry.availableNow === false)).toBe(true);
expect(exhausted.nationTargets.che_피장파장?.find((entry) => entry.value === 4)?.description).toContain(
'사용할 수 있는 전략이 없습니다.'
);
expect(exhausted.nationTargets.che_이호경식?.some((entry) => entry.availableNow)).toBe(true);
});
it('keeps the Ref nation order for appointment targets and exports purified scout messages', () => {
const joinNations: NationTargetSource[] = [
{ ...nations[0]!, scoutMessage: '<p>어서 오시오</p>' },
{ ...nations[1]!, blockScout: true },
{ ...nations[2]!, name: 'ⓤ태수국' },
{ ...nations[3]!, name: 'ⓞ이민족' },
{ ...nations[3]!, id: 5, name: '초반만원국', generalCount: 3, gennum: 10 },
];
const join = { actorNpcState: 0, relYear: 1, openingPartYear: 3, initialNationGenLimit: 10 };
const result = buildRefNationTargetOptions({ actorNationId: 0, nations: joinNations, join });
expect(result.nationTargets.che_임관?.map((entry) => [entry.value, entry.availableNow])).toEqual([
[1, true],
[2, false],
[3, false],
[4, false],
[5, false],
]);
expect(result.nationTargets.che_임관?.map((entry) => entry.description?.split(' · ')[0])).toEqual([
'현재 임관 가능',
'임관이 금지되어 있습니다.',
'유저장은 태수국에 임관할 수 없습니다.',
'이민족 국가에 임관할 수 없습니다.',
'임관이 제한되고 있습니다.',
]);
// 초반 제한이 끝나면 인원 제한은 없고, NPC(9)는 이민족 국가에 임관할 수 있다.
const later = buildRefNationTargetOptions({
actorNationId: 0,
nations: joinNations,
join: { ...join, actorNpcState: 9, relYear: 3 },
});
expect(later.nationTargets.che_임관?.map((entry) => entry.availableNow)).toEqual([
true,
false,
true,
true,
true,
]);
expect(result.nationScoutMessages).toEqual(
joinNations.map((entry) => ({
nationId: entry.id,
name: entry.name,
color: entry.color,
message: entry.scoutMessage ?? '',
}))
);
// join 정보가 없으면 임관은 기존 공통 국가 목록을 그대로 쓴다.
expect(buildRefNationTargetOptions({ actorNationId: 0, nations }).nationTargets.che_임관).toBeUndefined();
});
}); });
describe('Ref amount presets', () => { describe('Ref amount presets', () => {
@@ -384,8 +384,8 @@ integration('generic general creation through the durable turn daemon', () => {
actorUserId: userId, actorUserId: userId,
}); });
expect(access.lastRefresh?.getTime()).toBe(event.createdAt.getTime()); expect(access.lastRefresh?.getTime()).toBe(event.createdAt.getTime());
const turnGridOffsetSeconds = // 상속 턴 시간대는 daemon cursor가 아닌 5분 격자의 위상을 보존한다.
((created.turnTime.getTime() - runtime!.world.getState().lastTurnTime.getTime()) / 1000 + 300) % 300; const turnGridOffsetSeconds = ((created.turnTime.getTime() / 1000) % 300 + 300) % 300;
expect(turnGridOffsetSeconds).toBeGreaterThanOrEqual(35); expect(turnGridOffsetSeconds).toBeGreaterThanOrEqual(35);
expect(turnGridOffsetSeconds).toBeLessThan(40); expect(turnGridOffsetSeconds).toBeLessThan(40);
@@ -12,6 +12,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { import {
DeferredGeneralAccessWorker, DeferredGeneralAccessWorker,
flushDeferredGeneralAccessBatch,
enqueueDeferredGeneralAccess, enqueueDeferredGeneralAccess,
getDeferredGeneralAccessLimit, getDeferredGeneralAccessLimit,
} from '../src/services/deferredGeneralAccess.js'; } from '../src/services/deferredGeneralAccess.js';
@@ -91,7 +92,7 @@ liveIntegration('deferred general access with PostgreSQL and Redis', () => {
await redis.disconnect(); await redis.disconnect();
} }
if (db) { if (db) {
await db.generalAccessBatch.deleteMany({ where: { id: batchId } }); await db.generalAccessBatch.deleteMany({ where: { id: { startsWith: batchId } } });
await db.generalAccessLog.deleteMany({ where: { generalId } }); await db.generalAccessLog.deleteMany({ where: { generalId } });
await db.general.deleteMany({ where: { id: generalId } }); await db.general.deleteMany({ where: { id: generalId } });
await db.worldState.deleteMany({ where: { id: worldStateId } }); await db.worldState.deleteMany({ where: { id: worldStateId } });
@@ -153,9 +154,19 @@ liveIntegration('deferred general access with PostgreSQL and Redis', () => {
nextAccessAt, nextAccessAt,
}); });
const counters =
await db.$queryRaw`SELECT 'access' AS kind,last_value FROM general_access_log_id_seq UNION ALL SELECT 'period',last_value FROM traffic_period_id_seq`;
const accessId = (await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).id;
await flushDeferredGeneralAccessBatch(workerDb as never, `${batchId}-second`, [
{ generalId, userId, weight: 1, lastRefresh: now },
]);
expect(
await db.$queryRaw`SELECT 'access' AS kind,last_value FROM general_access_log_id_seq UNION ALL SELECT 'period',last_value FROM traffic_period_id_seq`
).toEqual(counters);
expect((await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).id).toBe(accessId);
await worker.flushOnce(); await worker.flushOnce();
await expect(db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).resolves.toMatchObject({ await expect(db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).resolves.toMatchObject({
refreshTotal: 2, refreshTotal: 3,
}); });
}); });
}); });
@@ -1,3 +1,4 @@
import { asRecord } from '@sammo-ts/common';
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { JosaUtil } from '@sammo-ts/common'; import { JosaUtil } from '@sammo-ts/common';
@@ -742,7 +743,7 @@ integration('diplomacy document message persistence', () => {
} }
); );
it('rolls back the document and notices if the audit insert fails', async () => { it('commits the document and notices with a gap when audit insert fails', async () => {
await db.$executeRawUnsafe(`CREATE FUNCTION reject_audit_document_fixture() RETURNS trigger LANGUAGE plpgsql AS $$ await db.$executeRawUnsafe(`CREATE FUNCTION reject_audit_document_fixture() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN RAISE EXCEPTION 'injected audit insert failure'; END; $$`); BEGIN RAISE EXCEPTION 'injected audit insert failure'; END; $$`);
await db.$executeRawUnsafe(`CREATE TRIGGER reject_audit_document_fixture BEFORE INSERT ON play_audit_diplomacy_event await db.$executeRawUnsafe(`CREATE TRIGGER reject_audit_document_fixture BEFORE INSERT ON play_audit_diplomacy_event
@@ -754,15 +755,19 @@ integration('diplomacy document message persistence', () => {
brief: '감사 실패', brief: '감사 실패',
detail: '롤백', detail: '롤백',
}) })
).rejects.toThrow('injected audit insert failure'); ).resolves.toBeDefined();
expect(await db.diplomacyLetter.count({ where: { textBrief: '감사 실패' } })).toBe(0); expect(await db.diplomacyLetter.count({ where: { textBrief: '감사 실패' } })).toBe(1);
expect(await db.message.count({ where: { mailbox: { in: [...fixtureMailboxes] } } })).toBe(0); expect(await db.message.count({ where: { mailbox: { in: [...fixtureMailboxes] } } })).toBeGreaterThan(0);
expect(await db.playAuditDiplomacyEvent.count({ where: { serverId: requestPrefix } })).toBe(0); expect(await db.playAuditDiplomacyEvent.count({ where: { serverId: requestPrefix } })).toBe(0);
expect( expect(
await db.inputEvent.findUniqueOrThrow({ await db.inputEvent.findUniqueOrThrow({
where: { requestId: `${requestPrefix}:audit-failure:diplomacy.sendLetter` }, where: { requestId: `${requestPrefix}:audit-failure:diplomacy.sendLetter` },
}) })
).toMatchObject({ status: 'FAILED', attempts: 1 }); ).toMatchObject({ status: 'SUCCEEDED', attempts: 1 });
expect(
asRecord((await db.worldState.findUniqueOrThrow({ where: { id: fixtureWorldStateId } })).meta)
.playAuditGap
).toMatchObject({ serverId: requestPrefix });
} finally { } finally {
await db.$executeRawUnsafe('DROP TRIGGER reject_audit_document_fixture ON play_audit_diplomacy_event'); await db.$executeRawUnsafe('DROP TRIGGER reject_audit_document_fixture ON play_audit_diplomacy_event');
await db.$executeRawUnsafe('DROP FUNCTION reject_audit_document_fixture()'); await db.$executeRawUnsafe('DROP FUNCTION reject_audit_document_fixture()');
@@ -68,6 +68,7 @@ const restoreEnv = (): void => {
const deleteProfileRedisKeys = async (): Promise<void> => { const deleteProfileRedisKeys = async (): Promise<void> => {
if (!redis) return; if (!redis) return;
await redis.client.del(`${redisPrefix}:flush:revocation:v1:baseline`);
for await (const keys of redis.client.scanIterator({ MATCH: `sammo:game:*:${profileName}:*`, COUNT: 100 })) { for await (const keys of redis.client.scanIterator({ MATCH: `sammo:game:*:${profileName}:*`, COUNT: 100 })) {
if (keys.length > 0) await redis.client.del(keys); if (keys.length > 0) await redis.client.del(keys);
} }
@@ -199,6 +200,8 @@ integration('diplomacy HTML purification over PostgreSQL and HTTP transport', ()
if (!created) throw new Error('failed to seed diplomacy HTML access token'); if (!created) throw new Error('failed to seed diplomacy HTML access token');
accessToken = created.accessToken; accessToken = created.accessToken;
// 이 격리 fixture는 이미 전환된 Gateway namespace를 합성 cutoff 0으로 준비한다.
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:baseline`, '0', { NX: true });
server = await createGameApiServer(); server = await createGameApiServer();
baseUrl = await server.app.listen({ host: server.config.host, port: server.config.port }); baseUrl = await server.app.listen({ host: server.config.host, port: server.config.port });
}, 30_000); }, 30_000);
@@ -59,6 +59,7 @@ const classifications = {
'join.reselectPoolGeneral', 'join.reselectPoolGeneral',
'join.selectPoolGeneral', 'join.selectPoolGeneral',
'nation.appoint', 'nation.appoint',
'nation.cityOfficeRequest',
'nation.changePermission', 'nation.changePermission',
'nation.kick', 'nation.kick',
'nation.setBill', 'nation.setBill',
@@ -161,7 +162,6 @@ describe('game-api direct mutation journal inventory', () => {
// count independently catches mutations that were added to a router but never mounted. // count independently catches mutations that were added to a router but never mounted.
expect(declaredCount).toBe(actual.length); expect(declaredCount).toBe(actual.length);
expect(new Set(classified).size).toBe(classified.length); expect(new Set(classified).size).toBe(classified.length);
expect(classified).toHaveLength(88);
expect(actual).toEqual(classified); expect(actual).toEqual(classified);
}); });
+84 -1
View File
@@ -1,6 +1,8 @@
import { createServer, type Socket } from 'node:net';
import { createClient } from 'redis';
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { InMemoryFlushStore, RedisGatewayFlushSubscriber } from '../src/auth/flushStore.js'; import { InMemoryFlushStore, RedisFlushStore, RedisGatewayFlushSubscriber } from '../src/auth/flushStore.js';
describe('RedisGatewayFlushSubscriber', () => { describe('RedisGatewayFlushSubscriber', () => {
it('reports asynchronous flush handler failures with event context', async () => { it('reports asynchronous flush handler failures with event context', async () => {
@@ -66,3 +68,84 @@ describe('RedisGatewayFlushSubscriber', () => {
expect(stopped).toBe(true); expect(stopped).toBe(true);
}); });
}); });
describe('RedisFlushStore authorization state', () => {
it('merges startup, persistent and local watermarks without caching an allow result', async () => {
let persisted: string | null = null;
const mGet = vi.fn(async (keys: string[]) => (keys.length === 1 ? ['100'] : ['100', persisted]));
const store = new RedisFlushStore({ mGet }, 'fixture:flush');
await store.assertInitialized();
expect((await store.getFlushedAt('user-1'))?.getTime()).toBe(100);
persisted = '200';
expect((await store.getFlushedAt('user-1'))?.getTime()).toBe(200);
store.applyFlush({ userId: 'user-1', flushedAt: new Date(300).toISOString() });
expect((await store.getFlushedAt('user-1'))?.getTime()).toBe(300);
expect(mGet).toHaveBeenCalledTimes(4);
});
it('retains revocation after local cache eviction and a fresh store instance', async () => {
const client = { mGet: async (keys: string[]) => (keys.length === 1 ? ['0'] : ['0', '200']) };
const store = new RedisFlushStore(client, 'fixture:flush');
for (let i = 0; i < 4100; i++)
store.applyFlush({ userId: `user-${i}`, flushedAt: new Date(100).toISOString() });
expect((await store.getFlushedAt('user-0'))?.getTime()).toBe(200);
expect((await new RedisFlushStore(client, 'fixture:flush').getFlushedAt('user-0'))?.getTime()).toBe(200);
});
it.each([null, '', 'invalid', '-1', '8640000000000001'])(
'rejects missing or corrupt initialization %s',
async (baseline) => {
const store = new RedisFlushStore({ mGet: async () => [baseline] }, 'fixture:flush');
await expect(store.assertInitialized()).rejects.toThrow();
}
);
it('rejects corrupted per-user state instead of treating it as never revoked', async () => {
const store = new RedisFlushStore({ mGet: async () => ['0', 'not-a-watermark'] }, 'fixture:flush');
await expect(store.getFlushedAt('user-1')).rejects.toThrow();
});
it('does not perform an offline read and bounds a connected read with an abort signal', async () => {
const offlineGet = vi.fn(async () => ['0', null]);
const offline = new RedisFlushStore({ isReady: false, mGet: offlineGet }, 'fixture:flush');
await expect(offline.getFlushedAt('user')).rejects.toThrow('unavailable');
expect(offlineGet).not.toHaveBeenCalled();
const mGet = vi.fn(async () => ['0', '100']);
const withAbortSignal = vi.fn((_signal: AbortSignal) => ({ mGet }));
const store = new RedisFlushStore({ mGet, withAbortSignal }, 'fixture:flush');
expect((await store.getFlushedAt('user'))?.getTime()).toBe(100);
expect(withAbortSignal.mock.calls[0]?.[0]).toBeInstanceOf(AbortSignal);
});
});
it('bounds a connected Redis read when the actual TCP peer stops replying', async () => {
const sockets = new Set<Socket>();
let receivedRead = false;
const server = createServer((socket) => {
sockets.add(socket);
socket.on('close', () => sockets.delete(socket));
socket.on('data', (data) => {
const request = data.toString();
// Real node-redis handshake succeeds; its MGET command receives no reply.
if (request.includes('MGET')) {
receivedRead = true;
return;
}
const count = [...request.matchAll(/\*\d+\r\n/g)].length;
socket.write('+OK\r\n'.repeat(count));
});
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
if (!address || typeof address === 'string') throw new Error('Expected TCP fixture address');
const client = createClient({ socket: { host: '127.0.0.1', port: address.port, reconnectStrategy: false } });
try {
await client.connect();
expect(client.isReady).toBe(true);
const start = Date.now();
await expect(new RedisFlushStore(client, 'fixture:flush').getFlushedAt('user')).rejects.toThrow();
expect(receivedRead).toBe(true);
expect(Date.now() - start).toBeGreaterThanOrEqual(1800);
expect(Date.now() - start).toBeLessThan(4500);
} finally {
if (client.isOpen) client.destroy();
for (const socket of sockets) socket.destroy();
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())));
}
});
@@ -25,7 +25,7 @@ const deferredBatchId = `deferred-access-${endpointGeneralId}`;
const endpointAuth = (roles = ['user']): GameSessionTokenPayload => ({ const endpointAuth = (roles = ['user']): GameSessionTokenPayload => ({
version: 1, version: 1,
profile: `${yearbookProfile}:default`, profile: yearbookProfile,
issuedAt: '2026-07-26T00:00:00.000Z', issuedAt: '2026-07-26T00:00:00.000Z',
expiresAt: '2026-07-27T00:00:00.000Z', expiresAt: '2026-07-27T00:00:00.000Z',
sessionId: `access-session-${endpointGeneralId}`, sessionId: `access-session-${endpointGeneralId}`,
@@ -167,6 +167,9 @@ integration('general access tracking persistence', () => {
scoreStartedAt: new Date('2026-07-26T03:00:00.000Z'), scoreStartedAt: new Date('2026-07-26T03:00:00.000Z'),
}; };
await upsertGeneralAccess(db, { ...firstWindow, weight: 2 }); await upsertGeneralAccess(db, { ...firstWindow, weight: 2 });
const counters =
await db.$queryRaw`SELECT 'access' AS kind,last_value FROM general_access_log_id_seq UNION ALL SELECT 'period',last_value FROM traffic_period_id_seq`;
const accessId = (await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).id;
await Promise.all( await Promise.all(
Array.from({ length: 20 }, (_, index) => Array.from({ length: 20 }, (_, index) =>
upsertGeneralAccess(db, { upsertGeneralAccess(db, {
@@ -177,6 +180,10 @@ integration('general access tracking persistence', () => {
) )
); );
expect(
await db.$queryRaw`SELECT 'access' AS kind,last_value FROM general_access_log_id_seq UNION ALL SELECT 'period',last_value FROM traffic_period_id_seq`
).toEqual(counters);
expect((await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).id).toBe(accessId);
expect(await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).toMatchObject({ expect(await db.generalAccessLog.findUniqueOrThrow({ where: { generalId } })).toMatchObject({
userId: 'access-user-a', userId: 'access-user-a',
refresh: 22, refresh: 22,
@@ -387,7 +387,7 @@ describe('general access tracking', () => {
db, db,
generalAccessTracking: true, generalAccessTracking: true,
requestId: 'access-boundary-test', requestId: 'access-boundary-test',
profile: { id: 'che:default', name: 'che' }, profile: { id: 'che', name: 'che:default', scenario: 'default' },
profileStatusSource, profileStatusSource,
} as unknown as GameApiContext; } as unknown as GameApiContext;
@@ -26,7 +26,7 @@ const buildContext = (executeRaw = vi.fn(async (_query: unknown) => 1), token =
auth: token, auth: token,
db: { $executeRaw: executeRaw }, db: { $executeRaw: executeRaw },
generalAccessTracking: true, generalAccessTracking: true,
profile: { id: 'che:default', name: 'che', scenario: 'default' }, profile: { id: 'che', name: 'che:default', scenario: 'default' },
}) as unknown as GameApiContext; }) as unknown as GameApiContext;
const activityRouter = router({ const activityRouter = router({
@@ -0,0 +1,51 @@
import fastify from 'fastify';
import { TRPCError } from '@trpc/server';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import { describe, expect, it } from 'vitest';
import { router as gameRouter, sessionActivityProcedure } from '../src/trpc.js';
const internalFailure = () => {
throw new Error('synthetic-db-secret', { cause: new Error('synthetic-cause-secret') });
};
const badInput = () => {
throw new TRPCError({
code: 'BAD_REQUEST',
message: '잘못된 입력입니다.',
cause: new Error('synthetic-private-cause'),
});
};
describe('public HTTP error privacy', () => {
it('the real router factory hides internal errors and all stacks while preserving validation messages', async () => {
const app = fastify();
await app.register(fastifyTRPCPlugin, {
prefix: '/game',
trpcOptions: {
router: gameRouter({
internal: sessionActivityProcedure.query(internalFailure),
invalid: sessionActivityProcedure.query(badInput),
}),
createContext: () => ({}),
},
});
try {
const address = await app.listen({ host: '127.0.0.1', port: 0 });
for (const prefix of ['game']) {
for (const procedure of ['internal', 'invalid']) {
const response = await fetch(`${address}/${prefix}/${procedure}`);
const body = (await response.json()) as {
error: { message: string; data: { code: string; stack?: string } };
};
expect(response.status).toBe(procedure === 'internal' ? 500 : 400);
expect(body.error.message).toBe(
procedure === 'internal' ? 'Internal server error' : '잘못된 입력입니다.'
);
expect(body.error.data.stack).toBeUndefined();
expect(JSON.stringify(body)).not.toContain('synthetic-');
}
}
} finally {
await app.close();
}
});
});
+15 -12
View File
@@ -108,7 +108,7 @@ const buildContext = (options: {
inheritancePoint?: number; inheritancePoint?: number;
inheritanceRows?: Array<{ key: string; value: number }>; inheritanceRows?: Array<{ key: string; value: number }>;
rankRows?: Array<{ type: string; value: number }>; rankRows?: Array<{ type: string; value: number }>;
inheritanceLogs?: Array<{ id: number; year: number; month: number; text: string; createdAt: Date }>; inheritanceLogs?: Array<{ id: number | bigint; year: number; month: number; text: string; createdAt: Date }>;
configConst?: Record<string, unknown>; configConst?: Record<string, unknown>;
scenarioCode?: string; scenarioCode?: string;
configMap?: Record<string, unknown>; configMap?: Record<string, unknown>;
@@ -460,11 +460,11 @@ describe('inherit router actor and permission boundaries', () => {
it('loads the first inheritance-log page without an out-of-range integer cursor', async () => { it('loads the first inheritance-log page without an out-of-range integer cursor', async () => {
const createdAt = new Date('2026-07-26T00:00:00Z'); const createdAt = new Date('2026-07-26T00:00:00Z');
const fixture = buildContext({ const fixture = buildContext({
inheritanceLogs: [{ id: 2_147_483_647, year: 200, month: 4, text: '경계 로그', createdAt }], inheritanceLogs: [{ id: 2_147_483_648n, year: 200, month: 4, text: '경계 로그', createdAt }],
}); });
await expect(appRouter.createCaller(fixture.context).inherit.getLogs({})).resolves.toEqual([ await expect(appRouter.createCaller(fixture.context).inherit.getLogs({})).resolves.toEqual([
{ id: 2_147_483_647, year: 200, month: 4, text: '경계 로그', createdAt }, { id: 2_147_483_648, year: 200, month: 4, text: '경계 로그', createdAt },
]); ]);
expect(fixture.inheritanceLogFindMany).toHaveBeenCalledWith({ expect(fixture.inheritanceLogFindMany).toHaveBeenCalledWith({
where: { userId: 'user-1' }, where: { userId: 'user-1' },
@@ -478,23 +478,26 @@ describe('inherit router actor and permission boundaries', () => {
const fixture = buildContext({ inheritanceLogs: [] }); const fixture = buildContext({ inheritanceLogs: [] });
const caller = appRouter.createCaller(fixture.context); const caller = appRouter.createCaller(fixture.context);
await expect(caller.inherit.getLogs({ lastId: 2_147_483_647 })).resolves.toEqual([]); await expect(caller.inherit.getLogs({ lastId: 2_147_483_648 })).resolves.toEqual([]);
expect(fixture.inheritanceLogFindMany).toHaveBeenCalledWith({ expect(fixture.inheritanceLogFindMany).toHaveBeenCalledWith({
where: { userId: 'user-1', id: { lt: 2_147_483_647 } }, where: { userId: 'user-1', id: { lt: 2_147_483_648 } },
orderBy: { id: 'desc' }, orderBy: { id: 'desc' },
take: 30, take: 30,
select: { id: true, year: true, month: true, text: true, createdAt: true }, select: { id: true, year: true, month: true, text: true, createdAt: true },
}); });
}); });
it.each([0, -1, 1.5, 2_147_483_648])('rejects an invalid inheritance-log cursor: %s', async (lastId) => { it.each([0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1])(
const fixture = buildContext({}); 'rejects an invalid inheritance-log cursor: %s',
async (lastId) => {
const fixture = buildContext({});
await expect(appRouter.createCaller(fixture.context).inherit.getLogs({ lastId })).rejects.toMatchObject({ await expect(appRouter.createCaller(fixture.context).inherit.getLogs({ lastId })).rejects.toMatchObject({
code: 'BAD_REQUEST', code: 'BAD_REQUEST',
}); });
expect(fixture.inheritanceLogFindMany).not.toHaveBeenCalled(); expect(fixture.inheritanceLogFindMany).not.toHaveBeenCalled();
}); }
);
it('delegates the authenticated actor and maps a missing-general daemon rejection', async () => { it('delegates the authenticated actor and maps a missing-general daemon rejection', async () => {
const fixture = buildContext({ const fixture = buildContext({
@@ -21,23 +21,35 @@ const auth: GameSessionTokenPayload = {
}; };
const buildContext = ( const buildContext = (
options: { auth?: GameSessionTokenPayload | null; hasVoted?: boolean; preopenClock?: boolean } = {} options: {
auth?: GameSessionTokenPayload | null;
hasVoted?: boolean;
preopenClock?: boolean;
officerLevel?: number;
officeRows?: Array<{ id: number; userId: string; meta: Record<string, unknown> }>;
} = {}
) => ) =>
({ ({
auth: options.auth === undefined ? auth : options.auth, auth: options.auth === undefined ? auth : options.auth,
profile: { id: 'che', name: 'che:default', scenario: 'default' },
db: { db: {
general: { general: {
findFirst: vi.fn(async () => ({ findFirst: vi.fn(async () => ({
id: 7, id: 7,
userId: 'owner', userId: 'owner',
officerLevel: options.officerLevel ?? 1,
nationId: 2, nationId: 2,
})), })),
findMany: vi.fn(async () => [ findMany: vi.fn(async (input: { where?: { meta?: unknown } }) =>
{ id: 7, name: '유비', nationId: 2 }, input.where?.meta
{ id: 8, name: '관우', nationId: 2 }, ? (options.officeRows ?? [])
{ id: 9, name: '조조', nationId: 3 }, : [
{ id: 10, name: '재야장수', nationId: 0 }, { id: 7, name: '유비', nationId: 2 },
]), { id: 8, name: '관우', nationId: 2 },
{ id: 9, name: '조조', nationId: 3 },
{ id: 10, name: '재야장수', nationId: 0 },
]
),
}, },
worldState: { worldState: {
findFirst: vi.fn(async () => ({ findFirst: vi.fn(async () => ({
@@ -109,6 +121,7 @@ describe('general.getFrontStatus', () => {
expect(result).toEqual({ expect(result).toEqual({
serverId: 'che_260819_front', serverId: 'che_260819_front',
cityOfficeRequests: { generalId: 7, nationId: 2, ids: [] },
onlineUserCount: 4, onlineUserCount: 4,
onlineNations: '【촉】, 【위】', onlineNations: '【촉】, 【위】',
onlineGenerals: '유비, 관우', onlineGenerals: '유비, 관우',
@@ -173,3 +186,31 @@ describe('general.getFrontStatus', () => {
await expect(caller.general.getFrontStatus()).rejects.toMatchObject({ code: 'UNAUTHORIZED' }); await expect(caller.general.getFrontStatus()).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
}); });
}); });
it('only notifies chiefs about pending requests belonging to current members and owners', async () => {
const pending = {
id: '8:760',
generalId: 8,
userId: 'user-8',
nationId: 2,
cityId: 1,
officerLevel: 4,
incumbentId: 0,
quarter: 760,
createdTick: 0,
dueTick: 3600000,
defaultDecision: 'approve',
defaultReason: 'vacant',
status: 'pending',
};
const officeRows = [
{ id: 8, userId: 'user-8', meta: { cityOfficeRequest: pending } },
{ id: 9, userId: 'changed-owner', meta: { cityOfficeRequest: { ...pending, id: '9:760' } } },
{ id: 10, userId: 'user-8', meta: { cityOfficeRequest: { ...pending, id: '10:760', nationId: 3 } } },
{ id: 11, userId: 'user-8', meta: { cityOfficeRequest: { ...pending, id: '11:760', status: 'rejected' } } },
];
const chief = appRouter.createCaller(buildContext({ officerLevel: 5, officeRows }));
expect((await chief.general.getFrontStatus()).cityOfficeRequests.ids).toEqual(['8:760']);
const member = appRouter.createCaller(buildContext({ officerLevel: 1, officeRows }));
expect((await member.general.getFrontStatus()).cityOfficeRequests.ids).toEqual([]);
});
+18 -1
View File
@@ -33,9 +33,10 @@ type LogQuery = {
select: { id: true; text: true; createdAt: true }; select: { id: true; text: true; createdAt: true };
}; };
const buildContext = (findMany: (query: LogQuery) => Promise<Array<{ id: number; text: string }>>) => const buildContext = (findMany: (query: LogQuery) => Promise<Array<{ id: number | bigint; text: string }>>) =>
({ ({
auth, auth,
profile: { id: 'che', name: 'che:default', scenario: 'default' },
db: { db: {
general: { general: {
findFirst: vi.fn(async ({ where }: { where: { userId: string } }) => ({ findFirst: vi.fn(async ({ where }: { where: { userId: string } }) => ({
@@ -93,6 +94,22 @@ describe('general.getRecentRecords', () => {
}); });
}); });
it('returns exact numeric JSON cursors above int4 and trims a bigint cursor row', async () => {
const findMany = vi.fn(async () => [
{ id: 2_147_483_650n, text: '최신' },
{ id: 2_147_483_648n, text: '이전 cursor' },
]);
const caller = appRouter.createCaller(buildContext(findMany));
const result = await caller.general.getRecentRecords({
lastGeneralRecordId: 2_147_483_648,
lastWorldHistoryId: 2_147_483_648,
});
expect(result.global).toEqual([{ id: 2_147_483_650, text: '최신' }]);
expect(result.general).toEqual(result.global);
expect(result.history).toEqual(result.global);
expect(() => JSON.stringify(result)).not.toThrow();
});
it('caps an initial bucket at the legacy 15-row limit', async () => { it('caps an initial bucket at the legacy 15-row limit', async () => {
const rows = Array.from({ length: 16 }, (_, index) => ({ const rows = Array.from({ length: 16 }, (_, index) => ({
id: 100 - index, id: 100 - index,
@@ -49,7 +49,7 @@ const allowedEvidenceLevels = new Set(['dynamic-ref', 'actual-db', 'redis', 'end
const expectedOwnerCounts: Record<string, number> = { const expectedOwnerCounts: Record<string, number> = {
'durable-journal': 19, 'durable-journal': 19,
'engine-owned': 38, 'engine-owned': 39,
'explicit-no-realtime-consumer': 7, 'explicit-no-realtime-consumer': 7,
'external-upload': 1, 'external-upload': 1,
'mixed-saga': 9, 'mixed-saga': 9,
@@ -105,7 +105,6 @@ describe('game-api mutation evidence manifest', () => {
const rows = parseManifest(); const rows = parseManifest();
const manifestRoutes = rows.map(({ route }) => route); const manifestRoutes = rows.map(({ route }) => route);
expect(rows).toHaveLength(88);
expect(new Set(manifestRoutes).size).toBe(manifestRoutes.length); expect(new Set(manifestRoutes).size).toBe(manifestRoutes.length);
expect(manifestRoutes).toEqual([...manifestRoutes].sort()); expect(manifestRoutes).toEqual([...manifestRoutes].sort());
expect(manifestRoutes).toEqual(mountedMutationNames()); expect(manifestRoutes).toEqual(mountedMutationNames());
@@ -62,6 +62,7 @@ const deleteProfileRedisKeys = async (): Promise<void> => {
if (!redis) { if (!redis) {
return; return;
} }
await redis.client.del(`${redisPrefix}:flush:revocation:v1:baseline`);
for await (const keys of redis.client.scanIterator({ for await (const keys of redis.client.scanIterator({
MATCH: `sammo:game:*:${profileName}:*`, MATCH: `sammo:game:*:${profileName}:*`,
COUNT: 100, COUNT: 100,
@@ -155,6 +156,8 @@ integration('nation HTML purification over HTTP transport', () => {
} }
accessToken = created.accessToken; accessToken = created.accessToken;
// 이 격리 fixture는 이미 전환된 Gateway namespace를 합성 cutoff 0으로 준비한다.
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:baseline`, '0', { NX: true });
server = await createGameApiServer(); server = await createGameApiServer();
baseUrl = await server.app.listen({ host: server.config.host, port: server.config.port }); baseUrl = await server.app.listen({ host: server.config.host, port: server.config.port });
}, 30_000); }, 30_000);
@@ -1,3 +1,6 @@
import { createHTTPServer } from '@trpc/server/adapters/standalone';
import { once } from 'node:events';
import type { AddressInfo } from 'node:net';
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { ChangeJournal } from '@sammo-ts/common'; import { ChangeJournal } from '@sammo-ts/common';
@@ -347,3 +350,134 @@ describe('nation personnel router', () => {
expect(memberCommand).not.toHaveBeenCalled(); expect(memberCommand).not.toHaveBeenCalled();
}); });
}); });
describe('city office requests', () => {
it.each(['approve', 'reject', 'withdraw'] as const)(
'binds %s to the session actor and durable request ID',
async (action) => {
const requestCommand = vi.fn().mockResolvedValue({ type: 'cityOfficeRequest', ok: true, generalId: 22 });
const caller = appRouter.createCaller(createContext({ requestId: 'office-http', requestCommand }));
await expect(
caller.nation.cityOfficeRequest({ action, targetGeneralId: 30, officeRequestId: '30:760' })
).resolves.toEqual({ ok: true });
expect(requestCommand).toHaveBeenCalledWith({
type: 'cityOfficeRequest',
userId: 'user-22',
generalId: 22,
requestId: 'office-http:nation.cityOfficeRequest:engine:0:cityOfficeRequest',
action,
targetGeneralId: 30,
officeRequestId: '30:760',
});
}
);
it('validates request fields and propagates engine rejection', async () => {
const requestCommand = vi
.fn()
.mockResolvedValue({ type: 'cityOfficeRequest', ok: false, generalId: 22, reason: '이번 분기 자원 완료' });
const caller = appRouter.createCaller(createContext({ requestCommand }));
await expect(caller.nation.cityOfficeRequest({ action: 'request', officerLevel: 4 })).rejects.toMatchObject({
message: '이번 분기 자원 완료',
});
// @ts-expect-error invalid officer must also be rejected at the HTTP boundary
await expect(caller.nation.cityOfficeRequest({ action: 'request', officerLevel: 12 })).rejects.toMatchObject({
code: 'BAD_REQUEST',
});
expect(requestCommand).toHaveBeenCalledTimes(1);
});
it.each([1, 5])('redacts other requests for ordinary members (officer %s)', async (officerLevel) => {
const office = {
id: '30:760',
generalId: 30,
userId: 'user-30',
nationId: 1,
cityId: 1,
officerLevel: 4,
incumbentId: 0,
quarter: 760,
createdTick: 0,
dueTick: 3600000,
defaultDecision: 'approve',
defaultReason: 'vacant',
status: 'pending',
};
const me = { ...baseGeneral, officerLevel };
const caller = appRouter.createCaller(
createContext({
me,
db: {
general: {
findFirst: vi.fn(async () => me),
findMany: vi.fn(async () => [
me,
{
...baseGeneral,
id: 30,
userId: 'user-30',
officerLevel: 1,
meta: { belong: 5, cityOfficeRequest: office },
},
]),
},
nation: { findUnique: vi.fn(async () => ({ meta: { secretlimit: 3 } })) },
city: { findMany: vi.fn(async () => [{ id: 1, name: '허창', nationId: 1, meta: {} }]) },
worldState: {
findFirst: vi.fn(async () => ({
currentYear: 190,
currentMonth: 1,
tickSeconds: 600,
meta: {},
config: { stat: { chiefMin: 65 } },
clockBaseTime: new Date('2026-01-01'),
clockTick: 0n,
clockMode: 'manual',
clockWallAnchor: new Date('2026-01-01'),
clockPhase: 'RUNNING',
})),
},
},
})
);
const result = await caller.nation.getCityOfficeRequests();
expect(result.requests).toHaveLength(officerLevel === 5 ? 1 : 0);
expect(JSON.stringify(result)).not.toContain('user-30');
expect(result.options.find((option) => option.level === 4)?.allowed).toBe(false);
});
});
it('city office HTTP transport rejects unauthenticated and malformed requests and ignores a forged actor', async () => {
const requestCommand = vi.fn().mockResolvedValue({ type: 'cityOfficeRequest', ok: true, generalId: 22 });
const server = createHTTPServer({
router: appRouter,
createContext: ({ req }): GameApiContext => ({
...createContext({ requestId: 'office-http-wire', requestCommand }),
auth: req.headers['x-test-session'] ? auth : null,
}),
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const address = server.address() as AddressInfo;
const send = (body: unknown, authenticated = true) =>
fetch(`http://127.0.0.1:${address.port}/nation.cityOfficeRequest`, {
method: 'POST',
headers: { 'content-type': 'application/json', ...(authenticated ? { 'x-test-session': 'fixture' } : {}) },
body: JSON.stringify(body),
});
try {
expect((await send({ action: 'request', officerLevel: 4 }, false)).status).toBe(401);
expect((await send({ action: 'request', officerLevel: 12 })).status).toBe(400);
expect(requestCommand).not.toHaveBeenCalled();
expect((await send({ action: 'request', officerLevel: 4, generalId: 99, userId: 'forged' })).status).toBe(200);
expect(requestCommand).toHaveBeenCalledWith(expect.objectContaining({ generalId: 22, userId: 'user-22' }));
requestCommand.mockResolvedValueOnce({
type: 'cityOfficeRequest',
ok: false,
generalId: 22,
reason: '이미 처리된 요청',
});
expect((await send({ action: 'approve', targetGeneralId: 30, officeRequestId: 'stale' })).status).toBe(400);
} finally {
server.closeAllConnections();
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())));
}
});
@@ -0,0 +1,375 @@
import { afterAll, beforeAll, expect, it } from 'vitest';
import fastify from 'fastify';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import { asRecord } from '@sammo-ts/common';
import {
decodeAuditDecisionSteps,
createGamePostgresConnector,
createRedisConnector,
GamePrisma,
} from '@sammo-ts/infra';
import { createTurnDaemonRuntime, seedScenarioToDatabase, type TurnDaemonRuntime } from '@sammo-ts/game-engine';
import { appRouter } from '../src/router.js';
import { DatabaseTurnDaemonTransport } from '../src/daemon/databaseTransport.js';
import { RedisAccessTokenStore } from '../src/auth/accessTokenStore.js';
import { InMemoryFlushStore } from '../src/auth/flushStore.js';
import type { GameApiContext } from '../src/context.js';
const databaseUrl = process.env.NPC_POLICY_DATABASE_URL;
const integration = it.skipIf(!databaseUrl || !process.env.REDIS_URL);
const connector = databaseUrl ? createGamePostgresConnector({ url: databaseUrl }) : null;
const redis = process.env.REDIS_URL ? createRedisConnector({ url: process.env.REDIS_URL }) : null;
let runtime: TurnDaemonRuntime | undefined;
let loop: Promise<void> | undefined;
const profile = `npc-policy:${databaseUrl ? new URL(databaseUrl).searchParams.get('schema') : 'fixture'}`;
const baseTime = new Date('0185-01-01T00:00:00Z');
beforeAll(async () => {
if (!databaseUrl) return;
if (!new URL(databaseUrl).searchParams.get('schema')?.endsWith('_npc_policy_fixture'))
throw new Error('Dedicated NPC policy fixture schema required');
await connector!.connect();
await redis!.connect();
});
afterAll(async () => {
if (loop) {
await runtime?.lifecycle.stop();
await loop;
}
await runtime?.close();
await connector?.disconnect();
await redis?.disconnect();
});
integration(
'HTTP user edits survive daemon restart and real NPC turns, with readable actor and before/after history',
async () => {
const db = connector!.prisma;
await db.playAuditPolicy.deleteMany({
where: {
OR: [
{ serverId: 'npc-policy-lifecycle' },
{ id: { in: ['c'.repeat(64), 'd'.repeat(64), 'e'.repeat(64)] } },
],
},
});
await db.playAuditMonth.deleteMany({ where: { serverId: 'npc-policy-lifecycle' } });
await db.playAuditDecisionChunk.deleteMany({ where: { decision: { serverId: 'npc-policy-lifecycle' } } });
await db.playAuditDecision.deleteMany({ where: { serverId: 'npc-policy-lifecycle' } });
await db.playAuditDiplomacyEvent.deleteMany({ where: { serverId: 'npc-policy-lifecycle' } });
await db.inputEvent.deleteMany({ where: { requestId: { startsWith: 'npc-policy-http-' } } });
await seedScenarioToDatabase({
scenarioId: 903,
databaseUrl: databaseUrl!,
now: new Date(),
installOptions: {
openAt: new Date(Date.now() + 86_400_000),
turnTermMinutes: 5,
npcMode: 2,
showImgLevel: 3,
serverId: 'npc-policy-lifecycle',
season: 1,
},
});
await db.generalTurn.deleteMany();
await db.nationTurn.deleteMany();
await db.general.deleteMany();
await db.nation.deleteMany();
await db.diplomacy.deleteMany();
// 185년 고정 fixture에 180년 시나리오의 지연 등장 이벤트를 이월하지 않는다.
// 일반턴·월간 재정/도시 갱신은 실제 runtime handler를 그대로 실행한다.
await db.event.deleteMany();
const city = await db.city.findFirstOrThrow({ orderBy: { id: 'asc' } });
const world = await db.worldState.findFirstOrThrow();
await db.worldState.update({
where: { id: world.id },
data: {
currentYear: 185,
currentMonth: 1,
clockBaseTime: baseTime,
clockTick: 0n,
lastTurnTick: 0n,
clockPhase: 'RUNNING',
clockMode: 'realtime',
clockWallAnchor: new Date(),
config: { ...asRecord(world.config), tournamentTrig: false },
},
});
await db.nation.create({
data: {
id: 1,
name: '정책국',
color: '#ffffff',
level: 3,
chiefGeneralId: 1,
capitalCityId: city.id,
gold: 100_000,
rice: 100_000,
tech: 3000,
meta: {},
},
});
await db.city.updateMany({ data: { nationId: 0 } });
await db.city.update({
where: { id: city.id },
data: {
nationId: 1,
population: 100_000,
populationMax: 200_000,
supplyState: 1,
frontState: 1,
meta: { trust: 80, trade: 100 },
},
});
await db.general.createMany({
data: [
{
id: 1,
userId: 'npc-policy-owner',
name: '정책군주',
nationId: 1,
cityId: city.id,
officerLevel: 12,
npcState: 0,
turnTime: new Date('0185-02-01Z'),
turnTick: 100_000_000n,
meta: { belong: 5, killturn: 24 },
},
{
id: 2,
name: '징병검증장',
nationId: 1,
cityId: city.id,
officerLevel: 5,
npcState: 2,
leadership: 80,
strength: 80,
intel: 30,
gold: 100_000,
rice: 100_000,
crew: 0,
turnTime: baseTime,
turnTick: 0n,
meta: { killturn: 24, armType: 1, dex1: 1000 },
},
],
});
const transport = new DatabaseTurnDaemonTransport(db, 10_000);
const auth: NonNullable<GameApiContext['auth']> = {
version: 1,
profile,
sessionId: 'fixture-session',
issuedAt: new Date().toISOString(),
expiresAt: '2999-01-01T00:00:00Z',
user: { id: 'npc-policy-owner', username: 'fixture', displayName: 'fixture', roles: [] },
sanctions: {},
};
const app = fastify();
let request = 0;
await app.register(fastifyTRPCPlugin, {
prefix: '/che/api/trpc',
trpcOptions: {
router: appRouter,
createContext: ({
req,
}: {
req: { headers: Record<string, string | string[] | undefined> };
}): GameApiContext => ({
db,
redis: redis!.client,
profile: { id: 'hwe', scenario: '903', name: profile },
auth: req.headers['x-session-token'] === 'fixture-session' ? auth : null,
requestId: `npc-policy-http-${++request}`,
turnDaemon: transport,
accessTokenStore: new RedisAccessTokenStore(redis!.client, profile),
flushStore: new InMemoryFlushStore(),
battleSim: {} as GameApiContext['battleSim'],
uploadDir: '/tmp',
uploadPath: '/uploads',
uploadPublicUrl: null,
gameTokenSecret: 'fixture-only',
}),
},
});
const actions: Array<{ kind: string; actionKey: string; generalId: number; completed?: boolean }> = [];
const start = async () => {
const instance = await createTurnDaemonRuntime({
profile,
databaseUrl: databaseUrl!,
enableLeaseHeartbeat: false,
onActionResolved: (action) => actions.push(action),
});
// 자연 진행은 정지하고, 사용자 command와 명시한 due 턴만 처리한다.
instance.lifecycle.pause('NPC policy fixture');
return instance;
};
const call = async (endpoint: string, input: unknown, mutation = false) => {
const response = await app.inject({
method: mutation ? 'POST' : 'GET',
url: `/che/api/trpc/npc.${endpoint}${mutation ? '' : `?input=${encodeURIComponent(JSON.stringify(input))}`}`,
headers: {
'x-session-token': 'fixture-session',
...(mutation ? { 'content-type': 'application/json' } : {}),
},
...(mutation ? { payload: JSON.stringify(input) } : {}),
});
expect(response.statusCode, response.body).toBe(200);
return response.json<{ result: { data: unknown } }>().result.data;
};
const configuration = (value: unknown) => {
const data = asRecord(value);
return {
values: data.currentNationPolicy,
nation: data.currentNationPriority,
general: data.currentGeneralActionPriority,
setters: data.lastSetters,
};
};
try {
runtime = await start();
loop = runtime.lifecycle.start();
// 발령과 징병을 목록에서 빼면 실제 턴에서도 수행하지 않아야 한다.
await call('setNationPriority', ['NPC포상'], true);
await call('setGeneralPriority', ['출병', '일반내정'], true);
await call(
'setNationPolicy',
{ minWarCrew: 2000, minNPCRecruitCityPopulation: 30000, safeRecruitCityPopulationRatio: 0 },
true
);
const withoutRecruitment = await call('getPolicy', {});
expect(withoutRecruitment).toMatchObject({
currentNationPriority: ['NPC포상'],
currentGeneralActionPriority: ['출병', '일반내정'],
});
await runtime.lifecycle.stop();
await loop;
loop = undefined;
await runtime.close();
runtime = await start();
expect(configuration(await call('getPolicy', {}))).toEqual(configuration(withoutRecruitment));
let result = await runtime.processor.run(baseTime, { budgetMs: 10_000, maxGenerals: 1, catchUpCap: 1 });
expect(result.processedGenerals).toBe(1);
expect(runtime.world.getGeneralById(2)!.crew).toBe(0);
expect(actions).toContainEqual(expect.objectContaining({ generalId: 2, kind: 'nation' }));
expect(
actions.filter((action) => action.generalId === 2).some((action) => action.actionKey === 'che_징병')
).toBe(false);
await runtime.hooks!.flushChanges!(result);
const nationDecision = await db.playAuditDecision.findFirstOrThrow({
where: { generalId: 2, phase: 'nation' },
include: { chunks: { orderBy: { ordinal: 'asc' } } },
});
const procedures = nationDecision.chunks.flatMap((chunk) =>
decodeAuditDecisionSteps(chunk.steps)
.map(asRecord)
.filter((step) => step.kind === 'PROCEDURE_START')
.map((step) => step.procedure)
);
expect(procedures).toEqual(['NPC포상']);
expect(configuration(await call('getPolicy', {}))).toEqual(configuration(withoutRecruitment));
// 같은 장수·자원으로 징병 우선순위를 맨 앞에 저장한 뒤 다시 시작한다.
loop = runtime.lifecycle.start();
await call('setGeneralPriority', ['징병', '출병', '일반내정'], true);
const saved = await call('getPolicy', {});
await runtime.lifecycle.stop();
await loop;
loop = undefined;
await runtime.close();
runtime = await start();
expect(configuration(await call('getPolicy', {}))).toEqual(configuration(saved));
const beforeMeta = asRecord(runtime.world.getNationById(1)!.meta);
// 실제 due 턴과 command를 실행한다. 전방의 중립 도시가 전쟁 상태를 만든다.
actions.length = 0;
result = await runtime.processor.run(runtime.world.getGeneralById(2)!.turnTime, {
budgetMs: 10_000,
maxGenerals: 1,
catchUpCap: 1,
});
expect(result.processedGenerals).toBe(1);
expect(runtime.world.getGeneralById(2)!.crew).toBeGreaterThan(0);
expect(actions).toContainEqual(
expect.objectContaining({ generalId: 2, actionKey: 'che_징병', completed: true })
);
await runtime.hooks!.flushChanges!(result);
expect((await db.general.findUniqueOrThrow({ where: { id: 2 } })).crew).toBeGreaterThan(0);
expect(asRecord(runtime.world.getNationById(1)!.meta).npc_nation_policy).toEqual(
beforeMeta.npc_nation_policy
);
expect(configuration(await call('getPolicy', {}))).toEqual(configuration(saved));
// 병력이 소진되고 도시 인구가 정책 하한 미만이면 징병을 거른다.
runtime.world.updateGeneral(2, { crew: 0 });
runtime.world.updateCity(city.id, { population: 30_100 });
actions.length = 0;
result = await runtime.processor.run(runtime.world.getGeneralById(2)!.turnTime, {
budgetMs: 10_000,
maxGenerals: 1,
catchUpCap: 1,
});
expect(result.processedGenerals).toBe(1);
expect(runtime.world.getGeneralById(2)!.crew).toBe(0);
expect(actions.some((action) => action.actionKey === 'che_징병')).toBe(false);
await runtime.hooks!.flushChanges!(result);
expect(configuration(await call('getPolicy', {}))).toEqual(configuration(saved));
// 이후의 개인턴/월간 갱신도 정책을 덮어쓰면 안 된다.
const target = new Date(runtime.world.getGeneralById(2)!.turnTime.getTime() + 30 * 60_000);
result = await runtime.processor.run(target, { budgetMs: 10_000, maxGenerals: 100, catchUpCap: 12 });
expect(result.processedGenerals).toBeGreaterThan(1);
await runtime.hooks!.flushChanges!(result);
expect(await call('getPolicy', {})).toMatchObject({
currentNationPriority: ['NPC포상'],
currentGeneralActionPriority: ['징병', '출병', '일반내정'],
currentNationPolicy: { minWarCrew: 2000, minNPCRecruitCityPopulation: 30000 },
});
expect(runtime.world.getState().meta.playAuditGap).toBeUndefined();
const history = await call('policyHistory', { area: 'NPC_NATION_PRIORITY' });
expect(history).toMatchObject({
items: [
expect.objectContaining({ source: 'CHANGE', actor: expect.objectContaining({ name: '정책군주' }) }),
expect.anything(),
],
});
const row = await db.playAuditPolicy.findFirstOrThrow({
where: { nationId: 1, area: 'NPC_NATION_PRIORITY', source: 'CHANGE' },
});
expect(await call('policyVersion', { id: row.id })).toMatchObject({
actor: { name: '정책군주' },
fields: [{ key: 'priority', beforeJson: 'null', afterJson: '["NPC포상"]', changed: true }],
});
expect(row.inputSequence).not.toBeNull();
expect((await db.inputEvent.findUniqueOrThrow({ where: { sequence: row.inputSequence! } })).status).toBe(
'SUCCEEDED'
);
// 외국/이전 기수/국방 정책 ID를 알아도 자국 NPC 정책으로 조회할 수 없다.
for (const [id, patch] of [
['c'.repeat(64), { nationId: 2 }],
['d'.repeat(64), { serverId: 'previous-season' }],
['e'.repeat(64), { area: 'DEFENCE' }],
] as const) {
await db.playAuditPolicy.create({
data: {
...row,
id,
...patch,
actor: row.actor ?? GamePrisma.DbNull,
before: row.before ?? GamePrisma.DbNull,
after: row.after ?? GamePrisma.JsonNull,
},
});
const response = await app.inject({
url: `/che/api/trpc/npc.policyVersion?input=${encodeURIComponent(JSON.stringify({ id }))}`,
headers: { 'x-session-token': 'fixture-session' },
});
expect(response.statusCode).toBe(404);
}
await db.general.update({ where: { id: 1 }, data: { penalty: { noChief: true } } });
const denied = await app.inject({
url: `/che/api/trpc/npc.policyHistory?input=${encodeURIComponent(JSON.stringify({ area: 'NPC_VALUES' }))}`,
headers: { 'x-session-token': 'fixture-session' },
});
expect(denied.statusCode).toBe(403);
} finally {
await app.close();
}
},
90_000
);
+154 -1
View File
@@ -1,4 +1,6 @@
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import fastify from 'fastify';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken'; import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
import type { RedisConnector } from '@sammo-ts/infra'; import type { RedisConnector } from '@sammo-ts/infra';
@@ -96,10 +98,16 @@ const createContext = (
options: { options: {
me?: GeneralRow; me?: GeneralRow;
nation?: typeof baseNation; nation?: typeof baseNation;
world?: typeof baseWorld; world?: {
config: typeof baseWorld.config;
meta: Record<string, unknown>;
currentYear?: number;
currentMonth?: number;
};
requestCommand?: ReturnType<typeof vi.fn>; requestCommand?: ReturnType<typeof vi.fn>;
troopRows?: Array<{ troopLeaderId: number }>; troopRows?: Array<{ troopLeaderId: number }>;
cityRows?: Array<{ id: number }>; cityRows?: Array<{ id: number }>;
historyRows?: Array<Record<string, unknown>>;
} = {} } = {}
): { context: GameApiContext; findFirst: ReturnType<typeof vi.fn>; requestCommand: ReturnType<typeof vi.fn> } => { ): { context: GameApiContext; findFirst: ReturnType<typeof vi.fn>; requestCommand: ReturnType<typeof vi.fn> } => {
const requestCommand = const requestCommand =
@@ -117,6 +125,11 @@ const createContext = (
worldState: { findFirst: vi.fn(async () => options.world ?? baseWorld) }, worldState: { findFirst: vi.fn(async () => options.world ?? baseWorld) },
troop: { findMany: vi.fn(async () => options.troopRows ?? [{ troopLeaderId: 101 }]) }, troop: { findMany: vi.fn(async () => options.troopRows ?? [{ troopLeaderId: 101 }]) },
city: { findMany: vi.fn(async () => options.cityRows ?? [{ id: 1 }, { id: 2 }]) }, city: { findMany: vi.fn(async () => options.cityRows ?? [{ id: 1 }, { id: 2 }]) },
playAuditPolicy: {
findMany: vi.fn(async () => options.historyRows ?? []),
findFirst: vi.fn(async () => options.historyRows?.[0] ?? null),
},
$transaction: async <T>(read: (tx: object) => Promise<T>): Promise<T> => read(db),
}; };
const redisClient = { get: async () => null, set: async () => null }; const redisClient = { get: async () => null, set: async () => null };
return { return {
@@ -140,6 +153,146 @@ const createContext = (
}; };
describe('NPC policy router', () => { describe('NPC policy router', () => {
const historyRow = {
id: 'a'.repeat(64),
schemaVersion: 1,
nationId: 1,
area: 'NPC_NATION_PRIORITY',
revision: 2,
previousId: 'b'.repeat(64),
source: 'CHANGE',
year: 185,
month: 1,
createdAt: new Date('2026-01-01Z'),
actor: {
userId: 'private-user',
generalId: 22,
name: '정책담당',
nationId: 1,
officerLevel: 12,
npcState: 0,
permission: 4,
},
before: { priority: ['NPC전방발령', 'NPC포상'] },
after: { priority: ['NPC포상'] },
};
const historyContext = (options: Parameters<typeof createContext>[0] = {}) =>
createContext({
...options,
world: {
...baseWorld,
currentYear: 185,
currentMonth: 1,
meta: { ...baseWorld.meta, serverId: 'current-season' },
},
historyRows: options.historyRows ?? [historyRow],
});
it('lets an ordinary nation reader inspect the actor and changes without admin request/account fields', async () => {
const fixture = historyContext({ me: { ...baseGeneral, officerLevel: 2 } });
const caller = appRouter.createCaller(fixture.context);
const page = await caller.npc.policyHistory({ area: 'NPC_NATION_PRIORITY', limit: 1 });
expect(page.items[0]).toMatchObject({ actor: { name: '정책담당' }, revision: 2 });
const detail = await caller.npc.policyVersion({ id: historyRow.id });
expect(detail.fields).toEqual([
{ key: 'priority', beforeJson: '["NPC전방발령","NPC포상"]', afterJson: '["NPC포상"]', changed: true },
]);
expect(JSON.stringify(detail)).not.toMatch(/private-user|permission|requestId|inputSequence/);
});
it.each([
{ nationId: 0 },
{ officerLevel: 0 },
{ officerLevel: 1, meta: { belong: 0 } },
{ penalty: { noChief: true } },
])('denies history when current nation read permission is missing: %j', async (patch) => {
const fixture = historyContext({ me: { ...baseGeneral, ...patch } });
await expect(
appRouter.createCaller(fixture.context).npc.policyHistory({ area: 'NPC_VALUES' })
).rejects.toBeDefined();
});
it('scopes cursor and version lookups to the authenticated nation and current season', async () => {
const fixture = historyContext();
const caller = appRouter.createCaller(fixture.context);
await caller.npc.policyHistory({ area: 'NPC_VALUES', cursor: 7, limit: 1 });
const tx = await fixture.context.db.$transaction!(async (db) => db);
expect(tx.playAuditPolicy.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { serverId: 'current-season', nationId: 1, area: 'NPC_VALUES', revision: { lt: 7 } },
take: 2,
})
);
await caller.npc.policyVersion({ id: historyRow.id });
expect(tx.playAuditPolicy.findFirst).toHaveBeenCalledWith(
expect.objectContaining({
where: {
id: historyRow.id,
serverId: 'current-season',
nationId: 1,
area: { in: ['NPC_VALUES', 'NPC_NATION_PRIORITY', 'NPC_GENERAL_PRIORITY'] },
},
})
);
await expect(caller.npc.policyHistory({ area: 'NPC_VALUES', limit: 51 })).rejects.toMatchObject({
code: 'BAD_REQUEST',
});
await expect(caller.npc.policyVersion({ id: 'bad' })).rejects.toMatchObject({ code: 'BAD_REQUEST' });
});
it('requires authentication and rejects caller-supplied nation or defence history over HTTP', async () => {
const fixture = historyContext();
const app = fastify();
await app.register(fastifyTRPCPlugin, {
prefix: '/che/api/trpc',
trpcOptions: {
router: appRouter,
createContext: ({ req }: { req: { headers: Record<string, string | string[] | undefined> } }) => ({
...fixture.context,
auth: req.headers['x-session-token'] === 'fixture-session' ? auth : null,
}),
},
});
try {
const query = (input: object, authenticated = true) =>
app.inject({
url: `/che/api/trpc/npc.policyHistory?input=${encodeURIComponent(JSON.stringify(input))}`,
headers: authenticated ? { 'x-session-token': 'fixture-session' } : {},
});
expect((await query({ area: 'NPC_VALUES' }, false)).statusCode).toBe(401);
expect((await query({ area: 'NPC_VALUES' })).statusCode).toBe(200);
expect((await query({ area: 'DEFENCE' })).statusCode).toBe(400);
expect((await query({ area: 'NPC_VALUES', nationId: 2 })).statusCode).toBe(400);
} finally {
await app.close();
}
});
it('denies sanctioned sessions before reading history', async () => {
const fixture = historyContext();
fixture.context.auth = { ...auth, sanctions: { suspendedUntil: '2999-01-01T00:00:00Z' } };
await expect(
appRouter.createCaller(fixture.context).npc.policyHistory({ area: 'NPC_VALUES' })
).rejects.toMatchObject({ code: 'FORBIDDEN' });
expect(fixture.findFirst).not.toHaveBeenCalled();
});
it('paginates revisions without exposing a missing or unavailable version', async () => {
const fixture = historyContext({ historyRows: [historyRow, { ...historyRow, revision: 1 }] });
const page = await appRouter
.createCaller(fixture.context)
.npc.policyHistory({ area: 'NPC_NATION_PRIORITY', limit: 1 });
expect(page.items).toHaveLength(1);
expect(page.nextCursor).toBe(2);
const missing = historyContext({ historyRows: [] });
await expect(
appRouter.createCaller(missing.context).npc.policyVersion({ id: historyRow.id })
).rejects.toMatchObject({ code: 'NOT_FOUND' });
const noIdentity = createContext();
await expect(
appRouter.createCaller(noIdentity.context).npc.policyHistory({ area: 'NPC_VALUES' })
).resolves.toMatchObject({ coverage: 'IDENTITY_MISSING', items: [], nextCursor: null });
});
it('loads server and nation overrides while calculating legacy zero-value hints from nation tech', async () => { it('loads server and nation overrides while calculating legacy zero-value hints from nation tech', async () => {
const fixture = createContext(); const fixture = createContext();
const result = await appRouter.createCaller(fixture.context).npc.getPolicy(); const result = await appRouter.createCaller(fixture.context).npc.getPolicy();
@@ -1,12 +1,15 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { import {
summarizeNationPeriod, summarizeNationPeriod,
projectCurrentSettlement,
zAuditNation,
type AuditNationData, type AuditNationData,
type NationMonthPoint, type NationMonthPoint,
} from '../src/router/playAudit/nationSeries.js'; } from '../src/router/playAudit/nationSeries.js';
const population = { const population = {
count: 0, count: 0,
crew: null,
gold: 0, gold: 0,
rice: 0, rice: 0,
dex: { dex1: 0, dex2: 0, dex3: 0, dex4: 0, dex5: 0 }, dex: { dex1: 0, dex2: 0, dex3: 0, dex4: 0, dex5: 0 },
@@ -89,3 +92,44 @@ describe('play audit half-year summary', () => {
}); });
}); });
}); });
describe('current committed settlement', () => {
it.each([
[1, 'gold'],
[7, 'rice'],
] as const)('exposes month %s settlement before month-end', (month, resource) => {
const meta = {
playAuditFlows: {
year: 200,
month,
complete: true,
entries: { [`2:${resource}`]: { nationId: 2, resource, income: 1234.5, paid: 234 } },
},
};
expect(projectCurrentSettlement(meta, 200, month, 2)[resource]).toEqual({ income: 1234.5, paid: 234 });
expect(projectCurrentSettlement(meta, 200, month + 1, 2)[resource]).toBeNull();
expect(projectCurrentSettlement(meta, 200, month, 3)[resource]).toBeNull();
});
it('keeps absent and malformed observations unknown, preserves actual zero', () => {
expect(projectCurrentSettlement({}, 200, 1, 2).gold).toBeNull();
const meta = {
playAuditFlows: {
year: 200,
month: 1,
complete: false,
entries: { '2:gold': { nationId: 2, resource: 'gold', income: 0, paid: 0 } },
},
};
expect(projectCurrentSettlement(meta, 200, 1, 2)).toMatchObject({
gold: { income: 0, paid: 0 },
complete: false,
});
meta.playAuditFlows.entries['2:gold'].income = NaN;
expect(projectCurrentSettlement(meta, 200, 1, 2).gold).toBeNull();
});
it('reads old snapshots without inventing troop counts', () => {
const old = JSON.parse(JSON.stringify(nation(1)));
delete old.populations.human.crew;
expect(zAuditNation.parse(old).populations.human.crew).toBeNull();
});
});
@@ -0,0 +1,62 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { createGamePostgresConnector, type GamePrismaClient } from '@sammo-ts/infra';
import { loadProfileReadiness } from '../src/services/clockReadiness.js';
const databaseUrl = process.env.INPUT_EVENT_DATABASE_URL;
const integration = describe.skipIf(!databaseUrl);
integration('profile deployment readiness', () => {
let db: GamePrismaClient;
let close: () => Promise<void>;
beforeAll(async () => {
const connector = createGamePostgresConnector({ url: databaseUrl! });
await connector.connect();
db = connector.prisma;
close = () => connector.disconnect();
});
afterAll(async () => {
await close?.();
});
it('rejects absent, initializing, expired and other-profile leases; accepts ready paused/preopen runtime', async () => {
const rollback = new Error('readiness fixture rollback');
await expect(
db.$transaction(async (tx) => {
// Shared integration fixtures may leave projection work; rollback restores it.
await tx.clockProjectionOutbox.deleteMany();
await tx.worldState.create({
data: {
id: -998901,
scenarioCode: 'readiness-fixture',
currentYear: 190,
currentMonth: 1,
tickSeconds: 300,
clockPhase: 'PREOPEN',
config: {},
meta: {},
},
});
const profile = 'readiness:fixture';
expect((await loadProfileReadiness(tx, profile)).ok).toBe(false);
await tx.turnDaemonLease.create({
data: {
profile,
ownerId: 'fixture',
fencingEpoch: 1n,
heartbeatAt: new Date(),
leaseUntil: new Date(Date.now() + 60_000),
clockReady: false,
},
});
expect((await loadProfileReadiness(tx, profile)).ok).toBe(false);
await tx.turnDaemonLease.update({ where: { profile }, data: { clockReady: true } });
expect((await loadProfileReadiness(tx, profile)).ok).toBe(true);
expect((await loadProfileReadiness(tx, 'readiness:other')).ok).toBe(false);
await tx.turnDaemonLease.update({
where: { profile },
data: { leaseUntil: new Date(Date.now() - 60_000) },
});
expect((await loadProfileReadiness(tx, profile)).ok).toBe(false);
throw rollback;
})
).rejects.toBe(rollback);
});
});
@@ -38,8 +38,8 @@ const buildContext = () => {
}, },
logEntry: { logEntry: {
findMany: vi.fn(async () => [ findMany: vi.fn(async () => [
{ id: 9, text: '<Y>최근 정세</>' }, { id: 9n, text: '<Y>최근 정세</>' },
{ id: 8, text: '이전 정세' }, { id: 8n, text: '이전 정세' },
]), ]),
}, },
$queryRaw: vi $queryRaw: vi
+2 -2
View File
@@ -20,7 +20,7 @@ const profile: GameProfile = {
const auth: GameSessionTokenPayload = { const auth: GameSessionTokenPayload = {
version: 1, version: 1,
profile: 'che', profile: 'che:default',
issuedAt: '2026-07-26T00:00:00.000Z', issuedAt: '2026-07-26T00:00:00.000Z',
expiresAt: '2026-07-27T00:00:00.000Z', expiresAt: '2026-07-27T00:00:00.000Z',
sessionId: 'ranking-session', sessionId: 'ranking-session',
@@ -239,7 +239,7 @@ const buildContext = (options?: {
turnDaemon: new InMemoryTurnDaemonTransport(), turnDaemon: new InMemoryTurnDaemonTransport(),
battleSim: new InMemoryBattleSimTransport(), battleSim: new InMemoryBattleSimTransport(),
profile: selectedProfile, profile: selectedProfile,
auth: options?.authenticated === false ? null : auth, auth: options?.authenticated === false ? null : { ...auth, profile: selectedProfile.name },
uploadDir: 'uploads', uploadDir: 'uploads',
uploadPath: '/uploads', uploadPath: '/uploads',
uploadPublicUrl: null, uploadPublicUrl: null,
@@ -45,6 +45,28 @@ const createFixture = (rows: readonly object[]) => {
}; };
describe('ReadModelOutboxWorker', () => { describe('ReadModelOutboxWorker', () => {
it('drains more than one full retention batch rather than leaving a permanent 100-per-minute backlog', async () => {
let now = new Date('2026-11-09T00:00:00Z');
const fixture = createFixture([]);
fixture.queryRaw
.mockReset()
.mockResolvedValueOnce([])
.mockResolvedValueOnce([{ id: 1n }, { id: 2n }])
.mockResolvedValueOnce([{ id: 3n }, { id: 4n }])
.mockResolvedValueOnce([]);
const worker = new ReadModelOutboxWorker(fixture.db, fixture.redis, 'che:default', {
intervalMs: 60000,
pruneLimit: 2,
now: () => now,
});
now = new Date('2026-11-09T00:01:00Z');
worker.start();
await vi.waitFor(() => expect(fixture.queryRaw).toHaveBeenCalledTimes(4));
await worker.stop();
const query = fixture.queryRaw.mock.calls[1]?.[0] as { sql: string };
expect(query.sql).toContain('FOR UPDATE SKIP LOCKED');
expect(query.sql).toContain('ORDER BY "delivered_at", "available_at", "id"');
});
it('publishes a survey-style global front-status invalidation without a turn daemon', async () => { it('publishes a survey-style global front-status invalidation without a turn daemon', async () => {
const fixture = createFixture([{ id: 10n, payload: payload('front.global'), attempts: 1 }]); const fixture = createFixture([{ id: 10n, payload: payload('front.global'), attempts: 1 }]);
const worker = new ReadModelOutboxWorker(fixture.db, fixture.redis, 'che:default', { const worker = new ReadModelOutboxWorker(fixture.db, fixture.redis, 'che:default', {
@@ -0,0 +1,151 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { PassThrough } from 'node:stream';
import type { ServerResponse } from 'node:http';
import type { GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
import { RealtimeConnectionAdmission } from '../src/realtime/connectionAdmission.js';
import { AuthenticatedRealtimeConnection } from '../src/realtime/authenticatedConnection.js';
afterEach(() => vi.useRealTimers());
describe('realtime connection admission', () => {
it('reserves the process total before authentication and counts live streams too', () => {
const budget = new RealtimeConnectionAdmission({ connections: 2, perUser: 2, opening: 2 });
const first = budget.begin(vi.fn())!;
const second = budget.begin(vi.fn())!;
expect(first.reserveUser('actor')).toBe(true);
expect(first.startStreaming()).toBe(true);
expect(budget.begin(vi.fn())).toBeNull();
second.release();
const replacement = budget.begin(vi.fn())!;
expect(replacement).not.toBeNull();
replacement.release();
first.release();
});
it('shares the authenticated actor limit while allowing another actor', () => {
const budget = new RealtimeConnectionAdmission({ connections: 5, perUser: 1, opening: 5 });
const first = budget.begin(vi.fn())!;
expect(first.reserveUser('one')).toBe(true);
const rejected = budget.begin(vi.fn())!;
expect(rejected.reserveUser('one')).toBe(false);
expect(rejected.startStreaming()).toBe(false);
rejected.release();
const other = budget.begin(vi.fn())!;
expect(other.reserveUser('two')).toBe(true);
first.release();
first.release();
const replacement = budget.begin(vi.fn())!;
expect(replacement.reserveUser('one')).toBe(true);
other.release();
replacement.release();
});
it('keeps timed-out and disconnected backend work admitted until it actually settles', async () => {
vi.useFakeTimers();
const budget = new RealtimeConnectionAdmission({ connections: 5, perUser: 5, opening: 2 });
const timeout = vi.fn();
const stalled = budget.begin(timeout)!;
const disconnected = budget.begin(vi.fn())!;
expect(stalled.reserveUser('stalled')).toBe(true);
disconnected.cancelOpening();
await vi.advanceTimersByTimeAsync(5000);
expect(timeout).toHaveBeenCalledTimes(1);
expect(stalled.canContinue()).toBe(false);
expect(stalled.startStreaming()).toBe(false);
expect(disconnected.canContinue()).toBe(false);
expect(budget.begin(vi.fn())).toBeNull();
stalled.release();
const recovered = budget.begin(vi.fn())!;
expect(recovered.reserveUser('stalled')).toBe(true);
expect(recovered.startStreaming()).toBe(true);
await vi.advanceTimersByTimeAsync(5000);
expect(timeout).toHaveBeenCalledTimes(1);
disconnected.release();
recovered.release();
});
it('stops pending setup on shutdown and permanently refuses new admissions', () => {
const budget = new RealtimeConnectionAdmission();
const timeout = vi.fn();
const pending = budget.begin(timeout)!;
budget.close();
budget.close();
expect(timeout).toHaveBeenCalledTimes(1);
expect(pending.canContinue()).toBe(false);
expect(budget.begin(vi.fn())).toBeNull();
pending.release();
});
it('keeps a closed stream counted while its in-flight authentication is still pending', async () => {
const budget = new RealtimeConnectionAdmission({ connections: 1, perUser: 1, opening: 1 });
const slot = budget.begin(vi.fn())!;
expect(slot.reserveUser('actor')).toBe(true);
expect(slot.startStreaming()).toBe(true);
let finish!: () => void;
let started!: () => void;
const inFlight = new Promise<void>((resolve) => {
finish = resolve;
});
const running = new Promise<void>((resolve) => {
started = resolve;
});
const auth: GameSessionTokenPayload = {
version: 1,
sessionId: 'session',
profile: 'che:default',
issuedAt: new Date().toISOString(),
expiresAt: new Date(Date.now() + 60_000).toISOString(),
user: {
id: 'actor',
username: 'actor',
displayName: 'actor',
roles: ['user'],
createdAt: new Date().toISOString(),
},
sanctions: {},
};
const response = new PassThrough();
const connection = new AuthenticatedRealtimeConnection(
auth,
response as unknown as ServerResponse,
async () => {
started();
await inFlight;
return auth;
}
);
connection.onClose(() => {
void connection.whenIdle().then(slot.release, slot.release);
});
connection.enqueue(async () => 'event: ready\ndata: {}\n\n');
await running;
connection.close();
expect(budget.begin(vi.fn())).toBeNull();
finish();
await connection.whenIdle();
const replacement = budget.begin(vi.fn())!;
expect(replacement.reserveUser('actor')).toBe(true);
replacement.release();
});
it('enforces the production 512 total and 32 pending openings without accumulating a queue', () => {
const budget = new RealtimeConnectionAdmission();
const pending = Array.from({ length: 32 }, () => budget.begin(vi.fn())!);
expect(pending.every(Boolean)).toBe(true);
expect(budget.begin(vi.fn())).toBeNull();
for (const slot of pending) slot.release();
const live = Array.from({ length: 512 }, (_, index) => {
const slot = budget.begin(vi.fn())!;
expect(slot.reserveUser(`actor-${index}`)).toBe(true);
expect(slot.startStreaming()).toBe(true);
return slot;
});
expect(budget.begin(vi.fn())).toBeNull();
live[0]!.release();
const replacement = budget.begin(vi.fn())!;
expect(replacement.reserveUser('replacement')).toBe(true);
expect(replacement.startStreaming()).toBe(true);
for (const slot of live) slot.release();
replacement.release();
});
});
@@ -36,4 +36,15 @@ describe('remote content image store', () => {
expect(String(captured?.input)).toBe(`https://sam-image.hided.net${pathname}`); expect(String(captured?.input)).toBe(`https://sam-image.hided.net${pathname}`);
expect(Object.values(headers)).not.toContain(secret); expect(Object.values(headers)).not.toContain(secret);
}); });
it('rejects oversized upload response JSON before trusting its path', async () => {
const filename = `${'d'.repeat(32)}.png`;
const store = new RemoteContentImageStore(
'https://image.example',
'https://image.example/public',
'synthetic-secret',
async () =>
new Response(JSON.stringify({ path: `uploads/core2026/${filename}`, extra: 'x'.repeat(20_000) }))
);
await expect(store.upload({ filename, contentType: 'image/png', body: Buffer.from('x') })).rejects.toThrow();
});
}); });
+23 -26
View File
@@ -69,8 +69,7 @@ const buildDb = (autorunLimit: number | null = null) => {
}, },
createMany: async ({ data }: GeneralTurnCreateManyArgs) => { createMany: async ({ data }: GeneralTurnCreateManyArgs) => {
const dataList = (Array.isArray(data) ? data : [data]) as Record<string, unknown>[]; const dataList = (Array.isArray(data) ? data : [data]) as Record<string, unknown>[];
const rows: GeneralTurnRow[] = dataList.map((row, index: number) => ({ const rows: GeneralTurnRow[] = dataList.map((row) => ({
id: index + 1,
generalId: row.generalId as number, generalId: row.generalId as number,
turnIdx: row.turnIdx as number, turnIdx: row.turnIdx as number,
actionCode: row.actionCode as string, actionCode: row.actionCode as string,
@@ -136,8 +135,7 @@ const buildDb = (autorunLimit: number | null = null) => {
}, },
createMany: async ({ data }: NationTurnCreateManyArgs) => { createMany: async ({ data }: NationTurnCreateManyArgs) => {
const dataList = (Array.isArray(data) ? data : [data]) as Record<string, unknown>[]; const dataList = (Array.isArray(data) ? data : [data]) as Record<string, unknown>[];
const rows: NationTurnRow[] = dataList.map((row, index: number) => ({ const rows: NationTurnRow[] = dataList.map((row) => ({
id: index + 1,
nationId: row.nationId as number, nationId: row.nationId as number,
officerLevel: row.officerLevel as number, officerLevel: row.officerLevel as number,
turnIdx: row.turnIdx as number, turnIdx: row.turnIdx as number,
@@ -203,26 +201,27 @@ const buildDb = (autorunLimit: number | null = null) => {
describe('reservedTurns', () => { describe('reservedTurns', () => {
it('loads multiple nation officer queues with two batched queries and preserves defaults', async () => { it('loads multiple nation officer queues with two batched queries and preserves defaults', async () => {
const findTurns = vi.fn(async () => [ const findTurns = vi.fn(
{ async () =>
id: 1, [
nationId: 4, {
officerLevel: 12, nationId: 4,
turnIdx: 1, officerLevel: 12,
actionCode: 'che_징병', turnIdx: 1,
arg: { amount: 100 }, actionCode: 'che_징병',
createdAt: new Date(), arg: { amount: 100 },
}, createdAt: new Date(),
{ },
id: 2, {
nationId: 4, nationId: 4,
officerLevel: 10, officerLevel: 10,
turnIdx: 0, turnIdx: 0,
actionCode: 'che_훈련', actionCode: 'che_훈련',
arg: {}, arg: {},
createdAt: new Date(), createdAt: new Date(),
}, },
] satisfies NationTurnRow[]); ] satisfies NationTurnRow[]
);
const findRevisions = vi.fn(async () => [ const findRevisions = vi.fn(async () => [
{ nationId: 4, officerLevel: 12, revision: 7, updatedAt: new Date() }, { nationId: 4, officerLevel: 12, revision: 7, updatedAt: new Date() },
]); ]);
@@ -411,7 +410,6 @@ describe('reservedTurns', () => {
nationRevisions.set('6:12', 2); nationRevisions.set('6:12', 2);
nationTurns.set('6:12', [ nationTurns.set('6:12', [
{ {
id: 1,
nationId: 6, nationId: 6,
officerLevel: 12, officerLevel: 12,
turnIdx: 0, turnIdx: 0,
@@ -420,7 +418,6 @@ describe('reservedTurns', () => {
createdAt: new Date(), createdAt: new Date(),
}, },
{ {
id: 2,
nationId: 6, nationId: 6,
officerLevel: 12, officerLevel: 12,
turnIdx: 1, turnIdx: 1,
+7 -10
View File
@@ -901,7 +901,6 @@ describe('appRouter', () => {
const general = buildGeneralRow({ id: 11, meta: { autorun_limit: 2408 } }); const general = buildGeneralRow({ id: 11, meta: { autorun_limit: 2408 } });
const generalTurns: GeneralTurnRow[] = [ const generalTurns: GeneralTurnRow[] = [
{ {
id: 1,
generalId: 11, generalId: 11,
turnIdx: 0, turnIdx: 0,
actionCode: 'che_화계', actionCode: 'che_화계',
@@ -922,7 +921,6 @@ describe('appRouter', () => {
const general = buildGeneralRow({ id: 12, nationId: 3, officerLevel: 5 }); const general = buildGeneralRow({ id: 12, nationId: 3, officerLevel: 5 });
const nationTurns: NationTurnRow[] = [ const nationTurns: NationTurnRow[] = [
{ {
id: 1,
nationId: 3, nationId: 3,
officerLevel: 5, officerLevel: 5,
turnIdx: 0, turnIdx: 0,
@@ -1008,7 +1006,6 @@ describe('appRouter', () => {
general, general,
generalTurns: [ generalTurns: [
{ {
id: 1,
generalId: general.id, generalId: general.id,
turnIdx: 0, turnIdx: 0,
actionCode: 'che_훈련', actionCode: 'che_훈련',
@@ -1016,7 +1013,6 @@ describe('appRouter', () => {
createdAt: new Date(), createdAt: new Date(),
}, },
{ {
id: 2,
generalId: general.id, generalId: general.id,
turnIdx: 1, turnIdx: 1,
actionCode: 'che_사기진작', actionCode: 'che_사기진작',
@@ -1110,7 +1106,10 @@ describe('appRouter', () => {
expect(nationWrites).toHaveLength(2); expect(nationWrites).toHaveLength(2);
}); });
it('stores Ref-sanitized nation command strings in the reserved queue', async () => { it.each([
[' <신-국># ', '&lt;신국&gt;'],
['풍년F&B', '풍년F&B'],
])('stores the sanitized nation name %s in the reserved queue as %s', async (input, stored) => {
const general = buildGeneralRow({ id: 19, nationId: 3, officerLevel: 12 }); const general = buildGeneralRow({ id: 19, nationId: 3, officerLevel: 12 });
const nationWrites: unknown[] = []; const nationWrites: unknown[] = [];
const caller = appRouter.createCaller( const caller = appRouter.createCaller(
@@ -1121,16 +1120,14 @@ describe('appRouter', () => {
generalId: general.id, generalId: general.id,
turnIndex: 0, turnIndex: 0,
action: 'che_국호변경', action: 'che_국호변경',
args: { nationName: ' <신-국># ' }, args: { nationName: input },
expectedRevision: 0, expectedRevision: 0,
}); });
expect(response.turns[0]?.args).toEqual({ nationName: '&lt;신국&gt;' }); expect(response.turns[0]?.args).toEqual({ nationName: stored });
expect(nationWrites).toHaveLength(1); expect(nationWrites).toHaveLength(1);
expect(nationWrites[0]).toMatchObject({ expect(nationWrites[0]).toMatchObject({
data: expect.arrayContaining([ data: expect.arrayContaining([expect.objectContaining({ turnIdx: 0, arg: { nationName: stored } })]),
expect.objectContaining({ turnIdx: 0, arg: { nationName: '&lt;신국&gt;' } }),
]),
}); });
}); });
+46
View File
@@ -0,0 +1,46 @@
import { Writable } from 'node:stream';
import fastify from 'fastify';
import { describe, expect, it } from 'vitest';
import { safeHttpLoggerOptions, safeHttpErrorHandler } from '@sammo-ts/common';
describe('HTTP log credential boundaries', () => {
it('logs static routes/status but no query, unmatched path, header, body, stack or cause secrets', async () => {
let output = '';
const stream = new Writable({
write(chunk, _encoding, done) {
output += String(chunk);
done();
},
});
const app = fastify({ logger: { ...safeHttpLoggerOptions, stream } });
app.setErrorHandler(safeHttpErrorHandler);
app.post('/events', async () => {
throw Object.assign(new Error('synthetic-error-secret', { cause: new Error('synthetic-cause-secret') }), {
code: 'P2002',
detail: 'synthetic-detail-secret',
});
});
try {
const address = await app.listen({ host: '127.0.0.1', port: 0 });
const response = await fetch(`${address}/events?token=synthetic-query-secret`, {
method: 'POST',
headers: { authorization: 'Bearer synthetic-header-secret', 'content-type': 'application/json' },
body: JSON.stringify({ password: 'synthetic-body-secret' }),
});
expect(await response.json()).toMatchObject({ message: 'Internal server error' });
await fetch(`${address}/synthetic-path-secret`);
const logs = output
.trim()
.split('\n')
.map((line) => JSON.parse(line));
expect(logs.some((entry) => entry.req?.url === '/events')).toBe(true);
expect(logs.some((entry) => entry.res?.statusCode === 500)).toBe(true);
expect(logs.some((entry) => entry.err?.code === 'P2002')).toBe(true);
for (const field of ['query', 'header', 'body', 'error', 'cause', 'detail', 'path']) {
expect(output).not.toContain(`synthetic-${field}-secret`);
}
} finally {
await app.close();
}
});
});
@@ -1,3 +1,5 @@
import sharp from 'sharp';
import { buildGameEventChannel } from '@sammo-ts/common';
import { createHash } from 'node:crypto'; import { createHash } from 'node:crypto';
import { projectCurrentGeneral } from '../src/router/playAudit/projection.js'; import { projectCurrentGeneral } from '../src/router/playAudit/projection.js';
import { asRecord } from '@sammo-ts/common'; import { asRecord } from '@sammo-ts/common';
@@ -11,6 +13,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { encryptGameSessionToken, type GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken'; import { encryptGameSessionToken, type GameSessionTokenPayload } from '@sammo-ts/common/auth/gameToken';
import { import {
createGamePostgresConnector, createGamePostgresConnector,
encodeAuditDecisionSteps,
GamePrisma, GamePrisma,
hashAuditDiplomacyDocument, hashAuditDiplomacyDocument,
createRedisConnector, createRedisConnector,
@@ -68,6 +71,7 @@ const envKeys = [
'GATEWAY_REDIS_PREFIX', 'GATEWAY_REDIS_PREFIX',
'GATEWAY_INTERNAL_API_URL', 'GATEWAY_INTERNAL_API_URL',
'GAME_UPLOAD_DIR', 'GAME_UPLOAD_DIR',
'API_ALLOWED_ORIGINS',
] as const; ] as const;
const originalEnv = new Map(envKeys.map((key) => [key, process.env[key]])); const originalEnv = new Map(envKeys.map((key) => [key, process.env[key]]));
@@ -172,7 +176,7 @@ const deleteProfileRedisKeys = async (): Promise<void> => {
if (!redis) { if (!redis) {
return; return;
} }
for (const pattern of [`sammo:game:*:${profileName}:*`, `sammo:${profileName}:*`]) { for (const pattern of [`sammo:game:*:${profileName}:*`, `sammo:${profileName}:*`, `${redisPrefix}:*`]) {
for await (const keys of redis.client.scanIterator({ for await (const keys of redis.client.scanIterator({
MATCH: pattern, MATCH: pattern,
COUNT: 100, COUNT: 100,
@@ -416,7 +420,7 @@ const readAccessTelemetryState = async () => ({
const readRealtimeRedisState = async (): Promise<Array<[string, string | null]>> => { const readRealtimeRedisState = async (): Promise<Array<[string, string | null]>> => {
if (!redis) return []; if (!redis) return [];
const keys = new Set<string>(); const keys = new Set<string>();
for (const pattern of [`sammo:game:*:${profileName}:*`, `sammo:${profileName}:*`]) { for (const pattern of [`sammo:game:*:${profileName}:*`, `sammo:${profileName}:*`, `${redisPrefix}:*`]) {
for await (const batch of redis.client.scanIterator({ MATCH: pattern, COUNT: 100 })) { for await (const batch of redis.client.scanIterator({ MATCH: pattern, COUNT: 100 })) {
for (const key of batch) keys.add(key); for (const key of batch) keys.add(key);
} }
@@ -640,6 +644,7 @@ integration('game API security over HTTP transport', () => {
process.env.GATEWAY_REDIS_PREFIX = redisPrefix; process.env.GATEWAY_REDIS_PREFIX = redisPrefix;
process.env.GATEWAY_INTERNAL_API_URL = await listenGatewayStatusStub(); process.env.GATEWAY_INTERNAL_API_URL = await listenGatewayStatusStub();
process.env.GAME_UPLOAD_DIR = uploadDir; process.env.GAME_UPLOAD_DIR = uploadDir;
process.env.API_ALLOWED_ORIGINS = 'https://sam.hided.net';
const connector = createGamePostgresConnector({ url: dedicatedTarget.databaseUrl }); const connector = createGamePostgresConnector({ url: dedicatedTarget.databaseUrl });
await connector.connect(); await connector.connect();
@@ -742,6 +747,8 @@ integration('game API security over HTTP transport', () => {
await redis.connect(); await redis.connect();
accessTokenStore = new RedisAccessTokenStore(redis.client, profileName); accessTokenStore = new RedisAccessTokenStore(redis.client, profileName);
// 이 격리 fixture는 이미 전환된 Gateway namespace를 합성 cutoff 0으로 준비한다.
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:baseline`, '0', { NX: true });
server = await createGameApiServer(); server = await createGameApiServer();
baseUrl = await server.app.listen({ baseUrl = await server.app.listen({
host: server.config.host, host: server.config.host,
@@ -818,6 +825,28 @@ integration('game API security over HTTP transport', () => {
} }
}); });
it('image upload enforces real HTTP authentication, officer authority and decoded pixel limits', async () => {
const input = { dataUrl: 'data:image/png;base64,AA==' };
const unauthenticated = await requestTrpc('board.uploadImage', { method: 'POST', input });
expect(unauthenticated.response.status).toBe(401);
const ordinary = await createAccessToken('image-ordinary', {}, ordinaryUserId);
const denied = await requestTrpc('board.uploadImage', { method: 'POST', input, accessToken: ordinary });
expect(denied.response.status).toBe(403);
const officer = await createAccessToken('image-officer', {});
const malformed = await requestTrpc('board.uploadImage', { method: 'POST', input, accessToken: officer });
expect(malformed.response.status).toBe(400);
const png = await sharp({ create: { width: 4096, height: 4097, channels: 3, background: '#224466' } })
.png()
.toBuffer();
const oversized = await requestTrpc('board.uploadImage', {
method: 'POST',
accessToken: officer,
input: { dataUrl: `data:image/png;base64,${png.toString('base64')}` },
});
expect(oversized.response.status).toBe(400);
expect(oversized.body).toMatchObject({ error: { data: { code: 'BAD_REQUEST' } } });
});
it('accepts an authenticated query from a POST JSON body', async () => { it('accepts an authenticated query from a POST JSON body', async () => {
const accessToken = await createAccessToken('json-query-body', {}); const accessToken = await createAccessToken('json-query-body', {});
const general = await requestTrpc('general.me', { const general = await requestTrpc('general.me', {
@@ -940,7 +969,7 @@ integration('game API security over HTTP transport', () => {
const telemetryAfter = await readAccessTelemetryState(); const telemetryAfter = await readAccessTelemetryState();
expect(telemetryAfter.periods).toEqual([ expect(telemetryAfter.periods).toEqual([
{ {
id: expect.any(Number), id: expect.any(BigInt),
worldStateId: fixtureWorldId, worldStateId: fixtureWorldId,
year: 190, year: 190,
month: 1, month: 1,
@@ -963,7 +992,7 @@ integration('game API security over HTTP transport', () => {
]); ]);
expect(telemetryAfter.accessLogs).toEqual([ expect(telemetryAfter.accessLogs).toEqual([
{ {
id: expect.any(Number), id: expect.any(BigInt),
generalId, generalId,
userId, userId,
lastRefresh: trafficPeriod.lastRefresh, lastRefresh: trafficPeriod.lastRefresh,
@@ -1519,8 +1548,8 @@ integration('game API security over HTTP transport', () => {
arg, arg,
})) }))
).toEqual(databaseAfter.generalTurns); ).toEqual(databaseAfter.generalTurns);
expect(new Set(committedGeneralTurns.map(({ id }) => id)).size).toBe(30); expect(new Set(committedGeneralTurns.map(({ generalId, turnIdx }) => `${generalId}:${turnIdx}`)).size).toBe(30);
expect(committedGeneralTurns.every(({ id, createdAt }) => id > 0 && createdAt instanceof Date)).toBe(true); expect(committedGeneralTurns.every(({ createdAt }) => createdAt instanceof Date)).toBe(true);
expect(allowedTablesAfter.generalTurnRevisions.filter((row) => row.generalId !== generalId)).toEqual( expect(allowedTablesAfter.generalTurnRevisions.filter((row) => row.generalId !== generalId)).toEqual(
allowedTablesBefore.generalTurnRevisions.filter((row) => row.generalId !== generalId) allowedTablesBefore.generalTurnRevisions.filter((row) => row.generalId !== generalId)
); );
@@ -1538,7 +1567,7 @@ integration('game API security over HTTP transport', () => {
); );
expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === generalId)).toEqual([ expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === generalId)).toEqual([
{ {
id: expect.any(Number), id: expect.any(BigInt),
generalId, generalId,
userId, userId,
lastRefresh: null, lastRefresh: null,
@@ -1704,8 +1733,14 @@ integration('game API security over HTTP transport', () => {
arg, arg,
})) }))
).toEqual(expectedTurns); ).toEqual(expectedTurns);
expect(new Set(committedNationTurns.map(({ id }) => id)).size).toBe(12); expect(
expect(committedNationTurns.every(({ id, createdAt }) => id > 0 && createdAt instanceof Date)).toBe(true); new Set(
committedNationTurns.map(
({ nationId, officerLevel, turnIdx }) => `${nationId}:${officerLevel}:${turnIdx}`
)
).size
).toBe(12);
expect(committedNationTurns.every(({ createdAt }) => createdAt instanceof Date)).toBe(true);
expect( expect(
allowedTablesAfter.nationTurnRevisions.filter( allowedTablesAfter.nationTurnRevisions.filter(
(row) => row.nationId !== ownerNationId || row.officerLevel !== 5 (row) => row.nationId !== ownerNationId || row.officerLevel !== 5
@@ -1734,7 +1769,7 @@ integration('game API security over HTTP transport', () => {
); );
expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === sameNationGeneralId)).toEqual([ expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === sameNationGeneralId)).toEqual([
{ {
id: expect.any(Number), id: expect.any(BigInt),
generalId: sameNationGeneralId, generalId: sameNationGeneralId,
userId: sameNationUserId, userId: sameNationUserId,
lastRefresh: null, lastRefresh: null,
@@ -1851,8 +1886,14 @@ integration('game API security over HTTP transport', () => {
arg, arg,
})) }))
).toEqual(committed.nationTurns); ).toEqual(committed.nationTurns);
expect(new Set(committedNationTurns.map(({ id }) => id)).size).toBe(12); expect(
expect(committedNationTurns.every(({ id, createdAt }) => id > 0 && createdAt instanceof Date)).toBe(true); new Set(
committedNationTurns.map(
({ nationId, officerLevel, turnIdx }) => `${nationId}:${officerLevel}:${turnIdx}`
)
).size
).toBe(12);
expect(committedNationTurns.every(({ createdAt }) => createdAt instanceof Date)).toBe(true);
expect( expect(
allowedTablesAfter.nationTurnRevisions.filter( allowedTablesAfter.nationTurnRevisions.filter(
(row) => row.nationId !== ownerNationId || row.officerLevel !== 12 (row) => row.nationId !== ownerNationId || row.officerLevel !== 12
@@ -1881,7 +1922,7 @@ integration('game API security over HTTP transport', () => {
); );
expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === generalId)).toEqual([ expect(allowedTablesAfter.generalAccessLogs.filter((row) => row.generalId === generalId)).toEqual([
{ {
id: expect.any(Number), id: expect.any(BigInt),
generalId, generalId,
userId, userId,
lastRefresh: null, lastRefresh: null,
@@ -2317,9 +2358,53 @@ integration('game API security over HTTP transport', () => {
{ {
decisionId: decisionIds[0]!, decisionId: decisionIds[0]!,
ordinal: 0, ordinal: 0,
steps: Array.from({ length: 128 }, (_, sequence) => sequence === 127 steps: encodeAuditDecisionSteps(
? { ...step, sequence, kind: 'DECISION_START', reservedAction: '휴식', effectivePolicy: {"schemaVersion":1,"general":{"priority":["징병"],"flags":{"징병":true,"출병":false}},"nation":{"priority":["천도"],"flags":{"천도":true},"values":{"reqNationGold":4321,"reqNationRice":100,"reqHumanWarUrgentGold":100,"reqHumanWarUrgentRice":100,"reqHumanWarRecommandGold":100,"reqHumanWarRecommandRice":100,"reqHumanDevelGold":100,"reqHumanDevelRice":100,"reqNpcWarGold":100,"reqNpcWarRice":100,"reqNpcDevelGold":100,"reqNpcDevelRice":100,"minimumResourceActionAmount":100,"maximumResourceActionAmount":100,"minNpcWarLeadership":100,"minWarCrew":100,"minNpcRecruitCityPopulation":100,"safeRecruitCityPopulationRatio":100,"properWarTrainAtmos":100,"cureThreshold":100},"combatForce":{"1":[2,3]},"supportForce":[4],"developForce":[5],"secret":"decision-secret"},"secret":"decision-secret"} } Array.from({ length: 128 }, (_, sequence) =>
: ({ ...step, sequence })), sequence === 127
? {
...step,
sequence,
kind: 'DECISION_START',
reservedAction: '휴식',
effectivePolicy: {
schemaVersion: 1,
general: { priority: ['징병'], flags: { 징병: true, 출병: false } },
nation: {
priority: ['천도'],
flags: { 천도: true },
values: {
reqNationGold: 4321,
reqNationRice: 100,
reqHumanWarUrgentGold: 100,
reqHumanWarUrgentRice: 100,
reqHumanWarRecommandGold: 100,
reqHumanWarRecommandRice: 100,
reqHumanDevelGold: 100,
reqHumanDevelRice: 100,
reqNpcWarGold: 100,
reqNpcWarRice: 100,
reqNpcDevelGold: 100,
reqNpcDevelRice: 100,
minimumResourceActionAmount: 100,
maximumResourceActionAmount: 100,
minNpcWarLeadership: 100,
minWarCrew: 100,
minNpcRecruitCityPopulation: 100,
safeRecruitCityPopulationRatio: 100,
properWarTrainAtmos: 100,
cureThreshold: 100,
},
combatForce: { '1': [2, 3] },
supportForce: [4],
developForce: [5],
secret: 'decision-secret',
},
secret: 'decision-secret',
},
}
: { ...step, sequence }
)
),
}, },
{ {
decisionId: decisionIds[0]!, decisionId: decisionIds[0]!,
@@ -2352,6 +2437,33 @@ integration('game API security over HTTP transport', () => {
], ],
}); });
const decisionInput = { generalId: decisionGeneral, month: { year: 190, month: 1 }, limit: 1 }; const decisionInput = { generalId: decisionGeneral, month: { year: 190, month: 1 }, limit: 1 };
// 월 경계 뒤 장수의 첫 턴 전에도 직전 결정에 도달할 수 있어야 한다.
expect((await get('decisionHistory', admin, { generalId: decisionGeneral })).body).toMatchObject({
result: {
data: {
selection: 'LATEST',
month: { year: 190, month: 1 },
currentMonth: { year: 190, month: 2 },
items: expect.arrayContaining([expect.objectContaining({ id: decisionIds[0] })]),
},
},
});
expect(
(await get('decisionHistory', admin, { generalId: decisionGeneral, month: { year: 190, month: 2 } }))
.body
).toMatchObject({
result: { data: { selection: 'MONTH', month: { year: 190, month: 2 }, items: [] } },
});
expect(
(await get('decisionHistory', admin, { generalId: decisionGeneral, phase: 'nation' })).body
).toMatchObject({
result: {
data: { selection: 'LATEST', month: { year: 190, month: 1 }, items: [{ id: decisionIds[1] }] },
},
});
expect((await get('decisionHistory', admin, { generalId: 2147483647 })).body).toMatchObject({
result: { data: { selection: 'LATEST', month: { year: 190, month: 2 }, items: [] } },
});
expect((await get('decisionHistory', undefined, decisionInput)).status).toBe(401); expect((await get('decisionHistory', undefined, decisionInput)).status).toBe(401);
expect((await get('decisionHistory', await token(['admin']), decisionInput)).status).toBe(403); expect((await get('decisionHistory', await token(['admin']), decisionInput)).status).toBe(403);
const decisionList = await get('decisionHistory', admin, decisionInput); const decisionList = await get('decisionHistory', admin, decisionInput);
@@ -2768,7 +2880,7 @@ integration('game API security over HTTP transport', () => {
result: { result: {
data: { data: {
coverage: 'IDENTIFIED_LOGS_ONLY', coverage: 'IDENTIFIED_LOGS_ONLY',
items: [{ id: ownLogs[index]!.id }], items: [{ id: Number(ownLogs[index]!.id) }],
nextCursor: null, nextCursor: null,
}, },
}, },
@@ -2777,17 +2889,19 @@ integration('game API security over HTTP transport', () => {
} }
expect( expect(
(await get('generalLogs', admin, { generalId: logGeneralId, type: 'generalHistory', limit: 1 })).body (await get('generalLogs', admin, { generalId: logGeneralId, type: 'generalHistory', limit: 1 })).body
).toMatchObject({ result: { data: { items: [{ id: latest.id }], nextCursor: latest.id } } }); ).toMatchObject({
result: { data: { items: [{ id: Number(latest.id) }], nextCursor: Number(latest.id) } },
});
expect( expect(
( (
await get('generalLogs', admin, { await get('generalLogs', admin, {
generalId: logGeneralId, generalId: logGeneralId,
type: 'generalHistory', type: 'generalHistory',
limit: 1, limit: 1,
cursor: latest.id, cursor: Number(latest.id),
}) })
).body ).body
).toMatchObject({ result: { data: { items: [{ id: ownLogs[0]!.id }], nextCursor: null } } }); ).toMatchObject({ result: { data: { items: [{ id: Number(ownLogs[0]!.id) }], nextCursor: null } } });
for (const invalid of [ for (const invalid of [
{ limit: 201 }, { limit: 201 },
{ cursor: 0 }, { cursor: 0 },
@@ -2852,6 +2966,132 @@ integration('game API security over HTTP transport', () => {
expect((await get('capabilities', admin)).body).toMatchObject({ expect((await get('capabilities', admin)).body).toMatchObject({
result: { data: { read: true, accounts: false } }, result: { data: { read: true, accounts: false } },
}); });
// 현재/과거 정렬은 전체 조건 안에서 수행하고 동점은 ID로 안정적으로 넘긴다.
const originalSortRows = await db.general.findMany({
where: { id: { in: [generalId, sameNationGeneralId] } },
select: { id: true, gold: true },
});
await db.general.updateMany({
where: { id: { in: [generalId, sameNationGeneralId] } },
data: { gold: 54321 },
});
const sortedInput = { nationId: ownerNationId, sort: 'gold', order: 'desc', limit: 1 };
expect((await get('generals', admin, sortedInput)).body).toMatchObject({
result: {
data: {
items: [{ id: generalId, gold: 54321 }],
nextCursor: { id: generalId, value: 54321 },
},
},
});
expect(
(await get('generals', admin, { ...sortedInput, cursor: { id: generalId, value: 54321 } })).body
).toMatchObject({ result: { data: { items: [{ id: sameNationGeneralId }] } } });
for (const row of originalSortRows)
await db.general.update({ where: { id: row.id }, data: { gold: row.gold } });
for (const sort of [
'gold',
'rice',
'crew',
'train',
'atmos',
'leadership',
'strength',
'intelligence',
'experience',
'dedication',
'dex1',
'dex2',
'dex3',
'dex4',
'dex5',
]) {
for (const at of [undefined, { year: 190, month: 1 }]) {
expect((await get('generals', admin, { sort, at })).status).toBe(200);
}
}
const oldGeneral = await db.playAuditGeneral.findUniqueOrThrow({
where: { sampleId_generalId: { sampleId, generalId } },
});
await db.playAuditGeneral.create({
data: {
sampleId,
generalId: sameNationGeneralId,
nationId: oldGeneral.nationId,
cityId: oldGeneral.cityId,
npcState: oldGeneral.npcState,
data: { ...asRecord(oldGeneral.data), id: sameNationGeneralId, name: '동점과거장수' },
},
});
const historicalValue = past.gold;
expect(
(await get('generals', admin, { sort: 'gold', order: 'desc', at: { year: 190, month: 1 }, limit: 1 }))
.body
).toMatchObject({
result: { data: { items: [{ id: generalId }], nextCursor: { id: generalId, value: historicalValue } } },
});
expect(
(
await get('generals', admin, {
sort: 'gold',
order: 'desc',
at: { year: 190, month: 1 },
limit: 1,
cursor: { id: generalId, value: historicalValue },
})
).body
).toMatchObject({ result: { data: { items: [{ id: sameNationGeneralId }], nextCursor: null } } });
await db.playAuditGeneral.delete({
where: { sampleId_generalId: { sampleId, generalId: sameNationGeneralId } },
});
expect((await get('generals', admin, { sort: 'gold', cursor: 1 })).status).toBe(400);
expect((await get('generals', admin, { sort: 'gold; DROP TABLE general' })).status).toBe(400);
expect(
(await get('generals', admin, { sort: 'gold', at: { year: 190, month: 1 }, name: '%' })).body
).toMatchObject({ result: { data: { items: [] } } });
for (const accessToken of [
undefined,
await token(['admin']),
await token(['admin.playAudit.read:other:default']),
]) {
expect((await get('cityMap', accessToken, {})).status).toBe(accessToken ? 403 : 401);
expect((await get('generals', accessToken, { sort: 'crew' })).status).toBe(accessToken ? 403 : 401);
}
const citySource = await db.playAuditCity.findFirstOrThrow({ where: { sampleId }, select: { data: true } });
await db.playAuditCity.create({
data: {
sampleId,
cityId: 1,
nationId: ownerNationId,
data: { ...asRecord(citySource.data), id: 1, name: '지도과거도시' },
},
});
const map = await get('cityMap', admin, { at: { year: 190, month: 1 } });
expect(map.status).toBe(200);
expect(map.body).toMatchObject({
result: {
data: {
collected: true,
map: {
year: 190,
month: 1,
cityList: expect.arrayContaining([[1, 4, 0, ownerNationId, expect.any(Number), 1]]),
},
layout: {
cityList: expect.arrayContaining([
expect.objectContaining({ id: 1, name: '지도과거도시' }),
]),
},
},
},
});
expect(JSON.stringify(map.body)).not.toContain('must-not-expose');
await db.playAuditCity.delete({ where: { sampleId_cityId: { sampleId, cityId: 1 } } });
expect((await get('cityMap', admin, { at: { year: 190, month: 2 } })).body).toMatchObject({
result: { data: { collected: false, map: null } },
});
expect((await get('cityMap', admin, {})).status).toBe(200);
expect((await get('cityMap', admin, { at: { year: 9999, month: 1 } })).status).toBe(400);
const first = await get('generals', admin, { limit: 1 }); const first = await get('generals', admin, { limit: 1 });
expect(first.status).toBe(200); expect(first.status).toBe(200);
expect(first.body).toMatchObject({ expect(first.body).toMatchObject({
@@ -3119,6 +3359,39 @@ integration('game API security over HTTP transport', () => {
}, },
}, },
}); });
await db.playAuditNation.update({
where: { sampleId_nationId: { sampleId: `${seasonId}:190:6`, nationId: ownerNationId } },
data: {
data: {
...asRecord(finalNation.data),
populations: {
human: { ...population, count: 2 },
npc: { ...population, count: 1 },
troopNpc: population,
},
},
},
});
await db.playAuditGeneral.createMany({
data: [1, 2].map((id) => ({
sampleId: `${seasonId}:190:6`,
generalId: id,
nationId: ownerNationId,
cityId: 0,
npcState: 0,
data: { crew: id * 1200 },
})),
});
await db.playAuditGeneral.create({
data: {
sampleId: `${seasonId}:190:6`,
generalId: 3,
nationId: ownerNationId,
cityId: 0,
npcState: 2,
data: { crew: 'invalid' },
},
});
const series = await get('nationSeries', admin, { const series = await get('nationSeries', admin, {
nationId: ownerNationId, nationId: ownerNationId,
from: { year: 190, month: 1 }, from: { year: 190, month: 1 },
@@ -3134,7 +3407,10 @@ integration('game API security over HTTP transport', () => {
year: 190, year: 190,
month: 1, month: 1,
complete: true, complete: true,
stock: { gold: 600 }, stock: {
gold: 600,
populations: { human: { crew: 3600 }, npc: { crew: null }, troopNpc: { crew: 0 } },
},
flows: { incomeGold: 21, incomeRice: 0 }, flows: { incomeGold: 21, incomeRice: 0 },
}, },
], ],
@@ -3166,6 +3442,59 @@ integration('game API security over HTTP transport', () => {
result: { data: { items: [{ complete: false, stock: null, flows: { incomeGold: null } }] } }, result: { data: { items: [{ complete: false, stock: null, flows: { incomeGold: null } }] } },
}); });
// 정산 commit 직후, 아직 월말 표본이 없는 상태를 HTTP로 확인한다.
for (const [settlementMonth, resource] of [
[1, 'gold'],
[7, 'rice'],
] as const) {
await db.worldState.update({
where: { id: fixtureWorldId },
data: {
currentYear: 191,
currentMonth: settlementMonth,
meta: {
serverId: seasonId,
scenarioMeta: { startYear: 190 },
playAuditFlows: {
year: 191,
month: settlementMonth,
complete: true,
entries: {
[`${ownerNationId}:${resource}`]: {
nationId: ownerNationId,
resource,
income: 8765.5,
paid: 4321,
},
},
},
},
},
});
const input = {
nationId: ownerNationId,
from: { year: 191, month: settlementMonth },
to: { year: 191, month: settlementMonth },
};
expect((await get('nationSeries', admin, input)).body).toMatchObject({
result: {
data: {
currentSettlement: {
year: 191,
month: settlementMonth,
[resource]: { income: 8765.5, paid: 4321 },
},
items: [{ stock: null, complete: false }],
},
},
});
expect((await get('nationSeries', undefined, input)).status).toBe(401);
expect((await get('nationSeries', await token(['admin']), input)).status).toBe(403);
expect(
(await get('nationSeries', await token(['admin.playAudit.read:other:default']), input)).status
).toBe(403);
}
// A synchronized opening may start before the scenario's gameplay year. // A synchronized opening may start before the scenario's gameplay year.
await db.worldState.update({ await db.worldState.update({
where: { id: fixtureWorldId }, where: { id: fixtureWorldId },
@@ -3288,6 +3617,414 @@ integration('game API security over HTTP transport', () => {
} }
}); });
const openRealtime = async (accessToken: string) => {
const abort = new AbortController();
const response = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${accessToken}` },
signal: abort.signal,
});
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toBe('text/event-stream');
const reader = response.body!.getReader();
const ready = await reader.read();
expect(new TextDecoder().decode(ready.value)).toContain('event: ready');
return { abort, reader };
};
it('SSE limits simultaneous openings for one authenticated user', async () => {
const actor = `security-http-sse-concurrent-${process.pid}`;
const token = await createAccessToken('sse-concurrent', {}, actor);
const aborts = Array.from({ length: 10 }, () => new AbortController());
try {
const responses = await Promise.all(
aborts.map((abort) =>
fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${token}` },
signal: abort.signal,
})
)
);
expect(responses.filter((response) => response.status === 200)).toHaveLength(8);
expect(responses.filter((response) => response.status === 429)).toHaveLength(2);
for (const response of responses.filter((item) => item.status === 429)) {
expect(response.headers.get('retry-after')).toBe('15');
expect(response.headers.get('content-type')).not.toBe('text/event-stream');
expect(await response.json()).toEqual({ ok: false, error: 'realtime_connection_limit' });
}
} finally {
for (const abort of aborts) abort.abort();
await accessTokenStore.revoke(token);
}
});
it('SSE limits the same user across tokens and scopes and recovers after disconnect', async () => {
const actor = `security-http-sse-shared-${process.pid}`;
const token = await createAccessToken('sse-shared-a', {}, actor);
const otherToken = await createAccessToken('sse-shared-b', {}, actor);
const otherActorToken = await createAccessToken('sse-independent', {}, `${actor}-other`);
const streams: Awaited<ReturnType<typeof openRealtime>>[] = [];
const replacementAbort = new AbortController();
try {
for (let index = 0; index < 8; index += 1) streams.push(await openRealtime(token));
const rejected = await fetch(`${baseUrl}${server!.config.eventsPath}?scope=tournament`, {
headers: { authorization: `Bearer ${otherToken}` },
});
expect(rejected.status).toBe(429);
expect(await rejected.json()).toEqual({ ok: false, error: 'realtime_connection_limit' });
streams.push(await openRealtime(otherActorToken));
await streams[0]!.reader.cancel();
streams[0]!.abort.abort();
let replacement: Response | undefined;
await expect
.poll(async () => {
const response = await fetch(`${baseUrl}${server!.config.eventsPath}?scope=tournament`, {
headers: { authorization: `Bearer ${otherToken}` },
signal: replacementAbort.signal,
});
if (response.status === 200) replacement = response;
else await response.arrayBuffer();
return response.status;
})
.toBe(200);
const ready = await replacement!.body!.getReader().read();
expect(new TextDecoder().decode(ready.value)).toContain('event: ready');
} finally {
replacementAbort.abort();
for (const stream of streams) stream.abort.abort();
await Promise.all([token, otherToken, otherActorToken].map((value) => accessTokenStore.revoke(value)));
}
});
it('SSE bounds pending openings after disconnect and backend deadline until real SQL work settles', async () => {
if (!/^[A-Za-z0-9_]+$/.test(profileId)) throw new Error('Invalid isolated schema identifier');
const actor = `security-http-sse-pending-${process.pid}`;
const tokens = await Promise.all(
Array.from({ length: 33 }, (_, index) => createAccessToken(`sse-pending-${index}`, {}, `${actor}-${index}`))
);
const aborts = Array.from({ length: 33 }, () => new AbortController());
let unlock!: () => void;
const release = new Promise<void>((resolve) => {
unlock = resolve;
});
let ready!: (tx: GamePrisma.TransactionClient) => void;
let lockError!: (error: unknown) => void;
const locked = new Promise<GamePrisma.TransactionClient>((resolve, reject) => {
ready = resolve;
lockError = reject;
});
// Only the guarded, dedicated fixture schema is locked. No runtime or Ref table is touched.
const locking = db.$transaction(
async (tx) => {
await tx.$executeRawUnsafe(`LOCK TABLE "${profileId}"."general" IN ACCESS EXCLUSIVE MODE`);
ready(tx);
await release;
},
{ timeout: 20_000 }
);
void locking.catch(lockError);
try {
const tx = await locked;
const first = fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${tokens[0]!}` },
signal: aborts[0]!.signal,
}).then(
() => false,
() => true
);
await expect
.poll(
async () => {
const rows = await tx.$queryRaw<Array<{ count: number }>>`
SELECT COUNT(*)::int AS count FROM pg_locks
WHERE NOT granted AND relation = ${`${profileId}.general`}::regclass
AND database = (SELECT oid FROM pg_database WHERE datname = current_database())
`;
return rows[0]!.count;
},
{ timeout: 3000 }
)
.toBeGreaterThan(0);
const startedAt = Date.now();
const waiting = tokens.slice(1, 32).map((token, index) =>
fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${token}` },
signal: aborts[index + 1]!.signal,
})
);
aborts[0]!.abort();
expect(await first).toBe(true);
const responses = await Promise.all(waiting);
expect(Date.now() - startedAt).toBeGreaterThanOrEqual(4500);
expect(Date.now() - startedAt).toBeLessThan(8500);
for (const response of responses) {
expect(response.status).toBe(503);
expect(await response.json()).toEqual({ ok: false, error: 'realtime_unavailable' });
}
const refusedAt = Date.now();
const refused = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${tokens[32]!}` },
signal: aborts[32]!.signal,
});
expect(refused.status).toBe(503);
expect(refused.headers.get('retry-after')).toBe('15');
expect(Date.now() - refusedAt).toBeLessThan(2000);
await refused.arrayBuffer();
unlock();
await locking;
let recovered: Response | undefined;
await expect
.poll(async () => {
const response = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${tokens[32]!}` },
signal: aborts[32]!.signal,
});
if (response.status === 200) recovered = response;
else await response.arrayBuffer();
return response.status;
})
.toBe(200);
const frame = await recovered!.body!.getReader().read();
expect(new TextDecoder().decode(frame.value)).toContain('event: ready');
} finally {
unlock();
for (const abort of aborts) abort.abort();
await locking;
await Promise.all(tokens.map((token) => accessTokenStore.revoke(token)));
}
}, 15_000);
it('rejects an untrusted browser Origin before an authenticated logout mutation', async () => {
const token = await createAccessToken('origin-mutation', {});
const response = await fetch(`${baseUrl}/trpc/auth.logout`, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
origin: 'https://untrusted.example',
'content-type': 'application/json',
},
body: JSON.stringify({}),
});
expect(response.status).toBe(403);
expect(response.headers.get('access-control-allow-origin')).toBeNull();
expect(await accessTokenStore.get(token)).not.toBeNull();
for (const origin of ['https://sam.hided.net', 'https://untrusted.example', 'null']) {
const preflight = await fetch(`${baseUrl}/trpc/auth.logout`, {
method: 'OPTIONS',
headers: {
origin,
'access-control-request-method': 'POST',
'access-control-request-headers': 'authorization,content-type',
},
});
expect(preflight.status).toBe(origin === 'https://sam.hided.net' ? 204 : 403);
expect(preflight.headers.get('access-control-allow-origin')).toBe(
origin === 'https://sam.hided.net' ? origin : null
);
}
await accessTokenStore.revoke(token);
});
it('rejects a same-namespace foreign-profile token over tRPC before any DB mutation', async () => {
const foreign = await accessTokenStore.create({
...buildPayload('trpc-foreign', {}),
profile: 'other:default',
});
if (!foreign) throw new Error('Foreign token fixture failed');
const before = await readReservedMutationState();
try {
const response = await fetch(
`${baseUrl}/trpc/turns.reserved.getGeneral?input=${encodeURIComponent(JSON.stringify({ generalId }))}`,
{
headers: { authorization: `Bearer ${foreign.accessToken}` },
}
);
expect(response.status).toBe(401);
expect(await readReservedMutationState()).toEqual(before);
} finally {
await accessTokenStore.revoke(foreign.accessToken);
}
});
it('SSE accepts header auth and rejects query or cross-profile credentials', async () => {
const token = await createAccessToken('sse-query', {});
const queryOnly = await fetch(`${baseUrl}${server!.config.eventsPath}?token=${encodeURIComponent(token)}`);
expect(queryOnly.status).toBe(401);
const foreign = await accessTokenStore.create({ ...buildPayload('sse-foreign', {}), profile: 'other:default' });
if (!foreign) throw new Error('SSE foreign token fixture failed');
try {
const response = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${foreign.accessToken}` },
});
expect(response.status).toBe(401);
const stream = await openRealtime(token);
stream.abort.abort();
} finally {
await accessTokenStore.revoke(token);
await accessTokenStore.revoke(foreign.accessToken);
}
});
it.each([
{ label: 'ban', sanctions: { bannedUntil: '2099-01-01T00:00:00.000Z' } },
{ label: 'suspension', sanctions: { suspendedUntil: '2099-01-01T00:00:00.000Z' } },
{
label: 'profile game restriction',
sanctions: { serverRestrictions: { [profileName]: { blockedFeatures: ['game'] } } },
},
])('SSE rejects $label before opening a stream', async ({ label, sanctions }) => {
const token = await createAccessToken(`sse-${label}`, sanctions);
try {
const response = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${token}` },
});
expect(response.status).toBe(401);
expect(response.headers.get('content-type')).not.toBe('text/event-stream');
} finally {
await accessTokenStore.revoke(token);
}
});
it('SSE closes on idle expiry without forwarding another frame', async () => {
const auth = { ...buildPayload('sse-expiry', {}), expiresAt: new Date(Date.now() + 3500).toISOString() };
const created = await accessTokenStore.create(auth);
if (!created) throw new Error('SSE expiry token fixture failed');
const stream = await openRealtime(created.accessToken);
try {
expect(await stream.reader.read()).toMatchObject({ done: true });
expect((await requestTrpc('general.me', { accessToken: created.accessToken })).response.status).toBe(401);
} finally {
stream.abort.abort();
await accessTokenStore.revoke(created.accessToken);
}
});
it('SSE rechecks a revoked token before forwarding a relevant event or refresh grant', async () => {
const accessToken = await createAccessToken('sse-revoke', {});
const stream = await openRealtime(accessToken);
const channel = server!.config.profileName;
try {
const event = JSON.stringify({ type: 'tournamentChanged' });
await redis!.client.publish(buildGameEventChannel(channel), event);
const valid = await stream.reader.read();
expect(new TextDecoder().decode(valid.value)).toContain('refreshGrant');
expect(await accessTokenStore.revoke(accessToken)).toBe(true);
await redis!.client.publish(buildGameEventChannel(channel), event);
expect(await stream.reader.read()).toMatchObject({ done: true });
} finally {
stream.abort.abort();
await accessTokenStore.revoke(accessToken);
}
});
it('SSE closes an idle flushed user immediately without waiting for heartbeat', async () => {
const sseUser = `security-http-sse-flush-${process.pid}`;
const created = await accessTokenStore.create(buildPayload('sse-flush', {}, sseUser));
if (!created) throw new Error('SSE flush fixture failed');
const stream = await openRealtime(created.accessToken);
try {
await redis!.client.publish(
`${redisPrefix}:flush`,
JSON.stringify({ userId: sseUser, flushedAt: new Date().toISOString() })
);
expect(await stream.reader.read()).toMatchObject({ done: true });
} finally {
stream.abort.abort();
await accessTokenStore.revoke(created.accessToken);
}
});
it('rejects access using persisted revocation state without any pubsub delivery', async () => {
const actor = `security-http-durable-missed-${process.pid}`;
const accessToken = await createAccessToken('durable-missed', {}, actor);
try {
expect((await requestTrpc('auth.status', { accessToken })).response.status).toBe(200);
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:user:${actor}`, String(Date.now()));
expect((await requestTrpc('auth.status', { accessToken })).response.status).toBe(401);
} finally {
await accessTokenStore.revoke(accessToken);
}
});
it('retains persisted revocation state across an actual API restart', async () => {
const actor = `security-http-durable-restart-${process.pid}`;
const accessToken = await createAccessToken('durable-restart', {}, actor);
try {
const gatewayToken = encryptGameSessionToken(buildPayload('unused-durable-restart', {}, actor), secret);
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:user:${actor}`, String(Date.now()));
await server!.app.close();
server = await createGameApiServer();
baseUrl = await server.app.listen({ host: '127.0.0.1', port: 0 });
expect((await requestTrpc('auth.status', { accessToken })).response.status).toBe(401);
const exchanged = await requestTrpc('auth.exchangeGatewayToken', {
method: 'POST',
input: { gatewayToken },
});
expect(exchanged.response.status).toBe(401);
} finally {
await accessTokenStore.revoke(accessToken);
}
});
it('SSE closes after persisted revocation even when the flush notification was missed', async () => {
const actor = `security-http-durable-sse-${process.pid}`;
const accessToken = await createAccessToken('durable-sse', {}, actor);
const stream = await openRealtime(accessToken);
try {
await redis!.client.set(`${redisPrefix}:flush:revocation:v1:user:${actor}`, String(Date.now()));
await redis!.client.publish(
buildGameEventChannel(profileName),
JSON.stringify({ type: 'tournamentChanged' })
);
expect(await stream.reader.read()).toMatchObject({ done: true });
} finally {
stream.abort.abort();
await accessTokenStore.revoke(accessToken);
}
});
it('denies legacy credentials before the initial cutoff and admits a fresh one afterwards', async () => {
const actor = `security-http-durable-cutoff-${process.pid}`;
const accessToken = await createAccessToken('durable-cutoff', {}, actor);
const key = `${redisPrefix}:flush:revocation:v1:baseline`;
try {
await redis!.client.set(key, String(Date.now()));
expect((await requestTrpc('auth.status', { accessToken })).response.status).toBe(401);
const fresh = await accessTokenStore.create({
...buildPayload('fresh-cutoff', {}, actor),
issuedAt: new Date(Date.now() + 1).toISOString(),
});
expect(fresh).not.toBeNull();
try {
expect((await requestTrpc('auth.status', { accessToken: fresh!.accessToken })).response.status).toBe(
200
);
} finally {
await accessTokenStore.revoke(fresh!.accessToken);
}
} finally {
await redis!.client.set(key, '0');
await accessTokenStore.revoke(accessToken);
}
});
it('fails closed for missing initialization in HTTP, SSE and API startup', async () => {
const actor = `security-http-durable-missing-${process.pid}`;
const accessToken = await createAccessToken('durable-missing', {}, actor);
const key = `${redisPrefix}:flush:revocation:v1:baseline`;
try {
await redis!.client.del(key);
expect((await requestTrpc('auth.status', { accessToken })).response.status).toBe(503);
const sse = await fetch(`${baseUrl}${server!.config.eventsPath}`, {
headers: { authorization: `Bearer ${accessToken}` },
});
expect(sse.status).toBe(503);
await expect(createGameApiServer()).rejects.toThrow('startup refused');
} finally {
await redis!.client.set(key, '0');
await accessTokenStore.revoke(accessToken);
}
});
// Flush invalidates every token issued before the user watermark. Keep it // Flush invalidates every token issued before the user watermark. Keep it
// last so this lifecycle assertion cannot invalidate the actor tokens used // last so this lifecycle assertion cannot invalidate the actor tokens used
// by the transport authorization matrix above. // by the transport authorization matrix above.
+99 -35
View File
@@ -76,35 +76,21 @@ class TournamentTransport implements TurnDaemonTransport {
async requestCommand(command: TurnDaemonCommand): Promise<TurnDaemonCommandResult | null> { async requestCommand(command: TurnDaemonCommand): Promise<TurnDaemonCommandResult | null> {
this.commands.push(command); this.commands.push(command);
if (command.type === 'adjustGeneralResources') { if (command.type === 'tournamentAdjustGeneral') {
const adjustment = command.adjustments[0]; if (command.betGoldDelta !== undefined && this.failNextRankUpdate) {
if (!adjustment) { this.failNextRankUpdate = false;
return { type: 'adjustGeneralResources', ok: false, reason: '조정 대상이 없습니다.' }; return { type: command.type, ok: false, code: 'BAD_REQUEST', reason: 'rank update failed' };
} }
const nextGold = (this.gold.get(adjustment.generalId) ?? 0) + (adjustment.goldDelta ?? 0); const nextGold = (this.gold.get(command.generalId) ?? 0) + command.goldDelta;
if (nextGold < (adjustment.minGoldAfter ?? 0)) { if (nextGold < command.minGoldAfter) {
return { type: 'adjustGeneralResources', ok: false, reason: '자원이 부족합니다.' }; return { type: command.type, ok: false, code: 'BAD_REQUEST', reason: '자원이 부족합니다.' };
} }
this.gold.set(adjustment.generalId, nextGold); this.gold.set(command.generalId, nextGold);
return { return { type: command.type, ok: true, generalId: command.generalId };
type: 'adjustGeneralResources',
ok: true,
processed: 1,
missing: 0,
totalGoldDelta: adjustment.goldDelta ?? 0,
totalRiceDelta: 0,
};
} }
if (command.type === 'setMySetting') { if (command.type === 'setMySetting') {
return { type: 'setMySetting', ok: true, generalId: command.generalId }; return { type: 'setMySetting', ok: true, generalId: command.generalId };
} }
if (command.type === 'adjustGeneralMeta') {
if (this.failNextRankUpdate) {
this.failNextRankUpdate = false;
return { type: 'adjustGeneralMeta', ok: false, reason: 'rank update failed' };
}
return { type: 'adjustGeneralMeta', ok: true, processed: 1, missing: 0 };
}
return null; return null;
} }
@@ -404,12 +390,12 @@ describe('tournament router permissions and mutations', () => {
await expect(caller.tournament.join()).resolves.toEqual({ ok: true, count: 1 }); await expect(caller.tournament.join()).resolves.toEqual({ ok: true, count: 1 });
expect(transport.gold.get(general.id)).toBe(1_800); expect(transport.gold.get(general.id)).toBe(1_800);
expect(transport.commands.filter((command) => command.type === 'adjustGeneralResources')).toHaveLength(1); expect(transport.commands.filter((command) => command.type === 'tournamentAdjustGeneral')).toHaveLength(1);
expect(transport.commands).toContainEqual( expect(transport.commands).toContainEqual(
expect.objectContaining({ expect.objectContaining({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
requestId: 'http:tournament-join:tournamentJoin:resources', requestId: 'http:tournament-join:tournamentJoin:resources',
reason: 'tournamentJoin', userId: 'user-1',
}) })
); );
expect(outerApiTransaction).not.toHaveBeenCalled(); expect(outerApiTransaction).not.toHaveBeenCalled();
@@ -459,9 +445,12 @@ describe('tournament router permissions and mutations', () => {
await expect(caller.tournament.join()).resolves.toEqual({ ok: true, count: 1 }); await expect(caller.tournament.join()).resolves.toEqual({ ok: true, count: 1 });
expect(transport.gold.get(general.id)).toBe(1_936); expect(transport.gold.get(general.id)).toBe(1_936);
expect(transport.commands).toContainEqual({ expect(transport.commands).toContainEqual({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
userId: 'user-1',
reason: 'tournamentJoin', reason: 'tournamentJoin',
adjustments: [{ generalId: general.id, goldDelta: -64, minGoldAfter: 0 }], generalId: general.id,
goldDelta: -64,
minGoldAfter: 0,
}); });
}); });
@@ -534,21 +523,96 @@ describe('tournament router permissions and mutations', () => {
expect((await caller.tournament.getBettingSummary()).myAmount).toBe(600); expect((await caller.tournament.getBettingSummary()).myAmount).toBe(600);
expect(transport.commands).toContainEqual( expect(transport.commands).toContainEqual(
expect.objectContaining({ expect.objectContaining({
type: 'adjustGeneralResources', type: 'tournamentAdjustGeneral',
requestId: 'http:suspended-tournament-bet:tournamentBet:resources', requestId: 'http:suspended-tournament-bet:tournamentBet:resources',
reason: 'tournamentBet', userId: 'user-1',
}) })
); );
expect(outerApiTransaction).not.toHaveBeenCalled(); expect(outerApiTransaction).not.toHaveBeenCalled();
expect(transport.commands).toContainEqual( expect(transport.commands).toContainEqual(
expect.objectContaining({ expect.objectContaining({
type: 'adjustGeneralMeta', type: 'tournamentAdjustGeneral',
requestId: 'http:suspended-tournament-bet:tournamentBet:rank', userId: 'user-1',
reason: 'tournamentBet', betGoldDelta: 600,
}) })
); );
}); });
it("keeps the per-user cap across possession and separates a previous owner's bets", async () => {
const redis = new MemoryRedis();
const transport = new TournamentTransport();
const general = buildGeneral(1, 'user-2', 3_000);
transport.gold.set(general.id, general.gold);
await setTournamentFixture(redis, {
stage: 6,
phase: 0,
type: 0,
auto: true,
openYear: 193,
openMonth: 1,
termSeconds: 60,
nextAt: '2026-07-26T01:00:00.000Z',
bettingCloseAt: '2099-01-01T00:00:00.000Z',
});
await redis.set(
'sammo:che:default:tournament:betting',
JSON.stringify([
{ generalId: 1, userId: 'user-1', targetId: 11, amount: 900 },
{ generalId: 99, userId: 'user-2', targetId: 12, amount: 800 },
{ generalId: 98, userId: null, targetId: 11, amount: 100 },
])
);
const caller = appRouter.createCaller(
buildContext({ redis, transport, generals: [general], userId: 'user-2' })
);
expect(await caller.tournament.getBettingSummary()).toMatchObject({
myAmount: 800,
myTotals: { 12: 800 },
totalAmount: 1_800,
});
await expect(caller.tournament.placeBet({ targetId: 11, amount: 300 })).rejects.toMatchObject({
code: 'BAD_REQUEST',
});
expect(transport.commands).toHaveLength(0);
await expect(caller.tournament.placeBet({ targetId: 11, amount: 200 })).resolves.toEqual({ ok: true });
expect(await caller.tournament.getBettingSummary()).toMatchObject({
myAmount: 1_000,
myTotals: { 11: 200, 12: 800 },
});
expect(transport.gold.get(1)).toBe(2_800);
});
it('does not infer a legacy bet actor from the current general owner', async () => {
const redis = new MemoryRedis();
const transport = new TournamentTransport();
const general = buildGeneral(1, 'user-2', 3_000);
transport.gold.set(1, 3_000);
await setTournamentFixture(redis, {
stage: 6,
phase: 0,
type: 0,
auto: true,
openYear: 193,
openMonth: 1,
termSeconds: 60,
nextAt: '2026-07-26T01:00:00.000Z',
bettingCloseAt: '2099-01-01T00:00:00.000Z',
});
await redis.set(
'sammo:che:default:tournament:betting',
JSON.stringify([{ generalId: 1, targetId: 11, amount: 900 }])
);
const caller = appRouter.createCaller(
buildContext({ redis, transport, generals: [general], userId: 'user-2' })
);
expect(await caller.tournament.getBettingSummary()).toMatchObject({ myAmount: 0, totalAmount: 900 });
await expect(caller.tournament.placeBet({ targetId: 11, amount: 10 })).rejects.toMatchObject({
code: 'BAD_REQUEST',
});
expect(transport.commands).toHaveLength(0);
expect(transport.gold.get(1)).toBe(3_000);
});
it('rejects a tournament bet during reconciliation without debiting gold', async () => { it('rejects a tournament bet during reconciliation without debiting gold', async () => {
const redis = new MemoryRedis(); const redis = new MemoryRedis();
const transport = new TournamentTransport(); const transport = new TournamentTransport();
@@ -796,7 +860,7 @@ describe('tournament router permissions and mutations', () => {
expect(snapshot.sourceRevision).toBe('41'); expect(snapshot.sourceRevision).toBe('41');
}); });
it('refunds gold when the tournament bet rank update fails', async () => { it('does not debit gold when the atomic tournament adjustment is rejected', async () => {
const redis = new MemoryRedis(); const redis = new MemoryRedis();
const transport = new TournamentTransport(); const transport = new TournamentTransport();
const general = buildGeneral(1, 'user-1', 2_000); const general = buildGeneral(1, 'user-1', 2_000);
@@ -818,7 +882,7 @@ describe('tournament router permissions and mutations', () => {
); );
await expect(caller.tournament.placeBet({ targetId: 11, amount: 100 })).rejects.toMatchObject({ await expect(caller.tournament.placeBet({ targetId: 11, amount: 100 })).rejects.toMatchObject({
code: 'INTERNAL_SERVER_ERROR', code: 'BAD_REQUEST',
}); });
expect(transport.gold.get(general.id)).toBe(2_000); expect(transport.gold.get(general.id)).toBe(2_000);
await expect(caller.tournament.getBettingSummary()).resolves.toMatchObject({ await expect(caller.tournament.getBettingSummary()).resolves.toMatchObject({
+55 -30
View File
@@ -53,7 +53,15 @@ const isRecord = (value: unknown): value is Record<string, unknown> => typeof va
const createNoopDaemonTransport = (): TurnDaemonTransport => ({ const createNoopDaemonTransport = (): TurnDaemonTransport => ({
sendCommand: async () => 'ok', sendCommand: async () => 'ok',
requestCommand: async () => null, requestCommand: async (command) =>
command.type === 'tournamentSeedNpcBets'
? {
type: command.type,
ok: true,
processedGeneralIds: command.bets.map((bet) => bet.generalId),
skippedGeneralIds: [],
}
: null,
requestStatus: async () => null, requestStatus: async () => null,
}); });
@@ -440,10 +448,10 @@ describe('tournament worker (in-memory)', () => {
const commands: TurnDaemonCommand[] = []; const commands: TurnDaemonCommand[] = [];
const daemonTransport: TurnDaemonTransport = { const daemonTransport: TurnDaemonTransport = {
...createNoopDaemonTransport(), ...createNoopDaemonTransport(),
sendCommand: async (command) => { requestCommand: async (command) => {
expect((await store.getState())?.stage).toBe(5); expect((await store.getState())?.stage).toBe(5);
commands.push(command); commands.push(command);
return 'ok'; return createNoopDaemonTransport().requestCommand(command);
}, },
}; };
const opened = await applyPreBattleStage(store, prisma, state, 'opening-seed', daemonTransport); const opened = await applyPreBattleStage(store, prisma, state, 'opening-seed', daemonTransport);
@@ -455,21 +463,14 @@ describe('tournament worker (in-memory)', () => {
expect(new Set(bets.slice(1).map((bet) => bet.targetId)).size).toBeGreaterThan(1); expect(new Set(bets.slice(1).map((bet) => bet.targetId)).size).toBeGreaterThan(1);
expect(commands).toEqual([ expect(commands).toEqual([
{ {
type: 'adjustGeneralResources', type: 'tournamentSeedNpcBets',
requestId: 'tournament:123:npc-bet:resources', requestId: 'tournament:123:npc-bet:atomic',
reason: 'tournamentNpcBet', bets: [3, 4, 5, 6, 7].map((generalId) => ({ generalId, amount })),
adjustments: [3, 4, 5, 6, 7].map((generalId) => ({ generalId, goldDelta: -amount })),
},
{
type: 'adjustGeneralMeta',
requestId: 'tournament:123:npc-bet:meta',
reason: 'tournamentNpcBet',
adjustments: [3, 4, 5, 6, 7].map((generalId) => ({ generalId, metaDelta: { betgold: amount } })),
}, },
]); ]);
await seedNpcBets({ prisma, store, state: opened, baseSeed: 'opening-seed', daemonTransport }); await seedNpcBets({ prisma, store, state: opened, baseSeed: 'opening-seed', daemonTransport });
expect(await store.getBettingEntries()).toEqual(bets); expect(await store.getBettingEntries()).toEqual(bets);
expect(commands).toHaveLength(2); expect(commands).toHaveLength(1);
// A fresh projection with the same seed and DB inputs has the same choices. // A fresh projection with the same seed and DB inputs has the same choices.
await store.setBettingEntries([bets[0]!]); await store.setBettingEntries([bets[0]!]);
await seedNpcBets({ await seedNpcBets({
@@ -482,6 +483,32 @@ describe('tournament worker (in-memory)', () => {
expect(await store.getBettingEntries()).toEqual(bets); expect(await store.getBettingEntries()).toEqual(bets);
}); });
it('publishes only NPC bets acknowledged after the current-owner check', async () => {
const store = new TournamentStore(new MemoryRedis(), buildTournamentKeys('npc-owned-after-plan'));
const state = createTournamentState({
stage: 5,
bettingId: 789,
npcBettingPlan: [
{ generalId: 3, userId: null, targetId: 11, amount: 10 },
{ generalId: 4, userId: null, targetId: 12, amount: 10 },
],
});
await store.setState(state);
await store.setMatches([{ id: 1, stage: 7, roundIndex: 0, attackerId: 11, defenderId: 12 }]);
const transport: TurnDaemonTransport = {
...createNoopDaemonTransport(),
requestCommand: async (command) => {
expect(command.type).toBe('tournamentSeedNpcBets');
expect((await store.getState())?.stage).toBe(5);
expect(await store.getBettingEntries()).toEqual([]);
return { type: 'tournamentSeedNpcBets', ok: true, processedGeneralIds: [4], skippedGeneralIds: [3] };
},
};
const opened = await applyPreBattleStage(store, createPrismaMock({}), state, 'seed', transport);
expect(opened.stage).toBe(6);
expect(await store.getBettingEntries()).toEqual([{ generalId: 4, userId: null, targetId: 12, amount: 10 }]);
});
it('retries an opening query failure without advancing stage or changing the betting identity', async () => { it('retries an opening query failure without advancing stage or changing the betting identity', async () => {
const store = new TournamentStore(new MemoryRedis(), buildTournamentKeys('npc-opening-retry')); const store = new TournamentStore(new MemoryRedis(), buildTournamentKeys('npc-opening-retry'));
const state = createTournamentState({ stage: 5 }); const state = createTournamentState({ stage: 5 });
@@ -509,7 +536,7 @@ describe('tournament worker (in-memory)', () => {
expect(await store.getBettingEntries()).toHaveLength(1); expect(await store.getBettingEntries()).toHaveLength(1);
}); });
it('reuses durable command identities after a partial enqueue failure', async () => { it('reuses durable command identities after an uncertain atomic command response', async () => {
const store = new TournamentStore(new MemoryRedis(), buildTournamentKeys('npc-enqueue-retry')); const store = new TournamentStore(new MemoryRedis(), buildTournamentKeys('npc-enqueue-retry'));
const state = createTournamentState({ stage: 5, bettingId: 456 }); const state = createTournamentState({ stage: 5, bettingId: 456 });
await store.setState(state); await store.setState(state);
@@ -523,15 +550,15 @@ describe('tournament worker (in-memory)', () => {
let fail = true; let fail = true;
const transport: TurnDaemonTransport = { const transport: TurnDaemonTransport = {
...createNoopDaemonTransport(), ...createNoopDaemonTransport(),
sendCommand: async (command) => { requestCommand: async (command) => {
commands.push(command); commands.push(command);
if (command.type === 'adjustGeneralMeta' && fail) { if (command.type === 'tournamentSeedNpcBets' && fail) {
fail = false; fail = false;
// The accepted debit may already have committed before retry. // The accepted debit may already have committed before retry.
prisma.general.findMany = async () => []; prisma.general.findMany = async () => [];
throw new Error('enqueue unavailable'); throw new Error('enqueue unavailable');
} }
return 'ok'; return createNoopDaemonTransport().requestCommand(command);
}, },
}; };
await expect(applyPreBattleStage(store, prisma, state, 'seed', transport)).rejects.toThrow( await expect(applyPreBattleStage(store, prisma, state, 'seed', transport)).rejects.toThrow(
@@ -539,16 +566,14 @@ describe('tournament worker (in-memory)', () => {
); );
expect(await store.getState()).toMatchObject({ expect(await store.getState()).toMatchObject({
stage: 5, stage: 5,
npcBettingPlan: [{ generalId: 3, targetId: expect.any(Number), amount: 10 }], npcBettingPlan: [{ generalId: 3, userId: null, targetId: expect.any(Number), amount: 10 }],
}); });
expect(await store.getBettingEntries()).toEqual([]); expect(await store.getBettingEntries()).toEqual([]);
await applyPreBattleStage(store, prisma, (await store.getState())!, 'seed', transport); await applyPreBattleStage(store, prisma, (await store.getState())!, 'seed', transport);
expect(commands.slice(2)).toEqual(commands.slice(0, 2)); expect(commands.slice(1)).toEqual(commands.slice(0, 1));
expect(commands.map((command) => command.requestId)).toEqual([ expect(commands.map((command) => command.requestId)).toEqual([
'tournament:456:npc-bet:resources', 'tournament:456:npc-bet:atomic',
'tournament:456:npc-bet:meta', 'tournament:456:npc-bet:atomic',
'tournament:456:npc-bet:resources',
'tournament:456:npc-bet:meta',
]); ]);
expect(await store.getBettingEntries()).toHaveLength(1); expect(await store.getBettingEntries()).toHaveLength(1);
}); });
@@ -577,7 +602,10 @@ describe('tournament worker (in-memory)', () => {
commands.push(command); commands.push(command);
return 'ok'; return 'ok';
}, },
requestCommand: async () => null, requestCommand: async (command) => {
commands.push(command);
return createNoopDaemonTransport().requestCommand(command);
},
requestStatus: async () => null, requestStatus: async () => null,
}; };
@@ -598,11 +626,8 @@ describe('tournament worker (in-memory)', () => {
expect(rankedGeneralIds.size).toBeGreaterThanOrEqual(10); expect(rankedGeneralIds.size).toBeGreaterThanOrEqual(10);
expect(commands).toContainEqual( expect(commands).toContainEqual(
expect.objectContaining({ expect.objectContaining({
type: 'adjustGeneralMeta', type: 'tournamentSeedNpcBets',
reason: 'tournamentNpcBet', bets: expect.arrayContaining([expect.objectContaining({ amount: expect.any(Number) })]),
adjustments: expect.arrayContaining([
expect.objectContaining({ metaDelta: { betgold: expect.any(Number) } }),
]),
}) })
); );
} }
@@ -142,6 +142,10 @@ const buildContext = (
meta: options.worldMeta ?? { serverId: currentServerId }, meta: options.worldMeta ?? { serverId: currentServerId },
}), }),
}, },
gameHistory: {
findUnique: async ({ where }: { where: { serverId: string } }) =>
where.serverId === archiveServerId ? { map: 'miniche' } : null,
},
yearbookHistory: { yearbookHistory: {
findFirst: async (args: { findFirst: async (args: {
where: { profileName: string; year?: number; month?: number }; where: { profileName: string; year?: number; month?: number };
@@ -187,11 +191,13 @@ describe('historical yearbook access from dynasty', () => {
const caller = appRouter.createCaller(buildContext(authFor('owner-a'))); const caller = appRouter.createCaller(buildContext(authFor('owner-a')));
const result = await caller.yearbook.getRange({ serverID: archiveServerId }); const result = await caller.yearbook.getRange({ serverID: archiveServerId });
expect(result).toEqual({ expect(result).toMatchObject({
firstYearMonth: 200 * 12, firstYearMonth: 200 * 12,
lastYearMonth: 200 * 12 + 1, lastYearMonth: 200 * 12 + 1,
currentYearMonth: 200 * 12 + 1, currentYearMonth: 200 * 12 + 1,
mapLayout: { mapName: 'miniche' },
}); });
expect(result.mapLayout.cityList.length).toBeGreaterThan(0);
}); });
it('returns the same archived public history for distinct general owners', async () => { it('returns the same archived public history for distinct general owners', async () => {
@@ -249,20 +255,22 @@ describe('historical yearbook access from dynasty', () => {
expect(canonical).toEqual(alias); expect(canonical).toEqual(alias);
expect(alias).toEqual(omitted); expect(alias).toEqual(omitted);
expect(canonical).toEqual({ expect(canonical).toMatchObject({
firstYearMonth: 219 * 12 + 11, firstYearMonth: 219 * 12 + 11,
lastYearMonth: 219 * 12 + 11, lastYearMonth: 219 * 12 + 11,
currentYearMonth: 220 * 12, currentYearMonth: 220 * 12,
mapLayout: { mapName: 'che' },
}); });
}); });
it('reads imported history under the short profile ID when world metadata has no server ID', async () => { it('reads imported history under the short profile ID when world metadata has no server ID', async () => {
const caller = appRouter.createCaller(buildContext(authFor('owner-a'), { worldMeta: {} })); const caller = appRouter.createCaller(buildContext(authFor('owner-a'), { worldMeta: {} }));
await expect(caller.yearbook.getRange()).resolves.toEqual({ await expect(caller.yearbook.getRange()).resolves.toMatchObject({
firstYearMonth: 219 * 12 + 10, firstYearMonth: 219 * 12 + 10,
lastYearMonth: 219 * 12 + 10, lastYearMonth: 219 * 12 + 10,
currentYearMonth: 220 * 12, currentYearMonth: 220 * 12,
mapLayout: { mapName: 'che' },
}); });
}); });
+5 -1
View File
@@ -3,7 +3,11 @@
"compilerOptions": { "compilerOptions": {
"rootDir": "src", "rootDir": "src",
"outDir": "dist", "outDir": "dist",
"composite": false "composite": false,
"paths": {
"@sammo-ts/game-api": ["./src/index.ts"],
"@sammo-ts/game-api/*": ["./src/*"]
}
}, },
"include": ["src"] "include": ["src"]
} }
+5 -3
View File
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { resolveMessageTargetIcon } from '@sammo-ts/logic'; import { resolveMessageTargetIcon } from '@sammo-ts/logic';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
@@ -580,8 +581,8 @@ export const createAuctionBidder = async (options: {
} }
await tx.$executeRaw( await tx.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO rank_data (nation_id, general_id, type, value) INSERT INTO rank_data (id, nation_id, general_id, type, value)
SELECT nation_id, id, 'inherit_spent_dyn', ${morePoint} SELECT ${existingRowIdOrNext('rank_data', GamePrisma.sql`stable_id.general_id = "general".id AND stable_id.type = 'inherit_spent_dyn'`)}, nation_id, id, 'inherit_spent_dyn', ${morePoint}
FROM general FROM general
WHERE id = ${command.generalId} WHERE id = ${command.generalId}
ON CONFLICT (general_id, type) ON CONFLICT (general_id, type)
@@ -598,8 +599,9 @@ export const createAuctionBidder = async (options: {
} }
await tx.$executeRaw( await tx.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO inheritance_point (user_id, key, value, updated_at) INSERT INTO inheritance_point (id, user_id, key, value, updated_at)
VALUES ( VALUES (
${existingRowIdOrNext('inheritance_point', GamePrisma.sql`stable_id.user_id = ${prevUserId} AND stable_id.key = 'previous'`)},
${prevUserId}, ${prevUserId},
'previous', 'previous',
${highestBid.amount}, ${highestBid.amount},
+3 -2
View File
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { resolveMessageTargetIcon } from '@sammo-ts/logic'; import { resolveMessageTargetIcon } from '@sammo-ts/logic';
import { createGamePostgresConnector, GamePrisma } from '@sammo-ts/infra'; import { createGamePostgresConnector, GamePrisma } from '@sammo-ts/infra';
import { ActionLogger, ItemLoader, LogFormat, isItemKey, type MessageDraft } from '@sammo-ts/logic'; import { ActionLogger, ItemLoader, LogFormat, isItemKey, type MessageDraft } from '@sammo-ts/logic';
@@ -212,8 +213,8 @@ const refundInheritancePoint = async (options: {
} }
await prisma.$executeRaw( await prisma.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO inheritance_point (user_id, key, value, updated_at) INSERT INTO inheritance_point (id, user_id, key, value, updated_at)
VALUES (${userId}, 'previous', ${amount}, ${new Date()}) VALUES (${existingRowIdOrNext('inheritance_point', GamePrisma.sql`stable_id.user_id = ${userId} AND stable_id.key = 'previous'`)}, ${userId}, 'previous', ${amount}, ${new Date()})
ON CONFLICT (user_id, key) ON CONFLICT (user_id, key)
DO UPDATE SET DO UPDATE SET
value = inheritance_point.value + EXCLUDED.value, value = inheritance_point.value + EXCLUDED.value,
+3 -2
View File
@@ -1,3 +1,4 @@
import { existingRowIdOrNext } from '@sammo-ts/infra';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { asRecord, JosaUtil } from '@sammo-ts/common'; import { asRecord, JosaUtil } from '@sammo-ts/common';
@@ -289,8 +290,8 @@ const openUniqueAuction = async (
}); });
await db.$executeRaw( await db.$executeRaw(
GamePrisma.sql` GamePrisma.sql`
INSERT INTO rank_data (nation_id, general_id, type, value) INSERT INTO rank_data (id, nation_id, general_id, type, value)
VALUES (${general.nationId}, ${general.id}, 'inherit_spent_dyn', ${command.amount}) VALUES (${existingRowIdOrNext('rank_data', GamePrisma.sql`stable_id.general_id = ${general.id} AND stable_id.type = 'inherit_spent_dyn'`)}, ${general.nationId}, ${general.id}, 'inherit_spent_dyn', ${command.amount})
ON CONFLICT (general_id, type) ON CONFLICT (general_id, type)
DO UPDATE SET DO UPDATE SET
nation_id = EXCLUDED.nation_id, nation_id = EXCLUDED.nation_id,
+1
View File
@@ -13,6 +13,7 @@ export * from './scenario/databaseUrl.js';
export * from './scenario/mapLoader.js'; export * from './scenario/mapLoader.js';
export * from './scenario/scenarioSeeder.js'; export * from './scenario/scenarioSeeder.js';
export * from './scenario/gameCancellation.js'; export * from './scenario/gameCancellation.js';
export * from './playAudit/retention.js';
export * from './turn/types.js'; export * from './turn/types.js';
export * from './turn/worldLoader.js'; export * from './turn/worldLoader.js';
export * from './turn/inMemoryWorld.js'; export * from './turn/inMemoryWorld.js';
@@ -120,7 +120,13 @@ export class DatabaseTurnDaemonCommandQueue implements TurnDaemonControlQueue, T
const claimCoordinate = await readInputEventClockCoordinate(transaction); const claimCoordinate = await readInputEventClockCoordinate(transaction);
const world = await transaction.worldState.findFirst({ const world = await transaction.worldState.findFirst({
orderBy: { id: 'asc' }, orderBy: { id: 'asc' },
select: { clockPhase: true, clockRevision: true, deadlineGeneration: true, clockTick: true, meta: true }, select: {
clockPhase: true,
clockRevision: true,
deadlineGeneration: true,
clockTick: true,
meta: true,
},
}); });
// 가오픈도 장수 생성·삭제·거병·예약 등 사용자 명령은 처리한다. // 가오픈도 장수 생성·삭제·거병·예약 등 사용자 명령은 처리한다.
// 자동 턴의 RUNNING/MANUAL gate는 TurnDaemonLifecycle이 별도로 지킨다. // 자동 턴의 RUNNING/MANUAL gate는 TurnDaemonLifecycle이 별도로 지킨다.
@@ -184,7 +190,7 @@ export class DatabaseTurnDaemonCommandQueue implements TurnDaemonControlQueue, T
OR (${postUnificationSurvey} AND "event_type" = 'voteReward') OR (${postUnificationSurvey} AND "event_type" = 'voteReward')
OR ( OR (
${suspendedTournamentBetCommand} ${suspendedTournamentBetCommand}
AND "event_type" IN ('adjustGeneralResources', 'adjustGeneralMeta') AND "event_type" IN ('adjustGeneralResources', 'adjustGeneralMeta', 'tournamentAdjustGeneral')
AND "payload" ->> 'reason' IN ('tournamentBet', 'tournamentBetRollback') AND "payload" ->> 'reason' IN ('tournamentBet', 'tournamentBetRollback')
) )
OR ( OR (
@@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
import { performance } from 'node:perf_hooks'; import { performance } from 'node:perf_hooks';
import { createGamePostgresConnector, GamePrisma, type GamePrismaClient } from '@sammo-ts/infra'; import { createGamePostgresConnector, GamePrisma, type GamePrismaClient } from '@sammo-ts/infra';
import type { HostBootMonitor } from '../turn/hostBootMonitor.js';
export interface TurnDaemonLeaseToken { export interface TurnDaemonLeaseToken {
profile: string; profile: string;
@@ -14,6 +15,7 @@ export interface DatabaseTurnDaemonLeaseOptions {
ownerId?: string; ownerId?: string;
leaseDurationMs?: number; leaseDurationMs?: number;
heartbeat?: boolean; heartbeat?: boolean;
hostBootMonitor?: HostBootMonitor;
} }
export class TurnDaemonLeaseUnavailableError extends Error { export class TurnDaemonLeaseUnavailableError extends Error {
@@ -45,6 +47,7 @@ export class DatabaseTurnDaemonLease {
private readonly ownerId: string; private readonly ownerId: string;
private readonly leaseDurationMs: number; private readonly leaseDurationMs: number;
private readonly heartbeatEnabled: boolean; private readonly heartbeatEnabled: boolean;
private readonly hostBootMonitor?: HostBootMonitor;
private token: TurnDaemonLeaseToken | null = null; private token: TurnDaemonLeaseToken | null = null;
private heartbeatTimer: NodeJS.Timeout | null = null; private heartbeatTimer: NodeJS.Timeout | null = null;
private expiryTimer: NodeJS.Timeout | null = null; private expiryTimer: NodeJS.Timeout | null = null;
@@ -63,6 +66,7 @@ export class DatabaseTurnDaemonLease {
this.ownerId = options.ownerId ?? randomUUID(); this.ownerId = options.ownerId ?? randomUUID();
this.leaseDurationMs = normalizeLeaseDuration(options.leaseDurationMs); this.leaseDurationMs = normalizeLeaseDuration(options.leaseDurationMs);
this.heartbeatEnabled = options.heartbeat ?? true; this.heartbeatEnabled = options.heartbeat ?? true;
this.hostBootMonitor = options.hostBootMonitor;
} }
static async connect( static async connect(
@@ -75,6 +79,8 @@ export class DatabaseTurnDaemonLease {
} }
async acquire(): Promise<TurnDaemonLeaseToken | null> { async acquire(): Promise<TurnDaemonLeaseToken | null> {
await this.checkHostBoot();
if (this.lost) throw this.getLossError();
const requestStartedAt = performance.now(); const requestStartedAt = performance.now();
const rows = await this.db.$queryRaw<LeaseRow[]>(GamePrisma.sql` const rows = await this.db.$queryRaw<LeaseRow[]>(GamePrisma.sql`
INSERT INTO "turn_daemon_lease" ( INSERT INTO "turn_daemon_lease" (
@@ -145,6 +151,12 @@ export class DatabaseTurnDaemonLease {
return new TurnDaemonLeaseLostError(this.profile, this.lossReason); return new TurnDaemonLeaseLostError(this.profile, this.lossReason);
} }
async checkHostBoot(): Promise<void> {
if (!this.lost && (await this.hostBootMonitor?.hasChanged())) {
this.markLost('Host boot identity changed; restart with a fresh database snapshot.');
}
}
async renew(): Promise<boolean> { async renew(): Promise<boolean> {
const token = this.token; const token = this.token;
if (!token || this.lost || this.renewalInFlight) { if (!token || this.lost || this.renewalInFlight) {
@@ -153,6 +165,8 @@ export class DatabaseTurnDaemonLease {
const requestStartedAt = performance.now(); const requestStartedAt = performance.now();
this.renewalInFlight = true; this.renewalInFlight = true;
try { try {
await this.checkHostBoot();
if (this.lost) return false;
const rows = await this.db.$queryRaw<LeaseRow[]>(GamePrisma.sql` const rows = await this.db.$queryRaw<LeaseRow[]>(GamePrisma.sql`
UPDATE "turn_daemon_lease" UPDATE "turn_daemon_lease"
SET SET
@@ -180,6 +194,7 @@ export class DatabaseTurnDaemonLease {
} }
async assertActive(transaction?: GamePrisma.TransactionClient): Promise<void> { async assertActive(transaction?: GamePrisma.TransactionClient): Promise<void> {
await this.checkHostBoot();
const token = this.token; const token = this.token;
if (!token || this.lost) { if (!token || this.lost) {
throw this.getLossError(); throw this.getLossError();
@@ -207,12 +222,13 @@ export class DatabaseTurnDaemonLease {
this.stopExpiryWatchdog(); this.stopExpiryWatchdog();
const token = this.token; const token = this.token;
this.token = null; this.token = null;
if (!token || this.lost) { if (!token) {
return; return;
} }
await this.db.$executeRaw(GamePrisma.sql` await this.db.$executeRaw(GamePrisma.sql`
UPDATE "turn_daemon_lease" UPDATE "turn_daemon_lease"
SET "lease_until" = CURRENT_TIMESTAMP AT TIME ZONE 'UTC', SET "clock_ready" = FALSE,
"lease_until" = CURRENT_TIMESTAMP AT TIME ZONE 'UTC',
"heartbeat_at" = CURRENT_TIMESTAMP AT TIME ZONE 'UTC' "heartbeat_at" = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
WHERE WHERE
"profile" = ${token.profile} "profile" = ${token.profile}
@@ -45,6 +45,7 @@ export interface TurnDaemonLifecycleDeps {
commandResponder?: TurnDaemonCommandResponder; commandResponder?: TurnDaemonCommandResponder;
pauseGate?: () => Promise<boolean>; pauseGate?: () => Promise<boolean>;
stateManager?: Pick<EngineStateManager, 'transaction'>; stateManager?: Pick<EngineStateManager, 'transaction'>;
runErrorRetry?: { wait(error: unknown): Promise<boolean>; reset(): void };
} }
export class TurnDaemonLifecycle { export class TurnDaemonLifecycle {
@@ -59,6 +60,7 @@ export class TurnDaemonLifecycle {
private readonly commandResponder?: TurnDaemonCommandResponder; private readonly commandResponder?: TurnDaemonCommandResponder;
private readonly pauseGate?: () => Promise<boolean>; private readonly pauseGate?: () => Promise<boolean>;
private readonly stateManager?: Pick<EngineStateManager, 'transaction'>; private readonly stateManager?: Pick<EngineStateManager, 'transaction'>;
private readonly runErrorRetry?: TurnDaemonLifecycleDeps['runErrorRetry'];
private readonly options: TurnDaemonLifecycleOptions; private readonly options: TurnDaemonLifecycleOptions;
private status: TurnDaemonStatus; private status: TurnDaemonStatus;
@@ -79,6 +81,7 @@ export class TurnDaemonLifecycle {
this.commandResponder = deps.commandResponder; this.commandResponder = deps.commandResponder;
this.pauseGate = deps.pauseGate; this.pauseGate = deps.pauseGate;
this.stateManager = deps.stateManager; this.stateManager = deps.stateManager;
this.runErrorRetry = deps.runErrorRetry;
this.options = options; this.options = options;
this.status = { this.status = {
state: 'idle', state: 'idle',
@@ -192,8 +195,9 @@ export class TurnDaemonLifecycle {
// 수동 실행 요청도 가오픈·정지·재조정의 턴 실행 gate를 통과해야 한다. // 수동 실행 요청도 가오픈·정지·재조정의 턴 실행 gate를 통과해야 한다.
// 사용자 명령 처리는 루프 시작에서 계속하되 시간 진행은 여기서 분리한다. // 사용자 명령 처리는 루프 시작에서 계속하되 시간 진행은 여기서 분리한다.
if (this.pendingRun) { if (this.pendingRun) {
await this.runOnce(this.pendingRun); const pending = this.pendingRun;
this.pendingRun = null; this.pendingRun = null;
await this.runOnce(pending);
continue; continue;
} }
@@ -447,6 +451,15 @@ export class TurnDaemonLifecycle {
}; };
result = this.stateManager ? await this.stateManager.transaction(runAndFlush) : await runAndFlush(); result = this.stateManager ? await this.stateManager.transaction(runAndFlush) : await runAndFlush();
} catch (error) { } catch (error) {
// savepoint 복원이 성공한 획득 충돌만 대기한다. 복원 실패/AggregateError나
// 저장 중 소유권 상실을 자동 재실행하면 중복 효과를 숨길 수 있다.
if (this.stateManager && (await this.runErrorRetry?.wait(error))) {
this.status.running = false;
this.status.state = 'idle';
this.pendingRun = pending;
return;
}
this.runErrorRetry?.reset();
this.status.running = false; this.status.running = false;
this.status.state = 'paused'; this.status.state = 'paused';
this.status.paused = true; this.status.paused = true;
@@ -459,6 +472,7 @@ export class TurnDaemonLifecycle {
} }
await this.applyRunResult(result, startMs); await this.applyRunResult(result, startMs);
this.runErrorRetry?.reset();
this.status.state = 'idle'; this.status.state = 'idle';
try { try {
await this.hooks?.publishEvents?.(result); await this.hooks?.publishEvents?.(result);
@@ -0,0 +1,34 @@
import { asRecord } from '@sammo-ts/common';
import type { InMemoryTurnWorld } from '../turn/inMemoryWorld.js';
/** A bounded, sticky notice: later successful writes cannot make history complete again. */
export const markAuditGap = (world: InMemoryTurnWorld, stage: string): void => {
const state = world.getState();
const previous = asRecord(state.meta.playAuditGap);
world.updateWorldMeta({
playAuditGap: {
serverId: state.meta.serverId ?? null,
firstYear:
previous.serverId === state.meta.serverId
? (previous.firstYear ?? state.currentYear)
: state.currentYear,
firstMonth:
previous.serverId === state.meta.serverId
? (previous.firstMonth ?? state.currentMonth)
: state.currentMonth,
lastYear: state.currentYear,
lastMonth: state.currentMonth,
stage,
},
});
console.warn(`[play-audit] ${stage}: history gap recorded; gameplay continues.`);
};
export const collectPlayAudit = <T>(world: InMemoryTurnWorld, stage: string, collect: () => T, fallback: T): T => {
try {
return collect();
} catch {
markAuditGap(world, stage);
return fallback;
}
};
Loaded 100 of 494 files, more files were not shown because too many files have changed in this diff. Show more