csjiangHandler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. var quick = require('quick-pomelo');
  3. var P = quick.Promise;
  4. var _ = require('lodash');
  5. var C = require('../../../../share/constant');
  6. var logger = quick.logger.getLogger('fhmj', __filename);
  7. var Handler = function (app) {
  8. this.app = app;
  9. };
  10. module.exports = function (app) {
  11. return new Handler(app);
  12. };
  13. var proto = Handler.prototype;
  14. // 判断桌子是否存在
  15. proto.isTableExist = P.coroutine(function* (msg, session, next) {
  16. // console.warn("XXXXXXXXXXXXXX000");
  17. if (!session.uid) {
  18. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  19. }
  20. if (!msg.tableId) {
  21. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  22. }
  23. var reload = parseInt(msg.reload) || 0;//ts++重入 1为重入
  24. return this.app.controllers.csjiang.isTableExistAsync(session.uid, String(msg.tableId),reload).nodeify(next);
  25. });
  26. // 加入桌子
  27. proto.joinTable = P.coroutine(function* (msg, session, next) {
  28. if (!session.uid) {
  29. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  30. }
  31. if (!msg.tableId) {
  32. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  33. }
  34. return this.app.controllers.csjiang.joinTableAsync(session.uid, String(msg.tableId)).nodeify(next);
  35. });
  36. //////TL++创建或者加入桌子,用于玩家通过战绩分享进来之后
  37. proto.creatOrjoinTable = P.coroutine(function* (msg, session, next) {
  38. if (!session.uid) {
  39. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  40. }
  41. if (!msg.upId) {
  42. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  43. }
  44. if (!msg.other) {
  45. if(msg.other != 0){
  46. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  47. }
  48. }
  49. var round = parseInt(msg.round);
  50. var type = parseInt(msg.type) || 0;
  51. var kind = parseInt(msg.kind) || 1;////1:传统;2:割索;3:疯狂
  52. // console.warn("创建房间的 kind ",kind,msg.kind);
  53. if(kind != 1 && kind != 2 && kind != 3){
  54. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });/////既不是平搓也不是冲刺
  55. }
  56. var playercount = parseInt(msg.playercount) || 4;////游戏人数 = 2表示2人局 = 3表示3人局 = 4表示4人局
  57. var other = parseInt(msg.other) || 0;////TL++2人3人的游戏规则
  58. if (other < 0 || other > 1023) {
  59. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  60. }
  61. var upId = msg.upId;////TL++主动创建房间的话此值为"",通过战绩分享自动创建的房间此变量有值
  62. // console.warn("创建房间的other",other,msg.other);
  63. if(!(other & 512)){
  64. if(round != 16 && round != 12 && round != 8){//////游戏局数不合法
  65. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  66. }
  67. }
  68. if(playercount != 2 && playercount != 3 && playercount != 4){//////游戏人数不合法
  69. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  70. }
  71. var agentId=msg.agentId || "";//数据库中agenter(推广员)数据表的主键(_id)
  72. // console.warn("创建或者加入桌子 agentId "+agentId+" session.uid "+session.uid);
  73. return this.app.controllers.csjiang.creatOrjoinTableAsync(session.uid, round,type, kind,playercount,upId,other,agentId).nodeify(next);
  74. // return this.app.controllers.csjiang.creatOrjoinTableAsync(session.uid, msg).nodeify(next);
  75. });
  76. // 创建桌子
  77. proto.createTable = P.coroutine(function* (msg, session, next) {
  78. if (!session.uid) {
  79. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  80. }
  81. if (!msg.other) {
  82. if(msg.other != 0){
  83. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  84. }
  85. }
  86. var round = parseInt(msg.round);
  87. var type = parseInt(msg.type) || 0;
  88. var gameKindTL = parseInt(msg.gameKindTL) || 1;////平搓还是冲刺 = 1代表平搓 = 2代表冲刺
  89. if(gameKindTL != 1 && gameKindTL != 2 && gameKindTL != 3){
  90. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });/////既不是平搓也不是冲刺
  91. }
  92. var playerAllCount = parseInt(msg.playerAllCount) || 4;////游戏人数 = 2表示2人局 = 3表示3人局 = 4表示4人局
  93. var other = parseInt(msg.other) || 0;////TL++2人3人的游戏规则
  94. if (other < 0 || other > 1023) {
  95. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  96. }
  97. var upId = msg.upId || "";////TL++主动创建房间的话此值为"",通过战绩分享自动创建的房间此变量有值
  98. // console.warn("创建房间的other",other,msg.other);
  99. if(!(other & 512)){
  100. if(round != 16 && round != 12 && round != 8){//////游戏局数不合法
  101. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  102. }
  103. }
  104. if(playerAllCount != 2 && playerAllCount != 3 && playerAllCount != 4){//////游戏人数不合法
  105. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  106. }
  107. var agentId=msg.agentId || "";
  108. // console.warn("创建桌子, "+agentId+" session.uid "+session.uid);//数据库中agenter(推广员)数据表的主键(_id)
  109. return this.app.controllers.csjiang.createTableAsync(session.uid, round, type,gameKindTL,playerAllCount,upId,other,agentId).nodeify(next);/////修改局数
  110. });
  111. // TL++,得到玩家位置
  112. proto.sendLocation = P.coroutine(function* (msg, session, next) {
  113. if (!session.uid) {
  114. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  115. }
  116. return this.app.controllers.csjiang.sendLocationAsync(session.uid, msg).nodeify(next);
  117. });
  118. // 得到玩家位置
  119. proto.getLocation = P.coroutine(function* (msg, session, next) {
  120. if (!session.uid) {
  121. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  122. }
  123. return this.app.controllers.csjiang.getLocationAsync(session.uid, msg).nodeify(next);
  124. });
  125. // 离开桌子
  126. proto.leaveTable = P.coroutine(function* (msg, session, next) {
  127. if (!session.uid) {
  128. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  129. }
  130. return this.app.controllers.csjiang.leaveTableAsync(session.uid).nodeify(next);
  131. });
  132. // 坐下
  133. proto.seat = P.coroutine(function* (msg, session, next) {
  134. if (!session.uid) {
  135. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  136. }
  137. var chairId = parseInt(msg.chairId);
  138. if (!chairId && chairId != 0) {
  139. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  140. }
  141. return this.app.controllers.csjiang.seatAsync(session.uid, chairId).nodeify(next);
  142. });
  143. // 站起
  144. proto.stand = P.coroutine(function* (msg, session, next) {
  145. if (!session.uid) {
  146. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  147. }
  148. return this.app.controllers.csjiang.standAsync(session.uid).nodeify(next);
  149. });
  150. // 准备
  151. proto.ready = P.coroutine(function* (msg, session, next) {
  152. if (!session.uid) {
  153. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  154. }
  155. return this.app.controllers.csjiang.readyGameAsync(session.uid).nodeify(next);
  156. });
  157. // 开始
  158. proto.startGame = P.coroutine(function* (msg, session, next) {
  159. if (!session.uid) {
  160. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  161. }
  162. return this.app.controllers.csjiang.startGameAsync(session.uid).nodeify(next);
  163. });
  164. // 出牌
  165. proto.outCard = P.coroutine(function* (msg, session, next) {
  166. if (!session.uid) {
  167. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  168. }
  169. var card = parseInt(msg.card);
  170. let str3 = "cshander 出牌.......uid: " + session.uid + " card: " + card ;
  171. logger.warn(str3);////cssj
  172. if (!card) {
  173. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  174. }
  175. return this.app.controllers.csjiang.outCardAsync(session.uid, card).nodeify(next);
  176. });
  177. // 杠牌
  178. proto.gangCard = P.coroutine(function* (msg, session, next) {
  179. if (!session.uid) {
  180. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  181. }
  182. var card = parseInt(msg.card);
  183. var type = parseInt(msg.type);
  184. // console.warn("杠牌 card "+card+" type "+type);
  185. if (!card || !type) {
  186. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  187. }
  188. let str3 = "cshander 杠牌.......uid: " + session.uid + " card: " + card+ " type: " + type ;
  189. logger.warn(str3);////cssj
  190. return this.app.controllers.csjiang.gangCardAsync(session.uid, card, type).nodeify(next);
  191. });
  192. // 碰牌
  193. proto.pengCard = P.coroutine(function* (msg, session, next) {
  194. if (!session.uid) {
  195. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  196. }
  197. var card = parseInt(msg.card);
  198. // console.warn("碰牌 card "+card);
  199. if (!card) {
  200. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  201. }
  202. let str3 = "cshander 碰牌.......uid: " + session.uid + " card: " + card ;
  203. logger.warn(str3);////cssj
  204. return this.app.controllers.csjiang.pengCardAsync(session.uid, card).nodeify(next);
  205. });
  206. // 吃牌
  207. proto.eatCard = P.coroutine(function* (msg, session, next) {
  208. if (!session.uid) {
  209. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  210. }
  211. var card = parseInt(msg.card);
  212. var type = parseInt(msg.type);
  213. if (!card || !type) {
  214. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  215. }
  216. let str3 = "cshander 吃牌.......uid: " + session.uid + " card: " + card+ " type: " + type ;
  217. logger.warn(str3);////cssj
  218. return this.app.controllers.csjiang.eatCardAsync(session.uid, card, type).nodeify(next);
  219. });
  220. // 补牌
  221. proto.buCard = P.coroutine(function* (msg, session, next) {
  222. if (!session.uid) {
  223. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  224. }
  225. var card = parseInt(msg.card);
  226. var type = parseInt(msg.type);
  227. // console.warn("补牌 card "+card+" type "+type);
  228. if (!card || !type) {
  229. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  230. }
  231. let str3 = "cshander 补牌.......uid: " + session.uid + " card: " + card+ " type: " + type ;
  232. logger.warn(str3);////cssj
  233. return this.app.controllers.csjiang.buCardAsync(session.uid, card, type).nodeify(next);
  234. });
  235. // 起手胡牌
  236. proto.qshCard = P.coroutine(function* (msg, session, next) {
  237. if (!session.uid) {
  238. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  239. }
  240. var cards = msg.cards || [];
  241. var type = parseInt(msg.type);
  242. // console.warn(" 起手胡牌 type "+type + " cards " +cards);
  243. let isPARAM_ERROR = false;
  244. if(type < 0 || type > 5) isPARAM_ERROR = true;
  245. if((type == 0 || type == 4) && cards.length != 4) isPARAM_ERROR = true;
  246. if((type == 3 || type == 5) && cards.length != 6) isPARAM_ERROR = true;
  247. if(isPARAM_ERROR) return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  248. let cards2 = [];
  249. for (var i = 0; i < cards.length; i++) {
  250. let card = parseInt(cards[i]) || 0;
  251. if(!card){
  252. // console.warn("这里出错了??");
  253. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  254. }
  255. cards2[cards2.length] = card;
  256. }
  257. let str1 = "cshander 起手胡牌.......uid: " + session.uid+ " type: " + type ;
  258. logger.warn(str1);////cssj
  259. return this.app.controllers.csjiang.qshCardAsync(session.uid, type, cards2).nodeify(next);
  260. });
  261. // 胡牌
  262. proto.succCard = P.coroutine(function* (msg, session, next) {
  263. if (!session.uid) {
  264. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  265. }
  266. let str1 = "cshander 胡牌.......uid: " + session.uid;
  267. logger.warn(str1);////cssj
  268. return this.app.controllers.csjiang.succCardAsync(session.uid).nodeify(next);
  269. });
  270. // 放弃
  271. proto.giveUp = P.coroutine(function* (msg, session, next) {
  272. if (!session.uid) {
  273. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  274. }
  275. let str1 = "cshander 放弃.......uid: " + session.uid;
  276. logger.warn(str1);////cssj
  277. return this.app.controllers.csjiang.giveUpAsync(session.uid).nodeify(next);
  278. });
  279. /////TL++,海底捞月
  280. proto.haidilaoyue = P.coroutine(function* (msg, session, next) {
  281. // console.warn("海底捞月0");
  282. if (!session.uid) {
  283. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  284. }
  285. let str1 = "cshander 海底捞月.......uid: " + session.uid;
  286. logger.warn(str1);////cssj
  287. // console.warn("海底捞月1");
  288. return this.app.controllers.csjiang.haiDiLaoYue(session.uid,msg).nodeify(next);
  289. });
  290. /////TL++,操作是否自动出牌
  291. proto.autoOutCard = P.coroutine(function* (msg, session, next) {
  292. if (!session.uid) {
  293. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  294. }
  295. return next(null, { code: C.FAILD, msg: "敬请期待" });
  296. //return this.app.controllers.csjiang.autoOutCard(session.uid).nodeify(next);
  297. });
  298. // 聊天
  299. proto.chat = P.coroutine(function* (msg, session, next) {
  300. if (!session.uid) {
  301. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  302. }
  303. return this.app.controllers.csjiang.sendMsgAsync(session.uid, msg).nodeify(next);
  304. });
  305. // 收费聊天
  306. proto.vchat = P.coroutine(function* (msg, session, next) {
  307. if (!session.uid) {
  308. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  309. }
  310. var chairId = parseInt(msg.chairId);
  311. if (!(chairId >= 0)) {
  312. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  313. }
  314. if (!msg.msg) {
  315. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  316. }
  317. return this.app.controllers.csjiang.vsendMsgAsync(session.uid, chairId, msg.msg).nodeify(next);
  318. });
  319. // 角色信息
  320. proto.roleInfo = P.coroutine(function* (msg, session, next) {
  321. if (!session.uid) {
  322. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  323. }
  324. var chairId = parseInt(msg.chairId);
  325. if (!(chairId >= 0)) {
  326. return next(null, { code: C.FAILD, msg: C.GAME_PARAM_ERROR });
  327. }
  328. return this.app.controllers.csjiang.roleInfoAsync(session.uid, chairId).nodeify(next);
  329. });
  330. // 请求结束
  331. proto.overTable = P.coroutine(function* (msg, session, next) {
  332. if (!session.uid) {
  333. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  334. }
  335. return this.app.controllers.csjiang.overTableAsync(session.uid).nodeify(next);
  336. });
  337. // 拒绝结束
  338. proto.refuseOver = P.coroutine(function* (msg, session, next) {
  339. if (!session.uid) {
  340. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  341. }
  342. return this.app.controllers.csjiang.refuseOverAsync(session.uid).nodeify(next);
  343. });
  344. // 同意结束
  345. proto.agreeOver = P.coroutine(function* (msg, session, next) {
  346. if (!session.uid) {
  347. return next(null, { code: C.ERROR, msg: C.PLAYER_NOT_LOGIN });
  348. }
  349. return this.app.controllers.csjiang.agreeOverAsync(session.uid).nodeify(next);
  350. });