-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacontroller.js
81 lines (62 loc) · 2.63 KB
/
datacontroller.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
const mysql = require('mysql');
class DataController{
constructor(){
this.connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
});
this.connection.connect(function(err){
if(err) throw err;
console.log("Connected to database!");
});
}
getHighscore(sock, gridSize, cbfunc){
let highscoreList = new Array();
this.connection.query('SELECT * FROM highscore' + gridSize + ' ORDER BY points DESC', (err,rows) => {
if(err) throw err;
//console.log('Data received from Db: highscore'+ gridSize);
//console.log(rows);
//console.log(rows[1].name);
rows.forEach( (row) => {
highscoreList.push({name: row.name, points: row.points});
//console.log(`${row.name} hat ${row.points}`);
});
//console.log(highscoreList.length);
//console.log(highscoreList[0]);
cbfunc(sock, JSON.stringify({gridsize: gridSize, list: highscoreList}));
});
//console.log(highscoreList.length);
//return "highscoreList";
}
getHighscoreList(gridSize, cbfunc){
let highscoreList = new Array();
this.connection.query('SELECT * FROM highscore' + gridSize + ' ORDER BY points DESC', (err,rows) => {
if(err) throw err;
//console.log('Data received from Db: highscore' + gridSize);
//console.log(rows);
//console.log(rows[1].name);
rows.forEach( (row) => {
highscoreList.push({name: row.name, points: row.points});
//console.log(`${row.name} hat ${row.points}`);
});
//console.log(highscoreList.length);
//console.log(highscoreList[0]);
cbfunc(JSON.stringify({gridsize: gridSize, list: highscoreList}));
});
//console.log(highscoreList.length);
//return "highscoreList";
}
addHighscoreObject(data, cbfunc){
let highscoreObject = JSON.parse(data);
//console.log(highscoreObject);
let sql = "INSERT INTO highscore" + highscoreObject.gridSize + " (name, points) VALUES ('" + highscoreObject.name + "', " + highscoreObject.points + ")";
this.connection.query(sql, function(err, result){
if(err) throw err;
//console.log("1 highscore added");
});
this.getHighscoreList(highscoreObject.gridSize,cbfunc);
}
}
module.exports = DataController;