-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
83 lines (79 loc) · 3.19 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
var io = require('socket.io').listen(8080, {
log: 0,
});
var colors = require('colors');
var clients = [];
var waiting = null;
var string = {
info:' info - '.bold.green,
message:' message - '.bold.blue,
update:' update - '.bold.cyan,
}
var datetime = new Date().toISOString();
console.log('Date: '.underline.bold.red+datetime.bold.yellow);
console.log('>>>>>>>>>>|| ELECTRON ||<<<<<<<<<<'.bold.yellow);
io.sockets.on('connection', function(socket) {
socket.on('regis', function( name ) {
console.log(string.info+'Player Connected : '.red.bold+name+" ("+this.id+")");
socket.name = name;
clients.push(this.id);
});
socket.on('findMatch', function() {
console.log(string.message+'Player finding match'.bold);
console.log(string.info+'Player : '.bold+socket.name+" ("+this.id+")");
if( waiting == null )
waiting = this.id;
else {
var enemy = io.sockets.socket(waiting);
console.log(string.message+'Match start'.bold);
console.log(string.info+'Player One : '.bold+enemy.name+" ("+waiting+")");
console.log(string.info+'Player Two : '.bold+socket.name+" ("+this.id+")");
socket.emit('startGame' , waiting,enemy.name );
enemy.emit('startGame' , this.id,socket.name );
waiting = null;
}
});
socket.on('stopFinding', function() {
if( waiting == this.id ) {
console.log(string.message+'Player stop finding match'.bold);
console.log(string.info+'Player : '.bold+socket.name+" ("+this.id+")");
waiting = null;
}
});
socket.on('sendBattleItem', function( itemKey,enemy ) {
var enemy = io.sockets.socket(enemy);
console.log(string.message+'Player use item Key : '.bold+itemKey);
console.log(string.info+'Player : '.bold+socket.name+" ("+this.id+")");
enemy.emit('effectBattleItem' , itemKey);
});
socket.on('sendLog', function( message,enemy ) {
console.log(string.update+message);
var enemy = io.sockets.socket(enemy);
socket.emit('logMessage' , message);
enemy.emit('logMessage' , message);
});
socket.on('endGame', function( enemyID,name,score,maxCombo,perfect,great,cool,miss ) {
socket.endStatus = ['showEnemyScore', name,score,maxCombo,perfect,great,cool,miss];
var enemy = io.sockets.socket(enemyID);
if( enemy.endStatus != null ) {
console.log(string.message+'Match end'.bold);
console.log(string.info+'Player One : '.bold+enemy.name+" ("+enemyID+")");
console.log(string.info+'Player Two : '.bold+socket.name+" ("+this.id+")");
socket.emit.apply(socket, enemy.endStatus);
enemy.emit.apply(enemy, socket.endStatus);
enemy.endStatus = null;
socket.endStatus = null;
}
});
socket.on('disconnect', function() {
if( waiting == this.id )
waiting = null;
console.log(string.info+"Player Disconnected : ".bold.red+socket.name+" ("+this.id+")");
for( var i=0;i<clients.length;i++){
if(clients[i] == this.id) {
clients.splice(i, 1);
break;
}
}
});
});