From d2a8b0e6189e7fe919064ac53d67fc660048aab0 Mon Sep 17 00:00:00 2001 From: by <123456@qq.com> Date: Wed, 14 Aug 2024 12:06:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gamesrv/action/action_machine.go | 14 +++++++- machine/machinedoll/machinemgr.go | 43 +++++++++++------------- protocol/machine/machine.pb.go | 54 ++++++++++++++++++------------- protocol/machine/machine.proto | 1 + 4 files changed, 64 insertions(+), 48 deletions(-) diff --git a/gamesrv/action/action_machine.go b/gamesrv/action/action_machine.go index 61d61a9..d6a5b9c 100644 --- a/gamesrv/action/action_machine.go +++ b/gamesrv/action/action_machine.go @@ -50,13 +50,25 @@ func GetFreeDollMachineId() int { // 获取指定娃娃机链接状态 func GetDollMachineStatus(id int) int32 { + if MachineMap[id] == nil { + return 0 + } return MachineMap[id].MachineStatus } func MSUpdateDollMachineStatusHandler(session *netlib.Session, packetId int, data interface{}) error { logger.Logger.Tracef("MSUpdateDollMachineStatusHandler %v", data) if msg, ok := data.(*machine.MSUpdateDollMachineStatus); ok { - MachineMap[int(msg.Id)].MachineStatus = msg.Status + if MachineMap[int(msg.Id)] == nil { + MachineMap[int(msg.Id)] = &DollMachine{ + Id: int(msg.Id), + Status: false, + VideoAddr: msg.VideoAddr, + MachineStatus: msg.Status, + } + } else { + MachineMap[int(msg.Id)].MachineStatus = msg.Status + } logger.Logger.Tracef("更新娃娃机连接状态 id = %d,status= %d", msg.Id, msg.GetStatus()) } return nil diff --git a/machine/machinedoll/machinemgr.go b/machine/machinedoll/machinemgr.go index db51ca6..cc40046 100644 --- a/machine/machinedoll/machinemgr.go +++ b/machine/machinedoll/machinemgr.go @@ -76,14 +76,14 @@ func (this *MachineManager) Init() { } } - fmt.Println("Connected to server:\n", this.ConnMap[1].RemoteAddr()) - fmt.Println("投币请按Q!!!!") - fmt.Println("w向前s向后a向左d向右 j强力抓取k弱力抓取!") - // 监听 WASD 按键事件 - go listenKeyboardEvents(this.ConnMap[1]) + /* fmt.Println("Connected to server:\n", this.ConnMap[1].RemoteAddr()) + fmt.Println("投币请按Q!!!!") + fmt.Println("w向前s向后a向左d向右 j强力抓取k弱力抓取!") + // 监听 WASD 按键事件 + go listenKeyboardEvents(this.ConnMap[1]) - // 监听中断信号,等待用户退出 - waitForUserExit() + // 监听中断信号,等待用户退出 + waitForUserExit()*/ } @@ -96,6 +96,7 @@ func (this *MachineManager) Update() { delConn = append(delConn, v) v.Close() logger.Logger.Tracef("断开连接:%v", v.Addr) + this.UpdateToGameServer(int32(v.Id), 0, v.Addr) } } return nil @@ -104,12 +105,9 @@ func (this *MachineManager) Update() { delete(this.ConnMap, v.Id) this.DelConnMap[v.Id] = v.Addr } - if len(delConn) > 0 { - this.UpdateToGameServer() - } + // 重连 var delIds []*Conn - status := false task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { for id, addr := range this.DelConnMap { conn, err := net.DialTimeout("tcp", addr, 5*time.Second) @@ -122,16 +120,13 @@ func (this *MachineManager) Update() { Conn: conn, Addr: addr, }) - status = true } return nil }), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) { for _, v := range delIds { this.ConnMap[v.Id] = v delete(this.DelConnMap, v.Id) - } - if status { - this.UpdateToGameServer() + this.UpdateToGameServer(int32(v.Id), 1, v.Addr) } })).StartByFixExecutor(this.ModuleName()) })).StartByFixExecutor(this.ModuleName()) @@ -140,20 +135,18 @@ func (this *MachineManager) Update() { func (this *MachineManager) Shutdown() { for _, v := range this.ConnMap { v.Close() + this.UpdateToGameServer(int32(v.Id), 0, v.Addr) } - this.UpdateToGameServer() + module.UnregisteModule(this) } -func (this *MachineManager) UpdateToGameServer() { - msg := &machine.MSDollMachineList{} - for i, _ := range this.ConnMap { - info := &machine.DollMachine{} - info.Id = int32(i) - info.VideoAddr = "www.baidu.com" - msg.Data = append(msg.Data, info) - } - SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSDollMachineList), msg) +func (this *MachineManager) UpdateToGameServer(id int32, status int32, VideoAddr string) { + msg := &machine.MSUpdateDollMachineStatus{} + msg.Id = id + msg.Status = status + msg.VideoAddr = VideoAddr + SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSUpdateDollMachineStatus), msg) } func SendToGameServer(pid int, msg interface{}) { diff --git a/protocol/machine/machine.pb.go b/protocol/machine/machine.pb.go index 257ca48..b7f2ec9 100644 --- a/protocol/machine/machine.pb.go +++ b/protocol/machine/machine.pb.go @@ -422,8 +422,9 @@ type MSUpdateDollMachineStatus struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` - Status int32 `protobuf:"varint,2,opt,name=Status,proto3" json:"Status,omitempty"` //1-空闲 0-无法使用 + Id int32 `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"` + Status int32 `protobuf:"varint,2,opt,name=Status,proto3" json:"Status,omitempty"` //1-空闲 0-无法使用 + VideoAddr string `protobuf:"bytes,3,opt,name=VideoAddr,proto3" json:"VideoAddr,omitempty"` } func (x *MSUpdateDollMachineStatus) Reset() { @@ -472,6 +473,13 @@ func (x *MSUpdateDollMachineStatus) GetStatus() int32 { return 0 } +func (x *MSUpdateDollMachineStatus) GetVideoAddr() string { + if x != nil { + return x.VideoAddr + } + return "" +} + var File_machine_proto protoreflect.FileDescriptor var file_machine_proto_rawDesc = []byte{ @@ -501,30 +509,32 @@ var file_machine_proto_rawDesc = []byte{ 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, 0x72, 0x22, - 0x43, 0x0a, 0x19, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, + 0x61, 0x0a, 0x19, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x53, 0x74, - 0x61, 0x74, 0x75, 0x73, 0x2a, 0xfd, 0x01, 0x0a, 0x13, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, - 0x68, 0x69, 0x6e, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x49, 0x44, 0x12, 0x1c, 0x0a, 0x18, - 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, - 0x68, 0x69, 0x6e, 0x65, 0x5a, 0x65, 0x72, 0x6f, 0x10, 0x00, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, - 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x47, 0x61, 0x6d, 0x65, 0x4c, 0x69, 0x6e, 0x6b, 0x53, - 0x75, 0x63, 0x63, 0x65, 0x65, 0x64, 0x10, 0xa0, 0x9c, 0x01, 0x12, 0x20, 0x0a, 0x1a, 0x50, 0x41, + 0x61, 0x74, 0x75, 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, 0x64, + 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x56, 0x69, 0x64, 0x65, 0x6f, 0x41, 0x64, + 0x64, 0x72, 0x2a, 0xfd, 0x01, 0x0a, 0x13, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, + 0x6e, 0x65, 0x50, 0x61, 0x63, 0x6b, 0x65, 0x74, 0x49, 0x44, 0x12, 0x1c, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, - 0x6e, 0x65, 0x50, 0x65, 0x72, 0x61, 0x74, 0x65, 0x10, 0xa1, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, - 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, - 0x68, 0x69, 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa2, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, - 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, - 0x68, 0x69, 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa3, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, - 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, - 0x68, 0x69, 0x6e, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x10, 0xa4, 0x9c, 0x01, 0x12, 0x26, 0x0a, 0x20, - 0x50, 0x41, 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, - 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, - 0x10, 0xa5, 0x9c, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x6d, 0x6f, 0x6e, 0x67, 0x6f, 0x2e, 0x67, 0x61, - 0x6d, 0x65, 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x61, 0x6d, 0x65, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6e, 0x65, 0x5a, 0x65, 0x72, 0x6f, 0x10, 0x00, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, 0x43, 0x4b, + 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x47, 0x61, 0x6d, 0x65, 0x4c, 0x69, 0x6e, 0x6b, 0x53, 0x75, 0x63, + 0x63, 0x65, 0x65, 0x64, 0x10, 0xa0, 0x9c, 0x01, 0x12, 0x20, 0x0a, 0x1a, 0x50, 0x41, 0x43, 0x4b, + 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, + 0x50, 0x65, 0x72, 0x61, 0x74, 0x65, 0x10, 0xa1, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, + 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x53, 0x4d, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, + 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa2, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, + 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, + 0x6e, 0x65, 0x47, 0x72, 0x61, 0x62, 0x10, 0xa3, 0x9c, 0x01, 0x12, 0x1e, 0x0a, 0x18, 0x50, 0x41, + 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x44, 0x6f, 0x6c, 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, + 0x6e, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x10, 0xa4, 0x9c, 0x01, 0x12, 0x26, 0x0a, 0x20, 0x50, 0x41, + 0x43, 0x4b, 0x45, 0x54, 0x5f, 0x4d, 0x53, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x6c, + 0x6c, 0x4d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x10, 0xa5, + 0x9c, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x6d, 0x6f, 0x6e, 0x67, 0x6f, 0x2e, 0x67, 0x61, 0x6d, 0x65, + 0x73, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x61, 0x6d, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x63, 0x6f, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( diff --git a/protocol/machine/machine.proto b/protocol/machine/machine.proto index 642b236..8d941c7 100644 --- a/protocol/machine/machine.proto +++ b/protocol/machine/machine.proto @@ -51,4 +51,5 @@ message DollMachine{ message MSUpdateDollMachineStatus{ int32 Id = 1; int32 Status = 2; //1-空闲 0-无法使用 + string VideoAddr = 3; } \ No newline at end of file