snow/person/parties.go

387 lines
7.4 KiB
Go
Raw Normal View History

2024-02-11 00:58:23 +00:00
package person
import (
2024-02-11 19:09:23 +00:00
"sync"
"time"
2024-02-11 00:58:23 +00:00
"github.com/ectrc/snow/aid"
"github.com/google/uuid"
)
2024-02-17 14:40:17 +00:00
type PartyIntention struct{
Party *Party `json:"-"`
Requester *Person `json:"-"`
Towards *Person `json:"-"`
Meta map[string]interface{}
CreatedAt time.Time
ExpiresAt time.Time
}
func NewPartyIntention(party *Party, requester *Person, towards *Person, meta map[string]interface{}) *PartyIntention {
return &PartyIntention{
Party: party,
Requester: requester,
Towards: towards,
Meta: meta,
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Minute * 60),
}
}
func (pi *PartyIntention) GenerateFortnitePartyIntention() aid.JSON {
return aid.JSON{
"requester_id": pi.Requester.ID,
"requester_dn": pi.Requester.DisplayName,
"requester_pl": "",
"requester_pl_dn": "",
"requestee_id": pi.Towards.ID,
"meta": pi.Meta,
"expires_at": pi.ExpiresAt.Format(time.RFC3339),
"sent_at": pi.CreatedAt.Format(time.RFC3339),
}
}
2024-02-14 20:30:55 +00:00
type PartyInvite struct{
Party *Party `json:"-"`
Inviter *Person `json:"-"`
Towards *Person `json:"-"`
Meta map[string]interface{}
Status string
CreatedAt time.Time
UpdatedAt time.Time
ExpiresAt time.Time
}
func NewPartyInvite(party *Party, inviter *Person, towards *Person, meta map[string]interface{}) *PartyInvite {
return &PartyInvite{
Party: party,
Inviter: inviter,
Towards: towards,
Meta: meta,
Status: "SENT",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Minute * 60),
}
}
func (pi *PartyInvite) GenerateFortnitePartyInvite() aid.JSON {
return aid.JSON{
"party_id": pi.Party.ID,
"sent_by": pi.Inviter.ID,
"sent_to": pi.Towards.ID,
"sent_at": pi.CreatedAt.Format(time.RFC3339),
"status": pi.Status,
"meta": pi.Meta,
"inviter_pl": "win",
"inviter_pl_dn": pi.Inviter.DisplayName,
"expires_at": pi.ExpiresAt.Format(time.RFC3339),
"updated_at": pi.UpdatedAt.Format(time.RFC3339),
}
2024-02-12 20:29:02 +00:00
}
2024-02-11 19:09:23 +00:00
type PartyMember struct{
Person *Person
ConnectionID string
Meta map[string]interface{}
2024-02-14 20:30:55 +00:00
Connection aid.JSON
2024-02-11 19:09:23 +00:00
Role string
2024-02-12 20:29:02 +00:00
JoinedAt time.Time
UpdatedAt time.Time
2024-02-14 22:20:17 +00:00
Revision int
}
func (pm *PartyMember) IncreaseRevision() {
pm.Revision++
2024-02-12 20:29:02 +00:00
}
func (pm *PartyMember) GenerateFortnitePartyMember() aid.JSON {
2024-02-14 20:30:55 +00:00
conn := pm.Connection
conn["yield_leadership"] = true
2024-02-12 20:29:02 +00:00
return aid.JSON{
"account_id": pm.Person.ID,
"role": pm.Role,
"meta": pm.Meta,
"joined_at": pm.JoinedAt.Format(time.RFC3339),
2024-02-14 20:30:55 +00:00
"connections": []aid.JSON{conn},
2024-02-14 22:20:17 +00:00
"revision": pm.Revision,
2024-02-12 20:29:02 +00:00
}
2024-02-11 19:09:23 +00:00
}
2024-02-11 00:58:23 +00:00
type Party struct{
ID string
2024-02-14 20:30:55 +00:00
Captain *PartyMember
2024-02-12 20:29:02 +00:00
Members map[string]*PartyMember
2024-02-14 20:30:55 +00:00
Invites []*PartyInvite
2024-02-17 14:40:17 +00:00
Intentions []*PartyIntention
2024-02-11 19:09:23 +00:00
Config map[string]interface{}
Meta map[string]interface{}
2024-02-12 20:29:02 +00:00
CreatedAt time.Time
2024-02-14 22:20:17 +00:00
Revision int
2024-02-14 20:30:55 +00:00
m sync.Mutex
2024-02-11 00:58:23 +00:00
}
var (
Parties = aid.GenericSyncMap[Party]{}
)
func NewParty() *Party {
party := &Party{
ID: uuid.New().String(),
2024-02-17 14:40:17 +00:00
Members: map[string]*PartyMember{},
2024-02-12 20:29:02 +00:00
Config: map[string]interface{}{
"type": "DEFAULT",
"sub_type": "default",
"intention_ttl:": 60,
"invite_ttl:": 60,
},
2024-02-17 14:40:17 +00:00
Meta: map[string]interface{}{},
2024-02-14 20:30:55 +00:00
Invites: []*PartyInvite{},
2024-02-17 14:40:17 +00:00
Intentions: []*PartyIntention{},
2024-02-12 20:29:02 +00:00
CreatedAt: time.Now(),
2024-02-11 00:58:23 +00:00
}
2024-02-11 19:09:23 +00:00
2024-02-11 00:58:23 +00:00
Parties.Set(party.ID, party)
2024-02-11 19:09:23 +00:00
return party
}
2024-02-14 22:20:17 +00:00
func (p *Party) IncreaseRevision() {
p.m.Lock()
defer p.m.Unlock()
p.Revision++
}
2024-02-12 20:29:02 +00:00
func (p *Party) GetMember(person *Person) *PartyMember {
p.m.Lock()
defer p.m.Unlock()
return p.Members[person.ID]
}
2024-02-14 22:20:17 +00:00
func (p *Party) PromoteMember(member *PartyMember) {
p.m.Lock()
defer p.m.Unlock()
member.Role = "CAPTAIN"
member.UpdatedAt = time.Now()
p.Captain = member
}
func (p *Party) GetFirstMember() *PartyMember {
2024-02-14 20:30:55 +00:00
p.m.Lock()
defer p.m.Unlock()
for _, member := range p.Members {
2024-02-14 22:20:17 +00:00
return member
2024-02-14 20:30:55 +00:00
}
2024-02-14 22:20:17 +00:00
return nil
2024-02-14 20:30:55 +00:00
}
func (p *Party) AddMember(person *Person) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
partyMember := &PartyMember{
Person: person,
Meta: make(map[string]interface{}),
2024-02-14 20:30:55 +00:00
Role: "MEMBER",
2024-02-12 20:29:02 +00:00
JoinedAt: time.Now(),
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
p.Members[person.ID] = partyMember
2024-02-11 19:09:23 +00:00
person.Parties.Set(p.ID, p)
}
func (p *Party) RemoveMember(person *Person) {
p.m.Lock()
defer p.m.Unlock()
2024-02-14 20:30:55 +00:00
person.Parties.Delete(p.ID)
2024-02-12 20:29:02 +00:00
delete(p.Members, person.ID)
2024-02-11 19:09:23 +00:00
if len(p.Members) == 0 {
Parties.Delete(p.ID)
}
}
2024-02-12 20:29:02 +00:00
func (p *Party) UpdateMeta(m map[string]interface{}) {
p.m.Lock()
defer p.m.Unlock()
for key, value := range m {
p.Meta[key] = value
}
}
func (p *Party) DeleteMeta(keys []string) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-12 20:29:02 +00:00
for _, key := range keys {
delete(p.Meta, key)
}
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
func (p *Party) UpdateMemberMeta(person *Person, m map[string]interface{}) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-12 20:29:02 +00:00
member, ok := p.Members[person.ID]
if !ok {
return
}
for key, value := range m {
member.Meta[key] = value
}
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
func (p *Party) UpdateMemberRevision(person *Person, revision int) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-12 20:29:02 +00:00
member, ok := p.Members[person.ID]
if !ok {
return
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
member.Meta["revision"] = revision
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
func (p *Party) DeleteMemberMeta(person *Person, keys []string) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-12 20:29:02 +00:00
member, ok := p.Members[person.ID]
if !ok {
return
}
for _, key := range keys {
delete(member.Meta, key)
2024-02-11 19:09:23 +00:00
}
}
2024-02-14 20:30:55 +00:00
func (p *Party) UpdateMemberConnection(person *Person, m aid.JSON) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-14 20:30:55 +00:00
m["connected_at"] = time.Now().Format(time.RFC3339)
m["updated_at"] = time.Now().Format(time.RFC3339)
2024-02-12 20:29:02 +00:00
member, ok := p.Members[person.ID]
if !ok {
return
}
2024-02-14 20:30:55 +00:00
member.Connection = m
2024-02-11 19:09:23 +00:00
}
2024-02-12 20:29:02 +00:00
func (p *Party) UpdateConfig(m map[string]interface{}) {
2024-02-11 19:09:23 +00:00
p.m.Lock()
defer p.m.Unlock()
2024-02-12 20:29:02 +00:00
for key, value := range m {
p.Config[key] = value
}
2024-02-11 19:09:23 +00:00
}
2024-02-14 20:30:55 +00:00
func (p *Party) AddInvite(invite *PartyInvite) {
p.m.Lock()
defer p.m.Unlock()
p.Invites = append(p.Invites, invite)
}
2024-02-17 14:40:17 +00:00
func (p *Party) GetInvite(person *Person) *PartyInvite {
p.m.Lock()
defer p.m.Unlock()
for _, invite := range p.Invites {
if invite.Towards == person {
return invite
}
}
return nil
}
2024-02-14 20:30:55 +00:00
func (p *Party) RemoveInvite(invite *PartyInvite) {
p.m.Lock()
defer p.m.Unlock()
for i, v := range p.Invites {
if v == invite {
p.Invites = append(p.Invites[:i], p.Invites[i+1:]...)
break
}
}
}
2024-02-17 14:40:17 +00:00
func (p *Party) AddIntention(intention *PartyIntention) {
p.m.Lock()
defer p.m.Unlock()
p.Intentions = append(p.Intentions, intention)
}
func (p *Party) GetIntention(person *Person) *PartyIntention {
p.m.Lock()
defer p.m.Unlock()
for _, intention := range p.Intentions {
if intention.Towards == person {
return intention
}
}
return nil
}
func (p *Party) RemoveIntention(intention *PartyIntention) {
p.m.Lock()
defer p.m.Unlock()
for i, v := range p.Intentions {
if v == intention {
p.Intentions = append(p.Intentions[:i], p.Intentions[i+1:]...)
break
}
}
}
2024-02-11 19:09:23 +00:00
func (p *Party) GenerateFortniteParty() aid.JSON {
p.m.Lock()
defer p.m.Unlock()
party := aid.JSON{
"id": p.ID,
"config": p.Config,
"meta": p.Meta,
2024-02-12 20:29:02 +00:00
"applicants": []aid.JSON{},
"members": []aid.JSON{},
"invites": []aid.JSON{},
"intentions": []aid.JSON{},
"created_at": p.CreatedAt.Format(time.RFC3339),
2024-02-11 19:09:23 +00:00
"updated_at": time.Now().Format(time.RFC3339),
"revision": 0,
}
for _, member := range p.Members {
2024-02-12 20:29:02 +00:00
party["members"] = append(party["members"].([]aid.JSON), member.GenerateFortnitePartyMember())
2024-02-11 19:09:23 +00:00
}
2024-02-14 20:30:55 +00:00
for _, invite := range p.Invites {
party["invites"] = append(party["invites"].([]aid.JSON), invite.GenerateFortnitePartyInvite())
}
2024-02-17 14:40:17 +00:00
for _, intention := range p.Intentions {
party["intentions"] = append(party["intentions"].([]aid.JSON), intention.GenerateFortnitePartyIntention())
}
2024-02-11 00:58:23 +00:00
return party
}