Commit 1b7ee273 authored by mercury233's avatar mercury233

add text color, add jsonp support

parent a51672cb
......@@ -88,7 +88,7 @@ net.createServer (client) ->
tribute(server)
server.closed = true unless server.closed
unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接")
ygopro.stoc_send_chat(client, "服务器关闭了连接", 11)
client.end()
return
......@@ -97,7 +97,7 @@ net.createServer (client) ->
tribute(server)
server.closed = error
unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}")
ygopro.stoc_send_chat(client, "服务器错误: #{error}", 11)
client.end()
return
......@@ -233,7 +233,7 @@ ygopro.ctos_follow 'PLAYER_INFO', true, (buffer, info, client, server)->
ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#log.info info
if settings.modules.stop
ygopro.stoc_send_chat(client,settings.modules.stop)
ygopro.stoc_send_chat(client,settings.modules.stop, 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
......@@ -241,7 +241,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
client.end()
else if info.version != settings.version
ygopro.stoc_send_chat(client,settings.modules.update)
ygopro.stoc_send_chat(client,settings.modules.update, 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 4
code: settings.version
......@@ -249,7 +249,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
client.end()
else if !info.pass.length and !settings.modules.enable_random_duel
ygopro.stoc_send_chat(client,"房间为空,请修改房间名")
ygopro.stoc_send_chat(client,"房间名为空,请填写主机密码", 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
......@@ -261,7 +261,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
# msg: 1
# code: 1 #这返错有问题,直接双ygopro直连怎么都正常,在这里就经常弹不出提示
#}
ygopro.stoc_send_chat(client,"房间密码不正确")
ygopro.stoc_send_chat(client,"房间密码不正确", 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
......@@ -279,14 +279,14 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
#log.info 'join_game',info.pass, client.name
client.room = Room.find_or_create_by_name(info.pass, client.name)
if !client.room
ygopro.stoc_send_chat(client,"服务器已经爆满,请稍候再试")
ygopro.stoc_send_chat(client,"服务器已经爆满,请稍候再试", 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
}
client.end()
else if client.room.error
ygopro.stoc_send_chat(client,client.room.error)
ygopro.stoc_send_chat(client,client.room.error,11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
......@@ -297,11 +297,11 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
client.is_post_watcher = true
ygopro.stoc_send_chat_to_room client.room, "#{client.name} 加入了观战"
client.room.watchers.push client
ygopro.stoc_send_chat client, "观战中", 14
for buffer in client.room.watcher_buffers
client.write buffer
ygopro.stoc_send_chat client, "观战中."
else
ygopro.stoc_send_chat(client,"决斗已开始")
ygopro.stoc_send_chat(client,"决斗已开始,不允许观战", 11)
ygopro.stoc_send client, 'ERROR_MSG',{
msg: 1
code: 2
......@@ -317,7 +317,7 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
if settings.modules.welcome
ygopro.stoc_send_chat client, settings.modules.welcome
if client.room.welcome
ygopro.stoc_send_chat client, client.room.welcome
ygopro.stoc_send_chat client, client.room.welcome, 14
if settings.modules.post_start_watching and !client.room.watcher
client.room.watcher = watcher = net.connect client.room.port, ->
......@@ -386,7 +386,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp -= val
if 0 < client.room.dueling_players[pos].lp <= 100
ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!")
ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!", 15)
if ygopro.constants.MSG[msg] == 'RECOVER' and client.is_host
pos = buffer.readUInt8(1)
......@@ -406,7 +406,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
val = buffer.readInt32LE(2)
client.room.dueling_players[pos].lp -= val
if 0 < client.room.dueling_players[pos].lp <= 100
ygopro.stoc_send_chat_to_room(client.room, "背水一战!")
ygopro.stoc_send_chat_to_room(client.room, "背水一战!", 15)
#登场台词
if settings.modules.dialogues
......@@ -414,7 +414,7 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
card = buffer.readUInt32LE(1)
if dialogues[card]
for line in _.lines dialogues[card][Math.floor(Math.random() * dialogues[card].length)]
ygopro.stoc_send_chat client, line
ygopro.stoc_send_chat client, line, 15
return
#房间管理
......@@ -477,8 +477,6 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server)->
when '/tip'
ygopro.stoc_send_random_tip(client) if settings.modules.tips
when '/test'
log.info Room.players_oppentlist
return cancel
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
......@@ -492,57 +490,56 @@ ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
#http
if settings.modules.http
http_server = http.createServer (request, response)->
u = url.parse(request.url,1)
#log.info u
if u.pathname == '/count.json'
response.writeHead(200);
response.end(Room.all.length.toString())
else if u.pathname == '/rooms.js'
response.writeHead(200);
roomsjson = JSON.stringify rooms: (for room in Room.all when room.established
roomid: room.port.toString(),
roomname: room.name.split('$',2)[0],
needpass: (room.name.indexOf('$') != -1).toString(),
users: (for player in room.players when player.pos?
id: (-1).toString(),
name: player.name,
pos: player.pos
),
istart: if room.started then 'start' else 'wait'
)
response.end("loadroom( " + roomsjson + " );");
# todo: 增加JSONP支持
else if u.query.operation == 'getroomjson'
response.writeHead(200);
response.end JSON.stringify rooms: (for room in Room.all when room.established
roomid: room.port.toString(),
roomname: room.name.split('$',2)[0],
needpass: (room.name.indexOf('$') != -1).toString(),
users: (for player in room.players when player.pos?
id: (-1).toString(),
name: player.name,
pos: player.pos
),
istart: if room.started then "start" else "wait"
)
else if u.query.pass == settings.modules.http.password && u.query.shout
for room in Room.all
ygopro.stoc_send_chat_to_room(room, u.query.shout)
response.writeHead(200)
response.end("shout " + u.query.shout + " ok")
else if u.query.pass == settings.modules.http.password && u.query.stop
settings.modules.stop = u.query.stop
response.writeHead(200)
response.end("stop " + u.query.stop + " ok")
else if u.query.pass == settings.modules.http.password && u.query.welcome
settings.modules.welcome = u.query.welcome
response.writeHead(200)
response.end("welcome " + u.query.welcome + " ok")
parseQueryString = true
u = url.parse(request.url, parseQueryString)
pass_validated = u.query.pass == settings.modules.http.password
if u.pathname == '/api/getrooms'
if u.query.pass and !pass_validated
response.writeHead(200);
response.end(u.query.callback+'( {"rooms":[{"roomid":"0","roomname":"密码错误","needpass":"true"}]} );')
else
response.writeHead(200);
roomsjson = JSON.stringify rooms: (for room in Room.all when room.established
roomid: room.port.toString(),
roomname: if pass_validated then room.name else room.name.split('$',2)[0],
needpass: (room.name.indexOf('$') != -1).toString(),
users: (for player in room.players when player.pos?
id: (-1).toString(),
name: player.name,
pos: player.pos
),
istart: if room.started then 'start' else 'wait'
)
response.end(u.query.callback+"( " + roomsjson + " );")
else if u.pathname == '/api/message'
if !pass_validated
response.writeHead(200);
response.end(u.query.callback+"( '密码错误', 0 );");
return
if u.query.shout
for room in Room.all
ygopro.stoc_send_chat_to_room(room, u.query.shout, 16)
response.writeHead(200)
response.end(u.query.callback+"( 'shout ok', '" + u.query.shout + "' );")
else if u.query.stop
if u.query.stop == 'false'
u.query.stop=false
settings.modules.stop = u.query.stop
response.writeHead(200)
response.end(u.query.callback+"( 'stop ok', '" + u.query.stop + "' );")
else if u.query.welcome
settings.modules.welcome = u.query.welcome
response.writeHead(200)
response.end(u.query.callback+"( 'welcome ok', '" + u.query.welcome + "' );")
else
response.writeHead(404);
response.end();
else
response.writeHead(404);
......
......@@ -109,7 +109,7 @@
server.closed = true;
}
if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器关闭了连接");
ygopro.stoc_send_chat(client, "服务器关闭了连接", 11);
client.end();
}
});
......@@ -117,7 +117,7 @@
tribute(server);
server.closed = error;
if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器错误: " + error);
ygopro.stoc_send_chat(client, "服务器错误: " + error, 11);
client.end();
}
});
......@@ -258,28 +258,28 @@
ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
var k, len, ref;
if (settings.modules.stop) {
ygopro.stoc_send_chat(client, settings.modules.stop);
ygopro.stoc_send_chat(client, settings.modules.stop, 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (info.version !== settings.version) {
ygopro.stoc_send_chat(client, settings.modules.update);
ygopro.stoc_send_chat(client, settings.modules.update, 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 4,
code: settings.version
});
client.end();
} else if (!info.pass.length && !settings.modules.enable_random_duel) {
ygopro.stoc_send_chat(client, "房间为空,请修改房间名");
ygopro.stoc_send_chat(client, "房间名为空,请填写主机密码", 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (!Room.validate(info.pass)) {
ygopro.stoc_send_chat(client, "房间密码不正确");
ygopro.stoc_send_chat(client, "房间密码不正确", 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
......@@ -294,14 +294,14 @@
} else {
client.room = Room.find_or_create_by_name(info.pass, client.name);
if (!client.room) {
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试");
ygopro.stoc_send_chat(client, "服务器已经爆满,请稍候再试", 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
});
client.end();
} else if (client.room.error) {
ygopro.stoc_send_chat(client, client.room.error);
ygopro.stoc_send_chat(client, client.room.error, 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
......@@ -312,14 +312,14 @@
client.is_post_watcher = true;
ygopro.stoc_send_chat_to_room(client.room, client.name + " 加入了观战");
client.room.watchers.push(client);
ygopro.stoc_send_chat(client, "观战中", 14);
ref = client.room.watcher_buffers;
for (k = 0, len = ref.length; k < len; k++) {
buffer = ref[k];
client.write(buffer);
}
ygopro.stoc_send_chat(client, "观战中.");
} else {
ygopro.stoc_send_chat(client, "决斗已开始");
ygopro.stoc_send_chat(client, "决斗已开始,不允许观战", 11);
ygopro.stoc_send(client, 'ERROR_MSG', {
msg: 1,
code: 2
......@@ -341,7 +341,7 @@
ygopro.stoc_send_chat(client, settings.modules.welcome);
}
if (client.room.welcome) {
ygopro.stoc_send_chat(client, client.room.welcome);
ygopro.stoc_send_chat(client, client.room.welcome, 14);
}
if (settings.modules.post_start_watching && !client.room.watcher) {
client.room.watcher = watcher = net.connect(client.room.port, function() {
......@@ -415,7 +415,7 @@
val = buffer.readInt32LE(2);
client.room.dueling_players[pos].lp -= val;
if ((0 < (ref = client.room.dueling_players[pos].lp) && ref <= 100)) {
ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!");
ygopro.stoc_send_chat_to_room(client.room, "你的生命已经如风中残烛了!", 15);
}
}
if (ygopro.constants.MSG[msg] === 'RECOVER' && client.is_host) {
......@@ -442,7 +442,7 @@
val = buffer.readInt32LE(2);
client.room.dueling_players[pos].lp -= val;
if ((0 < (ref1 = client.room.dueling_players[pos].lp) && ref1 <= 100)) {
ygopro.stoc_send_chat_to_room(client.room, "背水一战!");
ygopro.stoc_send_chat_to_room(client.room, "背水一战!", 15);
}
}
if (settings.modules.dialogues) {
......@@ -452,7 +452,7 @@
ref2 = _.lines(dialogues[card][Math.floor(Math.random() * dialogues[card].length)]);
for (k = 0, len = ref2.length; k < len; k++) {
line = ref2[k];
ygopro.stoc_send_chat(client, line);
ygopro.stoc_send_chat(client, line, 15);
}
}
}
......@@ -535,9 +535,6 @@
if (settings.modules.tips) {
ygopro.stoc_send_random_tip(client);
}
break;
case '/test':
log.info(Room.players_oppentlist);
}
return cancel;
});
......@@ -566,102 +563,82 @@
if (settings.modules.http) {
http_server = http.createServer(function(request, response) {
var k, len, player, ref, room, roomsjson, u;
u = url.parse(request.url, 1);
if (u.pathname === '/count.json') {
response.writeHead(200);
response.end(Room.all.length.toString());
} else if (u.pathname === '/rooms.js') {
response.writeHead(200);
roomsjson = JSON.stringify({
rooms: (function() {
var k, len, ref, results;
ref = Room.all;
results = [];
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
if (room.established) {
results.push({
roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() {
var l, len1, ref1, results1;
ref1 = room.players;
results1 = [];
for (l = 0, len1 = ref1.length; l < len1; l++) {
player = ref1[l];
if (player.pos != null) {
results1.push({
id: (-1).toString(),
name: player.name,
pos: player.pos
});
}
}
return results1;
})(),
istart: room.started ? 'start' : 'wait'
});
}
}
return results;
})()
});
response.end("loadroom( " + roomsjson + " );");
} else if (u.query.operation === 'getroomjson') {
response.writeHead(200);
response.end(JSON.stringify({
rooms: (function() {
var k, len, ref, results;
ref = Room.all;
results = [];
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
if (room.established) {
results.push({
roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() {
var l, len1, ref1, results1;
ref1 = room.players;
results1 = [];
for (l = 0, len1 = ref1.length; l < len1; l++) {
player = ref1[l];
if (player.pos != null) {
results1.push({
id: (-1).toString(),
name: player.name,
pos: player.pos
});
var k, len, parseQueryString, pass_validated, player, ref, room, roomsjson, u;
parseQueryString = true;
u = url.parse(request.url, parseQueryString);
pass_validated = u.query.pass === settings.modules.http.password;
if (u.pathname === '/api/getrooms') {
if (u.query.pass && !pass_validated) {
response.writeHead(200);
response.end(u.query.callback + '( {"rooms":[{"roomid":"0","roomname":"密码错误","needpass":"true"}]} );');
} else {
response.writeHead(200);
roomsjson = JSON.stringify({
rooms: (function() {
var k, len, ref, results;
ref = Room.all;
results = [];
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
if (room.established) {
results.push({
roomid: room.port.toString(),
roomname: pass_validated ? room.name : room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() {
var l, len1, ref1, results1;
ref1 = room.players;
results1 = [];
for (l = 0, len1 = ref1.length; l < len1; l++) {
player = ref1[l];
if (player.pos != null) {
results1.push({
id: (-1).toString(),
name: player.name,
pos: player.pos
});
}
}
}
return results1;
})(),
istart: room.started ? "start" : "wait"
});
return results1;
})(),
istart: room.started ? 'start' : 'wait'
});
}
}
}
return results;
})()
}));
} else if (u.query.pass === settings.modules.http.password && u.query.shout) {
ref = Room.all;
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
ygopro.stoc_send_chat_to_room(room, u.query.shout);
return results;
})()
});
response.end(u.query.callback + "( " + roomsjson + " );");
}
} else if (u.pathname === '/api/message') {
if (!pass_validated) {
response.writeHead(200);
response.end(u.query.callback + "( '密码错误', 0 );");
return;
}
if (u.query.shout) {
ref = Room.all;
for (k = 0, len = ref.length; k < len; k++) {
room = ref[k];
ygopro.stoc_send_chat_to_room(room, u.query.shout, 16);
}
response.writeHead(200);
response.end(u.query.callback + "( 'shout ok', '" + u.query.shout + "' );");
} else if (u.query.stop) {
if (u.query.stop === 'false') {
u.query.stop = false;
}
settings.modules.stop = u.query.stop;
response.writeHead(200);
response.end(u.query.callback + "( 'stop ok', '" + u.query.stop + "' );");
} else if (u.query.welcome) {
settings.modules.welcome = u.query.welcome;
response.writeHead(200);
response.end(u.query.callback + "( 'welcome ok', '" + u.query.welcome + "' );");
} else {
response.writeHead(404);
response.end();
}
response.writeHead(200);
response.end("shout " + u.query.shout + " ok");
} else if (u.query.pass === settings.modules.http.password && u.query.stop) {
settings.modules.stop = u.query.stop;
response.writeHead(200);
response.end("stop " + u.query.stop + " ok");
} else if (u.query.pass === settings.modules.http.password && u.query.welcome) {
settings.modules.welcome = u.query.welcome;
response.writeHead(200);
response.end("welcome " + u.query.welcome + " ok");
} else {
response.writeHead(404);
response.end();
......
......@@ -114,6 +114,8 @@ for name, declaration of structs_declaration
console.log "err stoc_send_chat"
return
for line in _.lines(msg)
if player>=10
line="[System]: "+line
@stoc_send client, 'CHAT', {
player: player
msg: line
......
......@@ -178,6 +178,9 @@
ref = _.lines(msg);
for (j = 0, len1 = ref.length; j < len1; j++) {
line = ref[j];
if (player >= 10) {
line = "[System]: " + line;
}
this.stoc_send(client, 'CHAT', {
player: player,
msg: line
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment