Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the backend api #10

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open

add the backend api #10

wants to merge 31 commits into from

Conversation

maloong2022
Copy link
Collaborator

I completed some backend api

@vercel
Copy link

vercel bot commented Sep 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sketchit-challenge ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2023 4:48pm

@maloong2022
Copy link
Collaborator Author

deal with the openai error

@FedericoLeiva12
Copy link
Collaborator

I think that we need to draw the backend flow, when the frontend will need to execute a query or mutation and it will help to ensure that everything is okay.


const response = await openai.completions.create({
model: "text-davinci-003",
prompt: `Suggest ${args.numberOfPlayer} words for an topic that is '${args.topic}'`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this prompt is not so specific as it has to be. I just checked it in ChatGPT and the answer is not an array (as expected). You must be very specific, eg:

const prompt = `Give me a list of ${args.nnumberOfPlayer} word related
to the topic "${args.topic}" as a json array format. Don't
include anything else in your response. The json array
must be as this: '{"words": ["word1", "word2", "word3"]}'.`

This prompt is just an example and we're not sure that it works. Maybe you need to test it in ChatGPT and show in this pr some examples of what ChatGPT returned. In your code, you will need to check if the response is in the expected format and try again if it fails. Limit the tries to 10 or something like that to avoid executing infinite calls to openai.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This api I can't test, because I can't use it In China,So if you have the pay key, you can fix this problem

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can test using ChatGPT from https://chat.openai.com/ , but I'll try to get a key

convex/openai.ts Outdated
// ! TODO: check if this acutally works!!!
const checkTopicResponse = await openai.completions.create({
model: "text-davinci-003",
prompt: `if the topic '${args.topic}' is a good topic`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to send prompts as you're talking with other person. For example here it can be:

const prompt = `Is the topic '${args.topic}' a good topic?
A good topic must be a single or two word topic like 'animal',
'plants', 'cars', 'celebrities', etc. Anything that can be suitable
for all audiences. Your answert must be just false or true, with
any other word included.`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This api I can't test, because I can't use it In China,So if you have the pay key, you can fix this problem

if (games?.words?.includes(args.wordFromUser)) {
const player = await ctx.db.get(args.playerId);
const newScore = player?.score ? player?.score + 1 : 1;
ctx.db.patch(args.playerId, { score: newScore });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to check if the user have not answered it yet. If you don't check it, I can spam any of the word and get unlimited score. Also, check that the word is not my word. I shouldn't be able to get score by sending my own word.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, here can change use your idea, thank you

convex/schema.ts Outdated
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

// Why is there a different schema for game and room??? - Jakob
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we can merge the schemas

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the office code, And I use the Convex at the first time, so I don't know may be you are right, but I don't know, sorry

convex/rooms.ts Outdated
if (players.length > 8) {
return {
message: "The backend got an error",
error: "the maxinum of a room is 8!",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maximum*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, Thanks a lot

convex/rooms.ts Outdated
.collect();
if (players.length > 8) {
return {
message: "The backend got an error",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why message and error? We can just send one of them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, here I must check the rooms all player's amount, because a room can only maximum 8 player

convex/player.ts Outdated
.collect();
for (let i = 0; i < playerList.length; i++) {
const player = playerList[i];
if (player === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? I think you can just do something like:

if (player) {
  await ctx.db...
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, here I will change the code, thank you

convex/rooms.ts Outdated
.query("player")
.filter((q) => q.eq(q.field("roomId"), args.roomId))
.collect();
if (players.length > 8) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check if the room is full (players.length === 8)??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll not let 4 players to play for example? Only 8 players?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we have to make the maximum amount parametrizable. Is not a good idea to manually write 8 in all the code.

convex/openai.ts Outdated
} else {
console.error("The Open AI return nothing");
return {
message: "The backend got an error",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still has two error messages here

convex/rooms.ts Outdated
.query("player")
.filter((q) => q.eq(q.field("roomId"), args.roomId))
.collect();
if (players.length > 8) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll not let 4 players to play for example? Only 8 players?

convex/rooms.ts Outdated
.query("player")
.filter((q) => q.eq(q.field("roomId"), args.roomId))
.collect();
if (players.length > 8) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we have to make the maximum amount parametrizable. Is not a good idea to manually write 8 in all the code.

apiKey: process.env.OPENAI_API_KEY, // This is also the default, can be omitted
});
// start a game
export const startGames = action({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a check that if the user that sends the the request to start the game is the creator of the room

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants