From 154a037e08d1a3d3377e608a2fb522017ce50fe6 Mon Sep 17 00:00:00 2001 From: by <123456@qq.com> Date: Thu, 15 Aug 2024 11:31:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A8=83=E5=A8=83=E6=9C=BA=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gamesrv/action/action_machine.go | 2 +- machine/action/action_server.go | 110 +++++++++++++++++++++++++------ machine/machinedoll/command.go | 71 ++++++++++---------- 3 files changed, 126 insertions(+), 57 deletions(-) diff --git a/gamesrv/action/action_machine.go b/gamesrv/action/action_machine.go index 4f2f1d5..c577f4e 100644 --- a/gamesrv/action/action_machine.go +++ b/gamesrv/action/action_machine.go @@ -13,7 +13,7 @@ var MachineMapLock = sync.Mutex{} type DollMachine struct { Id int MachineStatus int32 //娃娃机链接状态 0:离线 1:在线 - Status bool //是否空闲 + Status bool //标记是否被占用 VideoAddr string } diff --git a/machine/action/action_server.go b/machine/action/action_server.go index 117cea1..c67697e 100644 --- a/machine/action/action_server.go +++ b/machine/action/action_server.go @@ -1,6 +1,7 @@ package action import ( + "bytes" "fmt" "net" "time" @@ -74,27 +75,9 @@ 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, 0*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Coin}, []DoneFunc{}, false) - // 读取服务端的响应 - buf := make([]byte, 1024) - n, err := conn.Read(buf) - if err != nil { - fmt.Println("Failed to read response from server:", err) - return nil - } - if buf[4] == 1 { - fmt.Println("上分成功!!!!n = ", n) - } - if buf[4] == 0 { - fmt.Println("上分失败!!!") - } - //返回消息 - session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineoPerateResult), &machine.MSDollMachineoPerateResult{ - Snid: msg.Snid, - Id: msg.GetId(), - Result: int32(buf[4]), - TypeId: 1, - }) + //Process(conn, 0*time.Millisecond, []DoneFunc{machinedoll.Coin}, []DoneFunc{}, false) + machinedoll.Coin(conn) + go CoinResult(session, conn, msg.Snid, msg.GetId()) } return nil } @@ -129,9 +112,94 @@ func SMDollMachineGrabHandler(session *netlib.Session, packetId int, data interf //强力抓 Process(conn, 0, []DoneFunc{machinedoll.Grab}, []DoneFunc{send}, false) } + go DollMachineGrabResult(session, conn, msg.Snid, msg.GetId()) return nil } +// 监听抓取结果返回 +func DollMachineGrabResult(session *netlib.Session, conn *machinedoll.Conn, snid, id int32) { + for { + // 读取数据 + fmt.Println("监听抓取结果返回!") + buf := make([]byte, 1024) + conn.SetDeadline(time.Now().Add(10 * time.Second)) + n, err := conn.Read(buf) + if err != nil { + fmt.Println("Failed to read response from client:", err) + return + } + + // 将读取到的数据按照 221 进行分割 + parts := bytes.Split(buf[:n], []byte{221}) + fmt.Println("获取到的返回值:", parts) + instruction := []byte{0xAA, 0x05, 0x02, 0x50, 0x09, 0x00} + instruction1 := []byte{0xAA, 0x05, 0x02, 0x50, 0x09, 0x01} + // 遍历分割结果,打印出每个部分 + for i, part := range parts { + if len(part) > 0 { + part = part[:len(part)-1] // 去除最后一个字节,该字节为分隔符 + fmt.Println("比较返回结果 part = ", part) + if bytes.Contains(part, instruction) { + fmt.Printf("Part %d: %s\n", i+1, part) + //回应数据 + _, err = conn.Write([]byte{0xAA, 0x04, 0x01, 0x50, 0x09, 0x5c, 0xdd}) + if err != nil { + fmt.Println("Failed to read response from server:", err) + return + } + session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineoPerateResult), &machine.MSDollMachineoPerateResult{ + Snid: snid, + Id: id, + Result: 0, + TypeId: 2, + }) + fmt.Println("没有抓到礼品!!!!!!!!") + return + } + if bytes.Contains(part, instruction1) { + fmt.Printf("Part %d: %s\n", i+1, part) + //回应数据 + _, err = conn.Write([]byte{0xAA, 0x04, 0x01, 0x50, 0x09, 0x5c, 0xdd}) + if err != nil { + fmt.Println("Failed to read response from server:", err) + return + } + session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineoPerateResult), &machine.MSDollMachineoPerateResult{ + Snid: snid, + Id: id, + Result: 1, + TypeId: 2, + }) + fmt.Println("抓到礼品了!!!!!!!!") + return + } + } + } + } +} +func CoinResult(session *netlib.Session, conn *machinedoll.Conn, snid, id int32) { + // 读取服务端的响应 + buf := make([]byte, 1024) + n, err := conn.Read(buf) + if err != nil { + fmt.Println("Failed to read response from server:", err) + return + } + if buf[4] == 1 { + fmt.Println("上分成功!!!!n = ", n) + } + if buf[4] == 0 { + fmt.Println("上分失败!!!") + } + //返回消息 + session.Send(int(machine.DollMachinePacketID_PACKET_MSDollMachineoPerateResult), &machine.MSDollMachineoPerateResult{ + Snid: snid, + Id: id, + Result: int32(buf[4]), + TypeId: 1, + }) +} + // 与游戏服务器连接成功,向游戏服务器推送所有娃娃机连接 func SMGameLinkSucceedHandler(session *netlib.Session, packetId int, data interface{}) error { logger.Logger.Trace("与游戏服务器连接成功") diff --git a/machine/machinedoll/command.go b/machine/machinedoll/command.go index 9e56bb7..8932d5a 100644 --- a/machine/machinedoll/command.go +++ b/machine/machinedoll/command.go @@ -8,7 +8,7 @@ import ( // 向前aa 05 01 50 01 01 54 dd func Forward(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x01} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -19,7 +19,7 @@ func Forward(conn net.Conn) { // 向前停止aa 05 01 50 01 00 55 dd func ForwardStop(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x00} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -31,7 +31,7 @@ func ForwardStop(conn net.Conn) { // aa 05 01 50 02 01 57 dd func Backward(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x01} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -42,7 +42,7 @@ func Backward(conn net.Conn) { // 向后停止aa 05 01 50 02 00 56 dd func BackwardStop(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x00} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -53,7 +53,7 @@ func BackwardStop(conn net.Conn) { // 向左aa 05 01 50 03 01 56 dd func Left(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x01} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -64,7 +64,7 @@ func Left(conn net.Conn) { // 向左停止aa 05 01 50 03 00 57 dd func LeftStop(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x00} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -75,7 +75,7 @@ func LeftStop(conn net.Conn) { // 向右 func Right(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x01} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -86,7 +86,7 @@ func Right(conn net.Conn) { // 向右停止aa 05 01 50 04 00 50 dd func RightStop(conn net.Conn) { instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x00} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -97,25 +97,26 @@ func RightStop(conn net.Conn) { // 强抓下抓 func Grab(conn net.Conn) { instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01} - instruction = calculateChecksum(instruction) + 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 - } - 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 - } + /* + // 读取服务端的响应 + buf := make([]byte, 1024) + _, err = conn.Read(buf) + if err != nil { + 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 + }*/ } // 必中抓 @@ -123,7 +124,7 @@ func Grab2(conn net.Conn) { //设置电压 instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -141,7 +142,7 @@ func Grab2(conn net.Conn) { // 弱抓aa 05 01 50 06 00 52 dd func WeakGrab(conn net.Conn) { instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x00} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -152,7 +153,7 @@ func WeakGrab(conn net.Conn) { // 投币 func Coin(conn net.Conn) { moveCommand := []byte{0xaa, 0x08, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00} - moveCommand = calculateChecksum(moveCommand) + moveCommand = CalculateChecksum(moveCommand) // 发送指令到服务端 _, err := conn.Write(moveCommand) if err != nil { @@ -177,7 +178,7 @@ func Coin(conn net.Conn) { // 剩余局数清零 func ClearRemainingGames(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x32} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -194,7 +195,7 @@ func ClearRemainingGames(conn net.Conn) { } // 计算校验码 -func calculateChecksum(data []byte) []byte { +func CalculateChecksum(data []byte) []byte { var value = byte(0) for i, datum := range data { if i > 0 { @@ -212,7 +213,7 @@ func OpenMusic(conn net.Conn) { data[43] = 0x01 instruction := []byte{0xAA, 0x33, 0x01, 0x06} instruction = append(instruction, data...) - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) //instruction[1] = byte(len(instruction) - 3) _, err := conn.Write(instruction) if err != nil { @@ -234,7 +235,7 @@ func CloseMusic(conn net.Conn) { data[43] = 0x00 instruction := []byte{0xAA, 0x33, 0x01, 0x06} instruction = append(instruction, data...) - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -253,7 +254,7 @@ func CloseMusic(conn net.Conn) { // 恢复出厂设置 func RestoreFactorySettings(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x38} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -272,7 +273,7 @@ func RestoreFactorySettings(conn net.Conn) { // 重启主板 func Reboot(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x39} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -291,7 +292,7 @@ func Reboot(conn net.Conn) { // 暂停服务 func StopServer(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x37} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -302,7 +303,7 @@ func StopServer(conn net.Conn) { // 开启服务 func StartServer(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x36} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -313,7 +314,7 @@ func StartServer(conn net.Conn) { // 查询基础参数 func queryBaseParam(conn net.Conn) { instruction := []byte{0xAA, 0x03, 0x01, 0x05} - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err) @@ -336,7 +337,7 @@ func SetPower(conn net.Conn) { fmt.Println("data.len = ", len(data)) instruction := []byte{0xAA, 0x04, 0x01, 0x06} instruction = append(instruction, data...) - instruction = calculateChecksum(instruction) + instruction = CalculateChecksum(instruction) _, err := conn.Write(instruction) if err != nil { fmt.Println("Failed to send command to server:", err)