Spaces:
Sleeping
Sleeping
world change try
Browse files- patches/convex/world.ts +30 -18
patches/convex/world.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import { ConvexError, v } from 'convex/values';
|
2 |
import { internalMutation, mutation, query } from './_generated/server';
|
3 |
import { characters } from '../data/characters';
|
4 |
-
import { Descriptions } from '../data/characters';
|
5 |
import { insertInput } from './aiTown/insertInput';
|
|
|
6 |
import {
|
7 |
DEFAULT_NAME,
|
8 |
ENGINE_ACTION_DURATION,
|
@@ -114,7 +114,6 @@ export const userStatus = query({
|
|
114 |
|
115 |
export const joinWorld = mutation({
|
116 |
args: {
|
117 |
-
worldId: v.id('worlds'),
|
118 |
oauthToken: v.optional(v.string()),
|
119 |
|
120 |
},
|
@@ -130,27 +129,41 @@ export const joinWorld = mutation({
|
|
130 |
// const name =
|
131 |
// identity.givenName || identity.nickname || (identity.email && identity.email.split('@')[0]);
|
132 |
const name = oauthToken;
|
133 |
-
|
134 |
-
// if (!name) {
|
135 |
-
// throw new ConvexError(`Missing name on ${JSON.stringify(identity)}`);
|
136 |
-
// }
|
137 |
const world = await ctx.db.get(args.worldId);
|
138 |
if (!world) {
|
139 |
throw new ConvexError(`Invalid world ID: ${args.worldId}`);
|
140 |
}
|
141 |
-
// Select a random character description
|
142 |
-
const randomCharacter = Descriptions[Math.floor(Math.random() * Descriptions.length)];
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
});
|
151 |
-
},
|
152 |
-
});
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
export const leaveWorld = mutation({
|
156 |
args: {
|
@@ -165,7 +178,6 @@ export const leaveWorld = mutation({
|
|
165 |
if (!oauthToken) {
|
166 |
throw new ConvexError(`Not logged in`);
|
167 |
}
|
168 |
-
|
169 |
const world = await ctx.db.get(args.worldId);
|
170 |
if (!world) {
|
171 |
throw new Error(`Invalid world ID: ${args.worldId}`);
|
|
|
1 |
import { ConvexError, v } from 'convex/values';
|
2 |
import { internalMutation, mutation, query } from './_generated/server';
|
3 |
import { characters } from '../data/characters';
|
|
|
4 |
import { insertInput } from './aiTown/insertInput';
|
5 |
+
import { Descriptions } from '../data/characters';
|
6 |
import {
|
7 |
DEFAULT_NAME,
|
8 |
ENGINE_ACTION_DURATION,
|
|
|
114 |
|
115 |
export const joinWorld = mutation({
|
116 |
args: {
|
|
|
117 |
oauthToken: v.optional(v.string()),
|
118 |
|
119 |
},
|
|
|
129 |
// const name =
|
130 |
// identity.givenName || identity.nickname || (identity.email && identity.email.split('@')[0]);
|
131 |
const name = oauthToken;
|
|
|
|
|
|
|
|
|
132 |
const world = await ctx.db.get(args.worldId);
|
133 |
if (!world) {
|
134 |
throw new ConvexError(`Invalid world ID: ${args.worldId}`);
|
135 |
}
|
|
|
|
|
136 |
|
137 |
+
const playerIds = [...world.playersInit.values()].map(player => player.id)
|
138 |
+
|
139 |
+
const playerDescriptions = await ctx.db
|
140 |
+
.query('playerDescriptions')
|
141 |
+
.withIndex('worldId', (q) => q.eq('worldId', args.worldId))
|
142 |
+
.collect();
|
|
|
|
|
|
|
143 |
|
144 |
+
const namesInGame = playerDescriptions.map(description => {
|
145 |
+
if (playerIds.includes(description.playerId)) {
|
146 |
+
return description.name
|
147 |
+
}
|
148 |
+
})
|
149 |
+
const availableDescriptions = Descriptions.filter(
|
150 |
+
description => !namesInGame.includes(description.name)
|
151 |
+
);
|
152 |
+
|
153 |
+
const randomCharacter = availableDescriptions[Math.floor(Math.random() * availableDescriptions.length)];
|
154 |
+
|
155 |
+
// const { tokenIdentifier } = identity;
|
156 |
+
return await insertInput(ctx, world._id, 'join', {
|
157 |
+
name: randomCharacter.name,
|
158 |
+
character: randomCharacter.character,
|
159 |
+
description: randomCharacter.identity,
|
160 |
+
// description: `${identity.givenName} is a human player`,
|
161 |
+
tokenIdentifier: oauthToken, // TODO: change for multiplayer to oauth
|
162 |
+
// By default everybody is a villager
|
163 |
+
type: 'villager',
|
164 |
+
});
|
165 |
+
},
|
166 |
+
});
|
167 |
|
168 |
export const leaveWorld = mutation({
|
169 |
args: {
|
|
|
178 |
if (!oauthToken) {
|
179 |
throw new ConvexError(`Not logged in`);
|
180 |
}
|
|
|
181 |
const world = await ctx.db.get(args.worldId);
|
182 |
if (!world) {
|
183 |
throw new Error(`Invalid world ID: ${args.worldId}`);
|