From 2478b741377d60fb857051c385497567aef5bd90 Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Sat, 8 Jan 2022 16:49:38 -0800 Subject: [PATCH] Refactor: handshake composer --- protocol/composers/handshake_composers.go | 37 ++++++++++----------- protocol/composers/navigator_composers.go | 39 +++++++++++++---------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/protocol/composers/handshake_composers.go b/protocol/composers/handshake_composers.go index bc6c4c7..a552a2a 100644 --- a/protocol/composers/handshake_composers.go +++ b/protocol/composers/handshake_composers.go @@ -6,17 +6,19 @@ import ( "github.com/yunginnanet/habbgo/protocol/packets" ) +type Param uint8 + const ( // Used in ComposeSessionParams - registerCoppa = 0 // toggle conf_coppa or conf_strong_coppa_req by setting value > 0 or > 1 - voucherEnabled = 1 // Enables in-game vouchers when value is set > 0 - registerRequireParentEmail = 2 // Requires parent email when registering if value is set > 0 - registerSendParentEmail = 3 // conf_parent_email_request_reregistration - allowDirectMail = 4 // conf_allow_direct_mail - dateFormat = 5 // Sets the date formatter used across the client - partnerIntegrationEnabled = 6 // conf_partner_integration. Value is either 1 or 0 (enabled or disabled) - allowProfileEditing = 7 // Enables the in-game profile editor - trackingHeader = 8 // tracking_header - used in stats.tracking.javascript(?) - tutorialEnabled = 9 // Enables the in-game tutorial when value is set to 1 and disables it when 0 + registerCoppa Param = iota // toggle conf_coppa or conf_strong_coppa_req by setting value > 0 or > 1 + voucherEnabled // Enables in-game vouchers when value is set > 0 + registerRequireParentEmail // Requires parent email when registering if value is set > 0 + registerSendParentEmail // conf_parent_email_request_reregistration + allowDirectMail // conf_allow_direct_mail + dateFormat // Sets the date formatter used across the client + partnerIntegrationEnabled // conf_partner_integration. Value is either 1 or 0 (enabled or disabled) + allowProfileEditing // Enables the in-game profile editor + trackingHeader // tracking_header - used in stats.tracking.javascript(?) + tutorialEnabled // Enables the in-game tutorial when value is set to 1 and disables it when 0 ) type headerID int @@ -49,7 +51,7 @@ func ComposeEndCrypto() *packets.OutgoingPacket { func ComposeSessionParams() *packets.OutgoingPacket { packet := packets.NewOutgoing(headerID(257)) // Base64 Header DA - params := make(map[int]string, 9) + params := make(map[Param]string, 9) params[voucherEnabled] = strconv.Itoa(0) // TODO create config to enable if vouchers are enabled params[registerRequireParentEmail] = strconv.Itoa(0) params[registerSendParentEmail] = strconv.Itoa(0) @@ -63,10 +65,9 @@ func ComposeSessionParams() *packets.OutgoingPacket { packet.WriteInt(len(params)) for i, v := range params { - packet.WriteInt(i) + packet.WriteInt(int(i)) - if isNumber(v) { - num, _ := strconv.Atoi(v) + if num := isNumber(v); num != -1 { packet.WriteInt(num) } else { packet.WriteString(v) @@ -87,10 +88,10 @@ func ComposeLoginOk() *packets.OutgoingPacket { return packet } -func isNumber(s string) bool { - if _, err := strconv.Atoi(s); err == nil { - return true +func isNumber(s string) int { + if num, err := strconv.Atoi(s); err == nil { + return num } - return false + return -1 } diff --git a/protocol/composers/navigator_composers.go b/protocol/composers/navigator_composers.go index 3d995b7..1a0d25d 100644 --- a/protocol/composers/navigator_composers.go +++ b/protocol/composers/navigator_composers.go @@ -5,13 +5,20 @@ import ( "strings" navigator2 "github.com/yunginnanet/habbgo/game/navigator" - room2 "github.com/yunginnanet/habbgo/game/room" + "github.com/yunginnanet/habbgo/game/room" "github.com/yunginnanet/habbgo/models" "github.com/yunginnanet/habbgo/protocol/packets" ) -func ComposeNavNodeInfo(player models.Player, cat *navigator2.Category, nodeMask bool, subcats []*navigator2.Category, - rooms []*room2.Room, currentVisitors int, maxVisitors int) *packets.OutgoingPacket { +func ComposeNavNodeInfo( + player models.Player, + cat *navigator2.Category, + nodeMask bool, + subcats []*navigator2.Category, + rooms []*room.Room, + currentVisitors, maxVisitors int, +) *packets.OutgoingPacket { + p := packets.NewOutgoing(headerID(220)) // Base64 Header C\ p.WriteBool(nodeMask) // hideCategory @@ -43,18 +50,18 @@ func ComposeNavNodeInfo(player models.Player, cat *navigator2.Category, nodeMask door, _ = strconv.Atoi(data[1]) } - p.WriteInt(r.Details.Id + room2.PublicRoomOffset) // writeInt roomId - p.WriteInt(1) // writeInt 1 - p.WriteString(r.Details.Name) // writeString roomName - p.WriteInt(r.Details.CurrentVisitors) // writeInt currentVisitors - p.WriteInt(r.Details.MaxVisitors) // writeInt maxVisitors - p.WriteInt(r.Details.CatId) // writeInt catId - p.WriteString(desc) // writeString roomDesc - p.WriteInt(r.Details.Id) // writeInt roomId - p.WriteInt(door) // writeInt door - p.WriteString(r.Details.CCTs) // writeString roomCCTs - p.WriteInt(0) // writeInt 0 - p.WriteInt(1) // writeInt 1 + p.WriteInt(r.Details.Id + room.PublicRoomOffset) // writeInt roomId + p.WriteInt(1) // writeInt 1 + p.WriteString(r.Details.Name) // writeString roomName + p.WriteInt(r.Details.CurrentVisitors) // writeInt currentVisitors + p.WriteInt(r.Details.MaxVisitors) // writeInt maxVisitors + p.WriteInt(r.Details.CatId) // writeInt catId + p.WriteString(desc) // writeString roomDesc + p.WriteInt(r.Details.Id) // writeInt roomId + p.WriteInt(door) // writeInt door + p.WriteString(r.Details.CCTs) // writeString roomCCTs + p.WriteInt(0) // writeInt 0 + p.WriteInt(1) // writeInt 1 } else { p.WriteInt(r.Details.Id) p.WriteString(r.Details.Name) @@ -66,7 +73,7 @@ func ComposeNavNodeInfo(player models.Player, cat *navigator2.Category, nodeMask p.WriteString("-") } - p.WriteString(room2.AccessType(r.Details.AccessType)) + p.WriteString(room.AccessType(r.Details.AccessType)) p.WriteInt(r.Details.CurrentVisitors) p.WriteInt(r.Details.MaxVisitors) p.WriteString(r.Details.Desc)