-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
126 lines (103 loc) · 4.12 KB
/
game.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var path = require('path');
var Player = require('./player.js');
var Lobby = require('./lobby.js');
var Model = require('./models');
// console.log(sid);
var PlayerList = {};
var SudokuGame = require('./sudokuGame.js');
var sudokuGame = new SudokuGame(3);
//app.use(express.static(path.join(__dirname, 'assets')));
// app.get('/', function(req, res) {
// res.sendFile(__dirname + '/client/main.html'), (__dirname + '/client/assets/css/style.css');
// });
// app.use(express.static('client/'));
// var io = require('socket.io')(serv, {});
var rooms = ['room1', 'room2', 'room3'];
var testArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var testArray2 = [5, 8, 3, 8, 9, 2, 4, 6, 7, 8];
var testArray3 = ['', '', '', ''];
var tempArray = ['bill', 'john', 'mike', 'mando', 'sarah', 'pip', 'lucky', 'christy', 'katie', 'maddox', 'ella'];
var SOCKET_LIST = {};
var PLAYER_LIST = {};
var GAME_LIST = {};
var USER_LIST = {
bob: 'abc',
mike: 'def',
sam: 'ghi',
};
module.exports = function(io) {
var lobby = new Lobby();
Player.connectPlayer = function(socket) { // this is where the database code need to go
var player = new Player(socket.id);
player.setName(tempArray[Math.floor(10 * Math.random())]);
PLAYER_LIST[socket.id] = player;
lobby.addPlayer(player);
socket.emit("playerObject", player);
}
Player.disconnect = function(socket) {
delete PLAYER_LIST[socket.id];
}
io.sockets.on('connection', function(socket) {
console.log("connected ");
socket.id = Math.random() * 100;
Player.connectPlayer(socket);
SOCKET_LIST[socket.id] = socket;
socket.on("numberPressed", function(data) // listens for number pressed
{
console.dir("data id " + JSON.stringify(data));
var player = lobby.getPlayer(data.id);
// console.log(JSON.stringify(player));
var game = lobby.getGame(player.gameId);
console.log(game.getGameId());
// var playerFromList = lobby.getPlayer()
// var numberOfCorrect = checkArray(data.array, lobby.testArray);
// console.log("numberPressed " + numberOfCorrect);
// var percentComplete = (numberOfCorrect / testArray.length) * 100;
// player.completed = percentComplete;
// console.log(percentComplete + " pecent complerte");
console.log("number pressed " + data.number);
});
socket.on('arrayTester', function(data) { // todo
var numberOfCorrect = checkArray(data, testArray);
console.log("numberPressed " + numberOfCorrect);
var percentComplete = (numberOfCorrect / testArray.length) * 100;
player.completed = percentComplete;
if (percentComplete === 100) {
console.log("winner");
}
})
socket.on("disconnect", function() {
delete PLAYER_LIST[socket.id];
});
socket.on("chatMessage", function(data, playerName) {
console.log("chat " + data);
for (var i in SOCKET_LIST) {
SOCKET_LIST[i].emit("addchat", playerName + ":" + data);
}
});
}); // end of io on
var checkArray = function(ary1, ary2) { // TODO move this
var correctAnsewers = 0;
for (var i = 0; i < ary1.length; i++) {
if (parseInt(ary1[i]) === parseInt(ary2[i])) {
correctAnsewers++;
}
}
return correctAnsewers;
}
console.log("check array " + checkArray(testArray, testArray2));
console.log("%correct " + (checkArray(testArray, testArray2) / 10) * 100);
setInterval(function() {
//console.log()
if (lobby.checkIfEnoughPlayers()) {
console.log("inside checkIfEnoughPlayers if statement");
lobby.createGame();
}
if (lobby.checkForWinner() != null) {
var list = lobby.checkForWinner();
for (var i in list) {
console.log(list[i].name);
}
}
}, 10000 / 5);
}