forked from rekyuu-archive/rock-paper-scissors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
167 lines (145 loc) · 5.74 KB
/
gulpfile.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
var gulp = require('gulp');
// In-house plugins
var del = require('del'),
spawn = require('child_process').spawn,
concat = require('gulp-concat'),
livereload = require('gulp-livereload'),
plumber = require('gulp-plumber'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
path = require('path');
var port = 8080;
// Clean the build folder
gulp.task('clean', function (cb) {
del(['build'], cb);
});
// Process SCSS files
gulp.task('scss', function () {
gulp.src('./src/scss/**/*.scss')
.pipe(plumber())
.pipe(sass())
.pipe(gulp.dest('./build/static/css'))
.pipe(livereload());
});
// Process JS files
gulp.task('js', function () {
return gulp.src('./src/js/**/*.js')
.pipe(plumber())
// .pipe(sourcemaps.init())
// .pipe(uglify())
// .pipe(concat('all.min.js'))
// .pipe(sourcemaps.write())
.pipe(gulp.dest('./build/static/js'))
.pipe(livereload());
});
// Process images
gulp.task('img', function () {
gulp.src('./src/img/**/*')
.pipe(plumber())
.pipe(gulp.dest('./build/static/img'))
.pipe(livereload());
});
// Process HTML files
gulp.task('html', function () {
return gulp.src('./src/jade/**/*.jade')
.pipe(gulp.dest('./build/views'))
.pipe(livereload());
});
// Socket.io
gulp.task('io', function () {
var usernames = {};
var choices = [];
// Fires when the user connects.
io.on('connection', function (socket) {
var user_added = false;
// Checks to see if there are already two players.
// Otherwise, the connector will spectate.
if (Object.keys(usernames).length == 2) {
io.emit('room full');
io.emit('user list', usernames);
}
// Fires once the connector types a username and hits ENTER.
socket.on('add user', function (username) {
// Double checks to make sure a third user is not added.
if (Object.keys(usernames).length == 2) {
io.emit('room full');
io.emit('user list', usernames);
} else {
socket.username = username;
usernames[username] = username;
user_added = true;
io.emit('user list', usernames);
console.log('[socket.io] %s has connected.', socket.username);
// Once there are two players, the game will start.
if (Object.keys(usernames).length == 2) {
io.emit('game start');
}
}
});
// Listens for choice submissions from the players.
socket.on('player choice', function (username, choice) {
choices.push({'user': username, 'choice': choice});
console.log('[socket.io] %s chose %s.', username, choice);
// Once both players have submitted a choice, the game checks for the winner.
if (choices.length == 2) {
console.log('[socket.io] Both players have made choices.');
if (choices[0]['choice'] === 'rock') {
if (choices[1]['choice'] === 'rock') io.emit('tie', choices);
if (choices[1]['choice'] === 'paper') io.emit('player 2 win', choices);
if (choices[1]['choice'] === 'scissors') io.emit('player 1 win', choices);
choices = [];
} else if (choices[0]['choice'] === 'paper') {
if (choices[1]['choice'] === 'rock') io.emit('player 1 win', choices);
if (choices[1]['choice'] === 'paper') io.emit('tie', choices);
if (choices[1]['choice'] === 'scissors') io.emit('player 2 win', choices);
choices = [];
} else if (choices[0]['choice'] === 'scissors') {
if (choices[1]['choice'] === 'rock') io.emit('player 2 win', choices);
if (choices[1]['choice'] === 'paper') io.emit('player 1 win', choices);
if (choices[1]['choice'] === 'scissors') io.emit('tie', choices);
choices = [];
}
}
});
// Fires when a user disconnects.
socket.on('disconnect', function () {
// Removes player from the list and resets the game.
if (user_added) {
delete usernames[socket.username];
io.emit('user list', usernames);
console.log('[socket.io] %s has disconnected.', socket.username);
choices = [];
}
});
});
});
// Server routing
gulp.task('server', function () {
app.set('view engine', 'jade');
app.set('views', './build/views');
app.use(express.static('./build/static'));
app.get('/', function (req, res) {
res.render('index', {
title: 'Basic Page',
text: "This index.html page is a placeholder with the CSS, font and favicon. It's just waiting for you to add some content! If you need some help hit up the <a href='http://www.getskeleton.com'>Skeleton documentation</a>."
});
});
http.listen(port, function(){
console.log('[Server] Listening on *:%d', port);
});
});
// Run the server and watch files for changes
gulp.task('watch', function () {
livereload.listen();
gulp.watch('./src/scss/**/*.scss', ['scss']);
gulp.watch('./src/js/**/*.js', ['js']);
gulp.watch('./src/img/**/*', ['img']);
gulp.watch('./src/jade/**/*.jade', ['html']);
});
// Run gulp tasks
gulp.task('default', ['scss', 'js', 'img', 'html', 'io', 'server', 'watch']);