-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobots.go
283 lines (265 loc) · 6.25 KB
/
robots.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
/**********************************************************
* Author : piaohua
* Email : 814004090@qq.com
* Last modified : 2017-11-19 11:32:23
* Filename : robots.go
* Description : 机器人
* *******************************************************/
package main
import (
"time"
"gohappy/glog"
"gohappy/pb"
"utils"
)
//' 接口
//Msg2Robots 消息通知
func Msg2Robots(msg interface{}, num uint32) {
for num > 0 {
rbs.Send2rbs(msg)
num--
}
}
//RegistRoom 房间列表
func RegistRoom(roomid string, ltype int32) {
msg := &pb.RobotRoomList{
Roomid: roomid,
Ltype: ltype,
}
glog.Debugf("regist room %s ltype %d", roomid, ltype)
Msg2Robots(msg, 1)
}
//EnterRoom 进入房间
func EnterRoom(phone, roomid string, ltype int32) {
msg := &pb.RobotEnterRoom{
Phone: phone,
Roomid: roomid,
Ltype: ltype,
}
Msg2Robots(msg, 1)
}
//Logined 登录成功
func Logined(phone string, ltype int32) {
msg := &pb.RobotLogin{
Phone: phone,
Ltype: ltype,
}
Msg2Robots(msg, 1)
}
//Logout 登出成功
func Logout(roomid, phone, code string, chip int64) {
msg := &pb.RobotLogout{
Roomid: roomid,
Phone: phone,
Code: code,
Chip: chip,
}
Msg2Robots(msg, 1)
}
//ReLogined 已经注册,重新登录
func ReLogined(roomid, phone, code string, rtype, envBet int32) {
msg := &pb.RobotReLogin{
Roomid: roomid,
Phone: phone,
Code: code,
Rtype: rtype,
EnvBet: envBet,
}
Msg2Robots(msg, 1)
}
//Send2rbs 发送消息
func (r *RobotServer) Send2rbs(msg interface{}) {
select {
case <-r.stopCh:
return
default:
r.msgCh <- msg
}
}
//.
//' 机器人测试
func (r *RobotServer) runTest() {
glog.Infof("runTest started phone -> %s", r.phone)
tick := time.Tick(20 * time.Second)
msg4 := &pb.RobotMsg{
Num: 1,
}
for {
select {
case <-tick:
glog.Infof("r.online -> %d", len(r.online))
glog.Infof("r.offline -> %d", len(r.offline))
glog.Infof("r.phone -> %s", r.phone)
//TODO:优化,按时间段运行
//运行指定数量机器人(每个创建一个牌局)
//code = "create" 表示机器人创建房间
//go Msg2Robots(msg1, 5)
if len(r.online) < 3 {
go Msg2Robots(msg4, 1)
}
case <-r.stopCh:
return
}
}
}
//.
//' 消息处理服务
func (r *RobotServer) run() {
defer func() {
glog.Infof("Robots closed online -> %d", len(r.online))
glog.Infof("Robots closed offline -> %d", len(r.offline))
glog.Infof("Robots closed phone -> %s", r.phone)
}()
glog.Infof("Robots started -> %s", r.phone)
tick := time.Tick(time.Minute)
for {
select {
case m, ok := <-r.msgCh:
if !ok {
glog.Errorf("Robots msgCh closed phone -> %s", r.phone)
return
}
switch m.(type) {
case *pb.RobotMsg:
//启动机器人
msg := m.(*pb.RobotMsg)
glog.Infof("run msg -> %#v", msg)
r.run2(msg)
case *pb.RobotReLogin:
//重新尝试登录
msg := m.(*pb.RobotReLogin)
glog.Infof("ReLogin -> %#v", msg)
go r.runRobot(msg.Roomid, msg.Phone, msg.Code, msg.Rtype, msg.Ltype, msg.Gtype, msg.EnvBet, false)
case *pb.RobotLogin:
//登录成功
msg := m.(*pb.RobotLogin)
glog.Infof("login -> %#v", msg)
delete(r.offline, msg.Phone)
r.online[msg.Phone] = true
case *pb.RobotLogout:
//登出断开
msg := m.(*pb.RobotLogout)
glog.Infof("logout -> %#v", msg)
if _, ok := r.online[msg.Phone]; ok {
delete(r.online, msg.Phone)
}
if msg.Chip < 20000 {
//TODO 自动充值
//r.unused[msg.Phone] = msg.Chip
} else {
//TODO 暂时不重复
//r.offline[msg.Phone] = true
}
if v, ok := r.rooms[msg.Roomid]; ok && v > 0 {
r.rooms[msg.Roomid]--
}
case *pb.RobotStop:
msg := m.(*pb.RobotStop)
glog.Infof("robot stop %#v", msg)
//r.mutexConns.Lock()
//for conn := range r.conns {
// conn.Close()
//}
//r.conns = nil
//r.mutexConns.Unlock()
case *pb.RobotRoomList:
msg := m.(*pb.RobotRoomList)
if v, ok := r.ltypes[msg.Ltype]; ok {
var have bool
for _, val := range v {
if val == msg.Roomid {
have = true
break
}
}
if !have {
v = append(v, msg.Roomid)
r.ltypes[msg.Ltype] = v
}
} else {
v := make([]string, 0)
v = append(v, msg.Roomid)
r.ltypes[msg.Ltype] = v
}
glog.Debugf("room list %s ltypes %#v", msg.Roomid, r.ltypes)
case *pb.RobotEnterRoom:
msg := m.(*pb.RobotEnterRoom)
glog.Debugf("RobotEnterRoom -> %#v", msg)
r.rooms[msg.Roomid]++
glog.Debugf("rooms -> %#v", r.rooms)
case closeFlag:
//停止发送消息
close(r.stopCh)
return
}
case <-tick:
//逻辑处理
}
}
}
//.
//' 启动机器人
func (r *RobotServer) run2(msg *pb.RobotMsg) {
var code string = msg.Code
var rtype int32 = msg.Rtype
var ltype int32 = msg.Ltype
var envBet int32 = msg.EnvBet
var gtype int32 = msg.Gtype
var phone string
//选择一个房间
glog.Debugf("ltypes %#v", r.ltypes)
var roomid string
if len(msg.Roomid) != 0 {
roomid = msg.Roomid
} else {
if s, ok := r.ltypes[ltype]; ok {
for _, v := range s {
//每个房间5个人
if r.rooms[v] < 8 {
roomid = v
break
}
}
}
}
//房间已经存在列表
if roomid == "" && len(r.ltypes[ltype]) != 0 {
return
}
glog.Debugf("roomid %s, ltype %d", roomid, ltype)
for k, v := range r.offline {
if v { //已经断开
phone = k
r.offline[k] = false //登录中
glog.Infof("run offline robot -> %s, %s", roomid, phone)
go r.runRobot(roomid, phone, code, rtype, ltype, gtype, envBet,false)
break
}
}
glog.Infof("offline phone -> %s", phone)
if len(phone) == 0 {
phone = r.phone
r.phone = utils.StringAdd(r.phone)
if _, ok := r.unused[phone]; ok {
//TODO 自动充值
//Msg2Robots(msg, 1)
//TODO 会出现死循环
} else if _, ok := r.online[phone]; ok {
//重复, TODO 会出现死循环
//Msg2Robots(msg, 1)
} else {
//新机器人不用注册
glog.Infof("run new robot -> %s, %s", roomid, phone)
go r.runRobot(roomid, phone, code, rtype, ltype, gtype, envBet,false)
}
}
glog.Infof("new phone -> %s", phone)
//重置
phone1 := cfg.Section("robot").Key("phone").Value()
//if r.phone > utils.StringAdd2(phone1, "750") {
if r.phone > utils.StringAdd2(phone1, "286") {
r.phone = phone1
}
}
//.
// vim: set foldmethod=marker foldmarker=//',//.: