-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
97 lines (87 loc) · 2.22 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict"
let express = require('express')
let mysql = require('mysql');
let router = express.Router();
let database = {
connect : function() {
let connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '12345678',
database : 'dotbots'
});
connection.connect()
return connection
},
playerJoin : function(id,ip,name) {
let insert = {id : id,name : name,ip : ip,timestamp : Date.now(),score : 0,banned : 0};
let connection = database.connect();
connection.query("INSERT INTO players SET ?",insert,function(err,result) {
if(!err)
return true
else
return false
});
connection.end();
},
playerQuit : function(id) {
let connection = database.connect();
connection.query("DELETE FROM players WHERE id = " + connection.escape(id),function(err,result){
if(!err)
return true;
else
return false
});
connection.end();
},
getPlayerScore : function(id) {
if isNaN(id) return false
let connection = database.connect();
let sql = "SELECT score FROM players WHERE id = " + connection.escape(id);
connection.query(sql,function(err,rows,fields){
return rows[0].score;
});
connection.end();
},
getGlobalHighScores : function() {
let connection = database.connect();
let sql = "SELECT score FROM players ORDER BY DESC LIMIT 10"
connection.query(sql,function(err,rows,fields){
return rows
});
connection.end();
},
addChatLog : function(id,ip,message) {
let connection = database.connect();
let insert = {id : id,ip : ip,message : message,timestamp : Date.now()};
connection.query("INSERT INTO chats SET ?",insert,function(err,result){
if(!err)
return true;
});
connection.end();
},
banIp : function(ip) {
let connection = database.connect();
let sql = "UPDATE ips SET banned = 1 WHERE ip = " + ip;
connection.query(sql,function(err,results){
if(!err)
return true;
});
connection.end();
},
isIpBanned : function(ip) {
let connection = database.connect();
let sql = "SELECT banned FROM ips WHERE ip = " + ip;
connection.query(sql,function(err,rows,fields){
if(!err)
{
if(rows[0].banned)
return true;
else
return false;
}
});
connection.end();
}
};
module.exports = database;