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] =?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) } } }