/
player_decorators.go
80 lines (64 loc) · 2.02 KB
/
player_decorators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (C) 2015 TF2Stadium
// Use of this source code is governed by the GPLv3
// that can be found in the COPYING file.
package player
import (
"encoding/json"
"time"
db "github.com/TF2Stadium/Helen/database"
"github.com/TF2Stadium/Helen/helpers"
)
func (p *Player) DecoratePlayerTags() []string {
tags := []string{helpers.RoleNames[p.Role]}
if p.IsStreaming {
tags = append(tags, "twitch")
}
return tags
}
func (p *Player) setJSONFields(stats, lobbies, streaming, bans bool) {
db.DB.Preload("Stats").First(p, p.ID)
p.PlaceholderLobbiesPlayed = new(int)
*p.PlaceholderLobbiesPlayed = p.Stats.TotalLobbies()
if stats {
p.Stats.Total = p.Stats.TotalLobbies()
p.PlaceholderStats = &p.Stats
}
p.PlaceholderTags = new([]string)
p.PlaceholderRoleStr = new(string)
*p.PlaceholderRoleStr = helpers.RoleNames[p.Role]
*p.PlaceholderTags = p.DecoratePlayerTags()
// if lobbies {
// p.PlaceholderLobbies = new([]LobbyData)
// var lobbies []*Lobby
// db.DB.Table("lobbies").Joins("INNER JOIN lobby_slots ON lobbies.id = lobby_slots.lobby_id").Where("lobbies.match_ended = TRUE AND lobby_slots.player_id = ?", p.ID).Order("lobbies.ID DESC").Limit(5).Find(&lobbies)
// for _, lobby := range lobbies {
// *p.PlaceholderLobbies = append(*p.PlaceholderLobbies, DecorateLobbyData(lobby, true))
// }
// }
p.Name = p.Alias()
if p.TwitchName != "" {
if p.ExternalLinks == nil {
p.ExternalLinks = make(map[string]*string)
}
twitchURL := "https://twitch.tv/" + p.TwitchName
p.ExternalLinks["twitch"] = &twitchURL
}
p.setStreamingStatus()
if bans {
p.PlaceholderBans, _ = p.GetActiveBans()
}
}
func (ban *PlayerBan) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
Until time.Time `json:"until"`
Reason string `json:"reason"`
}{ban.Type.String(), ban.Until, ban.Reason})
}
func (p *Player) SetPlayerProfile() {
p.setJSONFields(true, true, true, true)
}
func (p *Player) SetPlayerSummary() {
p.setJSONFields(false, false, false, false)
p.ExternalLinks = nil
}