ygopro-server.js 15.2 KB
Newer Older
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
1 2
// Generated by CoffeeScript 1.6.3
(function() {
神楽坂玲奈's avatar
神楽坂玲奈 committed
3
  var Inotify, Room, debug, dialogues, fs, http, inotify, net, path, request, settings, tips, url, ygopro, _;
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
4

神楽坂玲奈's avatar
神楽坂玲奈 committed
5
  net = require('net');
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
6

神楽坂玲奈's avatar
神楽坂玲奈 committed
7 8 9
  http = require('http');

  url = require('url');
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
10

神楽坂玲奈's avatar
神楽坂玲奈 committed
11 12 13 14
  path = require('path');

  fs = require('fs');

神楽坂玲奈's avatar
神楽坂玲奈 committed
15 16
  _ = require('underscore');

神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
17 18 19 20
  _.str = require('underscore.string');

  _.mixin(_.str.exports());

神楽坂玲奈's avatar
神楽坂玲奈 committed
21 22
  Inotify = require('inotify').Inotify;

神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
23 24
  request = require('request');

神楽坂玲奈's avatar
神楽坂玲奈 committed
25
  ygopro = require('./ygopro.js');
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
26

神楽坂玲奈's avatar
神楽坂玲奈 committed
27
  Room = require('./room.js');
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
28 29 30

  settings = require('./config.json');

神楽坂玲奈's avatar
神楽坂玲奈 committed
31 32
  debug = false;

神楽坂玲奈's avatar
神楽坂玲奈 committed
33 34 35 36 37
  if (process.argv[2] === '--debug') {
    settings.port++;
    settings.http_port++;
  }

神楽坂玲奈's avatar
神楽坂玲奈 committed
38 39 40 41 42 43 44 45 46
  net.createServer(function(client) {
    var ctos_buffer, ctos_message_length, ctos_proto, server, stoc_buffer, stoc_message_length, stoc_proto;
    server = new net.Socket();
    client.server = server;
    client.on('close', function(had_error) {
      console.log("client closed " + had_error);
      if (!client.closed) {
        if (client.room) {
          client.room.disconnect(client);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
47
        }
神楽坂玲奈's avatar
神楽坂玲奈 committed
48
        client.closed = true;
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
49
      }
神楽坂玲奈's avatar
神楽坂玲奈 committed
50 51 52 53 54 55 56
      return server.end();
    });
    client.on('error', function(error) {
      console.log("client error " + error);
      if (!client.closed) {
        if (client.room) {
          client.room.disconnect(client, error);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
57
        }
神楽坂玲奈's avatar
神楽坂玲奈 committed
58
        client.closed = error;
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
59
      }
神楽坂玲奈's avatar
神楽坂玲奈 committed
60
      return server.end();
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
61
    });
神楽坂玲奈's avatar
神楽坂玲奈 committed
62 63 64 65
    server.on('close', function(had_error) {
      console.log("server closed " + had_error);
      if (!server.closed) {
        server.closed = true;
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
66
      }
神楽坂玲奈's avatar
神楽坂玲奈 committed
67 68 69 70
      if (!client.closed) {
        ygopro.stoc_send_chat(client, "服务器关闭了连接");
        client.closed = true;
        return client.end();
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
71 72
      }
    });
神楽坂玲奈's avatar
神楽坂玲奈 committed
73 74 75 76 77 78 79 80
    server.on('error', function(error) {
      console.log("server error " + error);
      server.closed = error;
      if (!client.closed) {
        ygopro.stoc_send_chat(client, "服务器错误: " + error);
        client.closed = true;
        return client.end();
      }
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
81 82 83 84
    });
    ctos_buffer = new Buffer(0);
    ctos_message_length = 0;
    ctos_proto = 0;
神楽坂玲奈's avatar
神楽坂玲奈 committed
85 86
    client.pre_establish_buffers = new Array();
    client.on('data', function(data) {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      var b, struct;
      ctos_buffer = Buffer.concat([ctos_buffer, data], ctos_buffer.length + data.length);
      while (true) {
        if (ctos_message_length === 0) {
          if (ctos_buffer.length >= 2) {
            ctos_message_length = ctos_buffer.readUInt16LE(0);
          } else {
            break;
          }
        } else if (ctos_proto === 0) {
          if (ctos_buffer.length >= 3) {
            ctos_proto = ctos_buffer.readUInt8(2);
          } else {
            break;
          }
        } else {
          if (ctos_buffer.length >= 2 + ctos_message_length) {
神楽坂玲奈's avatar
神楽坂玲奈 committed
104
            if (ygopro.ctos_follows[ctos_proto]) {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
105
              b = ctos_buffer.slice(3, ctos_message_length - 1 + 3);
神楽坂玲奈's avatar
神楽坂玲奈 committed
106
              if (struct = ygopro.structs[ygopro.proto_structs.CTOS[ygopro.constants.CTOS[ctos_proto]]]) {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
107
                struct._setBuff(b);
神楽坂玲奈's avatar
神楽坂玲奈 committed
108
                setTimeout(ygopro.ctos_follows[ctos_proto].callback, 0, b, _.clone(struct.fields), client, server);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
109
              } else {
神楽坂玲奈's avatar
神楽坂玲奈 committed
110
                setTimeout(ygopro.ctos_follows[ctos_proto].callback, 0, b, null, client, server);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
111 112 113 114 115 116 117 118 119 120
              }
            }
            ctos_buffer = ctos_buffer.slice(2 + ctos_message_length);
            ctos_message_length = 0;
            ctos_proto = 0;
          } else {
            break;
          }
        }
      }
神楽坂玲奈's avatar
神楽坂玲奈 committed
121 122
      if (!(ygopro.ctos_follows[ctos_proto] && ygopro.ctos_follows[ctos_proto].synchronous)) {
        if (client.established) {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
123 124
          return server.write(data);
        } else {
神楽坂玲奈's avatar
神楽坂玲奈 committed
125
          return client.pre_establish_buffers.push(data);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
126 127 128
        }
      }
    });
神楽坂玲奈's avatar
神楽坂玲奈 committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    stoc_buffer = new Buffer(0);
    stoc_message_length = 0;
    stoc_proto = 0;
    return server.on('data', function(data) {
      var b, struct;
      stoc_buffer = Buffer.concat([stoc_buffer, data], stoc_buffer.length + data.length);
      while (true) {
        if (stoc_message_length === 0) {
          if (stoc_buffer.length >= 2) {
            stoc_message_length = stoc_buffer.readUInt16LE(0);
          } else {
            break;
          }
        } else if (stoc_proto === 0) {
          if (stoc_buffer.length >= 3) {
            stoc_proto = stoc_buffer.readUInt8(2);
          } else {
            break;
          }
        } else {
          if (stoc_buffer.length >= 2 + stoc_message_length) {
            if (ygopro.stoc_follows[stoc_proto]) {
              b = stoc_buffer.slice(3, stoc_message_length - 1 + 3);
              if (struct = ygopro.structs[ygopro.proto_structs.STOC[ygopro.constants.STOC[stoc_proto]]]) {
                struct._setBuff(b);
                setImmediate(ygopro.stoc_follows[stoc_proto].callback, b, _.clone(struct.fields), client, server);
              } else {
                setImmediate(ygopro.stoc_follows[stoc_proto].callback, b, null, client, server);
              }
            }
            stoc_buffer = stoc_buffer.slice(2 + stoc_message_length);
            stoc_message_length = 0;
            stoc_proto = 0;
          } else {
            break;
          }
        }
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
166
      }
神楽坂玲奈's avatar
神楽坂玲奈 committed
167
      return client.write(data);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
168
    });
神楽坂玲奈's avatar
神楽坂玲奈 committed
169
  }).listen(settings.port, function() {
神楽坂玲奈's avatar
神楽坂玲奈 committed
170 171
    return console.log("server started on " + settings.ip + ":" + settings.port);
  });
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
172

神楽坂玲奈's avatar
神楽坂玲奈 committed
173 174
  ygopro.ctos_follow('PLAYER_INFO', true, function(buffer, info, client, server) {
    return client.name = info.name;
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
175 176
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
177
  ygopro.ctos_follow('JOIN_GAME', false, function(buffer, info, client, server) {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
178
    if (info.version !== settings.version) {
神楽坂玲奈's avatar
神楽坂玲奈 committed
179
      ygopro.stoc_send(client, 'ERROR_MSG', {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
180 181 182 183
        msg: 4,
        code: settings.version
      });
      return client.end();
神楽坂玲奈's avatar
神楽坂玲奈 committed
184 185 186
    } else if (!info.pass.length) {
      ygopro.stoc_send_chat(client, "房间为空,请修改房间名");
      ygopro.stoc_send(client, 'ERROR_MSG', {
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
187 188
        msg: 1,
        code: 2
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
189
      });
神楽坂玲奈's avatar
神楽坂玲奈 committed
190 191 192
      return client.end();
    } else if (info.pass === '[INCORRECT]') {
      ygopro.stoc_send(client, 'ERROR_MSG', {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
193 194 195 196
        msg: 1,
        code: 1
      });
      return client.end();
神楽坂玲奈's avatar
神楽坂玲奈 committed
197 198 199 200 201 202
    } else if (client.name === '[INCORRECT]') {
      ygopro.stoc_send(client, 'ERROR_MSG', {
        msg: 1,
        code: 2
      });
      return client.end();
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
203
    } else {
神楽坂玲奈's avatar
神楽坂玲奈 committed
204 205
      client.room = Room.find_or_create_by_name(info.pass);
      return client.room.connect(client);
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
206 207 208
    }
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
209 210
  ygopro.stoc_follow('JOIN_GAME', false, function(buffer, info, client, server) {
    ygopro.stoc_send(client, 'CHAT', {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
211
      player: 8,
神楽坂玲奈's avatar
神楽坂玲奈 committed
212
      msg: "Mycard Debugging Server"
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
213
    });
神楽坂玲奈's avatar
神楽坂玲奈 committed
214
    return ygopro.stoc_send(client, 'CHAT', {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
215
      player: 8,
神楽坂玲奈's avatar
神楽坂玲奈 committed
216
      msg: "这里是测试中的新服务器, 还不稳定, 随时可能崩溃, 遇到意外请淡定\n                           ˉˉˉˉˉ"
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
217 218 219
    });
  });

神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
220 221 222 223 224 225
  dialogues = {};

  request({
    url: 'https://my-card.in/dialogues.json',
    json: true
  }, function(error, response, body) {
神楽坂玲奈's avatar
神楽坂玲奈 committed
226 227 228 229 230 231
    if (_.isString(body)) {
      return console.log("[WARN]dialogues bad json " + body);
    } else {
      console.log("loaded " + (_.size(body)) + " dialogues");
      return dialogues = body;
    }
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
232
  });
神楽坂玲奈's avatar
神楽坂玲奈 committed
233

神楽坂玲奈's avatar
神楽坂玲奈 committed
234 235
  ygopro.stoc_follow('GAME_MSG', false, function(buffer, info, client, server) {
    var card, line, msg, _i, _len, _ref, _results;
神楽坂玲奈's avatar
神楽坂玲奈 committed
236
    msg = buffer.readInt8(0);
神楽坂玲奈's avatar
神楽坂玲奈 committed
237
    if (ygopro.constants.MSG[msg] === 'SUMMONING' || ygopro.constants.MSG[msg] === 'SPSUMMONING') {
神楽坂玲奈's avatar
神楽坂玲奈 committed
238
      card = buffer.readUInt32LE(1);
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
239 240
      if (dialogues[card]) {
        _ref = _.lines(dialogues[card][Math.floor(Math.random() * dialogues[card].length)]);
神楽坂玲奈's avatar
神楽坂玲奈 committed
241 242 243 244
        _results = [];
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          line = _ref[_i];
          _results.push(ygopro.stoc_send_chat(client, line));
神楽坂玲奈's avatar
神楽坂玲奈 committed
245
        }
神楽坂玲奈's avatar
神楽坂玲奈 committed
246
        return _results;
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
247 248 249 250
      }
    }
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  /*
    if ygopro.constants.MSG[msg] == 'WIN'
      room = Room.find_by_client(client)
      if !room
        console.log "[WARN]win: can't find room by player #{client.player}"
        return
      if _.startsWith(room.name, 'M#') and room.dueling
        room.dueling = false
  
        loser_name = room.pos_name[buffer.readUInt8(1)]
        winner_name = room.pos_name[1 - buffer.readUInt8(1)]
        #type = buffer.readUInt8(2)
        User.findOne { name: winner_name }, (err, winner)->
          if(err)
            console.log "#{err} when finding user #{winner_name}"
          else if(!winner)
            console.log "user #{winner_name} not exist"
          else
            User.findOne { name: loser_name }, (err, loser)->
              if(err)
                console.log "#{err} when finding user #{loser_name}"
              else if(!loser)
                console.log "user #{loser_name} not exist"
              else
                winner.points += 10
                loser.points -= 5
                winner.save()
                loser.save()
                console.log "#{winner} 增加10点积分,现在有#{winner.points}点"
                console.log "#{loser} 减少5点积分,现在有#{loser.points}点"
  
  
  #mongoose = require 'mongoose'
  #mongoose.connect('mongodb://localhost/mycard');
  #User = mongoose.model 'User',
  #  name: String
  #  points: Number
  
  #ygopro.stoc_follow 'HS_PLAYER_CHANGE', false, (buffer, info, client, server)->
  #  console.log 'HS_PLAYER_CHANGE', info
  */
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
292 293


神楽坂玲奈's avatar
神楽坂玲奈 committed
294 295 296
  ygopro.stoc_follow('HS_PLAYER_ENTER', false, function(buffer, info, client, server) {
    return console.log("PLAYER_ENTER to " + client.name + ": " + info.name + ", " + info.pos);
  });
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
297

神楽坂玲奈's avatar
神楽坂玲奈 committed
298 299
  ygopro.stoc_follow('HS_PLAYER_CHANGE', false, function(buffer, info, client, server) {
    return console.log("PLAYER_CHANGE to " + client.name + ": " + (info.status & 0xF !== 0) + ", " + (info.status >> 4));
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
300 301
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
302 303 304 305 306 307 308
  ygopro.stoc_follow('TYPE_CHANGE', false, function(buffer, info, client, server) {
    var is_host, selftype;
    selftype = info.type & 0xf;
    is_host = ((info.type >> 4) & 0xf) !== 0;
    client.is_host = is_host;
    client.pos = selftype;
    return console.log("TYPE_CHANGE to " + client.name + ":", info, selftype, is_host);
神楽坂玲奈's avatar
神楽坂玲奈 committed
309 310 311
  });

  http.createServer(function(request, response) {
神楽坂玲奈's avatar
神楽坂玲奈 committed
312 313 314 315 316 317 318 319 320 321
    var player, room;
    url = url.parse(request.url);
    if (url.pathname === '/count.json') {
      response.writeHead(200);
      response.end(Room.all.length.toString());
    } else if (url.pathname === '/rooms.json') {
      response.writeHead(404);
      response.end();
    }
    if (url.query === 'operation=getroomjson') {
神楽坂玲奈's avatar
神楽坂玲奈 committed
322
      response.writeHead(200);
神楽坂玲奈's avatar
神楽坂玲奈 committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
      return response.end(JSON.stringify({
        rooms: (function() {
          var _i, _len, _ref, _results;
          _ref = Room.all;
          _results = [];
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            room = _ref[_i];
            _results.push({
              roomid: room.port.toString(),
              roomname: room.name,
              needpass: false.toString(),
              users: (function() {
                var _j, _len1, _ref1, _results1;
                _ref1 = room.players;
                _results1 = [];
                for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
                  player = _ref1[_j];
                  _results1.push({
                    id: (-1).toString(),
                    name: player.name,
                    pos: player.pos
                  });
                }
                return _results1;
              })(),
              istart: "wait"
            });
          }
          return _results;
        })()
      }));
神楽坂玲奈's avatar
神楽坂玲奈 committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    } else {
      response.writeHead(404);
      return response.end();
    }
  }).listen(settings.http_port);

  inotify = new Inotify();

  inotify.addWatch({
    path: 'ygocore/replay',
    watch_for: Inotify.IN_CLOSE_WRITE | Inotify.IN_CREATE | Inotify.IN_MODIFY,
    callback: function(event) {
      var mask, port, room;
      mask = event.mask;
      if (event.name) {
        port = parseInt(path.basename(event.name, '.yrp'));
神楽坂玲奈's avatar
神楽坂玲奈 committed
370
        room = Room.find_by_port(port);
神楽坂玲奈's avatar
神楽坂玲奈 committed
371 372 373 374 375 376 377 378 379 380 381 382 383
        if (room) {
          if (mask & Inotify.IN_CREATE) {

          } else if (mask & Inotify.IN_CLOSE_WRITE) {
            return fs.unlink(path.join('ygocore/replay'), function(err) {});
          } else if (mask & Inotify.IN_MODIFY) {
            return room.alive = true;
          }
        }
      } else {
        return console.log('[warn] event without filename');
      }
    }
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
384 385
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
386
  setInterval(function() {
神楽坂玲奈's avatar
神楽坂玲奈 committed
387
    var player, room, _i, _j, _len, _len1, _ref, _ref1, _results;
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
388
    _ref = Room.all;
神楽坂玲奈's avatar
神楽坂玲奈 committed
389
    _results = [];
神楽坂玲奈's avatar
神楽坂玲奈 committed
390 391
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      room = _ref[_i];
神楽坂玲奈's avatar
神楽坂玲奈 committed
392 393 394
      if (room.alive) {
        _results.push(room.alive = false);
      } else {
神楽坂玲奈's avatar
 
神楽坂玲奈 committed
395
        console.log("kill room " + room.port);
神楽坂玲奈's avatar
神楽坂玲奈 committed
396 397 398 399 400 401 402
        _ref1 = room.players;
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          player = _ref1[_j];
          if (!player.closed) {
            ygopro.stoc_send_chat(player, "由于长时间没有活动被关闭");
          }
        }
神楽坂玲奈's avatar
神楽坂玲奈 committed
403
        _results.push(room.process.kill());
神楽坂玲奈's avatar
神楽坂玲奈 committed
404 405 406 407 408
      }
    }
    return _results;
  }, 900000);

神楽坂玲奈's avatar
神楽坂玲奈 committed
409 410
  ygopro.stoc_send_tip = function(client, tip) {
    var line, lines, _i, _len, _ref, _results;
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
411
    lines = _.lines(tip);
神楽坂玲奈's avatar
神楽坂玲奈 committed
412
    ygopro.stoc_send_chat(client, "Tip: " + lines[0]);
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
413 414
    _ref = lines.slice(1);
    _results = [];
神楽坂玲奈's avatar
神楽坂玲奈 committed
415 416 417
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      line = _ref[_i];
      _results.push(ygopro.stoc_send_chat(client, line));
神楽坂玲奈's avatar
神楽坂玲奈 committed
418
    }
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
419 420 421
    return _results;
  };

神楽坂玲奈's avatar
神楽坂玲奈 committed
422
  ygopro.stoc_send_random_tip = function(client) {
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
423
    if (tips) {
神楽坂玲奈's avatar
神楽坂玲奈 committed
424
      return ygopro.stoc_send_tip(client, tips[Math.floor(Math.random() * tips.length)]);
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
425 426
    }
  };
神楽坂玲奈's avatar
神楽坂玲奈 committed
427

神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
428 429 430 431 432 433 434 435 436 437
  tips = null;

  request({
    url: 'https://my-card.in/tips.json',
    json: true
  }, function(error, response, body) {
    tips = body;
    return console.log("loaded " + tips.length + " tips");
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  ygopro.stoc_follow('DUEL_START', false, function(buffer, info, client, server) {
    return ygopro.stoc_send_random_tip(client);
    /*
    room = Room.find_by_client(client)
    if !room
      console.log "[WARN]duel start: can't find room by player #{client.player}"
      return
    
    room.dueling = true
    if _.startsWith(room.name, 'M#')
      User.findOne { name: client.player }, (err, user)->
        if !user
          user = new User({name: client.player, points: 0})
          user.save()
        ygopro.stoc_send_chat(client, "积分系统测试中,你现在有#{user.points}点积分,这些积分以后可能会重置")
    */

神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
455 456
  });

神楽坂玲奈's avatar
神楽坂玲奈 committed
457
  ygopro.ctos_follow('CHAT', false, function(buffer, info, client, server) {
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
458
    if (_.trim(info.msg) === '/tip') {
神楽坂玲奈's avatar
神楽坂玲奈 committed
459
      return ygopro.stoc_send_random_tip(client);
神楽坂玲奈's avatar
tip  
神楽坂玲奈 committed
460 461
    }
  });
神楽坂玲奈's avatar
神楽坂玲奈 committed
462

神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
463 464 465 466 467 468 469 470 471 472 473 474
  /*
  # 开包大战
  
  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
  
神楽坂玲奈's avatar
神楽坂玲奈 committed
475 476
  ygopro.ctos_follow 'UPDATE_DECK', false, (buffer, info, client, server)->
    ygopro.ctos_send server, 'HS_NOTREADY'
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
477 478 479 480 481 482 483
  
    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)]
  
  
神楽坂玲奈's avatar
神楽坂玲奈 committed
484
    ygopro.ctos_send server, 'UPDATE_DECK', {
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
485 486 487 488
      mainc: deck.length,
      sidec: 0,
      deckbuf: deck
    }
神楽坂玲奈's avatar
神楽坂玲奈 committed
489
    ygopro.ctos_send server, 'HS_READY'
神楽坂玲奈's avatar
init  
神楽坂玲奈 committed
490 491 492 493
  */


}).call(this);