handle dice chat command

This commit is contained in:
wishu
2022-07-07 04:28:20 +10:00
parent 9ed5b50170
commit b053acc669

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"math"
"math/rand"
"time"
"github.com/Andoryuuta/byteframe"
"erupe-ce/network/binpacket"
@@ -50,9 +52,9 @@ func sendServerChatMessage(s *Session, message string) {
func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
pkt := p.(*mhfpacket.MsgSysCastBinary)
tmp := byteframe.NewByteFrameFromBytes(pkt.RawDataPayload)
if pkt.BroadcastType == 0x03 && pkt.MessageType == 0x03 && len(pkt.RawDataPayload) == 0x10 {
tmp := byteframe.NewByteFrameFromBytes(pkt.RawDataPayload)
if tmp.ReadUint16() == 0x0002 && tmp.ReadUint8() == 0x18 {
_ = tmp.ReadBytes(9)
tmp.SetLE()
@@ -64,17 +66,38 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
// Parse out the real casted binary payload
var realPayload []byte
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 {
s.logger.Warn("Failed to parse targeted cast binary")
return
}
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 {
realPayload = pkt.RawDataPayload
}
@@ -92,7 +115,11 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
case BroadcastTypeWorld:
s.server.BroadcastMHF(resp, s)
case BroadcastTypeStage:
s.stage.BroadcastMHF(resp, s)
if isDiceCommand {
s.stage.BroadcastMHF(resp, nil) // send dice result back to caller
} else {
s.stage.BroadcastMHF(resp, s)
}
case BroadcastTypeSemaphore:
if pkt.MessageType == 1 {
var session *Semaphore
@@ -106,8 +133,7 @@ func handleMsgSysCastBinary(s *Session, p mhfpacket.MHFPacket) {
(*session).BroadcastMHF(resp, s)
} else {
s.Lock()
haveStage := s.stage != nil
if haveStage {
if s.stage != nil {
s.stage.BroadcastMHF(resp, s)
}
s.Unlock()