Commit b6b8122a authored by mercury233's avatar mercury233

for 8.15 weekly game

parent 1423a319
...@@ -54,9 +54,9 @@ class Room ...@@ -54,9 +54,9 @@ class Room
@status = 'starting' @status = 'starting'
@established = false @established = false
@watcher_buffers = [] @watcher_buffers = []
@watcher_stanzas = [] #@watcher_stanzas = []
@watchers = [] @watchers = []
@ws_watchers = [] #@ws_watchers = []
Room.all.push this Room.all.push this
@hostinfo = @hostinfo =
...@@ -75,6 +75,7 @@ class Room ...@@ -75,6 +75,7 @@ class Room
@hostinfo.mode = 1 @hostinfo.mode = 1
else if name[0...2] == 'T#' else if name[0...2] == 'T#'
@hostinfo.mode = 2 @hostinfo.mode = 2
@hostinfo.start_lp = 16000
else if (param = name.match /^(\d)(\d)(T|F)(T|F)(T|F)(\d+),(\d+),(\d+)/i) else if (param = name.match /^(\d)(\d)(T|F)(T|F)(T|F)(\d+),(\d+),(\d+)/i)
@hostinfo.rule = parseInt(param[1]) @hostinfo.rule = parseInt(param[1])
@hostinfo.mode = parseInt(param[2]) @hostinfo.mode = parseInt(param[2])
...@@ -107,6 +108,7 @@ class Room ...@@ -107,6 +108,7 @@ class Room
@save_match() if _.startsWith(@name, 'M#') and @started and settings.modules.database @save_match() if _.startsWith(@name, 'M#') and @started and settings.modules.database
index = _.indexOf(Room.all, this) index = _.indexOf(Room.all, this)
Room.all[index] = null unless index == -1
Room.all.splice(index, 1) unless index == -1 Room.all.splice(index, 1) unless index == -1
@deleted = true @deleted = true
......
...@@ -72,9 +72,7 @@ ...@@ -72,9 +72,7 @@
this.status = 'starting'; this.status = 'starting';
this.established = false; this.established = false;
this.watcher_buffers = []; this.watcher_buffers = [];
this.watcher_stanzas = [];
this.watchers = []; this.watchers = [];
this.ws_watchers = [];
Room.all.push(this); Room.all.push(this);
this.hostinfo = { this.hostinfo = {
lflist: 0, lflist: 0,
...@@ -92,6 +90,7 @@ ...@@ -92,6 +90,7 @@
this.hostinfo.mode = 1; this.hostinfo.mode = 1;
} else if (name.slice(0, 2) === 'T#') { } else if (name.slice(0, 2) === 'T#') {
this.hostinfo.mode = 2; this.hostinfo.mode = 2;
this.hostinfo.start_lp = 16000;
} else if ((param = name.match(/^(\d)(\d)(T|F)(T|F)(T|F)(\d+),(\d+),(\d+)/i))) { } else if ((param = name.match(/^(\d)(\d)(T|F)(T|F)(T|F)(\d+),(\d+),(\d+)/i))) {
this.hostinfo.rule = parseInt(param[1]); this.hostinfo.rule = parseInt(param[1]);
this.hostinfo.mode = parseInt(param[2]); this.hostinfo.mode = parseInt(param[2]);
...@@ -143,6 +142,9 @@ ...@@ -143,6 +142,9 @@
this.save_match(); this.save_match();
} }
index = _.indexOf(Room.all, this); index = _.indexOf(Room.all, this);
if (index !== -1) {
Room.all[index] = null;
}
if (index !== -1) { if (index !== -1) {
Room.all.splice(index, 1); Room.all.splice(index, 1);
} }
......
...@@ -18,6 +18,8 @@ request = require 'request' ...@@ -18,6 +18,8 @@ request = require 'request'
bunyan = require 'bunyan' bunyan = require 'bunyan'
#heapdump = require 'heapdump'
#配置文件 #配置文件
settings = require './config.json' settings = require './config.json'
...@@ -183,6 +185,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -183,6 +185,7 @@ ygopro.ctos_follow 'JOIN_GAME', false, (buffer, info, client, server)->
code: settings.version code: settings.version
} }
client.end() client.end()
else if !info.pass.length else if !info.pass.length
ygopro.stoc_send_chat(client,"房间为空,请修改房间名") ygopro.stoc_send_chat(client,"房间为空,请修改房间名")
ygopro.stoc_send client, 'ERROR_MSG',{ ygopro.stoc_send client, 'ERROR_MSG',{
...@@ -267,15 +270,17 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -267,15 +270,17 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
} }
ygopro.ctos_send watcher, 'HS_TOOBSERVER' ygopro.ctos_send watcher, 'HS_TOOBSERVER'
###
watcher.ws_buffer = new Buffer(0) watcher.ws_buffer = new Buffer(0)
watcher.ws_message_length = 0 watcher.ws_message_length = 0
client.room.watcher_stanzas = [] client.room.watcher_stanzas = []
###
watcher.on 'data', (data)-> watcher.on 'data', (data)->
client.room.watcher_buffers.push data client.room.watcher_buffers.push data
for w in client.room.watchers for w in client.room.watchers
w.write data if w #a WTF fix w.write data if w #a WTF fix
###
watcher.ws_buffer = Buffer.concat([watcher.ws_buffer, data], watcher.ws_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学 watcher.ws_buffer = Buffer.concat([watcher.ws_buffer, data], watcher.ws_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学
while true while true
...@@ -295,13 +300,14 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -295,13 +300,14 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
watcher.ws_message_length = 0 watcher.ws_message_length = 0
else else
break break
###
watcher.on 'error', (error)-> watcher.on 'error', (error)->
#log.error "watcher error", error #log.error "watcher error", error
###
watcher.on 'close', (had_error)-> watcher.on 'close', (had_error)->
for w in client.room.ws_watchers for w in client.room.ws_watchers
w.close() w.close()
###
#登场台词 #登场台词
if settings.modules.dialogues if settings.modules.dialogues
...@@ -315,7 +321,7 @@ if settings.modules.dialogues ...@@ -315,7 +321,7 @@ if settings.modules.dialogues
else if error or !body else if error or !body
log.warn 'dialogues error', error, response log.warn 'dialogues error', error, response
else else
log.info "dialogues loaded", _.size body #log.info "dialogues loaded", _.size body
dialogues = body dialogues = body
ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
...@@ -533,7 +539,7 @@ if settings.modules.http ...@@ -533,7 +539,7 @@ if settings.modules.http
for i of level_points for i of level_points
waiting.push [] waiting.push []
log.info 'level_points loaded', level_points #log.info 'level_points loaded', level_points
http_server = http.createServer (request, response)-> http_server = http.createServer (request, response)->
#http://122.0.65.70:7922/?operation=getroomjson #http://122.0.65.70:7922/?operation=getroomjson
u = url.parse(request.url,1) u = url.parse(request.url,1)
...@@ -652,7 +658,7 @@ if settings.modules.http ...@@ -652,7 +658,7 @@ if settings.modules.http
, 2000 , 2000
### ###
###
originIsAllowed = (origin) -> originIsAllowed = (origin) ->
# allow all origin, for debug # allow all origin, for debug
true true
...@@ -684,8 +690,8 @@ if settings.modules.http ...@@ -684,8 +690,8 @@ if settings.modules.http
for stanza in room.watcher_stanzas for stanza in room.watcher_stanzas
connection.sendBytes stanza connection.sendBytes stanza
###
### ###
connection.on "message", (message) -> connection.on "message", (message) ->
if message.type is "utf8" if message.type is "utf8"
console.log "Received Message: " + message.utf8Data console.log "Received Message: " + message.utf8Data
...@@ -693,12 +699,13 @@ if settings.modules.http ...@@ -693,12 +699,13 @@ if settings.modules.http
else if message.type is "binary" else if message.type is "binary"
console.log "Received Binary Message of " + message.binaryData.length + " bytes" console.log "Received Binary Message of " + message.binaryData.length + " bytes"
connection.sendBytes message.binaryData connection.sendBytes message.binaryData
### ###
###
connection.on "close", (reasonCode, description) -> connection.on "close", (reasonCode, description) ->
index = _.indexOf(room.ws_watchers, connection) index = _.indexOf(room.ws_watchers, connection)
room.ws_watchers.splice(index, 1) unless index == -1 room.ws_watchers.splice(index, 1) unless index == -1
console.log (new Date()) + " Peer " + connection.remoteAddress + " disconnected." console.log (new Date()) + " Peer " + connection.remoteAddress + " disconnected."
###
#清理90s没活动的房间 #清理90s没活动的房间
### ###
inotify = new Inotify() inotify = new Inotify()
......
// Generated by CoffeeScript 1.9.3 // Generated by CoffeeScript 1.9.3
(function() { (function() {
var Deck, Room, User, WebSocketServer, _, bunyan, debug, dialogues, execFile, fs, http, http_server, i, level_points, log, mycard, net, originIsAllowed, os, path, request, settings, tips, url, victories, waiting, wsServer, ygopro, var Deck, Room, User, WebSocketServer, _, bunyan, debug, dialogues, execFile, fs, http, http_server, i, level_points, log, mycard, net, os, path, request, settings, tips, url, victories, waiting, ygopro,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
net = require('net'); net = require('net');
...@@ -324,59 +324,56 @@ ...@@ -324,59 +324,56 @@
}); });
return ygopro.ctos_send(watcher, 'HS_TOOBSERVER'); return ygopro.ctos_send(watcher, 'HS_TOOBSERVER');
}); });
watcher.ws_buffer = new Buffer(0);
watcher.ws_message_length = 0; /*
client.room.watcher_stanzas = []; watcher.ws_buffer = new Buffer(0)
watcher.ws_message_length = 0
client.room.watcher_stanzas = []
*/
watcher.on('data', function(data) { watcher.on('data', function(data) {
var j, k, len, len1, ref, ref1, results, stanza, w; var j, len, ref, results, w;
client.room.watcher_buffers.push(data); client.room.watcher_buffers.push(data);
ref = client.room.watchers; ref = client.room.watchers;
results = [];
for (j = 0, len = ref.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
w = ref[j]; w = ref[j];
if (w) { if (w) {
w.write(data); results.push(w.write(data));
}
}
watcher.ws_buffer = Buffer.concat([watcher.ws_buffer, data], watcher.ws_buffer.length + data.length);
results = [];
while (true) {
if (watcher.ws_message_length === 0) {
if (watcher.ws_buffer.length >= 2) {
results.push(watcher.ws_message_length = watcher.ws_buffer.readUInt16LE(0));
} else {
break;
}
} else { } else {
if (watcher.ws_buffer.length >= 2 + watcher.ws_message_length) { results.push(void 0);
stanza = watcher.ws_buffer.slice(2, watcher.ws_message_length + 2);
ref1 = client.room.ws_watchers;
for (k = 0, len1 = ref1.length; k < len1; k++) {
w = ref1[k];
if (w) {
w.sendBytes(stanza);
}
}
client.room.watcher_stanzas.push(stanza);
watcher.ws_buffer = watcher.ws_buffer.slice(2 + watcher.ws_message_length);
results.push(watcher.ws_message_length = 0);
} else {
break;
}
} }
} }
return results; return results;
}); });
watcher.on('error', function(error) {});
return watcher.on('close', function(had_error) { /*
var j, len, ref, results, w; watcher.ws_buffer = Buffer.concat([watcher.ws_buffer, data], watcher.ws_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学
ref = client.room.ws_watchers;
results = []; while true
for (j = 0, len = ref.length; j < len; j++) { if watcher.ws_message_length == 0
w = ref[j]; if watcher.ws_buffer.length >= 2
results.push(w.close()); watcher.ws_message_length = watcher.ws_buffer.readUInt16LE(0)
} else
return results; break
}); else
if watcher.ws_buffer.length >= 2 + watcher.ws_message_length
stanza = watcher.ws_buffer.slice(2, watcher.ws_message_length + 2)
for w in client.room.ws_watchers
w.sendBytes stanza if w #a WTF fix
client.room.watcher_stanzas.push stanza
watcher.ws_buffer = watcher.ws_buffer.slice(2 + watcher.ws_message_length)
watcher.ws_message_length = 0
else
break
*/
return watcher.on('error', function(error) {});
/*
watcher.on 'close', (had_error)->
for w in client.room.ws_watchers
w.close()
*/
} }
}); });
...@@ -391,7 +388,6 @@ ...@@ -391,7 +388,6 @@
} else if (error || !body) { } else if (error || !body) {
return log.warn('dialogues error', error, response); return log.warn('dialogues error', error, response);
} else { } else {
log.info("dialogues loaded", _.size(body));
return dialogues = body; return dialogues = body;
} }
}); });
...@@ -674,7 +670,6 @@ ...@@ -674,7 +670,6 @@
for (i in level_points) { for (i in level_points) {
waiting.push([]); waiting.push([]);
} }
log.info('level_points loaded', level_points);
http_server = http.createServer(function(request, response) { http_server = http.createServer(function(request, response) {
var j, len, level, name, password, player, ref, ref1, ref2, room, roomsjson, u; var j, len, level, name, password, player, ref, ref1, ref2, room, roomsjson, u;
u = url.parse(request.url, 1); u = url.parse(request.url, 1);
...@@ -861,43 +856,42 @@ ...@@ -861,43 +856,42 @@
, 2000 , 2000
*/ */
originIsAllowed = function(origin) {
return true; /*
}; originIsAllowed = (origin) ->
wsServer = new WebSocketServer({ * allow all origin, for debug
httpServer: http_server, true
wsServer = new WebSocketServer(
httpServer: http_server
autoAcceptConnections: false autoAcceptConnections: false
}); )
wsServer.on("request", function(request) { wsServer.on "request", (request) ->
var connection, j, len, ref, room, room_name, stanza; unless originIsAllowed(request.origin)
if (!originIsAllowed(request.origin)) { * Make sure we only accept requests from an allowed origin
request.reject(); request.reject()
console.log((new Date()) + " Connection from origin " + request.origin + " rejected."); console.log (new Date()) + " Connection from origin " + request.origin + " rejected."
return; return
}
room_name = decodeURIComponent(request.resource.slice(1)); room_name = decodeURIComponent(request.resource.slice(1))
if (room_name === 'started') { if room_name == 'started'
room = _.find(Room.all, function(room) { room = _.find Room.all, (room)->
return room.started; room.started
}); else
} else { room = Room.find_by_name room_name
room = Room.find_by_name(room_name); unless room
} request.reject()
if (!room) { console.log (new Date()) + " Connection from origin " + request.origin + " rejected. #{room_name}"
request.reject(); return
console.log((new Date()) + " Connection from origin " + request.origin + (" rejected. " + room_name));
return; connection = request.accept(null, request.origin)
} console.log (new Date()) + " Connection accepted. #{room.name}"
connection = request.accept(null, request.origin); room.ws_watchers.push connection
console.log((new Date()) + (" Connection accepted. " + room.name));
room.ws_watchers.push(connection); for stanza in room.watcher_stanzas
ref = room.watcher_stanzas; connection.sendBytes stanza
for (j = 0, len = ref.length; j < len; j++) { */
stanza = ref[j];
connection.sendBytes(stanza);
}
/* /*
connection.on "message", (message) -> connection.on "message", (message) ->
if message.type is "utf8" if message.type is "utf8"
console.log "Received Message: " + message.utf8Data console.log "Received Message: " + message.utf8Data
...@@ -905,16 +899,14 @@ ...@@ -905,16 +899,14 @@
else if message.type is "binary" else if message.type is "binary"
console.log "Received Binary Message of " + message.binaryData.length + " bytes" console.log "Received Binary Message of " + message.binaryData.length + " bytes"
connection.sendBytes message.binaryData connection.sendBytes message.binaryData
*/ */
return connection.on("close", function(reasonCode, description) {
var index; /*
index = _.indexOf(room.ws_watchers, connection); connection.on "close", (reasonCode, description) ->
if (index !== -1) { index = _.indexOf(room.ws_watchers, connection)
room.ws_watchers.splice(index, 1); room.ws_watchers.splice(index, 1) unless index == -1
} console.log (new Date()) + " Peer " + connection.remoteAddress + " disconnected."
return console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected."); */
});
});
} }
......
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