From 639a50fca03af074bfe8488fc407189e01ea94b0 Mon Sep 17 00:00:00 2001 From: Justin T <37750742+jtieri@users.noreply.github.com> Date: Fri, 3 Jan 2020 22:38:22 -0600 Subject: [PATCH] WIP: Basic room design Began work on modeling of rooms in the server as well as start on its related service/database components. --- habbgo/database/player_repo.go | 6 +-- habbgo/database/room_repo.go | 11 +++++ habbgo/game/model/room.go | 49 +++++++++++++++++++ habbgo/game/service/player_service.go | 4 +- habbgo/game/service/room_service.go | 31 ++++++++++++ .../protocol/composers/handshake_composers.go | 7 ++- habbgo/server/session.go | 2 +- habbgo/shared/num.go | 11 +++++ 8 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 habbgo/database/room_repo.go create mode 100644 habbgo/game/model/room.go create mode 100644 habbgo/game/service/room_service.go create mode 100644 habbgo/shared/num.go diff --git a/habbgo/database/player_repo.go b/habbgo/database/player_repo.go index 5101390..4ba7c41 100644 --- a/habbgo/database/player_repo.go +++ b/habbgo/database/player_repo.go @@ -45,9 +45,9 @@ func LoadBadges(player *model.Player) { func fillDetails(p *model.Player) { query := "SELECT P.id, P.username, P.sex, P.figure, P.pool_figure, P.film, P.credits, P.tickets, P.motto, " + - "P.console_motto, P.current_badge, P.display_badge, P.last_online, P.sound_enabled "+ - "FROM Players P " + - "WHERE P.username = ?" + "P.console_motto, P.current_badge, P.display_badge, P.last_online, P.sound_enabled " + + "FROM Players P " + + "WHERE P.username = ?" err := p.Session.Database().QueryRow(query, p.Details.Username).Scan(&p.Details.Id, &p.Details.Username, &p.Details.Sex, &p.Details.Figure, &p.Details.PoolFigure, &p.Details.Film, &p.Details.Credits, diff --git a/habbgo/database/room_repo.go b/habbgo/database/room_repo.go new file mode 100644 index 0000000..11c3d95 --- /dev/null +++ b/habbgo/database/room_repo.go @@ -0,0 +1,11 @@ +package database + +import "database/sql" + +type RoomRepo struct { + database *sql.DB +} + +func NewRoomRepo(db *sql.DB) *RoomRepo { + return &RoomRepo{database: db} +} diff --git a/habbgo/game/model/room.go b/habbgo/game/model/room.go new file mode 100644 index 0000000..92af7b2 --- /dev/null +++ b/habbgo/game/model/room.go @@ -0,0 +1,49 @@ +package model + +type Room struct { + Details *Data + Model *Model + Map *Map +} + +type Data struct { + Id int + CatId int + Name string + Desc string + CCTs string + Wallpaper int + Floor int + Landscape float32 + Owner_Id int + Owner_Name string + ShowOwner bool + SudoUsers bool + HideRoom bool + AccessType int + Password string + CurrentVisitors int + MaxVisitors int + Rating int + CreatedAt string + UpdatedAt string + ChildRooms []*Room +} + +type Model struct { + Id int + Name string + DoorX int + DoorY int + DoorZ float32 + DoorDirection int + Heightmap string +} + +type Map struct { + mapping [][]*Tile +} + +type Tile struct { + // May incorporate this into pathfinding later down the line +} diff --git a/habbgo/game/service/player_service.go b/habbgo/game/service/player_service.go index 1b46966..7648b55 100644 --- a/habbgo/game/service/player_service.go +++ b/habbgo/game/service/player_service.go @@ -9,10 +9,10 @@ import ( func Login(player *model.Player) { // Set player logged in & ping ready for latency test // Possibly add player to a list of online players? Health endpoint with server stats? - // Save current time to DB for players last online time + // Save current time to Conn for players last online time // Check if player is banned & if so send USER_BANNED - // Log IP address to DB + // Log IP address to Conn database.LoadBadges(player) go player.Session.Send(composers.ComposeLoginOk()) diff --git a/habbgo/game/service/room_service.go b/habbgo/game/service/room_service.go new file mode 100644 index 0000000..873ad88 --- /dev/null +++ b/habbgo/game/service/room_service.go @@ -0,0 +1,31 @@ +package service + +import ( + "database/sql" + "github.com/jtieri/HabbGo/habbgo/database" + "github.com/jtieri/HabbGo/habbgo/game/model" + "sync" +) + +var rs *roomService +var ronce sync.Once + +type roomService struct { + repo *database.RoomRepo + rooms []*model.Room +} + +func RoomService() *roomService { + ronce.Do(func() { + rs = &roomService{ + repo: nil, + rooms: make([]*model.Room, 50), + } + }) + + return rs +} + +func (rs *roomService) SetDBConn(db *sql.DB) { + rs.repo = database.NewRoomRepo(db) +} diff --git a/habbgo/protocol/composers/handshake_composers.go b/habbgo/protocol/composers/handshake_composers.go index 5172a1d..2c4191f 100644 --- a/habbgo/protocol/composers/handshake_composers.go +++ b/habbgo/protocol/composers/handshake_composers.go @@ -2,11 +2,10 @@ package composers import ( "github.com/jtieri/HabbGo/habbgo/protocol/packets" - "github.com/jtieri/HabbGo/habbgo/utils" + "github.com/jtieri/HabbGo/habbgo/shared" "strconv" ) - 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 @@ -26,7 +25,7 @@ func ComposeHello() *packets.OutgoingPacket { func ComposeCryptoParams() *packets.OutgoingPacket { packet := packets.NewOutgoing(277) // Base64 Header DU - packet.WriteInt(0) // Toggles server->client encryption; 0=off | non-zero=on + packet.WriteInt(0) // Toggles server->client encryption; 0=off | non-zero=on return packet } @@ -55,7 +54,7 @@ func ComposeSessionParams() *packets.OutgoingPacket { for i, v := range params { packet.WriteInt(i) - if utils.IsNumber(v) { + if shared.IsNumber(v) { num, _ := strconv.Atoi(v) packet.WriteInt(num) } else { diff --git a/habbgo/server/session.go b/habbgo/server/session.go index 9fd208b..b4200f4 100644 --- a/habbgo/server/session.go +++ b/habbgo/server/session.go @@ -134,7 +134,7 @@ func (session *Session) Flush(packet *packets.OutgoingPacket) { } } -// Database returns a pointer to a Session's DB access struct. +// Database returns a pointer to a Session's Conn access struct. func (session *Session) Database() *sql.DB { return session.database } diff --git a/habbgo/shared/num.go b/habbgo/shared/num.go new file mode 100644 index 0000000..47d68f5 --- /dev/null +++ b/habbgo/shared/num.go @@ -0,0 +1,11 @@ +package shared + +import "strconv" + +func IsNumber(s string) bool { + if _, err := strconv.Atoi(s); err == nil { + return true + } + + return false +}