refactor project structure; protocol

Needed to change the project structure to avoid circular dependencies in the design. Encoding added to protocol package as it made more sense there.
This commit is contained in:
Justin T 2019-12-13 01:16:05 -06:00
parent 502b9078ca
commit efa4e5396a
9 changed files with 150 additions and 33 deletions

View File

@ -0,0 +1,71 @@
package composers
import (
"github.com/jtieri/HabbGo/habbgo/protocol/packets"
"github.com/jtieri/HabbGo/habbgo/utils"
"strconv"
)
// Used in ComposeSessionParams
const (
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
)
func ComposeHello() *packets.OutgoingPacket {
return packets.NewOutgoing(0) // Base64 Header @@
}
func ComposeCryptoParams() *packets.OutgoingPacket {
packet := packets.NewOutgoing(277) // Base64 Header DU
packet.WriteInt(0) // Toggles server->client encryption; 0=off | non-zero=on
return packet
}
func ComposeEndCrypto() *packets.OutgoingPacket {
packet := packets.NewOutgoing(278) // Base 64 Header DV
return packet
}
func ComposeSessionParams() *packets.OutgoingPacket {
packet := packets.NewOutgoing(257) // Base64 Header DA
params := make(map[int]string, 10)
params[registerCoppa] = strconv.Itoa(0)
params[voucherEnabled] = strconv.Itoa(0) // TODO create config to enable if vouchers are enabled
params[registerRequireParentEmail] = strconv.Itoa(0)
params[registerSendParentEmail] = strconv.Itoa(0)
params[allowDirectMail] = strconv.Itoa(0)
params[dateFormat] = "dd-MM-yyyy"
params[partnerIntegrationEnabled] = strconv.Itoa(0)
params[allowProfileEditing] = strconv.Itoa(1) // TODO create config to enable if profile editing is enabled
params[trackingHeader] = ""
params[tutorialEnabled] = strconv.Itoa(0) // TODO check if player has finished tutorial then set appropriately
packet.WriteInt(len(params))
for i, v := range params {
packet.WriteInt(i)
if utils.IsNumber(v) {
num, _ := strconv.Atoi(v)
packet.WriteInt(num)
} else {
packet.WriteString(v)
}
}
return packet
}
func ComposeLoginOk() *packets.OutgoingPacket {
packet := packets.NewOutgoing(3) // Base 64 Header @C
return packet
}

View File

@ -0,0 +1,41 @@
package composers
import (
"github.com/jtieri/HabbGo/habbgo/game/model/player"
"github.com/jtieri/HabbGo/habbgo/protocol/packets"
"strconv"
)
func ComposeUserObj(player *player.Player) *packets.OutgoingPacket {
p := packets.NewOutgoing(5) // Base64 Header @E
p.WriteString(strconv.Itoa(player.Details.Id)) // writeString userId
p.WriteString(player.Details.Username) // writeString name
p.WriteString(player.Details.Figure) // writeString figure
p.WriteString(string(player.Details.Sex)) // writeString sex
p.WriteString(player.Details.Motto) // writeString motto
p.WriteInt(player.Details.Tickets) // writeInt ph_tickets
p.WriteString(player.Details.PoolFigure) // writeString ph_figure
p.WriteInt(player.Details.Film) // writeInt photo_film
//p.WriteInt(directMail)
return p
}
func ComposeCreditBalance(credits int) *packets.OutgoingPacket {
p := packets.NewOutgoing(6) // Base64 Header @F
p.WriteString(strconv.Itoa(credits) + ".0")
return p
}
func ComposeAvailableBadges() *packets.OutgoingPacket {
p := packets.NewOutgoing(229) // Base64 Header
// writeInt num of badges
// loop and writeString each badge id
// writeInt chosenBadge
// writeInt visible
return p
}

View File

@ -1,9 +1,10 @@
package handlers
import (
"github.com/jtieri/HabbGo/habbgo/database"
"github.com/jtieri/HabbGo/habbgo/game/model/player"
"github.com/jtieri/HabbGo/habbgo/server/protocol/composers"
"github.com/jtieri/HabbGo/habbgo/server/protocol/packets"
"github.com/jtieri/HabbGo/habbgo/protocol/composers"
"github.com/jtieri/HabbGo/habbgo/protocol/packets"
)
func HandleInitCrypto(player *player.Player, packet *packets.IncomingPacket) {
@ -11,7 +12,6 @@ func HandleInitCrypto(player *player.Player, packet *packets.IncomingPacket) {
}
func HandleGenerateKey(player *player.Player, packet *packets.IncomingPacket) {
// TODO send
player.Session.Send(composers.ComposeEndCrypto())
}
@ -20,9 +20,23 @@ func HandleGetSessionParams(player *player.Player, packet *packets.IncomingPacke
}
func HandleSSO(player *player.Player, packet *packets.IncomingPacket) {
token := packet.ReadString()
// TODO if player login with token is success login, otherwise send LOCALISED ERROR & disconnect from server
if token == "" {
player.Service.Login()
} else {
}
}
func HandleTryLogin(player *player.Player, packet *packets.IncomingPacket) {
username := packet.ReadString()
password := packet.ReadString()
if database.Login(player, username, password) {
player.Service.Login()
} else {
// TODO send LOCALISED ERROR
}
}

View File

@ -0,0 +1,19 @@
package handlers
import (
"github.com/jtieri/HabbGo/habbgo/game/model/player"
"github.com/jtieri/HabbGo/habbgo/protocol/composers"
"github.com/jtieri/HabbGo/habbgo/protocol/packets"
)
func HandleGetInfo(player *player.Player, packet *packets.IncomingPacket) {
player.Session.Send(composers.ComposeUserObj(player))
}
func HandleGetCredits(player *player.Player, packet *packets.IncomingPacket) {
player.Session.Send(composers.ComposeCreditBalance(player.Details.Credits))
}
func HandleGetAvailableBadges(player *player.Player, packet *packets.IncomingPacket) {
player.Session.Send(composers.ComposeAvailableBadges())
}

View File

@ -2,7 +2,7 @@ package packets
import (
"bytes"
"github.com/jtieri/HabbGo/habbgo/utils/encoding"
"github.com/jtieri/HabbGo/habbgo/protocol/encoding"
)
// IncomingPacket represents a client->server packet.

View File

@ -2,7 +2,7 @@ package packets
import (
"bytes"
"github.com/jtieri/HabbGo/habbgo/utils/encoding"
"github.com/jtieri/HabbGo/habbgo/protocol/encoding"
)
// OutgoingPacket represents a server->client packet.

View File

@ -1,28 +0,0 @@
package composers
import "github.com/jtieri/HabbGo/habbgo/server/protocol/packets"
func ComposeHello() *packets.OutgoingPacket {
return packets.NewOutgoing(0) // Base64 Header @@
}
func ComposeCryptoParams() *packets.OutgoingPacket {
packet := packets.NewOutgoing(277) // Base64 Header DU
packet.WriteInt(0) // Toggles server->client encryption; 0=off | non-zero=on
return packet
}
func ComposeEndCrypto() *packets.OutgoingPacket {
packet := packets.NewOutgoing(278) // Base 64 Header DV
return packet
}
func ComposeSessionParams() *packets.OutgoingPacket {
packet := packets.NewOutgoing(257) // Base64 Header DA
// Map the param types to their values
// writeInt number of params
// iterate over the params & writeInt(paramiD) then writeInt for numeric values and writeString for strings
return packet
}