fix(game): sync diplomatic responses into turn world
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { z } from 'zod';
|
||||
import { asRecord, ChangeJournal } from '@sammo-ts/common';
|
||||
@@ -14,7 +12,6 @@ import {
|
||||
authedProcedure,
|
||||
engineAuthedProcedure,
|
||||
router,
|
||||
scopeApiInputEventRequestId,
|
||||
wallAuthedProcedure,
|
||||
} from '../../trpc.js';
|
||||
import {
|
||||
@@ -357,7 +354,7 @@ export const messagesRouter = router({
|
||||
let journalPersisted = false;
|
||||
const response = await executeInputEvent({
|
||||
db: ctx.db,
|
||||
requestId: scopeApiInputEventRequestId(ctx.requestId ?? randomUUID(), 'messages.respond.diplomatic', 0),
|
||||
requestId: `messages.respond.diplomatic:${input.messageId}`,
|
||||
eventType: 'messages.respond.diplomatic',
|
||||
payload: input,
|
||||
actorUserId: ctx.auth?.user.id,
|
||||
@@ -393,13 +390,39 @@ export const messagesRouter = router({
|
||||
await writeReadModelChangeJournal(transaction, changeJournal.snapshot())
|
||||
);
|
||||
}
|
||||
return { result: result.result, reason: result.reason };
|
||||
return {
|
||||
result: result.result,
|
||||
reason: result.reason,
|
||||
affectedNationIds: result.affectedNationIds,
|
||||
affectedCityIds: result.affectedCityIds,
|
||||
};
|
||||
},
|
||||
});
|
||||
if (journalPersisted) {
|
||||
ctx.readModelOutbox?.wake();
|
||||
}
|
||||
return response;
|
||||
if (response.result && (response.affectedNationIds.length > 0 || response.affectedCityIds.length > 0)) {
|
||||
if (!ctx.auth) {
|
||||
throw new TRPCError({ code: 'UNAUTHORIZED' });
|
||||
}
|
||||
const synchronized = await ctx.turnDaemon.requestCommand({
|
||||
type: 'syncDiplomaticResponse',
|
||||
userId: ctx.auth.user.id,
|
||||
generalId: general.id,
|
||||
messageId: input.messageId,
|
||||
nationIds: response.affectedNationIds,
|
||||
cityIds: response.affectedCityIds,
|
||||
});
|
||||
if (!synchronized || synchronized.type !== 'syncDiplomaticResponse' || !synchronized.ok) {
|
||||
const synchronizationReason =
|
||||
synchronized?.type === 'syncDiplomaticResponse' ? synchronized.reason : undefined;
|
||||
throw new TRPCError({
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
message: synchronizationReason ?? '외교 상태를 게임 엔진에 동기화하지 못했습니다.',
|
||||
});
|
||||
}
|
||||
}
|
||||
return { result: response.result, reason: response.reason };
|
||||
}),
|
||||
getOld: authedProcedure
|
||||
.input(
|
||||
|
||||
@@ -959,6 +959,15 @@ describe('messages router missing-flow compatibility', () => {
|
||||
const messageActionUpdateMany = vi.fn(async () => ({ count: 1 }));
|
||||
const cityUpdate = vi.fn(async () => ({}));
|
||||
const changeJournal = new ChangeJournal();
|
||||
const requestCommand = vi.fn(async (command: { type: string; generalId: number; messageId: number }) => ({
|
||||
type: 'syncDiplomaticResponse' as const,
|
||||
ok: true,
|
||||
generalId: command.generalId,
|
||||
messageId: command.messageId,
|
||||
nations: 2,
|
||||
diplomacy: 2,
|
||||
cities: 0,
|
||||
}));
|
||||
const { caller } = buildContext(
|
||||
{
|
||||
general: {
|
||||
@@ -1049,7 +1058,7 @@ describe('messages router missing-flow compatibility', () => {
|
||||
messageAction: { updateMany: messageActionUpdateMany },
|
||||
$queryRaw: queryRaw,
|
||||
},
|
||||
{ changeJournal }
|
||||
{ changeJournal, turnDaemon: { requestCommand } }
|
||||
);
|
||||
return {
|
||||
caller,
|
||||
@@ -1062,6 +1071,7 @@ describe('messages router missing-flow compatibility', () => {
|
||||
messageUpdateMany,
|
||||
cityUpdate,
|
||||
changeJournal,
|
||||
requestCommand,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1075,6 +1085,14 @@ describe('messages router missing-flow compatibility', () => {
|
||||
});
|
||||
|
||||
expect(result).toEqual({ result: true, reason: 'success' });
|
||||
expect(setup.requestCommand).toHaveBeenCalledWith({
|
||||
type: 'syncDiplomaticResponse',
|
||||
userId: auth.user.id,
|
||||
generalId: setup.actor.id,
|
||||
messageId: 31,
|
||||
nationIds: [1, 2],
|
||||
cityIds: [],
|
||||
});
|
||||
expect(setup.diplomacyUpdate).toHaveBeenCalledTimes(2);
|
||||
expect(setup.nationUpdate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
|
||||
Reference in New Issue
Block a user