-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cfdfd1
commit 7737925
Showing
20 changed files
with
407 additions
and
470 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,58 @@ | ||
export default class Room { | ||
/** | ||
* @param {string} name - The name of the room | ||
*/ | ||
constructor (name) { | ||
this.name = name; | ||
|
||
this.players = {}; | ||
this.sockets = {}; | ||
this.timer = { | ||
interval: null, | ||
timeRemaining: 0 | ||
}; | ||
} | ||
|
||
async message (userId, message) { throw new Error('Not implemented'); } | ||
|
||
/** | ||
* Sends a message to all sockets | ||
* @param {{}} message | ||
*/ | ||
emitMessage (message) { | ||
message = JSON.stringify(message); | ||
for (const socket of Object.values(this.sockets)) { | ||
socket.send(message); | ||
} | ||
} | ||
|
||
/** | ||
* Sends a message to a socket at a specific userId | ||
* @param {string} userId | ||
* @param {{}} message | ||
*/ | ||
sendToSocket (userId, message) { | ||
message = JSON.stringify(message); | ||
this.sockets[userId].send(message); | ||
} | ||
|
||
/** | ||
* @param {number} time - time in ticks, where 10 ticks = 1 second | ||
* @param {(time: number) => void} ontick - called every tick | ||
* @param {() => void} callback - called when timer is up | ||
* @returns {void} | ||
*/ | ||
startServerTimer (time, ontick, callback) { | ||
clearInterval(this.timer.interval); | ||
this.timer.timeRemaining = time; | ||
|
||
this.timer.interval = setInterval(() => { | ||
if (this.timer.timeRemaining <= 0) { | ||
clearInterval(this.timer.interval); | ||
callback(); | ||
} | ||
ontick(this.timer.timeRemaining); | ||
this.timer.timeRemaining--; | ||
}, 100); | ||
} | ||
} |
Oops, something went wrong.