1
1
const MongoClient = require ( "mongodb" ) . MongoClient ;
2
- const mongoClient = new MongoClient ( "mongodb://127.0.0.1:27017/" ) ;
2
+ const config = require ( '../config' ) ;
3
+ const mongoClient = new MongoClient ( `mongodb://${ config . database . host } /` ) ;
3
4
4
5
class Database {
5
6
constructor ( ) {
@@ -10,16 +11,16 @@ class Database {
10
11
try {
11
12
await mongoClient . connect ( ) ;
12
13
13
- this . _db = mongoClient . db ( "l2db" ) ;
14
+ this . _db = mongoClient . db ( config . database . dbname ) ;
14
15
15
16
callback ( ) ;
16
17
} catch {
17
18
throw new Error ( "database connected: failed" ) ;
18
19
}
19
20
}
20
21
21
- async getAccountByLogin ( login ) {
22
- const user = await this . _db . collection ( 'accounts ' ) . findOne ( { login } ) ;
22
+ async getUserByLogin ( login ) {
23
+ const user = await this . _db . collection ( 'users ' ) . findOne ( { login } ) ;
23
24
24
25
if ( user ) {
25
26
return user ;
@@ -36,7 +37,7 @@ class Database {
36
37
return await this . _db . collection ( 'characters' ) . find ( { login } ) . toArray ( ) ;
37
38
}
38
39
39
- async checkNameExist ( name ) {
40
+ async checkCharacterNameExists ( name ) {
40
41
const character = await this . _db . collection ( 'characters' ) . findOne ( { name } ) ;
41
42
42
43
if ( character ) {
@@ -81,6 +82,54 @@ class Database {
81
82
async deleteCharacterByObjectId ( objectId ) {
82
83
await this . _db . collection ( 'characters' ) . deleteOne ( { objectId } ) ;
83
84
}
85
+
86
+ async addGameServer ( params ) {
87
+ await this . _db . collection ( 'gameservers' ) . insertOne ( {
88
+ id : params . id ,
89
+ host : params . host ,
90
+ port : params . port ,
91
+ ageLimit : params . ageLimit ,
92
+ isPvP : params . isPvP ,
93
+ maxPlayers : params . maxPlayers ,
94
+ status : params . status ,
95
+ type : params . type
96
+ } ) ;
97
+ }
98
+
99
+ async getGameServers ( ) {
100
+ return await this . _db . collection ( 'gameservers' ) . find ( ) . toArray ( ) ;
101
+ }
102
+
103
+ async getGameServerById ( id ) {
104
+ const gameserver = await this . _db . collection ( 'gameservers' ) . findOne ( { id } ) ;
105
+
106
+ if ( gameserver ) {
107
+ return gameserver ;
108
+ } else {
109
+ return null ;
110
+ }
111
+ }
112
+
113
+ async checkGameServerExistsById ( id ) {
114
+ const gameserver = await this . _db . collection ( 'gameservers' ) . findOne ( { id } ) ;
115
+
116
+ if ( gameserver ) {
117
+ return true ;
118
+ } else {
119
+ return false ;
120
+ }
121
+ }
122
+
123
+ async updateGameServerById ( id , field , value ) {
124
+ await this . _db . collection ( 'gameservers' ) . updateOne (
125
+ { id } ,
126
+ {
127
+ $set : {
128
+ [ field ] : value
129
+ }
130
+ }
131
+ ) ;
132
+ }
84
133
}
85
134
86
135
module . exports = new Database ( ) ;
0 commit comments