娃娃机监听消息

This commit is contained in:
by 2024-08-15 11:31:45 +08:00
parent f23cfda629
commit 154a037e08
3 changed files with 126 additions and 57 deletions

View File

@ -13,7 +13,7 @@ var MachineMapLock = sync.Mutex{}
type DollMachine struct { type DollMachine struct {
Id int Id int
MachineStatus int32 //娃娃机链接状态 0:离线 1:在线 MachineStatus int32 //娃娃机链接状态 0:离线 1:在线
Status bool //是否空闲 Status bool //标记是否被占用
VideoAddr string VideoAddr string
} }

View File

@ -1,6 +1,7 @@
package action package action
import ( import (
"bytes"
"fmt" "fmt"
"net" "net"
"time" "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) Process(conn, 200*time.Millisecond, []DoneFunc{machinedoll.Right}, []DoneFunc{machinedoll.RightStop}, false)
case 5: case 5:
//投币 //投币
Process(conn, 0*time.Millisecond, []DoneFunc{machinedoll.Coin, machinedoll.Coin}, []DoneFunc{}, false) //Process(conn, 0*time.Millisecond, []DoneFunc{machinedoll.Coin}, []DoneFunc{}, false)
// 读取服务端的响应 machinedoll.Coin(conn)
buf := make([]byte, 1024) go CoinResult(session, conn, msg.Snid, msg.GetId())
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,
})
} }
return nil return nil
} }
@ -129,9 +112,94 @@ func SMDollMachineGrabHandler(session *netlib.Session, packetId int, data interf
//强力抓 //强力抓
Process(conn, 0, []DoneFunc{machinedoll.Grab}, []DoneFunc{send}, false) Process(conn, 0, []DoneFunc{machinedoll.Grab}, []DoneFunc{send}, false)
} }
go DollMachineGrabResult(session, conn, msg.Snid, msg.GetId())
return nil 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 { func SMGameLinkSucceedHandler(session *netlib.Session, packetId int, data interface{}) error {
logger.Logger.Trace("与游戏服务器连接成功") logger.Logger.Trace("与游戏服务器连接成功")

View File

@ -8,7 +8,7 @@ import (
// 向前aa 05 01 50 01 01 54 dd // 向前aa 05 01 50 01 01 54 dd
func Forward(conn net.Conn) { func Forward(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x01} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 向前停止aa 05 01 50 01 00 55 dd
func ForwardStop(conn net.Conn) { func ForwardStop(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x00} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x01, 0x00}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // aa 05 01 50 02 01 57 dd
func Backward(conn net.Conn) { func Backward(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x01} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 向后停止aa 05 01 50 02 00 56 dd
func BackwardStop(conn net.Conn) { func BackwardStop(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x00} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x02, 0x00}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 向左aa 05 01 50 03 01 56 dd
func Left(conn net.Conn) { func Left(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x01} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 向左停止aa 05 01 50 03 00 57 dd
func LeftStop(conn net.Conn) { func LeftStop(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x00} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x03, 0x00}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -75,7 +75,7 @@ func LeftStop(conn net.Conn) {
// 向右 // 向右
func Right(conn net.Conn) { func Right(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x01} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 向右停止aa 05 01 50 04 00 50 dd
func RightStop(conn net.Conn) { func RightStop(conn net.Conn) {
instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x00} instruction := []byte{0xaa, 0x05, 0x01, 0x50, 0x04, 0x00}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -97,12 +97,13 @@ func RightStop(conn net.Conn) {
// 强抓下抓 // 强抓下抓
func Grab(conn net.Conn) { func Grab(conn net.Conn) {
instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01} instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
return return
} }
/*
// 读取服务端的响应 // 读取服务端的响应
buf := make([]byte, 1024) buf := make([]byte, 1024)
_, err = conn.Read(buf) _, err = conn.Read(buf)
@ -115,7 +116,7 @@ func Grab(conn net.Conn) {
if err != nil { if err != nil {
fmt.Println("Failed to read response from server:", err) fmt.Println("Failed to read response from server:", err)
return return
} }*/
} }
// 必中抓 // 必中抓
@ -123,7 +124,7 @@ func Grab2(conn net.Conn) {
//设置电压 //设置电压
instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01} instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x01}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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 // 弱抓aa 05 01 50 06 00 52 dd
func WeakGrab(conn net.Conn) { func WeakGrab(conn net.Conn) {
instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x00} instruction := []byte{0xAA, 0x05, 0x01, 0x50, 0x06, 0x00}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -152,7 +153,7 @@ func WeakGrab(conn net.Conn) {
// 投币 // 投币
func Coin(conn net.Conn) { func Coin(conn net.Conn) {
moveCommand := []byte{0xaa, 0x08, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00} moveCommand := []byte{0xaa, 0x08, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00}
moveCommand = calculateChecksum(moveCommand) moveCommand = CalculateChecksum(moveCommand)
// 发送指令到服务端 // 发送指令到服务端
_, err := conn.Write(moveCommand) _, err := conn.Write(moveCommand)
if err != nil { if err != nil {
@ -177,7 +178,7 @@ func Coin(conn net.Conn) {
// 剩余局数清零 // 剩余局数清零
func ClearRemainingGames(conn net.Conn) { func ClearRemainingGames(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x32} instruction := []byte{0xAA, 0x03, 0x01, 0x32}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) 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) var value = byte(0)
for i, datum := range data { for i, datum := range data {
if i > 0 { if i > 0 {
@ -212,7 +213,7 @@ func OpenMusic(conn net.Conn) {
data[43] = 0x01 data[43] = 0x01
instruction := []byte{0xAA, 0x33, 0x01, 0x06} instruction := []byte{0xAA, 0x33, 0x01, 0x06}
instruction = append(instruction, data...) instruction = append(instruction, data...)
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
//instruction[1] = byte(len(instruction) - 3) //instruction[1] = byte(len(instruction) - 3)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
@ -234,7 +235,7 @@ func CloseMusic(conn net.Conn) {
data[43] = 0x00 data[43] = 0x00
instruction := []byte{0xAA, 0x33, 0x01, 0x06} instruction := []byte{0xAA, 0x33, 0x01, 0x06}
instruction = append(instruction, data...) instruction = append(instruction, data...)
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -253,7 +254,7 @@ func CloseMusic(conn net.Conn) {
// 恢复出厂设置 // 恢复出厂设置
func RestoreFactorySettings(conn net.Conn) { func RestoreFactorySettings(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x38} instruction := []byte{0xAA, 0x03, 0x01, 0x38}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -272,7 +273,7 @@ func RestoreFactorySettings(conn net.Conn) {
// 重启主板 // 重启主板
func Reboot(conn net.Conn) { func Reboot(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x39} instruction := []byte{0xAA, 0x03, 0x01, 0x39}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -291,7 +292,7 @@ func Reboot(conn net.Conn) {
// 暂停服务 // 暂停服务
func StopServer(conn net.Conn) { func StopServer(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x37} instruction := []byte{0xAA, 0x03, 0x01, 0x37}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -302,7 +303,7 @@ func StopServer(conn net.Conn) {
// 开启服务 // 开启服务
func StartServer(conn net.Conn) { func StartServer(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x36} instruction := []byte{0xAA, 0x03, 0x01, 0x36}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -313,7 +314,7 @@ func StartServer(conn net.Conn) {
// 查询基础参数 // 查询基础参数
func queryBaseParam(conn net.Conn) { func queryBaseParam(conn net.Conn) {
instruction := []byte{0xAA, 0x03, 0x01, 0x05} instruction := []byte{0xAA, 0x03, 0x01, 0x05}
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)
@ -336,7 +337,7 @@ func SetPower(conn net.Conn) {
fmt.Println("data.len = ", len(data)) fmt.Println("data.len = ", len(data))
instruction := []byte{0xAA, 0x04, 0x01, 0x06} instruction := []byte{0xAA, 0x04, 0x01, 0x06}
instruction = append(instruction, data...) instruction = append(instruction, data...)
instruction = calculateChecksum(instruction) instruction = CalculateChecksum(instruction)
_, err := conn.Write(instruction) _, err := conn.Write(instruction)
if err != nil { if err != nil {
fmt.Println("Failed to send command to server:", err) fmt.Println("Failed to send command to server:", err)