Skip to content

itsDV7/TriviaGame-JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SCHEMAS

GAME schema

_id: default
players: Array of Strings of the ID's of players
joinCode: 6 digit number for players to join game. Set to -1 after creation
questions: Array of Question objects (see below)
currentQuestion: Array index for what question is currently being answered, starting with 0
started: True if the game has begun, False if not (players can only join if false)
completed: True if the game is over
custom: True if the game was initialized with a custom quiz
customName: The name of the custom quiz used. Default is "" when a custom quiz wasn't used.
waitingFor: Array of the playerIDs for everyone that hasn't played the current round.
lastAction: Date of the last time someone played
dateCreated: Date of when the game first initialized
score: Array of Numbers, matches length and order of players. Tracks score of every player.
public: Boolean, True if game is listed so anyone can join
questionCount: The number of questions that will be asked in this game.\ timeLimit: the time limit for answering a question in the game. Default is -1, which means no limit\

Question object:

prompt: String, the actual question being asked
answers: Array of strings, the correct and incorrect answers, pre-shuffled
correctIndex: the index of answers that contains the correct answer
submitted: the index of the answers each player chose. The i'th element of submitted refers to the i'th player in the players array in the game schema. -1 if the player hasn't answered the question yet (pre-initialized to an array full of -1s)
category: the category the question belongs to. TheTriviaAPI presorts into the following 10 categories, and this field will match one of the following strings:
"music" "sport_and_leisure" "film_and_tv" "arts_and_literature" "history" "society_and_culture" "science" "geography" "food_and_drink" "general_knowledge"\

User Schema:

_id: default
name: String
email: String
games: Array of strings, which are the ids of every game that this player is a part of
stats: { categoryName: { correct: Number, answered: Number } }

category name is one of the following values: "music" "sport_and_leisure" "film_and_tv" "arts_and_literature" "history" "society_and_culture" "science" "geography" "food_and_drink" "general_knowledge".

Quiz Schema:

_id: default
creatorId: String, _id of the user.
public: Boolean, true then any user can start a game with it, if false, only the creator can
name: String
description: String
questions: Array of Question objects (See above)
dateCreated: Date\

API CALLS

POST to /api/games

Body must be an object of the format {user: userID, questionCount: number, timeLimit: number, public: bool, quizID: ID string}, where userID is the id of the user that started the game, questionCount is the number of questions in the game (defaults to 10, max is 50), timeLimit is what to set the timeLimit value to (default is -1, no limit). Public is true if anyone can access the game and false otherwise. If random questions are being used, quizID doesn't have to be included in the request, or it can be set to null or "". Should be the _id of a quiz entry that is either public or created by the same user starting the game.
Initializes a game, response data field contains the created game. Other players may join. Questions aren't initialized unless a custom quiz is being used.
(Also updates the user's game array)\

GET to /api/games

returns all games stored on server. Same query parameters as MP3 can be used.
/api/games?where={"started": false, "public": true} Gets all public games that haven't started yet.\

GET to /api/games/:id

id is the _id of the game. Returns the game object or 404 error.\

PUT to /api/games/join/:joincode

Body must be an object of the format {user: userID}, where userID is the id of the user that is joining the game
Updates the game object to add the player to the game. 404 error if the joincode doesn't exist.
(Also updates the user's game array)
Note this does not use the games id, it uses the join code.
Response data is the updated game object, which will give the front end access to the id.\

PUT to /api/games/start/:id

No body
Changes the state of the game from able to join to playing. (Started is true, join code is -1, waitingFor now lists players that need to take their turn). If not using custom quiz, calls TheTriviaApi and initializes questionCount questions for the game, changes any relevant game modes
Returns the new game object in the response\

PUT to /api/games/answer/:id

body: {user: userID, answer: answerIndex}.
Answerindex is a value from 0-3 representing the index of the answer in the answers array that the user chose.
They can only answer currentQuestion.
In game object updates the answers, score, waitingFor, lastAction and currentQuestion and completed if applicable
(i.e. sets completed to true if the game is over) reponse is new game object Also updates users stats\

POST to /api/games/:id/extend/:questionCount

No body
Adds questionCount more questions to the game from the triviaAPI.
If the game was previously marked as complete, it is now marked as incomplete
Updates relevant state, total questions in the game, who needs to take a turn, etc.
The only way to get a game with more than 50 random questions is to call this AFTER THE GAME HAS BEEN STARTED (call this before and it'll break)
Also could be used if you want to add extra questions as a tie-breaker if game is tied at the end.
Or you could ignore this, this feature isn't needed. \

POST to /api/users

Create a user
Body must be {name: String, email: String} \

GET to /api/users/:id

Get the user with the given id \

DELETE to /api/users/:id

Delete the user with this id. \

GET to /api/users

Get all users. Same sorting params as MP3 apply
For example, you could get a user with a certain email with a GET to /api/users?where{"email": "[email protected]"}

POST to /api/quiz

Create a new custom quiz.
The body matches the Quiz object
If questions are included at this step, their category must match one of the categories above, and "submitted" field should be left as a blank array.
Every field has a default value except for ownerId and name, so everything else can be functionally left blank and edited later.
Questions are designed to be added and edited one at a time with the API calls below.
returns the new quiz object.

GET to /api/quiz/:id

Get the quiz with the given ID. \

DELETE to /api/quiz/:id

Delete the quiz with the give ID \

PUT to /api/quiz/:id

DOESN'T CHANGE THE WHOLE OBJECT
Only updates name, description, and public attributes
Body is of format: {name: String, description: String, public: boolean}.
None of these fields have to be included in the body. If they aren't included, that field isn't changed.
The entire quiz object is returned.

GET to /api/quiz

Gets all the quizzes.
Uses the same query parameters as MP3.
GET to /api/quiz?where={creatorId: creator} to get all quizzes created by a person (i.e. to see what they can edit)
See next api call \

GET to /api/quiz/visible/:userId

Gets all the quizzes that userId could play, i.e. either created by them or public, no repetition. \

POST to /api/quiz/:id/question

Body should match the question schema. "Submitted" should not be included (doesn't matter if it is tho).
Verifies if "category" matches one of the categories listed.
Adds the question to the end of the quiz with the given ID.
Updated quiz object for the entire quiz is returned \

POST to /api/quiz/:id/question/:questionIndex

Body should match the question schema. "Submitted" should not be included (doesn't matter if it is tho).
Verifies if "category" matches one of the categories listed.
Adds the question to the quiz with the given Id, at questionIndex (i.e. if questionIndex is 0, inserts at the start of the quiz)
Updated quiz object for the entire quiz is returned \

PUT to /api/quiz/:id/question/:questionIndex

Body should match the question schema. "Submitted" should not be included (doesn't matter if it is tho).
Verifies if "category" matches one of the categories listed.
Replaces the question at questionIndex of the quiz with the given Id, with the provided one
Updated quiz object for the entire quiz is returned \

DELETE to /api/quiz/:id/question/:questionIndex

Deletes the question at questionIndex of the quiz with the given Id
Updated quiz object for the entire quiz is returned \

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors