Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
FilterApi,
FunctionReference,
} from "convex/server";
import type * as rooms from "../rooms";

/**
* A utility for referencing Convex functions in your app's API.
Expand All @@ -23,7 +24,9 @@ import type {
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
declare const fullApi: ApiFromModules<{}>;
declare const fullApi: ApiFromModules<{
rooms: typeof rooms;
}>;
export declare const api: FilterApi<
typeof fullApi,
FunctionReference<any, "public">
Expand Down
31 changes: 14 additions & 17 deletions convex/_generated/dataModel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,25 @@
* @module
*/

import { AnyDataModel } from "convex/server";
import type { DataModelFromSchemaDefinition } from "convex/server";
import type { DocumentByName, TableNamesInDataModel } from "convex/server";
import type { GenericId } from "convex/values";

/**
* No `schema.ts` file found!
*
* This generated code has permissive types like `Doc = any` because
* Convex doesn't know your schema. If you'd like more type safety, see
* https://docs.convex.dev/using/schemas for instructions on how to add a
* schema file.
*
* After you change a schema, rerun codegen with `npx convex dev`.
*/
import schema from "../schema";

/**
* The names of all of your Convex tables.
*/
export type TableNames = string;
export type TableNames = TableNamesInDataModel<DataModel>;

/**
* The type of a document stored in Convex.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Doc = any;
export type Doc<TableName extends TableNames> = DocumentByName<
DataModel,
TableName
>;

/**
* An identifier for a document in Convex.
Expand All @@ -43,9 +39,10 @@ export type Doc = any;
*
* IDs are just strings at runtime, but this type can be used to distinguish them from other
* strings when type checking.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Id<TableName extends TableNames = TableNames> =
GenericId<TableName>;
export type Id<TableName extends TableNames> = GenericId<TableName>;

/**
* A type describing your Convex data model.
Expand All @@ -56,4 +53,4 @@ export type Id<TableName extends TableNames = TableNames> =
* This type is used to parameterize methods like `queryGeneric` and
* `mutationGeneric` to make them type-safe.
*/
export type DataModel = AnyDataModel;
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;
40 changes: 40 additions & 0 deletions convex/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { query, mutation } from "./_generated/server";
import { v } from "convex/values";

// start a game
export const startGames = mutation({
args: { topic: v.string(), words: v.string(), roomId: v.id("rooms") },
handler: async (ctx, args) => {
const gameId = await ctx.db.insert("games", { topic:args.topic, words:args.words });
// update the player who in this room
// const playerList = await ctx.db.query("player").filter((q) => q.eq(q.field('roomId'), args.roomId)).collect();
// for (let i = 0; i < playerList.length; i++) {
// await ctx.db.patch(playerList[i]?._id, { gameId: gameId, score: 0 })
// }

const playerList = await ctx.db.query("player").filter((q) => q.eq(q.field('roomId'), args.roomId)).collect();
for (let i = 0; i < playerList.length; i++) {
const playerId = playerList[i]?._id;
if (playerId) {
await ctx.db.patch(playerId, { gameId: gameId, score: 0 });
}
}
},
});


// update the player score
export const updateScore = mutation({
args: { _id: v.id("player"), score: v.number() },
handler: async (ctx, args) => {
await ctx.db.patch(args._id, { score: args.score })
}
})

// get all the players in a room
export const getPlayersByRoomId = query({
args: { _id: v.id("rooms") },
handler: async (ctx, args) => {
await ctx.db.query("player").filter((q) => q.eq(q.field('roomId'), args._id)).collect();
}
})
23 changes: 23 additions & 0 deletions convex/rooms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { mutation } from "./_generated/server";
import { v } from "convex/values";

// create room
export const createRoom = mutation({
args: { username: v.string() },
handler: async (ctx, args) => {
const roomName = `${args.username}'s Room`;
const roomId = await ctx.db.insert("rooms", {roomName });
const playerId = await ctx.db.insert("player",{username:args.username,roomId:roomId});
return {"playId":playerId,"roomId":roomId}
},
});


// invite somebody to room
export const invite = mutation({
args:{username:v.string(),roomId:v.id("rooms")},
handler: async (ctx,args) => {
await ctx.db.insert("player",{username:args.username,roomId:args.roomId});
return ctx.db.query("player").filter((q)=>q.eq(q.field('roomId'),args.roomId)).collect();
}
})
13 changes: 13 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";


export default defineSchema({
games: defineTable({
topic: v.string(),
words: v.string(),
}),
rooms: defineTable({
roomName:v.string(),
})
});
Loading