| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 'use strict';
- var quick = require('quick-pomelo');
- var P = quick.Promise;
- var _ = require('lodash');
- var url = require('url');
- var md5 = require('md5');
- var util = require('util');
- var http = require('http');
- var qs = require('querystring');
- var C = require('../share/constant');
- var logger = quick.logger.getLogger('query', __filename);
- // 构造方法
- var Controller = function (app) {
- this.app = app;
- this.uptime = Date.now();
- this.port = app.getCurServer().httpPort;
- this.server = http.createServer((req, res) => this.handleAsync(req, res));
- this.server.listen(this.port);
- };
- // 导出方法
- module.exports = function (app) {
- return new Controller(app);
- };
- // 原型对象
- var proto = Controller.prototype;
- // 路由函数
- proto.route = function (pathname) {
- switch (pathname) {
- case '/getGameRecord': return (query, method, res) => this.getGameRecordAsync(query, method, res);
- case '/makeHistory': return (query, method, res) => this.makeHistoryAsync(query, method, res);
- case '/recvHistory': return (query, method, res) => this.recvHistoryAsync(query, method, res);
- case '/getUserInfo': return (query, method, res) => this.getUserInfoAsync(query, method, res);
- }
- };
- // 写出数据
- proto.writeOut = function (query, res) {
- if (typeof query != 'object') {
- return res.end(String(query));
- }
- return res.end(JSON.stringify(query));
- };
- // 跨域选项
- const ACCESS = {
- "Content-Type": "text/plain;charset=utf-8",
- "Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Headers": "X-Requested-With",
- "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS"
- };
- // 入口函数
- proto.handleAsync = P.coroutine(function* (req, res) {
- var pathname = url.parse(req.url).pathname;
- var phandler = this.route(pathname);
- if (!phandler) {
- res.writeHead(404, ACCESS);
- return res.end('WARNING: Not Found!');
- }
- else {
- res.writeHead(200, ACCESS);
- if (req.method.toLowerCase() == 'get') {
- let query = url.parse(req.url, true).query;
- return phandler(query, req.method, res);
- }
- else if (req.method.toLowerCase() == 'post') {
- let data = '';
- req.on('data', (d) => { data += d; });
- req.on('end', () => {
- let query = { __post__: data };
- let regexp = /^\s*{(\s*?".*?":.*\s*)*}\s*$/;
- if (regexp.test(data)) try { query = JSON.parse(data); } catch (e) { }
- else if (data.indexOf('=') != -1) query = qs.parse(data);
- return phandler(query, req.method, res);
- });
- }
- }
- });
- // 查询记录
- proto.getGameRecordAsync = P.coroutine(function* (query, method, res) {
- var list = [];
- var playerId = query.playerId;
- if (!playerId) return this.writeOut(list, res);
- var opts = { 'users._id': playerId };
- var gameId = parseInt(query.gameId);
- if (gameId) opts.gameId = gameId;
- var count = 20;
- var fields = 'gameId tableId type round stime time users._id users.score';
- var records = yield this.app.models.Record.findMongoAsync(opts, fields, { sort: { time: -1 }, limit: count });
- for (let record of records) {
- let scorer = record.users.id(playerId);
- if (scorer) {
- list.push({ gameId: record.gameId, tableId: record.tableId, type: record.type, round: record.round, time: record.stime || record.time, score: scorer.score });
- }
- }
- return this.writeOut(list, res);
- });
- // 发包记录
- proto.makeHistoryAsync = P.coroutine(function* (query, method, res) {
- var playerId = query.playerId;
- if (!playerId) {
- return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
- }
- var list = yield this.app.models.RedPack.findMongoAsync({ sid: playerId }, 'diamond stime recvd', { sort: { stime: -1 }, limit: 20 });
- list = list.map(o => ({ rpId: o._id, diamond: o.diamond, stime: o.stime, recvd: o.recvd }));
- return this.writeOut({ code: C.OK, list: list }, res);
- });
- // 领包记录
- proto.recvHistoryAsync = P.coroutine(function* (query, method, res) {
- var playerId = query.playerId;
- if (!playerId) {
- return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
- }
- var list = yield this.app.models.RedPack.findMongoAsync({ rid: playerId }, 'diamond rtime', { sort: { rtime: -1 }, limit: 20 });
- list = list.map(o => ({ rpId: o._id, diamond: o.diamond, rtime: o.rtime }));
- return this.writeOut({ code: C.OK, list: list }, res);
- });
- // 个人信息
- proto.getUserInfoAsync = P.coroutine(function* (query, method, res) {
- var playerId = query.playerId;
- if (!playerId) {
- return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
- }
- var self = this;
- var app = this.app;
- return app.memdb.goose.transactionAsync(P.coroutine(function* () {
- var player = yield self.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond');
- if (!player) {
- return self.writeOut({ code: C.FAILD, msg: C.HTTP_NOT_PLAYER }, res);
- }
- return self.writeOut({ code: C.OK, diamond: player.diamond }, res);
- }), app.getServerId())
- .then(() => app.event.emit('transactionSuccess'), () => app.event.emit('transactionFail'));
- });
|