Commit 098a4dfa authored by mercury233's avatar mercury233

lite version test1

parent b6b8122a
{ {
"ip": "127.0.0.1", "ip": "123.45.67.89",
"port": 7911, "port": 233,
"version": 4917, "version": 4917,
"modules": { "modules": {
"welcome": "Mycard Server", "welcome": "YGOPRO Server",
"tips": "http://mercury233.me/ygosrv233/tips.json", "tips": "http://mercury233.me/ygosrv233/tips.json",
"dialogues": "http://mercury233.me/ygosrv233/dialogues.json", "dialogues": "http://mercury233.me/ygosrv233/dialogues.json",
"database": false,
"post_start_watching": true, "post_start_watching": true,
"skip_empty_side": true, "skip_empty_side": true,
"http": { "http": {
......
settings = require './config.json'
mongoose = require 'mongoose'
Deck = mongoose.model 'Deck',
name: String
card_usages: [{
card_id: Number,
side: Boolean,
count: Number
}],
user: mongoose.Schema.ObjectId
created_at: { type: Date, default: Date.now },
used_count: Number,
last_used_at: Date
module.exports = Deck
\ No newline at end of file
// Generated by CoffeeScript 1.9.3
(function() {
var Deck, mongoose, settings;
settings = require('./config.json');
mongoose = require('mongoose');
Deck = mongoose.model('Deck', {
name: String,
card_usages: [
{
card_id: Number,
side: Boolean,
count: Number
}
],
user: mongoose.Schema.ObjectId,
created_at: {
type: Date,
"default": Date.now
},
used_count: Number,
last_used_at: Date
});
module.exports = Deck;
}).call(this);
[-10,20,50,100,180,300,450,650,1000,1400,2000,3000]
\ No newline at end of file
settings = require './config.json'
mongoose = require 'mongoose'
Match = mongoose.model 'Match',
players: [{
user: mongoose.Schema.ObjectId,
deck: mongoose.Schema.ObjectId,
}],
duels: [{
winner: Number
reason: Number
}],
winner: mongoose.Schema.ObjectId,
created_at: { type: Date, default: Date.now },
ygopro_version: Number
module.exports = Match
\ No newline at end of file
// Generated by CoffeeScript 1.9.3
(function() {
var Match, mongoose, settings;
settings = require('./config.json');
mongoose = require('mongoose');
Match = mongoose.model('Match', {
players: [
{
user: mongoose.Schema.ObjectId,
deck: mongoose.Schema.ObjectId
}
],
duels: [
{
winner: Number,
reason: Number
}
],
winner: mongoose.Schema.ObjectId,
created_at: {
type: Date,
"default": Date.now
},
ygopro_version: Number
});
module.exports = Match;
}).call(this);
request = require 'request'
@key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="
@load_card_usages_from_cards = (main, side)->
result = []
last_id = null
for card_id in main
if card_id == last_id
count++
else
result.push {card_id: last_id, side: false, count: count} if last_id
last_id = card_id
count = 1
result.push {card_id: last_id, side: false, count: count} if last_id
last_id = null
for card_id in side
if card_id == last_id
count++
else
result.push {card_id: last_id, side: true, count: count} if last_id
last_id = card_id
count = 1
result.push {card_id: last_id, side: true, count: count} if last_id
result
@encode = (card_usages)->
result = ''
for card_usage in card_usages
c = card_usage.side << 29 | card_usage.count << 27 | card_usage.card_id
for i in [4..0]
result += @key.charAt((c >> i * 6) & 0x3F)
result
@deck_url = (name, card_usages, format)->
"https://my-card.in/decks/new#{if format then '.' + format else ''}?name=#{encodeURIComponent name}&cards=#{@encode card_usages}"
@deck_url_short = (name, card_usages, callback)->
request = require 'request'
request @deck_url(name, card_usages, 'short.url'), (error, response, body)->
callback body
\ No newline at end of file
// Generated by CoffeeScript 1.9.3
(function() {
var request;
request = require('request');
this.key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
this.load_card_usages_from_cards = function(main, side) {
var card_id, count, j, k, last_id, len, len1, result;
result = [];
last_id = null;
for (j = 0, len = main.length; j < len; j++) {
card_id = main[j];
if (card_id === last_id) {
count++;
} else {
if (last_id) {
result.push({
card_id: last_id,
side: false,
count: count
});
}
last_id = card_id;
count = 1;
}
}
if (last_id) {
result.push({
card_id: last_id,
side: false,
count: count
});
}
last_id = null;
for (k = 0, len1 = side.length; k < len1; k++) {
card_id = side[k];
if (card_id === last_id) {
count++;
} else {
if (last_id) {
result.push({
card_id: last_id,
side: true,
count: count
});
}
last_id = card_id;
count = 1;
}
}
if (last_id) {
result.push({
card_id: last_id,
side: true,
count: count
});
}
return result;
};
this.encode = function(card_usages) {
var c, card_usage, i, j, k, len, result;
result = '';
for (j = 0, len = card_usages.length; j < len; j++) {
card_usage = card_usages[j];
c = card_usage.side << 29 | card_usage.count << 27 | card_usage.card_id;
for (i = k = 4; k >= 0; i = --k) {
result += this.key.charAt((c >> i * 6) & 0x3F);
}
}
return result;
};
this.deck_url = function(name, card_usages, format) {
return "https://my-card.in/decks/new" + (format ? '.' + format : '') + "?name=" + (encodeURIComponent(name)) + "&cards=" + (this.encode(card_usages));
};
this.deck_url_short = function(name, card_usages, callback) {
request = require('request');
return request(this.deck_url(name, card_usages, 'short.url'), function(error, response, body) {
return callback(body);
});
};
}).call(this);
{ {
"name": "ygopro-server", "name": "ygopro-server",
"version": "2.1.0", "version": "2.3.3",
"description": "a server for ygopro", "description": "a server for ygopro",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/mycard/ygopro-server.git" "url": "https://github.com/mercury233/ygopro-server.git"
}, },
"keywords": [ "keywords": [
"mycard", "mycard",
"ygopro", "ygopro",
"server" "server"
], ],
"author": "zh99998 <zh99998@gmail.com>", "author": "zh99998 <zh99998@gmail.com>, mercury233 <me@mercury233.me>",
"dependencies": { "dependencies": {
"underscore": "*", "underscore": "*",
"underscore.string": "*", "underscore.string": "*",
"freeport": "*",
"request": "*", "request": "*",
"mongoose": "*",
"bunyan": "*", "bunyan": "*",
"websocket": "~1.0.8"
}, },
"license": "GPLv3", "license": "GPLv3",
"main": "ygopro-server.js", "main": "ygopro-server.js",
......
{
"1": [
{"card": 55586621, "count": 10},
{"card": 6061630, "count": 1},
{"card": 423585, "count": 1},
{"card": 29088922, "count": 1},
{"card": 87608852, "count": 1},
{"card": 19613556, "count": 1},
{"card": 25123082, "count": 1},
{"card": 52105192, "count": 1},
{"card": 53129443, "count": 1},
{"card": 68663748, "count": 1},
{"card": 75500286, "count": 1},
{"card": 81439173, "count": 1},
{"card": 83764718, "count": 1},
{"card": 89739383, "count": 1},
{"card": 98645731, "count": 1},
{"card": 14087893, "count": 1},
{"card": 88616795, "count": 1},
{"card": 9622164, "count": 1},
{"card": 52628687, "count": 1},
{"card": 67775894, "count": 1},
{"card": 33981008, "count": 1},
{"card": 97077563, "count": 1},
{"card": 1992816, "count": 1},
{"card": 64332231, "count": 1},
{"card": 91949988, "count": 1},
{"card": 10000030, "count": 1},
{"card": 29669359, "count": 1},
{"card": 770365, "count": 1},
{"card": 39987164, "count": 1},
{"card": 56840427, "count": 1},
{"card": 84013237, "count": 1},
{"card": 54719828, "count": 1},
{"card": 51735257, "count": 1},
{"card": 37926346, "count": 1},
{"card": 25341652, "count": 1},
{"card": 34086406, "count": 1},
{"card": 12014404, "count": 1}
],
"2": [
{"card": 4392470, "count": 10},
{"card": 40200834, "count": 1},
{"card": 69780745, "count": 1},
{"card": 10538007, "count": 1},
{"card": 82293134, "count": 1},
{"card": 20546916, "count": 1},
{"card": 33951077, "count": 1},
{"card": 89631139, "count": 1},
{"card": 95788410, "count": 1},
{"card": 39111158, "count": 1},
{"card": 42129512, "count": 1},
{"card": 74677424, "count": 1},
{"card": 47660516, "count": 1},
{"card": 96765646, "count": 1},
{"card": 48995978, "count": 1}
]
}
\ No newline at end of file
...@@ -5,14 +5,8 @@ spawn = require('child_process').spawn ...@@ -5,14 +5,8 @@ spawn = require('child_process').spawn
ygopro = require './ygopro.js' ygopro = require './ygopro.js'
bunyan = require 'bunyan' bunyan = require 'bunyan'
settings = require './config.json' settings = require './config.json'
log = bunyan.createLogger name: "mycard-room"
if settings.modules.database log = bunyan.createLogger name: "mycard-room"
mongoose = require 'mongoose'
mongoose.connect(settings.modules.database);
User = require './user.js'
Deck = require './deck.js'
Match = require './match.js'
class Room class Room
#name #name
...@@ -54,9 +48,7 @@ class Room ...@@ -54,9 +48,7 @@ class Room
@status = 'starting' @status = 'starting'
@established = false @established = false
@watcher_buffers = [] @watcher_buffers = []
#@watcher_stanzas = []
@watchers = [] @watchers = []
#@ws_watchers = []
Room.all.push this Room.all.push this
@hostinfo = @hostinfo =
...@@ -105,101 +97,11 @@ class Room ...@@ -105,101 +97,11 @@ class Room
delete: -> delete: ->
#积分 #积分
return if @deleted return if @deleted
@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[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
toString: ->
"room: #{@name} #{@port} #{@alive ? 'alive' : 'not-alive'} #{@dueling ? 'dueling' : 'not-dueling'} [#{("client #{typeof player.client} server #{typeof player.server} #{player.name} #{player.pos}. " for player in @players)}] #{JSON.stringify @pos_name}"
ensure_finish: ()->
#判断match是否正常结束
player_wins = [0,0,0]
for duel in @duels
player_wins[duel.winner] += 1
normal_ended = player_wins[0] >= 2 or player_wins[1] >= 2
if !normal_ended
if @disconnector == 'server'
return false
if @duels.length == 0 or _.last(@duels).reason != 4
@duels.push {winner: 1-@disconnector.pos, reason: 4}
true
save_match: ()->
return unless @ensure_finish()
match_winner = _.last(@duels).winner
return unless @dueling_players[0] and @dueling_players[1] #a WTF fix
User.findOne { name: @dueling_players[0].name }, (err, player0)=>
if(err)
#log.error "error when find user", @dueling_players[0].name, err
else if(!player0)
#log.error "can't find user ", @dueling_players[0].name
else
User.findOne { name: @dueling_players[1].name }, (err, player1)=>
if(err)
#log.error "error when find user", @dueling_players[1].name, err
else if(!player1)
#log.error "can't find user ", @dueling_players[1].name
else
#---------------------------------------------------------------------------
#卡组
#log.info user: player0._id, card_usages: @dueling_players[0].deck
Deck.findOne user: player0._id, card_usages: @dueling_players[0].deck, (err, deck0)=>
if(err)
#log.error "error when find deck"
else if(!deck0)
deck0 = new Deck({name: 'match', user: player0._id, card_usages: @dueling_players[0].deck, used_count: 1, last_used_at: Date.now()})
deck0.save()
else
deck0.used_count++
deck0.last_used_at = Date.now()
deck0.save()
#log.info deck0
#log.info @dueling_players[0].deck, @dueling_players[1].deck, @dueling_players
Deck.findOne user: player1._id, card_usages: @dueling_players[1].deck, (err, deck1)=>
if(err)
#log.error "error when find deck"
else if(!deck1)
deck1 = new Deck({name: 'match', user: player1._id, card_usages: @dueling_players[1].deck, used_count: 1, last_used_at: Date.now()})
deck1.save()
else
deck1.used_count++
deck1.last_used_at = Date.now()
deck1.save()
#log.info deck1
Match.create
players: [{user: player0._id, deck: deck0._id}, {user: player1._id, deck: deck1._id}]
duels: @duels
winner: if match_winner == 0 then player0._id else player1._id,
ygopro_version: settings.version
,(err, match)->
#log.info err, match
#积分
if match_winner == 0
winner = player0
loser = player1
else
winner = player1
loser = player0
#log.info('before_settle_result',winner.name, winner.points,loser.name, loser.points)
winner.points += 5
if _.last(@duels).reason == 4
loser.points -= 8
else
loser.points -= 3
#log.info('duel_settle_result',winner.name, winner.points,loser.name, loser.points)
winner.save()
loser.save()
connect: (client)-> connect: (client)->
@players.push client @players.push client
......
// Generated by CoffeeScript 1.9.3 // Generated by CoffeeScript 1.9.3
(function() { (function() {
var Deck, Match, Room, User, _, bunyan, log, mongoose, settings, spawn, ygopro; var Room, _, bunyan, log, settings, spawn, ygopro;
_ = require('underscore'); _ = require('underscore');
...@@ -20,14 +20,6 @@ ...@@ -20,14 +20,6 @@
name: "mycard-room" name: "mycard-room"
}); });
if (settings.modules.database) {
mongoose = require('mongoose');
mongoose.connect(settings.modules.database);
User = require('./user.js');
Deck = require('./deck.js');
Match = require('./match.js');
}
Room = (function() { Room = (function() {
Room.all = []; Room.all = [];
...@@ -138,9 +130,6 @@ ...@@ -138,9 +130,6 @@
if (this.deleted) { if (this.deleted) {
return; return;
} }
if (_.startsWith(this.name, 'M#') && this.started && settings.modules.database) {
this.save_match();
}
index = _.indexOf(Room.all, this); index = _.indexOf(Room.all, this);
if (index !== -1) { if (index !== -1) {
Room.all[index] = null; Room.all[index] = null;
...@@ -151,152 +140,6 @@ ...@@ -151,152 +140,6 @@
return this.deleted = true; return this.deleted = true;
}; };
Room.prototype.toString = function() {
var player, ref, ref1;
return "room: " + this.name + " " + this.port + " " + ((ref = this.alive) != null ? ref : {
'alive': 'not-alive'
}) + " " + ((ref1 = this.dueling) != null ? ref1 : {
'dueling': 'not-dueling'
}) + " [" + ((function() {
var i, len, ref2, results;
ref2 = this.players;
results = [];
for (i = 0, len = ref2.length; i < len; i++) {
player = ref2[i];
results.push("client " + (typeof player.client) + " server " + (typeof player.server) + " " + player.name + " " + player.pos + ". ");
}
return results;
}).call(this)) + "] " + (JSON.stringify(this.pos_name));
};
Room.prototype.ensure_finish = function() {
var duel, i, len, normal_ended, player_wins, ref;
player_wins = [0, 0, 0];
ref = this.duels;
for (i = 0, len = ref.length; i < len; i++) {
duel = ref[i];
player_wins[duel.winner] += 1;
}
normal_ended = player_wins[0] >= 2 || player_wins[1] >= 2;
if (!normal_ended) {
if (this.disconnector === 'server') {
return false;
}
if (this.duels.length === 0 || _.last(this.duels).reason !== 4) {
this.duels.push({
winner: 1 - this.disconnector.pos,
reason: 4
});
}
}
return true;
};
Room.prototype.save_match = function() {
var match_winner;
if (!this.ensure_finish()) {
return;
}
match_winner = _.last(this.duels).winner;
if (!(this.dueling_players[0] && this.dueling_players[1])) {
return;
}
return User.findOne({
name: this.dueling_players[0].name
}, (function(_this) {
return function(err, player0) {
if (err) {
} else if (!player0) {
} else {
return User.findOne({
name: _this.dueling_players[1].name
}, function(err, player1) {
var loser, winner;
if (err) {
} else if (!player1) {
} else {
Deck.findOne({
user: player0._id,
card_usages: _this.dueling_players[0].deck
}, function(err, deck0) {
if (err) {
} else if (!deck0) {
deck0 = new Deck({
name: 'match',
user: player0._id,
card_usages: _this.dueling_players[0].deck,
used_count: 1,
last_used_at: Date.now()
});
deck0.save();
} else {
deck0.used_count++;
deck0.last_used_at = Date.now();
deck0.save();
}
return Deck.findOne({
user: player1._id,
card_usages: _this.dueling_players[1].deck
}, function(err, deck1) {
if (err) {
} else if (!deck1) {
deck1 = new Deck({
name: 'match',
user: player1._id,
card_usages: _this.dueling_players[1].deck,
used_count: 1,
last_used_at: Date.now()
});
deck1.save();
} else {
deck1.used_count++;
deck1.last_used_at = Date.now();
deck1.save();
}
return Match.create({
players: [
{
user: player0._id,
deck: deck0._id
}, {
user: player1._id,
deck: deck1._id
}
],
duels: _this.duels,
winner: match_winner === 0 ? player0._id : player1._id,
ygopro_version: settings.version
}, function(err, match) {});
});
});
if (match_winner === 0) {
winner = player0;
loser = player1;
} else {
winner = player1;
loser = player0;
}
winner.points += 5;
if (_.last(_this.duels).reason === 4) {
loser.points -= 8;
} else {
loser.points -= 3;
}
winner.save();
return loser.save();
}
});
}
};
})(this));
};
Room.prototype.connect = function(client) { Room.prototype.connect = function(client) {
this.players.push(client); this.players.push(client);
if (this.established) { if (this.established) {
......
settings = require './config.json'
mongoose = require 'mongoose'
User = mongoose.model 'User',
name: String
points: Number
module.exports = User
\ No newline at end of file
// Generated by CoffeeScript 1.9.3
(function() {
var User, mongoose, settings;
settings = require('./config.json');
mongoose = require('mongoose');
User = mongoose.model('User', {
name: String,
points: Number
});
module.exports = User;
}).call(this);
{
"0":"投降",
"1":"LP变成0",
"2":"没有卡可抽",
"3":"超时",
"4":"失去连接",
"16":"「被封印的艾克佐迪亚」效果胜利",
"17": "「终焉的倒计时」效果胜利",
"18": "「毒蛇神 维诺米纳迦」效果胜利",
"19": "「光之创造神 哈拉克提」效果胜利",
"20": "「究极封印神 艾克佐迪奥斯」效果胜利",
"21": "「通灵盘」效果胜利",
"22": "「最终一战!」效果胜利",
"23": "「No.88 机关傀儡-命运狮子」效果胜利",
"32": "由于「%ls」的效果获得比赛胜利"
}
\ No newline at end of file
...@@ -13,7 +13,7 @@ _.str = require 'underscore.string' ...@@ -13,7 +13,7 @@ _.str = require 'underscore.string'
_.mixin(_.str.exports()); _.mixin(_.str.exports());
#Inotify = require('inotify').Inotify #Inotify = require('inotify').Inotify
WebSocketServer = require('websocket').server
request = require 'request' request = require 'request'
bunyan = require 'bunyan' bunyan = require 'bunyan'
...@@ -25,13 +25,7 @@ settings = require './config.json' ...@@ -25,13 +25,7 @@ settings = require './config.json'
#组件 #组件
ygopro = require './ygopro.js' ygopro = require './ygopro.js'
mycard = require './mycard.js'
Room = require './room.js' Room = require './room.js'
User = require './user.js' if settings.modules.database
Deck = require './deck.js' if settings.modules.database
victories = require './victories.json'
#debug模式 端口号+1 #debug模式 端口号+1
...@@ -51,33 +45,29 @@ net.createServer (client) -> ...@@ -51,33 +45,29 @@ net.createServer (client) ->
#释放处理 #释放处理
client.on 'close', (had_error) -> client.on 'close', (had_error) ->
#log.info "client closed", client.name, had_error log.info "client closed", client.name, had_error
client.room.disconnector = client if client.room and client.room.started and client in client.room.dueling_players and !client.room.disconnector
unless client.closed unless client.closed
client.closed = true client.closed = true
client.room.disconnect(client) if client.room client.room.disconnect(client) if client.room
server.end() server.end()
client.on 'error', (error)-> client.on 'error', (error)->
#log.info "client error", client.name, error log.info "client error", client.name, error
client.room.disconnector = client if client.room and client.room.started and client in client.room.dueling_players and !client.room.disconnector
unless client.closed unless client.closed
client.closed = error client.closed = error
client.room.disconnect(client, error) if client.room client.room.disconnect(client, error) if client.room
server.end() server.end()
server.on 'close', (had_error) -> server.on 'close', (had_error) ->
#log.info "server closed", client.name, had_error log.info "server closed", client.name, had_error
server.closed = true unless server.closed server.closed = true unless server.closed
client.room.disconnector = 'server' if client.room and client.room.started and client in client.room.dueling_players and !client.room.disconnector
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器关闭了连接") ygopro.stoc_send_chat(client, "服务器关闭了连接")
client.end() client.end()
server.on 'error', (error)-> server.on 'error', (error)->
#log.info "server error", client.name, error log.info "server error", client.name, error
server.closed = error server.closed = error
client.room.disconnector = 'server' if client.room and client.room.started and client in client.room.dueling_players and !client.room.disconnector
unless client.closed unless client.closed
ygopro.stoc_send_chat(client, "服务器错误: #{error}") ygopro.stoc_send_chat(client, "服务器错误: #{error}")
client.end() client.end()
...@@ -247,15 +237,6 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -247,15 +237,6 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)->
ygopro.stoc_send_chat client, settings.modules.welcome ygopro.stoc_send_chat client, settings.modules.welcome
if (os.freemem() / os.totalmem())<=0.1 if (os.freemem() / os.totalmem())<=0.1
ygopro.stoc_send_chat client, "服务器已经爆满,随时存在崩溃风险!" ygopro.stoc_send_chat client, "服务器已经爆满,随时存在崩溃风险!"
if settings.modules.database
if _.startsWith(client.room.name, 'M#')
User.findOne { name: client.name }, (err, user)->
if !user
user = new User({name: client.name, points: 0})
user.save()
User.count {points:{$gt:user.points}}, (err, count)->
rank = count + 1
ygopro.stoc_send_chat(client, "积分系统测试中,你现在有#{user.points}点积分,排名#{rank},这些积分以后正式使用时会重置")
if settings.modules.post_start_watching and !client.room.watcher if settings.modules.post_start_watching and !client.room.watcher
client.room.watcher = watcher = net.connect client.room.port, -> client.room.watcher = watcher = net.connect client.room.port, ->
...@@ -270,38 +251,13 @@ ygopro.stoc_follow 'JOIN_GAME', false, (buffer, info, client, server)-> ...@@ -270,38 +251,13 @@ 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_message_length = 0
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的错误使用方式,好孩子不要学
while true #watcher.on 'error', (error)->
if watcher.ws_message_length == 0
if watcher.ws_buffer.length >= 2
watcher.ws_message_length = watcher.ws_buffer.readUInt16LE(0)
else
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
###
watcher.on 'error', (error)->
#log.error "watcher error", error #log.error "watcher error", error
### ###
watcher.on 'close', (had_error)-> watcher.on 'close', (had_error)->
...@@ -388,26 +344,8 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)-> ...@@ -388,26 +344,8 @@ ygopro.stoc_follow 'GAME_MSG', false, (buffer, info, client, server)->
ygopro.stoc_send_chat client, line ygopro.stoc_send_chat client, line
###
#房间管理 #房间管理
ygopro.stoc_follow 'HS_PLAYER_ENTER', false, (buffer, info, client, server)->
#console.log "PLAYER_ENTER to #{client.name}: #{info.name}, #{info.pos}"
#room = client.room
#if !room
# console.log "[WARN]player_enter: can't find room by player #{client.player}"
# return
#room.pos_name[info.pos] = info.name
ygopro.stoc_follow 'HS_PLAYER_CHANGE', false, (buffer, info, client, server)->
#client.ready = info.status & 0xF != 0
#client.pos = info.status >> 4
#console.log "PLAYER_CHANGE to #{client.name}: #{info.status & 0xF != 0}, #{info.status >> 4}"
###
ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)-> ygopro.stoc_follow 'TYPE_CHANGE', false, (buffer, info, client, server)->
selftype = info.type & 0xf; selftype = info.type & 0xf;
...@@ -436,10 +374,10 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)-> ...@@ -436,10 +374,10 @@ ygopro.stoc_follow 'DUEL_START', false, (buffer, info, client, server)->
client.room.dueling_players = [] client.room.dueling_players = []
for player in client.room.players when player.pos != 7 for player in client.room.players when player.pos != 7
client.room.dueling_players[player.pos] = player client.room.dueling_players[player.pos] = player
if !player.main #if !player.main
#log.error 'WTF', client #log.error 'WTF', client
else #else
player.deck = mycard.load_card_usages_from_cards(player.main, player.side) #player.deck = mycard.load_card_usages_from_cards(player.main, player.side)
#if !client.room.dueling_players[0] or !client.room.dueling_players[1] #if !client.room.dueling_players[0] or !client.room.dueling_players[1]
#log.error 'incomplete room', client.room.dueling_players, client.room.players #log.error 'incomplete room', client.room.dueling_players, client.room.players
...@@ -460,31 +398,15 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)-> ...@@ -460,31 +398,15 @@ ygopro.ctos_follow 'CHAT', false, (buffer, info, client, server)->
else else
#log.warn 'ping', stdout #log.warn 'ping', stdout
ygopro.stoc_send_chat_to_room client.room, stdout ygopro.stoc_send_chat_to_room client.room, stdout
when '/ranktop'
if settings.modules.database
User.find null, null, { sort: { points : -1 }, limit: 8 }, (err, users)->
if err
return #log.error 'ranktop', err
for index, user of users
ygopro.stoc_send_chat client, [parseInt(index)+1, user.points, user.name].join(' ')
when '/help' when '/help'
ygopro.stoc_send_chat(client,"Mycard MatchServer 指令帮助") ygopro.stoc_send_chat(client,"YGOSrv233 指令帮助")
ygopro.stoc_send_chat(client,"/help 显示这个帮助信息") ygopro.stoc_send_chat(client,"/help 显示这个帮助信息")
ygopro.stoc_send_chat(client,"/tip 显示一条提示") if settings.modules.tips ygopro.stoc_send_chat(client,"/tip 显示一条提示") if settings.modules.tips
ygopro.stoc_send_chat(client,"/senddeck 发送自己的卡组") #ygopro.stoc_send_chat(client,"/senddeck 发送自己的卡组")
when '/tip' when '/tip'
ygopro.stoc_send_random_tip(client) if settings.modules.tips ygopro.stoc_send_random_tip(client) if settings.modules.tips
#发送卡组
when '/senddeck'
if client.deck?
ygopro.stoc_send_chat(client, "正在读取卡组信息... ")
mycard.deck_url_short client.name, client.deck, (url)->
ygopro.stoc_send_chat_to_room(client.room, "卡组链接: " + url)
else
ygopro.stoc_send_chat_to_room(client.room, "读取卡组信息失败")
when '/admin showroom'
log.info client.room
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)-> ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
#log.info info #log.info info
main = (info.deckbuf[i] for i in [0...info.mainc]) main = (info.deckbuf[i] for i in [0...info.mainc])
...@@ -502,44 +424,8 @@ if settings.modules.skip_empty_side ...@@ -502,44 +424,8 @@ if settings.modules.skip_empty_side
} }
ygopro.stoc_send_chat client, '等待更换副卡组中...' ygopro.stoc_send_chat client, '等待更换副卡组中...'
###
# 开包大战
packs_weighted_cards = {}
for pack, cards of require './packs.json'
packs_weighted_cards[pack] = []
for card in cards
for i in [0..card.count]
packs_weighted_cards[pack].push card.card
console.log packs_weighted_cards
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
ygopro.ctos_send server, 'HS_NOTREADY'
deck = []
for pack in client.player
for i in [0...5]
deck.push packs_weighted_cards[pack][Math.floor(Math.random()*packs_weighted_cards[pack].length)]
ygopro.ctos_send server, 'UPDATE_DECK', {
mainc: deck.length,
sidec: 0,
deckbuf: deck
}
ygopro.ctos_send server, 'HS_READY'
###
#http #http
if settings.modules.http if settings.modules.http
level_points = require './level_points.json'
waiting = [[]]
for i of level_points
waiting.push []
#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)
...@@ -547,33 +433,6 @@ if settings.modules.http ...@@ -547,33 +433,6 @@ if settings.modules.http
if u.pathname == '/count.json' if u.pathname == '/count.json'
response.writeHead(200); response.writeHead(200);
response.end(Room.all.length.toString()) response.end(Room.all.length.toString())
else if u.pathname == '/match'
if request.headers['authorization']
[name, password] = new Buffer(request.headers['authorization'].split(/\s+/).pop() ? '','base64').toString().split(':')
User.findOne { name: name }, (err, user)->
if !user
user = new User({name: name, points: 0, elo: 1400})
user.save()
level = level_points.length
for index, points of level_points
if user.points < points
level = index
break
response.allowance = 0
waiting[level].push response
request.on 'close', ()->
index = waiting[level].indexOf(response)
waiting[level].splice(index, 1) unless index == -1
else
#log.info 'unauth match'
#response.writeHead(401);
#response.end("请更新mycard到1.2.8版本");
level = 1
response.allowance = 0
waiting[level].push response
request.on 'close', ()->
index = waiting[level].indexOf(response)
waiting[level].splice(index, 1) unless index == -1
else if u.pathname == '/rooms.js' else if u.pathname == '/rooms.js'
response.writeHead(200); response.writeHead(200);
...@@ -589,6 +448,7 @@ if settings.modules.http ...@@ -589,6 +448,7 @@ if settings.modules.http
istart: if room.started then 'start' else 'wait' istart: if room.started then 'start' else 'wait'
) )
response.end("loadroom( " + roomsjson + " );"); response.end("loadroom( " + roomsjson + " );");
else if u.query.operation == 'getroomjson' else if u.query.operation == 'getroomjson'
response.writeHead(200); response.writeHead(200);
response.end JSON.stringify rooms: (for room in Room.all when room.established response.end JSON.stringify rooms: (for room in Room.all when room.established
...@@ -602,110 +462,18 @@ if settings.modules.http ...@@ -602,110 +462,18 @@ if settings.modules.http
), ),
istart: if room.started then "start" else "wait" istart: if room.started then "start" else "wait"
) )
else if u.query.pass == settings.modules.http.password && u.query.shout else if u.query.pass == settings.modules.http.password && u.query.shout
for room in Room.all for room in Room.all
ygopro.stoc_send_chat_to_room(room, u.query.shout) ygopro.stoc_send_chat_to_room(room, u.query.shout)
response.writeHead(200); response.writeHead(200);
response.end("shout " + u.query.shout + " ok"); response.end("shout " + u.query.shout + " ok");
else else
response.writeHead(404); response.writeHead(404);
response.end(); response.end();
http_server.listen settings.modules.http.port http_server.listen settings.modules.http.port
###
setInterval ()->
for level in [level_points.length..0]
for index, player of waiting[level]
opponent_level = null
opponent = _.find waiting[level], (opponent)->
log.info opponent,player
opponent isnt player
log.info '--------1--------', waiting, opponent
if opponent
opponent_level = level
else if player.allowance > 0
for displacement in [1..player.allowance]
if level+displacement <= level_points.length
opponent = waiting[level+displacement][0]
if opponent
opponent_level = level+displacement
break
if level-displacement >= 0
opponent = waiting[level-displacement][0]
if opponent
opponent_level = level-displacement
break
if opponent
if waiting[level].indexOf(player) == -1 or waiting[opponent_level].indexOf(opponent) == -1
log.info waiting, player, level, opponent, opponent_level
throw 'WTF'
waiting[level].splice(waiting[level].indexOf(player), 1)
waiting[opponent_level].splice(waiting[opponent_level].indexOf(opponent), 1)
index--
room = "mycard://#{settings.ip}:#{settings.port}/M##{_.uniqueId()}$#{_.random(999)}"
log.info 'matched', room
headers = {"Access-Control-Allow-Origin":"*","Content-Type": "text/plain"}
player.writeHead(200, headers)
player.end room
opponent.writeHead(200, headers)
opponent.end room
else
player.allowance++
, 2000
###
###
originIsAllowed = (origin) ->
# allow all origin, for debug
true
wsServer = new WebSocketServer(
httpServer: http_server
autoAcceptConnections: false
)
wsServer.on "request", (request) ->
unless originIsAllowed(request.origin)
# Make sure we only accept requests from an allowed origin
request.reject()
console.log (new Date()) + " Connection from origin " + request.origin + " rejected."
return
room_name = decodeURIComponent(request.resource.slice(1))
if room_name == 'started'
room = _.find Room.all, (room)->
room.started
else
room = Room.find_by_name room_name
unless room
request.reject()
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}"
room.ws_watchers.push connection
for stanza in room.watcher_stanzas
connection.sendBytes stanza
###
###
connection.on "message", (message) ->
if message.type is "utf8"
console.log "Received Message: " + message.utf8Data
connection.sendUTF message.utf8Data
else if message.type is "binary"
console.log "Received Binary Message of " + message.binaryData.length + " bytes"
connection.sendBytes message.binaryData
###
###
connection.on "close", (reasonCode, description) ->
index = _.indexOf(room.ws_watchers, connection)
room.ws_watchers.splice(index, 1) unless index == -1
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, os, path, request, settings, tips, url, victories, waiting, ygopro, var Room, _, bunyan, debug, dialogues, execFile, fs, http, http_server, log, net, os, path, request, settings, tips, url, 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; };
net = require('net'); net = require('net');
...@@ -23,8 +22,6 @@ ...@@ -23,8 +22,6 @@
_.mixin(_.str.exports()); _.mixin(_.str.exports());
WebSocketServer = require('websocket').server;
request = require('request'); request = require('request');
bunyan = require('bunyan'); bunyan = require('bunyan');
...@@ -33,20 +30,8 @@ ...@@ -33,20 +30,8 @@
ygopro = require('./ygopro.js'); ygopro = require('./ygopro.js');
mycard = require('./mycard.js');
Room = require('./room.js'); Room = require('./room.js');
if (settings.modules.database) {
User = require('./user.js');
}
if (settings.modules.database) {
Deck = require('./deck.js');
}
victories = require('./victories.json');
debug = false; debug = false;
log = null; log = null;
...@@ -70,9 +55,7 @@ ...@@ -70,9 +55,7 @@
server = new net.Socket(); server = new net.Socket();
client.server = server; client.server = server;
client.on('close', function(had_error) { client.on('close', function(had_error) {
if (client.room && client.room.started && indexOf.call(client.room.dueling_players, client) >= 0 && !client.room.disconnector) { log.info("client closed", client.name, had_error);
client.room.disconnector = client;
}
if (!client.closed) { if (!client.closed) {
client.closed = true; client.closed = true;
if (client.room) { if (client.room) {
...@@ -82,9 +65,7 @@ ...@@ -82,9 +65,7 @@
return server.end(); return server.end();
}); });
client.on('error', function(error) { client.on('error', function(error) {
if (client.room && client.room.started && indexOf.call(client.room.dueling_players, client) >= 0 && !client.room.disconnector) { log.info("client error", client.name, error);
client.room.disconnector = client;
}
if (!client.closed) { if (!client.closed) {
client.closed = error; client.closed = error;
if (client.room) { if (client.room) {
...@@ -94,22 +75,18 @@ ...@@ -94,22 +75,18 @@
return server.end(); return server.end();
}); });
server.on('close', function(had_error) { server.on('close', function(had_error) {
log.info("server closed", client.name, had_error);
if (!server.closed) { if (!server.closed) {
server.closed = true; server.closed = true;
} }
if (client.room && client.room.started && indexOf.call(client.room.dueling_players, client) >= 0 && !client.room.disconnector) {
client.room.disconnector = 'server';
}
if (!client.closed) { if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器关闭了连接"); ygopro.stoc_send_chat(client, "服务器关闭了连接");
return client.end(); return client.end();
} }
}); });
server.on('error', function(error) { server.on('error', function(error) {
log.info("server error", client.name, error);
server.closed = error; server.closed = error;
if (client.room && client.room.started && indexOf.call(client.room.dueling_players, client) >= 0 && !client.room.disconnector) {
client.room.disconnector = 'server';
}
if (!client.closed) { if (!client.closed) {
ygopro.stoc_send_chat(client, "服务器错误: " + error); ygopro.stoc_send_chat(client, "服务器错误: " + error);
return client.end(); return client.end();
...@@ -287,30 +264,6 @@ ...@@ -287,30 +264,6 @@
if ((os.freemem() / os.totalmem()) <= 0.1) { if ((os.freemem() / os.totalmem()) <= 0.1) {
ygopro.stoc_send_chat(client, "服务器已经爆满,随时存在崩溃风险!"); ygopro.stoc_send_chat(client, "服务器已经爆满,随时存在崩溃风险!");
} }
if (settings.modules.database) {
if (_.startsWith(client.room.name, 'M#')) {
User.findOne({
name: client.name
}, function(err, user) {
if (!user) {
user = new User({
name: client.name,
points: 0
});
user.save();
}
return User.count({
points: {
$gt: user.points
}
}, function(err, count) {
var rank;
rank = count + 1;
return ygopro.stoc_send_chat(client, "积分系统测试中,你现在有" + user.points + "点积分,排名" + rank + ",这些积分以后正式使用时会重置");
});
});
}
}
if (settings.modules.post_start_watching && !client.room.watcher) { if (settings.modules.post_start_watching && !client.room.watcher) {
client.room.watcher = watcher = net.connect(client.room.port, function() { client.room.watcher = watcher = net.connect(client.room.port, function() {
ygopro.ctos_send(watcher, 'PLAYER_INFO', { ygopro.ctos_send(watcher, 'PLAYER_INFO', {
...@@ -324,13 +277,7 @@ ...@@ -324,13 +277,7 @@
}); });
return ygopro.ctos_send(watcher, 'HS_TOOBSERVER'); return ygopro.ctos_send(watcher, 'HS_TOOBSERVER');
}); });
return watcher.on('data', function(data) {
/*
watcher.ws_buffer = new Buffer(0)
watcher.ws_message_length = 0
client.room.watcher_stanzas = []
*/
watcher.on('data', function(data) {
var j, len, ref, results, 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;
...@@ -346,29 +293,6 @@ ...@@ -346,29 +293,6 @@
return results; return results;
}); });
/*
watcher.ws_buffer = Buffer.concat([watcher.ws_buffer, data], watcher.ws_buffer.length + data.length) #buffer的错误使用方式,好孩子不要学
while true
if watcher.ws_message_length == 0
if watcher.ws_buffer.length >= 2
watcher.ws_message_length = watcher.ws_buffer.readUInt16LE(0)
else
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)-> watcher.on 'close', (had_error)->
for w in client.room.ws_watchers for w in client.room.ws_watchers
...@@ -466,23 +390,6 @@ ...@@ -466,23 +390,6 @@
} }
}); });
/*
#房间管理
ygopro.stoc_follow 'HS_PLAYER_ENTER', false, (buffer, info, client, server)->
#console.log "PLAYER_ENTER to #{client.name}: #{info.name}, #{info.pos}"
#room = client.room
#if !room
* console.log "[WARN]player_enter: can't find room by player #{client.player}"
* return
#room.pos_name[info.pos] = info.name
ygopro.stoc_follow 'HS_PLAYER_CHANGE', false, (buffer, info, client, server)->
#client.ready = info.status & 0xF != 0
#client.pos = info.status >> 4
#console.log "PLAYER_CHANGE to #{client.name}: #{info.status & 0xF != 0}, #{info.status >> 4}"
*/
ygopro.stoc_follow('TYPE_CHANGE', false, function(buffer, info, client, server) { ygopro.stoc_follow('TYPE_CHANGE', false, function(buffer, info, client, server) {
var is_host, selftype; var is_host, selftype;
selftype = info.type & 0xf; selftype = info.type & 0xf;
...@@ -517,14 +424,8 @@ ...@@ -517,14 +424,8 @@
ref = client.room.players; ref = client.room.players;
for (j = 0, len = ref.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
player = ref[j]; player = ref[j];
if (!(player.pos !== 7)) { if (player.pos !== 7) {
continue; client.room.dueling_players[player.pos] = player;
}
client.room.dueling_players[player.pos] = player;
if (!player.main) {
} else {
player.deck = mycard.load_card_usages_from_cards(player.main, player.side);
} }
} }
} }
...@@ -549,51 +450,17 @@ ...@@ -549,51 +450,17 @@
} }
} }
}); });
case '/ranktop':
if (settings.modules.database) {
return User.find(null, null, {
sort: {
points: -1
},
limit: 8
}, function(err, users) {
var index, results, user;
if (err) {
return;
}
results = [];
for (index in users) {
user = users[index];
results.push(ygopro.stoc_send_chat(client, [parseInt(index) + 1, user.points, user.name].join(' ')));
}
return results;
});
}
break;
case '/help': case '/help':
ygopro.stoc_send_chat(client, "Mycard MatchServer 指令帮助"); ygopro.stoc_send_chat(client, "YGOSrv233 指令帮助");
ygopro.stoc_send_chat(client, "/help 显示这个帮助信息"); ygopro.stoc_send_chat(client, "/help 显示这个帮助信息");
if (settings.modules.tips) { if (settings.modules.tips) {
ygopro.stoc_send_chat(client, "/tip 显示一条提示"); return ygopro.stoc_send_chat(client, "/tip 显示一条提示");
} }
return ygopro.stoc_send_chat(client, "/senddeck 发送自己的卡组"); break;
case '/tip': case '/tip':
if (settings.modules.tips) { if (settings.modules.tips) {
return ygopro.stoc_send_random_tip(client); return ygopro.stoc_send_random_tip(client);
} }
break;
case '/senddeck':
if (client.deck != null) {
ygopro.stoc_send_chat(client, "正在读取卡组信息... ");
return mycard.deck_url_short(client.name, client.deck, function(url) {
return ygopro.stoc_send_chat_to_room(client.room, "卡组链接: " + url);
});
} else {
return ygopro.stoc_send_chat_to_room(client.room, "读取卡组信息失败");
}
break;
case '/admin showroom':
return log.info(client.room);
} }
}); });
...@@ -634,112 +501,33 @@ ...@@ -634,112 +501,33 @@
}); });
} }
/*
* 开包大战
packs_weighted_cards = {}
for pack, cards of require './packs.json'
packs_weighted_cards[pack] = []
for card in cards
for i in [0..card.count]
packs_weighted_cards[pack].push card.card
console.log packs_weighted_cards
ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
ygopro.ctos_send server, 'HS_NOTREADY'
deck = []
for pack in client.player
for i in [0...5]
deck.push packs_weighted_cards[pack][Math.floor(Math.random()*packs_weighted_cards[pack].length)]
ygopro.ctos_send server, 'UPDATE_DECK', {
mainc: deck.length,
sidec: 0,
deckbuf: deck
}
ygopro.ctos_send server, 'HS_READY'
*/
if (settings.modules.http) { if (settings.modules.http) {
level_points = require('./level_points.json');
waiting = [[]];
for (i in level_points) {
waiting.push([]);
}
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, player, ref, room, roomsjson, u;
u = url.parse(request.url, 1); u = url.parse(request.url, 1);
if (u.pathname === '/count.json') { if (u.pathname === '/count.json') {
response.writeHead(200); response.writeHead(200);
return response.end(Room.all.length.toString()); return response.end(Room.all.length.toString());
} else if (u.pathname === '/match') {
if (request.headers['authorization']) {
ref1 = new Buffer((ref = request.headers['authorization'].split(/\s+/).pop()) != null ? ref : '', 'base64').toString().split(':'), name = ref1[0], password = ref1[1];
return User.findOne({
name: name
}, function(err, user) {
var index, level, points;
if (!user) {
user = new User({
name: name,
points: 0,
elo: 1400
});
user.save();
}
level = level_points.length;
for (index in level_points) {
points = level_points[index];
if (user.points < points) {
level = index;
break;
}
}
response.allowance = 0;
waiting[level].push(response);
return request.on('close', function() {
index = waiting[level].indexOf(response);
if (index !== -1) {
return waiting[level].splice(index, 1);
}
});
});
} else {
level = 1;
response.allowance = 0;
waiting[level].push(response);
return request.on('close', function() {
var index;
index = waiting[level].indexOf(response);
if (index !== -1) {
return waiting[level].splice(index, 1);
}
});
}
} else if (u.pathname === '/rooms.js') { } else if (u.pathname === '/rooms.js') {
response.writeHead(200); response.writeHead(200);
roomsjson = JSON.stringify({ roomsjson = JSON.stringify({
rooms: (function() { rooms: (function() {
var j, len, ref2, results; var j, len, ref, results;
ref2 = Room.all; ref = Room.all;
results = []; results = [];
for (j = 0, len = ref2.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
room = ref2[j]; room = ref[j];
if (room.established) { if (room.established) {
results.push({ results.push({
roomid: room.port.toString(), roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0], roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(), needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() { users: (function() {
var k, len1, ref3, results1; var k, len1, ref1, results1;
ref3 = room.players; ref1 = room.players;
results1 = []; results1 = [];
for (k = 0, len1 = ref3.length; k < len1; k++) { for (k = 0, len1 = ref1.length; k < len1; k++) {
player = ref3[k]; player = ref1[k];
if (player.pos != null) { if (player.pos != null) {
results1.push({ results1.push({
id: (-1).toString(), id: (-1).toString(),
...@@ -762,22 +550,22 @@ ...@@ -762,22 +550,22 @@
response.writeHead(200); response.writeHead(200);
return response.end(JSON.stringify({ return response.end(JSON.stringify({
rooms: (function() { rooms: (function() {
var j, len, ref2, results; var j, len, ref, results;
ref2 = Room.all; ref = Room.all;
results = []; results = [];
for (j = 0, len = ref2.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
room = ref2[j]; room = ref[j];
if (room.established) { if (room.established) {
results.push({ results.push({
roomid: room.port.toString(), roomid: room.port.toString(),
roomname: room.name.split('$', 2)[0], roomname: room.name.split('$', 2)[0],
needpass: (room.name.indexOf('$') !== -1).toString(), needpass: (room.name.indexOf('$') !== -1).toString(),
users: (function() { users: (function() {
var k, len1, ref3, results1; var k, len1, ref1, results1;
ref3 = room.players; ref1 = room.players;
results1 = []; results1 = [];
for (k = 0, len1 = ref3.length; k < len1; k++) { for (k = 0, len1 = ref1.length; k < len1; k++) {
player = ref3[k]; player = ref1[k];
if (player.pos != null) { if (player.pos != null) {
results1.push({ results1.push({
id: (-1).toString(), id: (-1).toString(),
...@@ -796,9 +584,9 @@ ...@@ -796,9 +584,9 @@
})() })()
})); }));
} else if (u.query.pass === settings.modules.http.password && u.query.shout) { } else if (u.query.pass === settings.modules.http.password && u.query.shout) {
ref2 = Room.all; ref = Room.all;
for (j = 0, len = ref2.length; j < len; j++) { for (j = 0, len = ref.length; j < len; j++) {
room = ref2[j]; room = ref[j];
ygopro.stoc_send_chat_to_room(room, u.query.shout); ygopro.stoc_send_chat_to_room(room, u.query.shout);
} }
response.writeHead(200); response.writeHead(200);
...@@ -809,104 +597,6 @@ ...@@ -809,104 +597,6 @@
} }
}); });
http_server.listen(settings.modules.http.port); http_server.listen(settings.modules.http.port);
/*
setInterval ()->
for level in [level_points.length..0]
for index, player of waiting[level]
opponent_level = null
opponent = _.find waiting[level], (opponent)->
log.info opponent,player
opponent isnt player
log.info '--------1--------', waiting, opponent
if opponent
opponent_level = level
else if player.allowance > 0
for displacement in [1..player.allowance]
if level+displacement <= level_points.length
opponent = waiting[level+displacement][0]
if opponent
opponent_level = level+displacement
break
if level-displacement >= 0
opponent = waiting[level-displacement][0]
if opponent
opponent_level = level-displacement
break
if opponent
if waiting[level].indexOf(player) == -1 or waiting[opponent_level].indexOf(opponent) == -1
log.info waiting, player, level, opponent, opponent_level
throw 'WTF'
waiting[level].splice(waiting[level].indexOf(player), 1)
waiting[opponent_level].splice(waiting[opponent_level].indexOf(opponent), 1)
index--
room = "mycard://#{settings.ip}:#{settings.port}/M##{_.uniqueId()}$#{_.random(999)}"
log.info 'matched', room
headers = {"Access-Control-Allow-Origin":"*","Content-Type": "text/plain"}
player.writeHead(200, headers)
player.end room
opponent.writeHead(200, headers)
opponent.end room
else
player.allowance++
, 2000
*/
/*
originIsAllowed = (origin) ->
* allow all origin, for debug
true
wsServer = new WebSocketServer(
httpServer: http_server
autoAcceptConnections: false
)
wsServer.on "request", (request) ->
unless originIsAllowed(request.origin)
* Make sure we only accept requests from an allowed origin
request.reject()
console.log (new Date()) + " Connection from origin " + request.origin + " rejected."
return
room_name = decodeURIComponent(request.resource.slice(1))
if room_name == 'started'
room = _.find Room.all, (room)->
room.started
else
room = Room.find_by_name room_name
unless room
request.reject()
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}"
room.ws_watchers.push connection
for stanza in room.watcher_stanzas
connection.sendBytes stanza
*/
/*
connection.on "message", (message) ->
if message.type is "utf8"
console.log "Received Message: " + message.utf8Data
connection.sendUTF message.utf8Data
else if message.type is "binary"
console.log "Received Binary Message of " + message.binaryData.length + " bytes"
connection.sendBytes message.binaryData
*/
/*
connection.on "close", (reasonCode, description) ->
index = _.indexOf(room.ws_watchers, connection)
room.ws_watchers.splice(index, 1) unless index == -1
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