mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-25 05:14:38 +01:00
handle dice chat command
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"math"
|
"math"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Andoryuuta/byteframe"
|
"github.com/Andoryuuta/byteframe"
|
||||||
"erupe-ce/network/binpacket"
|
"erupe-ce/network/binpacket"
|
||||||
@@ -50,9 +52,9 @@ func sendServerChatMessage(s *Session, message string) {
|
|||||||
|
|
||||||
func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
|
func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
|
||||||
pkt := p.(*mhfpacket.MsgSysCastBinary)
|
pkt := p.(*mhfpacket.MsgSysCastBinary)
|
||||||
|
tmp := byteframe.NewByteFrameFromBytes(pkt.RawDataPayload)
|
||||||
|
|
||||||
if pkt.BroadcastType == 0x03 && pkt.MessageType == 0x03 && len(pkt.RawDataPayload) == 0x10 {
|
if pkt.BroadcastType == 0x03 && pkt.MessageType == 0x03 && len(pkt.RawDataPayload) == 0x10 {
|
||||||
tmp := byteframe.NewByteFrameFromBytes(pkt.RawDataPayload)
|
|
||||||
if tmp.ReadUint16() == 0x0002 && tmp.ReadUint8() == 0x18 {
|
if tmp.ReadUint16() == 0x0002 && tmp.ReadUint8() == 0x18 {
|
||||||
_ = tmp.ReadBytes(9)
|
_ = tmp.ReadBytes(9)
|
||||||
tmp.SetLE()
|
tmp.SetLE()
|
||||||
@@ -64,17 +66,38 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
// Parse out the real casted binary payload
|
// Parse out the real casted binary payload
|
||||||
var realPayload []byte
|
var realPayload []byte
|
||||||
var msgBinTargeted *binpacket.MsgBinTargeted
|
var msgBinTargeted *binpacket.MsgBinTargeted
|
||||||
if pkt.BroadcastType == BroadcastTypeTargeted {
|
|
||||||
bf := byteframe.NewByteFrameFromBytes(pkt.RawDataPayload)
|
|
||||||
msgBinTargeted = &binpacket.MsgBinTargeted{}
|
|
||||||
err := msgBinTargeted.Parse(bf)
|
|
||||||
|
|
||||||
|
isDiceCommand := false
|
||||||
|
tmp.SetLE()
|
||||||
|
tmp.Seek(int64(0), 0)
|
||||||
|
_ = tmp.ReadUint32()
|
||||||
|
authorLen := tmp.ReadUint16()
|
||||||
|
msgLen := tmp.ReadUint16()
|
||||||
|
msg := tmp.ReadNullTerminatedBytes()
|
||||||
|
|
||||||
|
if pkt.BroadcastType == BroadcastTypeTargeted {
|
||||||
|
tmp.SetBE()
|
||||||
|
tmp.Seek(int64(0), 0)
|
||||||
|
msgBinTargeted = &binpacket.MsgBinTargeted{}
|
||||||
|
err := msgBinTargeted.Parse(tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Warn("Failed to parse targeted cast binary")
|
s.logger.Warn("Failed to parse targeted cast binary")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
realPayload = msgBinTargeted.RawDataPayload
|
realPayload = msgBinTargeted.RawDataPayload
|
||||||
|
} else if msgLen == 6 && string(msg) == "@dice" {
|
||||||
|
isDiceCommand = true
|
||||||
|
roll := byteframe.NewByteFrame()
|
||||||
|
roll.WriteInt16(1) // Unk
|
||||||
|
roll.SetLE()
|
||||||
|
roll.WriteUint16(4) // Unk
|
||||||
|
roll.WriteUint16(authorLen)
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
dice := fmt.Sprintf("%d", rand.Intn(100)+1)
|
||||||
|
roll.WriteUint16(uint16(len(dice)+1))
|
||||||
|
roll.WriteNullTerminatedBytes([]byte(dice))
|
||||||
|
roll.WriteNullTerminatedBytes(tmp.ReadNullTerminatedBytes())
|
||||||
|
realPayload = roll.Data()
|
||||||
} else {
|
} else {
|
||||||
realPayload = pkt.RawDataPayload
|
realPayload = pkt.RawDataPayload
|
||||||
}
|
}
|
||||||
@@ -92,7 +115,11 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
case BroadcastTypeWorld:
|
case BroadcastTypeWorld:
|
||||||
s.server.BroadcastMHF(resp, s)
|
s.server.BroadcastMHF(resp, s)
|
||||||
case BroadcastTypeStage:
|
case BroadcastTypeStage:
|
||||||
|
if isDiceCommand {
|
||||||
|
s.stage.BroadcastMHF(resp, nil) // send dice result back to caller
|
||||||
|
} else {
|
||||||
s.stage.BroadcastMHF(resp, s)
|
s.stage.BroadcastMHF(resp, s)
|
||||||
|
}
|
||||||
case BroadcastTypeSemaphore:
|
case BroadcastTypeSemaphore:
|
||||||
if pkt.MessageType == 1 {
|
if pkt.MessageType == 1 {
|
||||||
var session *Semaphore
|
var session *Semaphore
|
||||||
@@ -106,8 +133,7 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
|
|||||||
(*session).BroadcastMHF(resp, s)
|
(*session).BroadcastMHF(resp, s)
|
||||||
} else {
|
} else {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
haveStage := s.stage != nil
|
if s.stage != nil {
|
||||||
if haveStage {
|
|
||||||
s.stage.BroadcastMHF(resp, s)
|
s.stage.BroadcastMHF(resp, s)
|
||||||
}
|
}
|
||||||
s.Unlock()
|
s.Unlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user