From 775280e03f334423dab2d5ca2991f9b52a8232f3 Mon Sep 17 00:00:00 2001 From: by <123456@qq.com> Date: Wed, 14 Aug 2024 11:33:03 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/action/action_server.go | 132 ++++++++++++-------- machine/machinedoll/command.go | 195 +++++++++++++++++++++--------- machine/machinedoll/machinemgr.go | 166 ++++++++++++++----------- 3 files changed, 320 insertions(+), 173 deletions(-) diff --git a/machine/action/action_server.go b/machine/action/action_server.go index 36963fe..b67ff98 100644 --- a/machine/action/action_server.go +++ b/machine/action/action_server.go @@ -2,99 +2,131 @@ package action import ( "fmt" - "mongo.games.com/game/machine/machinedoll" - "mongo.games.com/game/protocol/machine" + "net" + "time" + + "mongo.games.com/goserver/core/basic" "mongo.games.com/goserver/core/logger" "mongo.games.com/goserver/core/netlib" - "time" + "mongo.games.com/goserver/core/task" + "mongo.games.com/goserver/core/timer" + + "mongo.games.com/game/machine/machinedoll" + "mongo.games.com/game/protocol/machine" ) +type DoneFunc func(c net.Conn) + +func Process(conn *machinedoll.Conn, sec time.Duration, f1, f2 []DoneFunc, isSync bool) { + var ch chan struct{} + if isSync { + ch = make(chan struct{}, 1) + } + task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { + for _, v := range f1 { + v(conn) + } + if len(f2) > 0 { + timer.AfterTimer(func(h timer.TimerHandle, ud interface{}) bool { + Process(conn, 0, f2, nil, isSync) + if isSync { + ch <- struct{}{} + } + return true + }, nil, sec) + } else { + if isSync { + ch <- struct{}{} + } + } + return nil + }), nil).StartByFixExecutor(fmt.Sprintf("Machine%v", conn.Addr)) + if isSync { + <-ch + } +} + // 移动 func SMDollMachinePerateHandler(session *netlib.Session, packetId int, data interface{}) error { + logger.Logger.Tracef("SMDollMachinePerateHandler %v", data) msg, ok := data.(*machine.SMDollMachineoPerate) if !ok { return nil } - conn := machinedoll.ConnMap[int(msg.Id)] - if conn == nil { + + conn, ok := machinedoll.MachineMgr.ConnMap[int(msg.GetId())] + if !ok || conn == nil { return nil } - if msg.Perate == 1 { + + switch msg.Perate { + case 1: //向前移动 - machinedoll.Backward(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.BackwardStop(conn) - } else if msg.Perate == 2 { + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Backward}, []DoneFunc{machinedoll.BackwardStop}, false) + case 2: //向后移动 - machinedoll.Forward(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.ForwardStop(conn) - } else if msg.Perate == 3 { + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Forward}, []DoneFunc{machinedoll.ForwardStop}, false) + case 3: //向左移动 - machinedoll.Left(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.LeftStop(conn) - } else if msg.Perate == 4 { + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Left}, []DoneFunc{machinedoll.LeftStop}, false) + case 4: //向右移动 - machinedoll.Right(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.RightStop(conn) - } else if msg.Perate == 5 { + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Right}, []DoneFunc{machinedoll.RightStop}, false) + case 5: //投币 - machinedoll.Coin(conn) - machinedoll.Backward(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.BackwardStop(conn) + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Backward}, []DoneFunc{machinedoll.BackwardStop}, false) } return nil } // 下抓 func SMDollMachineGrabHandler(session *netlib.Session, packetId int, data interface{}) error { + logger.Logger.Tracef("SMDollMachineGrabHandler %v", data) msg, ok := data.(*machine.SMDollMachineGrab) if !ok { return nil } - conn := machinedoll.ConnMap[int(msg.Id)] - if conn == nil { + + conn, ok := machinedoll.MachineMgr.ConnMap[int(msg.GetId())] + if !ok || conn == nil { return nil } - typeId := msg.TypeId - if typeId == 1 { - //弱抓 - machinedoll.WeakGrab(conn) - } else if typeId == 2 { - //强力抓 - machinedoll.Grab(conn) - } else if typeId == 3 { - //必中抓 - machinedoll.SetPower(conn) - time.Sleep(200 * time.Millisecond) - machinedoll.Grab(conn) + + send := func(net.Conn) { + session.Send(int(machine.DollMachinePacketID_PACKET_SMDollMachineGrab), &machine.MSDollMachineGrab{ + Snid: msg.Snid, + Id: msg.GetId(), + Result: 1, + }) + } + + switch msg.GetTypeId() { + case 1: + //弱抓 + Process(conn, 0, []DoneFunc{machinedoll.WeakGrab}, []DoneFunc{send}, false) + case 2: + //强力抓 + Process(conn, 0, []DoneFunc{machinedoll.Grab}, []DoneFunc{send}, false) + case 3: + //必中抓 + Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.SetPower}, []DoneFunc{machinedoll.Grab, send}, false) } - //返回消息 - session.Send(int(machine.DollMachinePacketID_PACKET_SMDollMachineGrab), &machine.MSDollMachineGrab{ - Snid: msg.Snid, - Id: msg.GetId(), - Result: 1, - }) return nil } // 与游戏服务器连接成功,向游戏服务器推送所有娃娃机连接 func SMGameLinkSucceedHandler(session *netlib.Session, packetId int, data interface{}) error { - logger.Logger.Trace("与游戏服务器连接成功!!\n") - fmt.Printf("与游戏服务器连接成功!!\n") + logger.Logger.Trace("与游戏服务器连接成功") //开始向游戏服务器发送娃娃机连接信息 msg := &machine.MSDollMachineList{} - for i, _ := range machinedoll.ConnMap { + for i, _ := range machinedoll.MachineMgr.ConnMap { info := &machine.DollMachine{} info.Id = int32(i) info.VideoAddr = "www.baidu.com" msg.Data = append(msg.Data, info) } session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineList), msg) - fmt.Printf("开始向游戏服务器发送娃娃机连接信息!\n", msg) + logger.Logger.Tracef("向游戏服务器发送娃娃机连接信息:%v", msg) return nil } func init() { diff --git a/machine/machinedoll/command.go b/machine/machinedoll/command.go index ea8b82b..9e56bb7 100644 --- a/machine/machinedoll/command.go +++ b/machine/machinedoll/command.go @@ -110,6 +110,32 @@ func Grab(conn net.Conn) { fmt.Println("Failed to read response from server:", err) return } + instruction = []byte{0xAA, 0x04, 0x01, 0x50, 0x09, 0x5c, 0xdd} + _, err = conn.Write(instruction) + if err != nil { + fmt.Println("Failed to read response from server:", err) + return + } +} + +// 必中抓 +func Grab2(conn net.Conn) { + //设置电压 + + instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01} + instruction = calculateChecksum(instruction) + _, err := conn.Write(instruction) + if err != nil { + fmt.Println("Failed to send command to server:", err) + return + } + // 读取服务端的响应 + buf := make([]byte, 1024) + _, err = conn.Read(buf) + if err != nil { + fmt.Println("Failed to read response from server:", err) + return + } } // 弱抓aa 05 01 50 06 00 52 dd @@ -187,6 +213,7 @@ func OpenMusic(conn net.Conn) { instruction := []byte{0xAA, 0x33, 0x01, 0x06} instruction = append(instruction, data...) instruction = calculateChecksum(instruction) + //instruction[1] = byte(len(instruction) - 3) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -303,15 +330,11 @@ func queryBaseParam(conn net.Conn) { fmt.Println("n", n) } -// 设置强力 +// 设置出奖模式 func SetPower(conn net.Conn) { - data[3] = 0x00 - data[16] = 0x01 - data[17] = 0xE0 - data[18] = 0x13 - data[19] = 0x88 + data[3] = 0x01 fmt.Println("data.len = ", len(data)) - instruction := []byte{0xAA, 0x33, 0x01, 0x06} + instruction := []byte{0xAA, 0x04, 0x01, 0x06} instruction = append(instruction, data...) instruction = calculateChecksum(instruction) _, err := conn.Write(instruction) @@ -330,52 +353,114 @@ func SetPower(conn net.Conn) { } var data = []byte{ - 0x65, - 0x00, - 0x0F, - 0x02, - 0x0F, - 0x00, - 0x01, - 0x00, - 0x00, - 0x01, - 0xC8, - 0x00, - 0x7C, - 0x01, - 0x5A, - 0x00, - 0xE0, - 0x01, - 0xC8, - 0x00, - 0x14, - 0x32, - 0x32, - 0x50, - 0x34, - 0x08, - 0x00, - 0x00, - 0x00, - 0x00, - 0x78, - 0x00, - 0x32, - 0x02, - 0x00, - 0x00, - 0xC8, - 0x00, - 0x96, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x0F, - 0x07, - 0x08, - 0x00, + 0x65, //0 几币几玩 + 0x00, //1 几币几玩占用位 + 0x1E, //2 游戏时间 + 0x00, //3 出奖模式 + 0x0F, //4 出奖概率 + 0x00, //5 出奖概率占用位 + 0x00, //6 空中抓物 0关闭 1开启 + 0x00, //7 连续投币赠送 范围0~100 + 0x00, //8 保夹次数 范围0~6, 6为无限次 + 0x01, //9 保夹赠送模式 0送游戏 1送中奖 2送游戏和中奖 + + 0x04, //10 强抓力电压 + 0x50, //11 强抓力电压占用位 + 0x7C, //12 中抓力电压 + 0x00, //13 中抓力电压占用位 + 0x5A, //14 弱抓力电压 + 0x00, //15 弱抓力电压占用位 + 0xE0, //16 中奖电压 + 0x01, //17 中奖电压占用位 + 0xC8, //18 强抓力时间 + 0x00, //19 强抓力时间占用位 + + 0x14, //20 放抓时间 + 0x32, //21 前后速度 + 0x32, //22 左右速度 + 0x50, //23 上下速度 + 0x34, //24 放线长度 + 0x08, //25 放线长度占用位 + 0x00, //26 礼品下放高度 + 0x00, //27 礼品下放高度占用位 + 0x00, //28 甩抓长度 + 0x00, //29 甩抓保护 + + 0x78, //30 甩抓电压 + 0x00, //31 甩抓电压占用位 + 0x32, //32 上拉保护 + 0x02, //33 天车自救时间 + 0x00, //34 下抓延时 + 0x00, //35 下抓延时占用位 + 0xC8, //36 抓物延时 + 0x00, //37 抓物延时占用位 + 0x96, //38 上停延时 + 0x00, //39 上停延时占用位 + + 0x00, //40 摇杆延时 + 0x00, //41 摇杆延时占用位 + 0x00, //42 抓物二收 + 0x00, //43 待机音乐开关 + 0x0F, //44 音量大小调整 + 0x07, //45 待机音乐选择 + 0x08, //46 游戏音乐选择 + 0x00, //47 概率队列自动 } + +/* +var data = []byte{ + 101, //0 几币几玩 + 0, //1 几币几玩占用位 + 30, //2 游戏时间 + 0, //3 出奖模式0无概率 1随机模式 2固定模式 3冠兴模式 + 15, //4 出奖概率 + 0, //5 出奖概率占用位 + 1, //6 空中抓物 0关闭 1开启 + 0, //7 连续投币赠送 范围0~100 + 0, //8 保夹次数 范围0~6, 6为无限次 + 1, //9 保夹赠送模式 0送游戏 1送中奖 2送游戏和中奖 + + 200, //10 强抓力电压 + 0, //11 强抓力电压占用位 + 124, //12 中抓力电压 + 1, //13 中抓力电压占用位 + 90, //14 弱抓力电压 + 0, //15 弱抓力电压占用位 + 224, //16 中奖电压 + 1, //17 中奖电压占用位 + 200, //18 强抓力时间 + 0, //19 强抓力时间占用位 + + 20, //20 放抓时间 + 50, //21 前后速度 + 50, //22 左右速度 + 80, //23 上下速度 + 52, //24 放线长度 + 8, //25 放线长度占用位 + 0, //26 礼品下放高度 + 0, //27 礼品下放高度占用位 + 0, //28 甩抓长度 + 0, //29 甩抓保护 + + 120, //30 甩抓电压 + 0, //31 甩抓电压占用位 + 50, //32 上拉保护 + 2, //33 天车自救时间 + 0, //34 下抓延时 + 0, //35 下抓延时占用位 + 200, //36 抓物延时 + 0, //37 抓物延时占用位 + 150, //38 上停延时 + 0, //39 上停延时占用位 + + 0, //40 摇杆延时 + 0, //41 摇杆延时占用位 + 0, //42 抓物二收 + 1, //43 待机音乐开关 + 15, //44 音量大小调整 + 7, //45 待机音乐选择 + 8, //46 游戏音乐选择 + 0, //47 概率队列自动 +} + +*/ diff --git a/machine/machinedoll/machinemgr.go b/machine/machinedoll/machinemgr.go index 196850e..db51ca6 100644 --- a/machine/machinedoll/machinemgr.go +++ b/machine/machinedoll/machinemgr.go @@ -3,38 +3,45 @@ package machinedoll import ( "encoding/json" "fmt" - "mongo.games.com/goserver/core/timer" - "os/signal" - "syscall" - - "mongo.games.com/game/protocol/machine" - "mongo.games.com/goserver/core/logger" - "mongo.games.com/goserver/core/netlib" - "mongo.games.com/goserver/srvlib" "net" "os" + "os/signal" "path/filepath" + "syscall" "time" + + "mongo.games.com/goserver/core/basic" + "mongo.games.com/goserver/core/logger" + "mongo.games.com/goserver/core/module" + "mongo.games.com/goserver/core/netlib" + "mongo.games.com/goserver/core/task" + "mongo.games.com/goserver/srvlib" + + "mongo.games.com/game/protocol/machine" ) var GameConn *netlib.Session -var ConnMap = make(map[int]net.Conn) - -type MachineManager struct { - DelConnMap map[int]string -} var MachineMgr = &MachineManager{ + ConnMap: map[int]*Conn{}, DelConnMap: make(map[int]string), } +type Conn struct { + Id int + net.Conn + Addr string +} + +type MachineManager struct { + ConnMap map[int]*Conn + DelConnMap map[int]string +} + func (this *MachineManager) ModuleName() string { return "MachineManager" } -// 心跳间隔时间(秒) -const heartbeatInterval = 1 - func (this *MachineManager) Init() { var serverAddrs []string programDir, err := os.Getwd() @@ -62,70 +69,91 @@ func (this *MachineManager) Init() { fmt.Println("Failed to connect to server:", err) continue } - ConnMap[i+1] = conn - go this.StartHeartbeat(i+1, &conn, addr) + this.ConnMap[i+1] = &Conn{ + Id: i + 1, + Conn: conn, + Addr: addr, + } + } - fmt.Println("Connected to server:\n", ConnMap[1].RemoteAddr()) + fmt.Println("Connected to server:\n", this.ConnMap[1].RemoteAddr()) fmt.Println("投币请按Q!!!!") fmt.Println("w向前s向后a向左d向右 j强力抓取k弱力抓取!") // 监听 WASD 按键事件 - /* go listenKeyboardEvents(ConnMap[1]) + go listenKeyboardEvents(this.ConnMap[1]) + + // 监听中断信号,等待用户退出 + waitForUserExit() - // 监听中断信号,等待用户退出 - waitForUserExit()*/ } -func (this *MachineManager) StartHeartbeat(id int, conn *net.Conn, addr string) { - // 定期发送心跳包 - ticker := time.NewTicker(heartbeatInterval * time.Second) - defer ticker.Stop() - for { - select { - case <-ticker.C: - // 发送心跳包 - _, err := (*conn).Write([]byte("heartbeat")) +func (this *MachineManager) Update() { + var delConn []*Conn + task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} { + for _, v := range this.ConnMap { + _, err := v.Write([]byte("heartbeat")) if err != nil { - fmt.Println("Failed to send heartbeat:", err) - delete(ConnMap, id) - this.DelConnMap[id] = addr - //通知游戏服 - this.UpdateToGameServer(id, 0) - fmt.Println("删除链接!!!!!!addr = ", addr) - go timer.StartTimer(timer.TimerActionWrapper(func(h timer.TimerHandle, ud interface{}) bool { - this.ReConnect() - return true - }), nil, time.Duration(5)*time.Second, 100) - return + delConn = append(delConn, v) + v.Close() + logger.Logger.Tracef("断开连接:%v", v.Addr) } } - } -} - -// 重连 -func (this *MachineManager) ReConnect() bool { - fmt.Println("================重连============") - delIds := []int{} - for id, addr := range this.DelConnMap { - conn, err := net.DialTimeout("tcp", addr, 5*time.Second) - if err != nil { - continue + return nil + }), task.CompleteNotifyWrapper(func(i interface{}, t task.Task) { + for _, v := range delConn { + delete(this.ConnMap, v.Id) + this.DelConnMap[v.Id] = v.Addr } - ConnMap[id] = conn - delIds = append(delIds, id) - this.UpdateToGameServer(id, 1) - } - for _, id := range delIds { - delete(this.DelConnMap, id) - fmt.Println("重新链接成功!!!!!!id = ", id) - } - return false + 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) + if err != nil { + continue + } + logger.Logger.Tracef("重连成功:%v", addr) + delIds = append(delIds, &Conn{ + Id: id, + 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() + } + })).StartByFixExecutor(this.ModuleName()) + })).StartByFixExecutor(this.ModuleName()) } -func (this *MachineManager) UpdateToGameServer(id int, status int32) { - msg := &machine.MSUpdateDollMachineStatus{} - msg.Status = status - msg.Id = int32(id) - SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSUpdateDollMachineStatus), msg) +func (this *MachineManager) Shutdown() { + for _, v := range this.ConnMap { + v.Close() + } + 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 SendToGameServer(pid int, msg interface{}) { @@ -140,7 +168,7 @@ func SendToGameServer(pid int, msg interface{}) { } func init() { - MachineMgr.Init() + module.RegisteModule(MachineMgr, time.Second, 0) } func listenKeyboardEvents(conn net.Conn) { @@ -218,6 +246,8 @@ func listenKeyboardEvents(conn net.Conn) { SetPower(conn) case "8": CloseMusic(conn) + case "9": + queryBaseParam(conn) } } } From 6ea5fb3943f058d6ea62e5fd7a02700bf2179efe Mon Sep 17 00:00:00 2001 From: by <123456@qq.com> Date: Wed, 14 Aug 2024 11:37:03 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA=E6=8A=95?= =?UTF-8?q?=E5=B8=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/action/action_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machine/action/action_server.go b/machine/action/action_server.go index b67ff98..7be9270 100644 --- a/machine/action/action_server.go +++ b/machine/action/action_server.go @@ -74,7 +74,7 @@ func SMDollMachinePerateHandler(session *netlib.Session, packetId int, data inte Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Right}, []DoneFunc{machinedoll.RightStop}, false) case 5: //投币 - Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Backward}, []DoneFunc{machinedoll.BackwardStop}, false) + Process(conn, 0*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Coin}, []DoneFunc{}, false) } return nil } 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 3/4] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA=E4=BF=AE?= =?UTF-8?q?=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 From b9191e05380e3c838033cbeb558de57761e1015a Mon Sep 17 00:00:00 2001 From: by <123456@qq.com> Date: Wed, 14 Aug 2024 13:43:42 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/machinedoll/machinemgr.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/machine/machinedoll/machinemgr.go b/machine/machinedoll/machinemgr.go index cc40046..3c6827d 100644 --- a/machine/machinedoll/machinemgr.go +++ b/machine/machinedoll/machinemgr.go @@ -96,7 +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) + this.UpdateToGameServer(v, 0) } } return nil @@ -126,7 +126,7 @@ func (this *MachineManager) Update() { for _, v := range delIds { this.ConnMap[v.Id] = v delete(this.DelConnMap, v.Id) - this.UpdateToGameServer(int32(v.Id), 1, v.Addr) + this.UpdateToGameServer(v, 1) } })).StartByFixExecutor(this.ModuleName()) })).StartByFixExecutor(this.ModuleName()) @@ -135,17 +135,17 @@ 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(v, 0) } module.UnregisteModule(this) } -func (this *MachineManager) UpdateToGameServer(id int32, status int32, VideoAddr string) { +func (this *MachineManager) UpdateToGameServer(conn *Conn, status int32) { msg := &machine.MSUpdateDollMachineStatus{} - msg.Id = id + msg.Id = int32(conn.Id) msg.Status = status - msg.VideoAddr = VideoAddr + msg.VideoAddr = conn.Addr SendToGameServer(int(machine.DollMachinePacketID_PACKET_MSUpdateDollMachineStatus), msg) }