-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
199 lines (162 loc) · 5.5 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const socketio = require('socket.io')
const express = require('express');
const Sequelize = require('sequelize');
const app = express();
const bodyParser = require('body-parser');
const morgan = require('morgan');
const tableOne = require('./server/models/questions.js');
const tableTwo = require('./server/models/user.js');
const tableThree = require('./server/models/answers.js');
//require for path.resolve
const path = require('path');
const apiRoutes = require('./routes/index.js');
const Questions = require('./server/repos/questionRepository')
const Answers = require('./server/repos/answerRepository')
const router = require('express').Router();
const { store } = require('./server/redux/store')
const { gettingQuestion, getRandomQuestion, gamePlayReducer, GET_QUESTION } = require('./server/redux/gamePlayReducer')
const { ADD_POINTS, addingPoints, addThePoints, pointsReducer } = require('./server/redux/pointsReducer')
const { addPlayer, updatePlayer, playerReducer, ADD_PLAYER, UPDATE_PLAYER } = require('./server/redux/playerReducer')
const { addNewPlayer } = require('./server/redux/newPlayerReducer')
//morgan middleware
app.use(morgan('dev'));
//implementing bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use('/api', apiRoutes)
//posting all static files in public folder
app.use(express.static('public'));
// current homepage
app.get('/*', function(req, res){
//path.resolve converts the relative paths to an absolute path
res.sendFile(path.resolve(__dirname, 'browser/index.html'))
})
let questionStored;
let answerStored;
let newPlayer
function getQuestion () {
return tableOne.findAll()
.then((questions) => {
let randomIndex = Math.floor(Math.random() * questions.length)
return questions[randomIndex]
})
.then((question) => {
questionStored = question.dataValues
tableThree.findAll({
where: {
QId: question.id
}}
)
.then((answers) => {
answerStored = answers
})
})
}
var allQuestions = []
function getAllQuestion() {
return tableOne.findAll()
.then((questions) => {
questions.forEach(function(question) {
//console.log('the question is !!!!!!', question.dataValues)
allQuestions.push(question.dataValues)
})
})
}
getAllQuestion()
var allAnswers = []
function getAllAnswers() {
return tableThree.findAll()
.then((answers) => {
answers.forEach(function(answer) {
allAnswers.push(answer.dataValues)
})
})
}
getAllAnswers()
getQuestion()
tableOne.sync({})
.then(function(){
return tableTwo.sync({})
})
.then(function(){
return tableThree.sync({})
})
.then(function(){
//listening on port 3001
const server = app.listen(3001, function(){
console.log('listening on 3001');
})
const io = socketio(server)
io.on('connection', function(socket) {
console.log('NEW CLIENT, id is', socket.id)
//listening for new players added
newPlayer = socket.id
store.dispatch(addNewPlayer(socket.id))
io.emit('newPlayer', socket.id)
store.dispatch(addPlayer(socket.id, 0))
let allPlayers = Object.keys(io.engine.clients)
socket.emit('multiPlayerReady', allPlayers)
socket.on('getNextQuestion', function() {
console.log('*****hitting get next question *******')
getQuestion()
store.dispatch(gettingQuestion({type: GET_QUESTION, question: {question: questionStored, answers: answerStored} }))
})
socket.on('updatePoints', function(updatedPoints){
//console.log('the new points are', updatedPoints.points, updatedPoints.id)
store.dispatch(addingPoints(updatedPoints.id, updatedPoints.points))
store.dispatch(updatePlayer(updatedPoints.id, updatedPoints.points))
//console.log('the store points are', store.getState().points)
socket.broadcast.emit('opponentUpdatedPoints', updatedPoints.points)
})
function updatePointsServerSide() {
socket.emit('pointsAreUpdated', function(){
console.log('points updated', store.getState())
})
}
// function startWhenAllPlayersJoin() {
// let allPlayers = Object.keys(io.engine.clients)
// if (allPlayers.length === 2) {
// socket.emit('multiPlayerReady', function () {})
// }
// }
//console.log('****************', allAnswers)
// let currState = store.getState().player
// function getOpponentPoints(playerId) {
// let opponentId
// for (let i = 0; i < allPlayers.length; i++) {
// if (playerId !== allPlayers[i]) {
// opponentId = allPlayers[i]
// }
// }
// for (let j = 0; j < currState.length; j++) {
// if (currState[j].id === opponentId) {
// let opponentPoints = currState[j].points
// return opponentPoints
// }
// }
// }
// function updateClientQuestion() {
// socket.emit('randomQuestion', {question: questionStored, answers: answerStored})
// }
console.log('the server store is*******************', store.getState())
function giveClientAllData() {
socket.emit('allData', {questions: allQuestions, answers: allAnswers})
}
socket.emit('newPlayerIsAdded', newPlayer)
store.subscribe(() => {
//updateClientQuestion()
updatePointsServerSide()
socket.emit('newPlayerIsAdded***********************', store.getState())
})
//emit this in the beginning to update the client store with questions
//updatePoints()
giveClientAllData()
//updateClientQuestion()
updatePointsServerSide()
//store.dispatch(gettingQuestion({type: GET_QUESTION, question: {question: questionStored, answers: answerStored} }))
//console.log('the store is',store.getState())
socket.on('disconnect', function(){
console.log('socket id ' + socket.id + ' has disconnected. :(')
})
})
})