-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·267 lines (215 loc) · 6.38 KB
/
index.js
File metadata and controls
executable file
·267 lines (215 loc) · 6.38 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/env node
// OpenShift Node application
var express = require('express');
var fs = require('fs');
var path = require('path');
var http = require('http');
var fs = require('fs');
var execF = require('child_process');
var users = {
0: {
id_ : null,
nombre : '',
color : 'r'
},
1: {
id_ : null,
nombre : '',
color : 'a'
}
};
var ganador = ''
var fichas = [];
var turnoUsuario = 0;
var lectura = fs.createReadStream('jugadas.txt');
/**
* Define the application.
*/
var App = function() {
// Scope.
var self = this;
/* ================================================================ */
/* Helper functions. */
/* ================================================================ */
/* ================================================================ */
/* App server functions (main app logic here). */
/* ================================================================ */
/**
* Create the routing table entries + handlers for the application.
*/
self.createRoutes = function() {
self.routes = { };
self.routes['/'] = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.render('index', { title: 'Partyfy' });
};
};
/**
* Initialize the server (express) and create the routes and register
* the handlers.
*/
self.initializeServer = function() {
self.app = express();
self.app.set('port', (process.env.PORT || 5000))
self.app.set('views', path.join(__dirname, 'views'));
self.app.set('view engine', 'jade');
self.app.use(express.logger('dev'));
self.app.use(express.json());
self.app.use(express.urlencoded());
self.app.use(express.methodOverride());
self.app.use(self.app.router);
self.app.use(express.static(path.join(__dirname, 'static')));
//detect OS
self.os = process.platform
if ('development' == self.app.get('env')) {
self.app.use(express.errorHandler());
console.log("env");
}
// Add handlers for the app (from the routes)
self.createRoutes();
for (var r in self.routes) {
self.app.get(r, self.routes[r]);
}
};
/**
* Initializes the application.
*/
self.initialize = function() {
self.initializeServer();
};
/**
* Start the server (starts up the application).
*/
self.start = function() {
// Start the app on the specific interface (and port).
self.server = http.Server(self.app)
self.io = require('socket.io')(self.server);
self.server.listen(self.app.get('port'), function() {
console.log('%s: Node server started on :%d ...', Date(Date.now() ), self.app.get('port'));
});
};
self.startGame = function(){
fichas[0] = [];
fichas[1] = [];
fichas[2] = [];
fichas[3] = [];
fichas[4] = [];
fichas[5] = [];
turnoUsuario = 0;
};
self.runDLV = function(){
if (self.os == 'linux')
execF.exec('./dlv.bin win.dlv jugadas.txt -N=6 -nofacts -silent', dlvResult);
else{
if (self.os == "win32")
execF.exec('dlv.exe win.dlv jugadas.txt -N=6 -nofacts -silent', dlvResult);
}
};
function dlvResult(err, data){
if (err) {
console.log(err);
}
//ver si ganó
else{
ganador = data.trim();
switch(ganador){
case '{g(a)}':
self.io.to(users[1].id_).emit('resultado', {resultado : 'Ganaste'});
self.io.to(users[0].id_).emit('resultado', {resultado : 'Perdiste'});
break;
case '{g(r)}':
self.io.to(users[0].id_).emit('resultado', {resultado : 'Ganaste'});
self.io.to(users[1].id_).emit('resultado', {resultado : 'Perdiste'});
break;
default:
break;
}
}
}
self.addSocketIOEvents = function() {
self.io.on('connection', function(socket){
// Sorry ;___;
if (users[0].nombre === '') {
socket.usuario = 'Usuario 1';
users[0].nombre = 'Usuario 1';
users[0].id_ = socket.id;
socket.emit('hi', {user: 'Usuario 1', play: true, clase: 'user-1'})
}
else{
if (users[1].nombre === '') {
socket.usuario = 'Usuario 2';
users[1].nombre = 'Usuario 2';
users[1].id_ = socket.id;
socket.emit('hi', {user: 'Usuario 2', play: true, clase: 'user-2'});
}
}
if (users[0].nombre != '' && users[1].nombre != '') {
self.io.to(users[0].id_).emit('start');
self.io.emit('clean');
}
socket.on('disconnect', function(){
//console.log('Desconecto ' + socket.usuario);
if (socket.usuario === 'Usuario 1') {
users[0].nombre = '';
self.io.emit('bye', {mensaje: 'Usuario 1 ha salido'});
}
else{
if (socket.usuario === 'Usuario 2'){
users[1].nombre = '';
self.io.emit('bye', {mensaje: 'Usuario 2 ha salido'});
}
}
});
socket.on('start', function(){
self.startGame();
//console.log('juego iniciado');
self.io.to(users[0].id_).emit('tu turno');
self.io.to(users[1].id_).emit('espera turno');
//limpiar archivo
fs.writeFile('jugadas.txt', '' , function(err){
if(err){
console.log(err);
}
});
});
socket.on('movimiento', function(ficha){
if (socket.id == users[turnoUsuario].id_) {
//validar movimiento
var casilla = {};
var jugada = "";
//console.log(ficha)
if (fichas[ficha].length < 6) {
casilla.col = ficha + 1
casilla.fila = fichas[ficha].push(1);
casilla.user = turnoUsuario+1;
//procesar movimiento
//console.log(casilla);
self.io.emit('movimiento', casilla);
//escribir archivo
jugada = 'casilla(' + casilla.col + ',' + casilla.fila +',' + users[turnoUsuario].color + ').\n';
fs.appendFileSync('jugadas.txt',jugada),function(err){
console.log(err);
if (err) {
console.log(err);
}
}
//ejecutar dlv
self.runDLV();
//ajustar variables de turno
self.io.to(users[turnoUsuario].id_).emit('espera turno');
turnoUsuario = turnoUsuario == 0? 1 : 0;
self.io.to(users[turnoUsuario].id_).emit('tu turno');
}
}
self.io.emit('pintar',{col: 0, fila: 0, usuario: 0});
});
});
};
}; /* Application. */
/**
* main(): Main code.
*/
var conecta4 = new App();
conecta4.initialize();
conecta4.start();
conecta4.addSocketIOEvents();