snow/handlers/parties.go

297 lines
7.9 KiB
Go
Raw Normal View History

2024-02-11 19:09:23 +00:00
package handlers
import (
"github.com/ectrc/snow/aid"
p "github.com/ectrc/snow/person"
2024-02-12 20:29:02 +00:00
"github.com/ectrc/snow/socket"
2024-02-11 19:09:23 +00:00
"github.com/gofiber/fiber/v2"
)
2024-02-14 20:30:55 +00:00
func GetPartiesForUser(c *fiber.Ctx) error {
2024-02-11 19:09:23 +00:00
person := c.Locals("person").(*p.Person)
response := aid.JSON{
"current": []aid.JSON{},
"invites": []aid.JSON{},
"pending": []aid.JSON{},
"pings": []aid.JSON{},
}
person.Parties.Range(func(key string, party *p.Party) bool {
response["current"] = append(response["current"].([]aid.JSON), party.GenerateFortniteParty())
return true
})
2024-02-14 20:30:55 +00:00
person.Invites.Range(func(key string, invite *p.PartyInvite) bool {
response["invites"] = append(response["invites"].([]aid.JSON), invite.GenerateFortnitePartyInvite())
return true
})
2024-02-11 19:09:23 +00:00
return c.Status(200).JSON(response)
2024-02-12 20:29:02 +00:00
}
2024-02-14 20:30:55 +00:00
func GetPartyUserPrivacy(c *fiber.Ctx) error {
2024-02-12 20:29:02 +00:00
person := c.Locals("person").(*p.Person)
recieveIntents := person.CommonCoreProfile.Attributes.GetAttributeByKey("party.recieveIntents")
if recieveIntents == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("No Privacy Found"))
}
recieveInvites := person.CommonCoreProfile.Attributes.GetAttributeByKey("party.recieveInvites")
if recieveIntents == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("No Privacy Found"))
}
return c.Status(200).JSON(aid.JSON{
"recieveIntents": aid.JSONParse(recieveIntents.ValueJSON),
"recieveInvites": aid.JSONParse(recieveInvites.ValueJSON),
})
}
2024-02-14 20:30:55 +00:00
func GetPartyNotifications(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
2024-02-12 20:29:02 +00:00
return c.Status(200).JSON(aid.JSON{
"pings": 0,
2024-02-14 20:30:55 +00:00
"invites": person.Invites.Len(),
2024-02-12 20:29:02 +00:00
})
}
func GetPartyForMember(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
2024-02-14 20:30:55 +00:00
party, ok := p.Parties.Get(c.Params("partyId"))
2024-02-12 20:29:02 +00:00
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
2024-02-14 20:30:55 +00:00
aid.Print(person.DisplayName, " is getting party ", party.ID)
2024-02-12 20:29:02 +00:00
return c.Status(200).JSON(party.GenerateFortniteParty())
}
2024-02-14 20:30:55 +00:00
func GetPartyPingsFromFriend(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
friend := p.Find(c.Params("friendId"))
if friend == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Friend Not Found"))
}
pings := []aid.JSON{}
person.Invites.Range(func(key string, ping *p.PartyInvite) bool {
if ping.Inviter.ID == friend.ID {
pings = append(pings, ping.Party.GenerateFortniteParty())
}
return true
})
return c.Status(200).JSON(pings)
}
func PostPartyCreate(c *fiber.Ctx) error {
2024-02-12 20:29:02 +00:00
person := c.Locals("person").(*p.Person)
person.Parties.Range(func(key string, party *p.Party) bool {
party.RemoveMember(person)
return true
})
var body struct {
Config map[string]interface{} `json:"config"`
Meta map[string]interface{} `json:"meta"`
JoinInformation struct {
Meta map[string]interface{} `json:"meta"`
Connection aid.JSON `json:"connection"`
} `json:"join_info"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Request"))
}
party := p.NewParty()
party.UpdateMeta(body.Meta)
party.UpdateConfig(body.Config)
2024-02-14 20:30:55 +00:00
party.AddMember(person)
2024-02-12 20:29:02 +00:00
party.UpdateMemberMeta(person, body.JoinInformation.Meta)
2024-02-14 20:30:55 +00:00
party.UpdateMemberConnection(person, body.JoinInformation.Connection)
party.ChangeNewCaptain()
socket.EmitPartyMemberJoined(party, party.GetMember(person))
2024-02-12 20:29:02 +00:00
return c.Status(200).JSON(party.GenerateFortniteParty())
}
2024-02-14 20:30:55 +00:00
func PatchPartyUpdateState(c *fiber.Ctx) error {
2024-02-12 20:29:02 +00:00
person := c.Locals("person").(*p.Person)
var body struct {
Config map[string]interface{} `json:"config"`
Meta struct {
Update map[string]interface{} `json:"update"`
Delete []string `json:"delete"`
} `json:"meta"`
}
2024-02-14 20:30:55 +00:00
2024-02-12 20:29:02 +00:00
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Request"))
}
2024-02-14 20:30:55 +00:00
aid.PrintJSON(body)
2024-02-12 20:29:02 +00:00
party, ok := person.Parties.Get(c.Params("partyId"))
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
member := party.GetMember(person)
if member == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Not in Party"))
}
if member.Role != "CAPTAIN" {
return c.Status(400).JSON(aid.ErrorBadRequest("Not Captain"))
}
party.UpdateConfig(body.Config)
party.UpdateMeta(body.Meta.Update)
party.DeleteMeta(body.Meta.Delete)
2024-02-14 20:30:55 +00:00
socket.EmitPartyMetaUpdated(party, body.Meta.Update, body.Meta.Delete, body.Meta.Update)
2024-02-12 20:29:02 +00:00
2024-02-14 20:30:55 +00:00
return c.SendStatus(204)
2024-02-12 20:29:02 +00:00
}
2024-02-14 20:30:55 +00:00
func PatchPartyUpdateMemberState(c *fiber.Ctx) error {
2024-02-12 20:29:02 +00:00
person := c.Locals("person").(*p.Person)
var body struct {
Update map[string]interface{} `json:"update"`
Delete []string `json:"delete"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Request"))
}
party, ok := person.Parties.Get(c.Params("partyId"))
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
member := party.GetMember(person)
if member == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Not in Party"))
}
if c.Params("accountId") != person.ID {
return c.Status(400).JSON(aid.ErrorBadRequest("Not owner of person"))
}
party.UpdateMemberMeta(person, body.Update)
party.DeleteMemberMeta(person, body.Delete)
2024-02-14 20:30:55 +00:00
socket.EmitPartyMemberMetaUpdated(party, member, body.Update, body.Delete)
2024-02-12 20:29:02 +00:00
2024-02-14 20:30:55 +00:00
return c.SendStatus(204)
}
func DeletePartyMember(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
party, ok := person.Parties.Get(c.Params("partyId"))
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
member := party.GetMember(person)
if member == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Not in Party"))
}
socket.EmitPartyMemberLeft(party, member)
party.RemoveMember(person)
if party.Captain.Person.ID == person.ID && len(party.Members) > 0 {
party.ChangeNewCaptain()
go socket.EmitPartyNewCaptain(party)
}
return c.SendStatus(204)
}
func PostPartyInvite(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
var body map[string]interface{}
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Request"))
}
towards := p.Find(c.Params("accountId"))
if towards == nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Person Not Found"))
}
party, ok := person.Parties.Get(c.Params("partyId"))
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
invite := p.NewPartyInvite(party, person, towards, body)
party.AddInvite(invite)
towards.Invites.Set(party.ID, invite)
socket.EmitPartyInvite(invite)
return c.SendStatus(204)
}
func PostPartyJoin(c *fiber.Ctx) error {
person := c.Locals("person").(*p.Person)
if person.Parties.Len() != 0 {
return c.Status(400).JSON(aid.ErrorBadRequest("Already in a party"))
}
party, ok := p.Parties.Get(c.Params("partyId"))
if !ok {
return c.Status(400).JSON(aid.ErrorBadRequest("Party Not Found"))
}
var body struct {
Meta map[string]interface{} `json:"meta"`
Connection aid.JSON `json:"connection"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(aid.ErrorBadRequest("Invalid Request"))
}
// joinability, ok := party.Config["joinability"].(string)
// if ok && joinability != "OPEN" {
// invite, ok := person.Invites.Get(c.Params("partyId"))
// if !ok {
// return c.Status(400).JSON(aid.ErrorBadRequest("Invite Not Found"))
// }
// if invite.Party.ID != party.ID {
// return c.Status(400).JSON(aid.ErrorBadRequest("Invite Not Found"))
// }
// person.Invites.Delete(c.Params("partyId"))
// party.RemoveInvite(invite)
// }
party.AddMember(person)
party.UpdateMemberMeta(person, body.Meta)
party.UpdateMemberConnection(person, body.Connection)
member := party.GetMember(person)
socket.EmitPartyMemberJoined(party, member)
socket.EmitPartyMemberMetaUpdated(party, party.GetMember(person), body.Meta, []string{})
socket.EmitPartyMetaUpdated(party, party.Meta, []string{}, map[string]interface{}{})
return c.Status(200).JSON(aid.JSON{
"party_id": party.ID,
"status": "JOINED",
})
2024-02-11 19:09:23 +00:00
}