generated from melaasar/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from AnoldGH/room
Room game functionalities
- Loading branch information
Showing
16 changed files
with
808 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const roomSchema = new mongoose.Schema({ | ||
players: { | ||
type: Array, | ||
default: [] // { userId, score } | ||
} | ||
}); | ||
|
||
// Save game records | ||
roomSchema.methods.saveGameRecords = async function (players) { | ||
const playerRecords = players.map((player) => ({ | ||
userId: player.id, | ||
score: player.score | ||
})); | ||
|
||
this.players = playerRecords; | ||
|
||
return await this.save(); | ||
} | ||
|
||
module.exports = mongoose.model("RoomModel", roomSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const express = require('express') | ||
require("dotenv").config() | ||
|
||
const DumbProxy = require("../controller/dumbProxy") | ||
// import { SpotifyProxy } from "../controller/spotifyProxy"; | ||
const { QuestionSet } = require("./questionSet") | ||
|
||
// Spotify proxy | ||
const proxy = DumbProxy.getInstance() | ||
|
||
// Generate a question set | ||
const generateQuestionSet = async(genre='pop', choices=4) => { | ||
let tracks = [] | ||
let withPreview = [] | ||
|
||
while (withPreview.length == 0) { | ||
tracks = await proxy.recommendTracks(genre, choices*2) // increase the hit rate of preview_url | ||
withPreview = tracks.filter((track) => track.preview_url) | ||
} | ||
|
||
const correctTrack = withPreview[0] | ||
const incorrectTracks = tracks | ||
.filter((track) => track !== correctTrack) | ||
.sort(() => 0.5 - Math.random()) | ||
.slice(0, choices - 1); | ||
const correct = Math.floor(Math.random() * choices) | ||
const finalTracks = [ | ||
...incorrectTracks.slice(0, correct), | ||
correctTrack, | ||
...incorrectTracks.slice(correct) | ||
] | ||
|
||
let qs = new QuestionSet(finalTracks, correct) // generate question sets | ||
return qs | ||
} | ||
|
||
// Generate question sets | ||
const generateQuestionSets = async(count, genre='pop', choices=4) => { | ||
let qss = [] | ||
|
||
for (let i = 0; i < count; i++) { | ||
qss.push(await generateQuestionSet(genre, choices)) | ||
} | ||
|
||
return qss | ||
} | ||
|
||
const test = async() => { | ||
let proxy = DumbProxy.getInstance() | ||
let qss = await generateQuestionSets(5) | ||
console.log(qss) | ||
} | ||
|
||
// test() | ||
|
||
module.exports = { | ||
generateQuestionSet, | ||
generateQuestionSets | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Player model used in Rooms | ||
class Player { // player is specific to room | ||
static STATE = { | ||
ACTIVE: 1, | ||
INACTIVE: 0 | ||
} | ||
|
||
constructor(userId) { | ||
this.id = userId // unique identifier | ||
this.score = 0 // starts with 0 score | ||
this.progress = 0 // idx of question set the player has reached | ||
this.state = Player.STATE.ACTIVE // 0 - inactive, 1 - active | ||
this.lastUpdate = Date.now() // last update | ||
} | ||
|
||
// Is the player expired? | ||
isExpired(expiration = 1000 * 60 * 5) { // default to be 5 minutes | ||
return Date.now() > this.lastUpdate + expiration | ||
} | ||
|
||
// Add score | ||
addScore(delta) { | ||
this.score += delta | ||
this.pulse() | ||
} | ||
|
||
// Answer question | ||
updateProgress(idx) { | ||
this.progress = idx + 1 | ||
this.pulse() | ||
} | ||
|
||
// Update the index of question set the player has reached | ||
answered(idx) { | ||
return idx < this.progress | ||
} | ||
|
||
// Pulse - update lastUpdate | ||
pulse() { | ||
this.lastUpdate = Date.now() | ||
} | ||
} | ||
|
||
module.exports = { | ||
Player | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class QuestionSet { // Class for multiple-choice with one answer | ||
static QID = 0 // unique identifier | ||
|
||
constructor(tracks, correct) { | ||
this.id = QuestionSet.QID++ | ||
|
||
this.options = tracks.filter((track) => { | ||
return { | ||
name: track.name, | ||
artist: track.artists[0].name | ||
} | ||
}) | ||
|
||
this.correct = correct | ||
this.url = tracks[correct].preview_url // url | ||
} | ||
|
||
// If choice is correct | ||
isCorrect(choice) { | ||
return choice == this.correct | ||
} | ||
} | ||
|
||
module.exports = { | ||
QuestionSet | ||
} |
Oops, something went wrong.