Skip to content

Commit

Permalink
Merge pull request #16 from hexis-revival/follower-packets
Browse files Browse the repository at this point in the history
Implement relationship packets
  • Loading branch information
Lekuruu authored Oct 10, 2024
2 parents 5b60b50 + 3eaadc9 commit f0d2c99
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 3 deletions.
31 changes: 31 additions & 0 deletions common/database_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ func FetchStatsByUserId(userId int, state *State) (*Stats, error) {
return stats, nil
}

func CreateUserRelationship(relationship *Relationship, state *State) error {
result := state.Database.Create(relationship)

if result.Error != nil {
return result.Error
}

return nil
}

func RemoveUserRelationship(relationship *Relationship, state *State) error {
result := state.Database.Delete(relationship)

if result.Error != nil {
return result.Error
}

return nil
}

func FetchUserRelationships(userId int, status RelationshipStatus, state *State) ([]*Relationship, error) {
relationships := []*Relationship{}
result := state.Database.Where("user_id = ? AND status = ?", userId, status).Find(&relationships)
Expand All @@ -80,3 +100,14 @@ func FetchUserRelationships(userId int, status RelationshipStatus, state *State)

return relationships, nil
}

func FetchUserRelationship(userId int, targetId int, state *State) (*Relationship, error) {
relationship := &Relationship{}
result := state.Database.First(relationship, "user_id = ? AND target_id = ?", userId, targetId)

if result.Error != nil {
return nil, result.Error
}

return relationship, nil
}
4 changes: 2 additions & 2 deletions hnet/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const (
CLIENT_LOGIN_RECONNECT uint32 = 2
CLIENT_CHANGE_STATUS uint32 = 4
CLIENT_REQUEST_STATS uint32 = 11
CLIENT_FOLLOW_USER uint32 = 22
CLIENT_UNFOLLOW_USER uint32 = 23
CLIENT_RELATIONSHIP_ADD uint32 = 22
CLIENT_RELATIONSHIP_REMOVE uint32 = 23
CLIENT_LEADERBOARD_REQUEST uint32 = 25
)

Expand Down
53 changes: 52 additions & 1 deletion hnet/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func handleLogin(stream *common.IOStream, player *Player) error {
}

// Send friends list
err = player.SendPacket(SERVER_FRIENDS_LIST, FriendsList{FriendIds: friendIds})
friendsList := FriendsList{FriendIds: friendIds}
err = player.SendPacket(SERVER_FRIENDS_LIST, friendsList)
if err != nil {
return err
}
Expand Down Expand Up @@ -150,8 +151,58 @@ func handleRequestStats(stream *common.IOStream, player *Player) error {
return nil
}

func handleUserRelationshipAdd(stream *common.IOStream, player *Player) error {
request := ReadRelationshipRequest(stream)

if request == nil {
return fmt.Errorf("failed to read relationship request")
}

target := player.Server.Players.ByID(request.UserId)

if target == nil {
return fmt.Errorf("user %d not found", request.UserId)
}

player.LogIncomingPacket(CLIENT_RELATIONSHIP_ADD, request)
err := player.AddRelationship(request.UserId, request.Status)

if err != nil {
return err
}

player.Logger.Infof("Set relationship status to '%s' for %s", request.Status, target.Info.Name)
return nil
}

func handleUserRelationshipRemove(stream *common.IOStream, player *Player) error {
request := ReadRelationshipRequest(stream)

if request == nil {
return fmt.Errorf("failed to read relationship request")
}

target := player.Server.Players.ByID(request.UserId)

if target == nil {
return fmt.Errorf("user %d not found", request.UserId)
}

player.LogIncomingPacket(CLIENT_RELATIONSHIP_REMOVE, request)
err := player.RemoveRelationship(request.UserId, request.Status)

if err != nil {
return err
}

player.Logger.Infof("Removed relationship status '%s' for %s", request.Status, target.Info.Name)
return nil
}

func init() {
Handlers[CLIENT_LOGIN] = handleLogin
Handlers[CLIENT_CHANGE_STATUS] = handleStatusChange
Handlers[CLIENT_REQUEST_STATS] = handleRequestStats
Handlers[CLIENT_RELATIONSHIP_ADD] = handleUserRelationshipAdd
Handlers[CLIENT_RELATIONSHIP_REMOVE] = handleUserRelationshipRemove
}
9 changes: 9 additions & 0 deletions hnet/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,12 @@ type QuitResponse struct {
func (response QuitResponse) String() string {
return common.FormatStruct(response)
}

type RelationshipRequest struct {
Status common.RelationshipStatus
UserId uint32
}

func (request RelationshipRequest) String() string {
return common.FormatStruct(request)
}
17 changes: 17 additions & 0 deletions hnet/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ func ReadStatsRequest(stream *common.IOStream) *StatsRequest {
}
}

func ReadRelationshipRequest(stream *common.IOStream) *RelationshipRequest {
defer handlePanic()

status := common.StatusBlocked
isFriend := stream.ReadBool()
userId := stream.ReadU32()

if isFriend {
status = common.StatusFriend
}

return &RelationshipRequest{
Status: status,
UserId: userId,
}
}

func handlePanic() {
_ = recover()
}
24 changes: 24 additions & 0 deletions hnet/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ func (player *Player) RevokeLogin() error {
return player.SendPacketData(SERVER_LOGIN_REVOKED, []byte{})
}

func (player *Player) AddRelationship(targetId uint32, status common.RelationshipStatus) error {
rel := &common.Relationship{
UserId: int(player.Info.Id),
TargetId: int(targetId),
Status: status,
}

return common.CreateUserRelationship(rel, player.Server.State)
}

func (player *Player) RemoveRelationship(targetId uint32, status common.RelationshipStatus) error {
rel, err := common.FetchUserRelationship(
int(player.Info.Id),
int(targetId),
player.Server.State,
)

if err != nil {
return err
}

return common.RemoveUserRelationship(rel, player.Server.State)
}

func (player *Player) GetFriendIds() ([]uint32, error) {
relationships, err := common.FetchUserRelationships(
int(player.Info.Id),
Expand Down
5 changes: 5 additions & 0 deletions hnet/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ func (response QuitResponse) Serialize(stream *common.IOStream) {
stream.WriteU8(0) // TODO: Unused?
stream.WriteU32(response.UserId)
}

func (request RelationshipRequest) Serialize(stream *common.IOStream) {
stream.WriteBool(request.Status == common.StatusFriend)
stream.WriteU32(request.UserId)
}

0 comments on commit f0d2c99

Please sign in to comment.