forked from topfreegames/khan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clan_helpers.go
136 lines (113 loc) · 3.79 KB
/
clan_helpers.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package api
import (
"strings"
"github.com/topfreegames/khan/log"
"github.com/topfreegames/khan/models"
"github.com/uber-go/zap"
)
func dispatchClanOwnershipChangeHook(app *App, hookType int, clan *models.Clan, previousOwner *models.Player, newOwner *models.Player) error {
newOwnerPublicID := ""
if newOwner != nil {
newOwnerPublicID = newOwner.PublicID
}
l := app.Logger.With(
zap.String("source", "clanHandler"),
zap.String("operation", "dispatchClanOwnershipChangeHook"),
zap.Int("hookType", hookType),
zap.String("gameID", clan.GameID),
zap.String("clanPublicID", clan.PublicID),
zap.String("newOwnerPublicID", newOwnerPublicID),
zap.String("previousOwnerPublicID", previousOwner.PublicID),
)
previousOwnerJSON := previousOwner.Serialize()
delete(previousOwnerJSON, "gameID")
clanJSON := clan.Serialize()
delete(clanJSON, "gameID")
result := map[string]interface{}{
"gameID": clan.GameID,
"clan": clanJSON,
"previousOwner": previousOwnerJSON,
"newOwner": nil,
"isDeleted": true,
}
if newOwner != nil {
newOwnerJSON := newOwner.Serialize()
delete(newOwnerJSON, "gameID")
result["newOwner"] = newOwnerJSON
result["isDeleted"] = false
}
log.D(l, "Dispatching hook...")
app.DispatchHooks(clan.GameID, hookType, result)
log.D(l, "Hook dispatch succeeded.")
return nil
}
func serializeClans(clans []models.Clan, includePublicID bool) []map[string]interface{} {
serializedClans := make([]map[string]interface{}, len(clans))
for i, clan := range clans {
serializedClans[i] = serializeClan(&clan, includePublicID)
}
return serializedClans
}
func serializeClan(clan *models.Clan, includePublicID bool) map[string]interface{} {
serial := map[string]interface{}{
"name": clan.Name,
"metadata": clan.Metadata,
"allowApplication": clan.AllowApplication,
"autoJoin": clan.AutoJoin,
"membershipCount": clan.MembershipCount,
}
if includePublicID {
serial["publicID"] = clan.PublicID
}
return serial
}
func validateUpdateClanDispatch(game *models.Game, sourceClan *models.Clan, clan *models.Clan, metadata map[string]interface{}, l zap.Logger) bool {
cl := l.With(
zap.String("clanUpdateMetadataFieldsHookTriggerWhitelist", game.ClanUpdateMetadataFieldsHookTriggerWhitelist),
)
changedName := clan.Name != sourceClan.Name
changedAllowApplication := clan.AllowApplication != sourceClan.AllowApplication
changedAutoJoin := clan.AutoJoin != sourceClan.AutoJoin
if changedName || changedAllowApplication || changedAutoJoin {
log.D(cl, "One of the main clan properties changed")
return true
}
if game.ClanUpdateMetadataFieldsHookTriggerWhitelist == "" {
log.D(cl, "Clan has no metadata whitelist for update hook")
return false
}
log.D(cl, "Verifying fields for clan update hook dispatch...")
fields := strings.Split(game.ClanUpdateMetadataFieldsHookTriggerWhitelist, ",")
for _, field := range fields {
oldVal, existsOld := sourceClan.Metadata[field]
newVal, existsNew := metadata[field]
log.D(l, "Verifying field for change...", func(cm log.CM) {
cm.Write(
zap.Bool("existsOld", existsOld),
zap.Bool("existsNew", existsNew),
zap.Object("oldVal", oldVal),
zap.Object("newVal", newVal),
zap.String("field", field),
)
})
if existsOld != existsNew {
log.D(l, "Found difference in field. Dispatching hook...", func(cm log.CM) {
cm.Write(zap.String("field", field))
})
return true
}
if existsOld && oldVal != newVal {
log.D(l, "Found difference in field. Dispatching hook...", func(cm log.CM) {
cm.Write(zap.String("field", field))
})
return true
}
}
return false
}