forked from zlcaosh/Paris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (69 loc) · 2.29 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
// service modules
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
var bodyParser = require('body-parser')
var jade = require('jade')
var path = require('path')
// game modules
var lang = require('./modules/lang')
var reshuffle = require('./modules/reshuffle')
var suitSelector = require('./modules/suit-selector')
// service setting
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
// template rendering setting
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
// routes
app.get('/', function(req, res) {
res.render('texas', lang)
})
app.get('/*', function(req, res) {
res.redirect('/')
})
players = {}
// web socket
io.of('/texas').on('connection', function(player) {
// home
player.on('home.enterRoom', function(data) {
console.log('home.enterRoom', data)
var playerStatus = players[player.id] = {name: data.name, cash: 10000, id: player.id}
player.emit('home.enteredRoom', playerStatus)
})
// room
player.on('room.joinGame', function(data) {
console.log('room.joinGame', data)
var playerStatus = players[data.id]
playerStatus.game = data.game
playerStatus.seat = data.seat
player.emit('room.joinedGame', playerStatus)
})
player.on('room.exitRoom', function(data) {
console.log('room.exitRoom', data)
players[data.id] = null
player.emit('room.exitedRoom', {id: data.id})
})
// game
player.on('game.exitGame', function(data) {
console.log('game.exitGame', data)
var playerStatus = players[data.id]
playerStatus.game = -1
playerStatus.seat = -1
playerStatus.ready = false
player.emit('game.exitedGame', playerStatus)
})
player.on('game.startGame', function(data) {
console.log('game.startGame', data)
var playerStatus = players[data.id]
playerStatus.ready = true
player.emit('game.startedGame', playerStatus) // TODO: emit on all players ready
})
player.emit('home.connected', {id: player.id})
})
// serving
var host = (process.env.VCAP_APP_HOST || 'localhost')
var port = (process.env.VCAP_APP_PORT || 80)
server.listen(port, host)