-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.server.js
148 lines (123 loc) · 2.89 KB
/
game.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
var socketio = require( "socket.io" );
var http = require( "http" );
var express = require('express');
var path = require('path');
var app = express();
var server = http.createServer(app);
server.listen( 9892, function()
{
console.log("RunnerGame Server Start port:9892");
});
//localhost/port번호로 GameFramework.html에 접근 가능하게 해줌
app.get('/chat', function(req,res){
res.sendfile(__dirname + '/Client/GameFramework.html');
});
// /Client의 Static 파일들에 대한 접근을 가능케 함
app.use(express.static(path.join(__dirname, 'Client')));
var io = socketio.listen( server );
io.set("log level", 1);
io.sockets.on("connection", function(socket)
{
console.log("[Client Connected] " + socket.id);
// add Student
var student = new Student(socket.id);
arrStudents.push(student);
console.log("Student Count : " + arrStudents.length);
socket.on('disconnect', function()
{
console.log("[client Disconnected]" );
sendAllExcept(socket, "other_has_leaved", socket.id);
leaveStudent(socket.id);
});
socket.on("join_room", function(data)
{
console.log("[join room]");
var student = getStudentById(socket.id);
student.x = data.x;
student.y = data.y;
if(student == null)
return;
var arr = new Array();
for(var i in arrStudents)
{
var sd = arrStudents[i];
if( sd.id == socket.id)
continue;
arr.push( {id: sd.id, x: sd.x, y:sd.y });
}
socket.emit("other_players", arr);
// notify join stude to all other players
sendAllExcept(socket,
"join_newStudent",
{ id: socket.id
, x : data.x
, y : data.y});
});
socket.on("move_to", function(data)
{
console.log("[move_to] dx : " + data.dx + ", dy : " + data.dy);
var s = getStudentById( socket.id );
s.x += data.dx;
s.y += data.dy;
sendAllExcept(socket,
"move_to",
{ id: socket.id
, dx : data.dx
, dy : data.dy
, dir: data.dir});
});
socket.on("speak_to_all", function(data)
{
console.log("[speak_to_all] msg : " + data.msg);
sendAllExcept(socket,
"speak",
{ id: socket.id, msg: data.msg });
});
});
function Student(id)
{
this.id = id;
this.x = 0;
this.y = 0;
}
Student.prototype.emit = function(event, data) {
// io.sockets.(event, data);
};
function getStudentById( id )
{
for( var i in arrStudents)
{
var other = arrStudents[i];
if(other.id == id)
return other;
}
return null;
}
function sendAllExcept(socket, msg, data)
{
for( var i=0; i<arrStudents.length; ++i)
{
var otherStudent = arrStudents[i];
if(otherStudent.id == socket.id) {
continue;
}
socket.broadcast.to(otherStudent.id).emit(
msg,data);
}
}
function studentIndexOf(id)
{
for( var i in arrStudents)
{
var other = arrStudents[i];
if(other.id == id)
return i;
}
return -1;
}
function leaveStudent(studentId)
{
var indexToDel = studentIndexOf(studentId);
arrStudents.splice(indexToDel, 1);
}
var arrStudents = new Array();