-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (113 loc) · 3.14 KB
/
server.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
import path from 'path'
import express from 'express'
import Validator from 'redux-validator'
import http from 'http'
import socketIO from 'socket.io'
import { compose, createStore, applyMiddleware } from 'redux'
import rootReducer from './src/reducers'
import { Actions } from './src/constants'
import {
clickCell,
selectPiece,
resetGame,
connectPlayer,
connectPlayers,
disconnectPlayer,
completeTurn
} from './src/actions'
import webpackDevMiddleWare from 'webpack-dev-middleware'
import webpackHotMiddleWare from 'webpack-hot-middleware'
import webpack from 'webpack'
import config from './webpack.config.dev'
const {
SELECT_PIECE,
CLICK_CELL,
RESET_GAME,
COMPLETE_TURN
} = Actions
const compiler = webpack(config)
const app = express()
const server = http.Server(app)
const io = socketIO(server)
const validator = Validator()
const finalCreateStore = compose(
applyMiddleware(
validator
)
)(createStore)
const store = finalCreateStore(rootReducer)
function select(state, clientId) {
if(clientId){
return {
me: state.players.find(function(player){ return player.id === clientId}),
game: state.game
}
}else{
return {
game: state.game
}
}
}
// Validate that the correct player is making the move
function checkCorrectClientId(state, id){
// Make sure it's the correct client id
var state = store.getState()
var game = state.game
var players = state.players
var clientId = players.find(function(player){ return player.color === game.currentPlayerColor }).id
return clientId === id
}
store.subscribe(
function(){ io.emit('state', select(store.getState())) }
)
io.on('connection', function(socket){
store.dispatch(connectPlayers(Object.keys(io.sockets.connected)))
socket.emit('state', select(store.getState(), socket.id))
socket.on(SELECT_PIECE, function(payload){
if(!checkCorrectClientId(store.getState(), socket.id)){
io.emit('state', select(store.getState()))
return
}
var result = store.dispatch(selectPiece(payload))
if(result.err){
io.emit('state', select(store.getState()))
}
})
socket.on(CLICK_CELL, function(payload){
if(!checkCorrectClientId(store.getState(), socket.id)){
io.emit('state', select(store.getState()))
return
}
var result = store.dispatch(clickCell(payload))
if(result.err){
io.emit('state', select(store.getState()))
}
})
socket.on(COMPLETE_TURN, function(payload){
if(!checkCorrectClientId(store.getState(), socket.id)){
io.emit('state', select(store.getState()))
return
}
var result = store.dispatch(completeTurn(payload))
if(result.err){
io.emit('state', select(store.getState()))
}
})
socket.on(RESET_GAME, function(){
store.dispatch(resetGame())
})
})
app.use(webpackDevMiddleWare(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}))
app.use(webpackHotMiddleWare(compiler));
app.use(express.static(__dirname + '/public'))
app.use('/static', express.static(__dirname + '/dist'))
server.listen(process.env.PORT || 3000, function(err) {
if (err) {
console.log(err);
return;
}
})
export default server