11let dev ;
2+ global . rooms = [ ] ;
3+
4+ class Room {
5+ /**
6+ * Create a Room
7+ * @param {string } domain
8+ * @param {number } game_id
9+ * @param {string } sessionid
10+ * @param {string } name
11+ * @param {number } max
12+ * @param {number } current
13+ * @param {string } password
14+ * @param {string } userid
15+ * @param { import("socket.io").Socket } socket
16+ * @param {any } extra
17+ *
18+
19+ */
20+ constructor ( domain , game_id , sessionid , name , max , current , password , userid , socket , extra ) {
21+ /** @type string */
22+ this . domain = domain ;
23+ /** @type number */
24+ this . game_id = game_id ;
25+ /** @type string */
26+ this . sessionid = sessionid ;
27+ /** @type string */
28+ this . name = name ;
29+ /** @type number */
30+ this . max = max ;
31+ /** @type number */
32+ this . current = current ;
33+ /** @type string */
34+ this . password = password . trim ( ) ;
35+ /** @type boolean */
36+ this . hasPassword = ! ! this . password ;
37+ /** @type string */
38+
39+ this . id = domain + ':' + game_id + ':' + sessionid ;
40+
41+ // define user type
42+
43+ /**
44+ * @typedef {Object } User
45+ * @property {string } userid
46+ * @property { import("socket.io").Socket } socket
47+ * @property {any } extra
48+ */
49+
50+ /**
51+ * @type User
52+ */
53+ this . owner = {
54+ userid,
55+ socket,
56+ extra
57+ }
58+ /** @type User[] */
59+ this . users = [ this . owner ] ;
60+ }
61+ /**
62+ * Checks to see if the specified password matches this password
63+ * @param {string } password
64+ * @returns
65+ */
66+ checkPassword ( password ) {
67+ return password . trim ( ) === this . password ;
68+ }
69+ /**
70+ * Adds the user
71+ * @param {User } user
72+ *
73+ * @typedef {Object } User
74+ * @property {string } userid
75+ * @property { import("socket.io").Socket } socket
76+ * @property {any } extra
77+ */
78+ addUser ( user ) {
79+ this . users . forEach ( userr => {
80+ user . socket . emit ( 'user-connected' , userr . userid ) ;
81+ } )
82+ this . users . push ( user ) ;
83+ this . current ++ ;
84+ }
85+ }
286
387function start ( io , rooms , numusers , devv ) {
488 dev = devv ;
@@ -17,6 +101,15 @@ function start(io, rooms, numusers, devv) {
17101 }
18102}
19103
104+ function getRoom ( domain , game_id , sessionid ) {
105+ for ( let i = 0 ; i < global . rooms . length ; i ++ ) {
106+ if ( global . rooms [ i ] . id === domain + ':' + game_id + ':' + sessionid ) {
107+ return global . rooms [ i ] ;
108+ }
109+ }
110+ return null ;
111+ }
112+
20113function transformArgs ( url ) {
21114 var args = { }
22115 var idx = url . indexOf ( '?' )
@@ -37,4 +130,4 @@ function consolelog(message){
37130 console . log ( message ) ;
38131 }
39132}
40- module . exports = { start, transformArgs } ;
133+ module . exports = { getRoom , start, transformArgs } ;
0 commit comments