Spaces:
Sleeping
Sleeping
File size: 5,052 Bytes
90cbf22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import { v } from 'convex/values';
import { agentId, conversationId, parseGameId } from './ids';
import { Player, activity } from './player';
import { Conversation, conversationInputs } from './conversation';
import { movePlayer } from './movement';
import { inputHandler } from './inputHandler';
import { point } from '../util/types';
import { Descriptions } from '../../data/characters';
import { AgentDescription } from './agentDescription';
import { Agent } from './agent';
import { CharacterTypeSchema } from './playerDescription';
export const agentInputs = {
finishRememberConversation: inputHandler({
args: {
operationId: v.string(),
agentId,
},
handler: (game, now, args) => {
const agentId = parseGameId('agents', args.agentId);
const agent = game.world.agents.get(agentId);
if (!agent) {
throw new Error(`Couldn't find agent: ${agentId}`);
}
if (
!agent.inProgressOperation ||
agent.inProgressOperation.operationId !== args.operationId
) {
console.debug(`Agent ${agentId} isn't remembering ${args.operationId}`);
} else {
delete agent.inProgressOperation;
delete agent.toRemember;
}
return null;
},
}),
finishDoSomething: inputHandler({
args: {
operationId: v.string(),
agentId: v.id('agents'),
destination: v.optional(point),
invitee: v.optional(v.id('players')),
activity: v.optional(activity),
},
handler: (game, now, args) => {
const agentId = parseGameId('agents', args.agentId);
const agent = game.world.agents.get(agentId);
if (!agent) {
throw new Error(`Couldn't find agent: ${agentId}`);
}
if (
!agent.inProgressOperation ||
agent.inProgressOperation.operationId !== args.operationId
) {
console.debug(`Agent ${agentId} didn't have ${args.operationId} in progress`);
return null;
}
delete agent.inProgressOperation;
const player = game.world.players.get(agent.playerId)!;
if (args.invitee) {
const inviteeId = parseGameId('players', args.invitee);
const invitee = game.world.players.get(inviteeId);
if (!invitee) {
throw new Error(`Couldn't find player: ${inviteeId}`);
}
Conversation.start(game, now, player, invitee);
agent.lastInviteAttempt = now;
}
if (args.destination) {
movePlayer(game, now, player, args.destination);
}
if (args.activity) {
player.activity = args.activity;
}
return null;
},
}),
agentFinishSendingMessage: inputHandler({
args: {
agentId,
conversationId,
timestamp: v.number(),
operationId: v.string(),
leaveConversation: v.boolean(),
},
handler: (game, now, args) => {
const agentId = parseGameId('agents', args.agentId);
const agent = game.world.agents.get(agentId);
if (!agent) {
throw new Error(`Couldn't find agent: ${agentId}`);
}
const player = game.world.players.get(agent.playerId);
if (!player) {
throw new Error(`Couldn't find player: ${agent.playerId}`);
}
const conversationId = parseGameId('conversations', args.conversationId);
const conversation = game.world.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Couldn't find conversation: ${conversationId}`);
}
if (
!agent.inProgressOperation ||
agent.inProgressOperation.operationId !== args.operationId
) {
console.debug(`Agent ${agentId} wasn't sending a message ${args.operationId}`);
return null;
}
delete agent.inProgressOperation;
conversationInputs.finishSendingMessage.handler(game, now, {
playerId: agent.playerId,
conversationId: args.conversationId,
timestamp: args.timestamp,
});
if (args.leaveConversation) {
conversation.leave(game, now, player);
}
return null;
},
}),
createAgent: inputHandler({
args: {
descriptionIndex: v.number(),
type: CharacterTypeSchema
},
handler: (game, now, args) => {
const description = Descriptions[args.descriptionIndex];
const playerId = Player.join(
game,
now,
description.name,
description.character,
description.identity,
args.type,
);
const agentId = game.allocId('agents');
game.world.agents.set(
agentId,
new Agent({
id: agentId,
playerId: playerId,
inProgressOperation: undefined,
lastConversation: undefined,
lastInviteAttempt: undefined,
toRemember: undefined,
}),
);
game.agentDescriptions.set(
agentId,
new AgentDescription({
agentId: agentId,
identity: description.identity,
plan: description.plan,
}),
);
return { agentId };
},
}),
};
|