RobotMj.lua
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
local RobotMj = class("RobotMj")
RobotMj.enabled = false;
--单机打牌机器人
function RobotMj:ctor(uid)
self.uid = uid;
self.pokers = {};
self.whoShotlistener = EventListener.addEventListener(nil,EnumEvent.GAME_WHO_SHOT,handler(self,self.onWhoShot));
self.pghListener = EventListener.addEventListener(nil,EnumEvent.MJ_HGPG,handler(self,self.onMjHGPG));
end
function RobotMj:setPokers(pokers)
self.pokers = pokers;
end
function RobotMj:addCards(pokers)
end
function RobotMj:removeCards(pokers)
for i,v in ipairs(pokers) do
for j=#self.pokers,1,-1 do
if v.key == self.pokers[j].key then
table.remove(self.pokers,j);
break;
end
end
end
end
--该谁出牌
function RobotMj:onWhoShot(params)
if self.enabled then
local uid = params.data.uid;
if uid == self.uid and not self.isPgh then
local key = params.data.key;
if uid == UserModel.user_info.uid then
self.shotDelayID = delayCall(2,function ()
self:startUserShot(uid,key);
end);
else
self.shotDelayID = delayCall(2,function ()
self:startAIShot(uid,key);
end);
end
end
end
end
--帮用户出牌
function RobotMj:startUserShot(uid,key)
if key > 0 then
local has_ding = MahjongTool.hasDing(self.pokers);
local user_ding = MahjongRoomModel.getUserDing(uid);
local faInfo = MahjongTool.getInfoByKey(key);
if (not has_ding or faInfo.suit == user_ding) then
MahjongRoomModel.da(MahjongTool.getInfoByKey(key));
else
MahjongTool:sortMjList(self.pokers);
MahjongRoomModel.da(self.pokers[#self.pokers]);
end
else
MahjongTool:sortMjList(self.pokers);
MahjongRoomModel.da(self.pokers[#self.pokers]);
end
end
--AI出牌
function RobotMj:startAIShot(key)
local faInfo = MahjongTool.getInfoByKey(key);
local user_ding = MahjongRoomModel.getUserDing(uid);
local has_ding = MahjongTool.hasDing(self.pokers);
if key > 0 and (not has_ding or faInfo.suit == user_ding)then
MahjongRoomModel.da(MahjongTool.getInfoByKey(key));
else
MahjongTool:sortMjList(self.pokers);
MahjongRoomModel.da(self.pokers[#self.pokers]);
end
end
--有碰杠胡
function RobotMj:onMjHGPG(params)
if self.enabled then
local hgp = params.data.hgp;
local mjInfo = params.data.mj;
self.isPgh = true;
local function pghFun()
-- if hgp[1] == "gang" then
-- SocketClient:getInstance():send("room_msg.xzdd_gang_msg",{key=mjInfo.key});
-- elseif hgp[1] == "peng" then
-- SocketClient:getInstance():send("room_msg.xzdd_peng_msg",{});
-- elseif hgp[1] == "hu" then
-- SocketClient:getInstance():send("room_msg.xzdd_hu_msg",{});
-- elseif hgp[1] == "guo" then
-- SocketClient:getInstance():send("room_msg.xzdd_guo_msg",{});
-- end
if TableUtil.IsInTable(hgp,"hu") then
SocketClient:getInstance():send("room_msg.xzdd_hu_msg",{});
elseif TableUtil.IsInTable(hgp,"guo") then
SocketClient:getInstance():send("room_msg.xzdd_guo_msg",{});
end
self.isPgh = false;
end
if #hgp > 0 then
self.delayID = delayCall(1,pghFun);
end
end
end
function RobotMj:pass()
if UserModel.where == EnumWhere.ROOM_SINGLE then
SingleManager.pass(self.uid);
elseif UserModel.where == EnumWhere.ROOM_MULTI_GD or UserModel.where == EnumWhere.ROOM_PERSONAL_GD then
MahjongRoomModel.pass();
end
end
function RobotMj:start()
self.enabled = true;
end
function RobotMj:stop()
self.isPgh = false;
self.enabled = false;
if self.delayID then
removeDelay(self.delayID);
self.delayID = nil;
end
if self.shotDelayID then
removeDelay(self.shotDelayID);
self.shotDelayID = nil;
end
end
function RobotMj:destroy()
print("RobotMj:destroy:",self.delayID)
if self.delayID then
removeDelay(self.delayID);
self.delayID = nil;
end
if self.shotDelayID then
removeDelay(self.shotDelayID);
self.shotDelayID = nil;
end
EventListener.removeEventListener(nil,self.whoShotlistener);
end
return RobotMj