diff --git a/go.mod b/go.mod index 8a4847e..8a3f1b8 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.bit5.ru/backend/push_common go 1.19 + +require git.bit5.ru/backend/errors v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..203d7fc --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.bit5.ru/backend/errors v1.0.0 h1:WWJ0sly44q1HQjN01X75ZAGKZwwY5Ml+XVDXMjCkToA= +git.bit5.ru/backend/errors v1.0.0/go.mod h1:75faRwsnpM0Se00/Bh7fysWQXV8oMjNJFQ6f7+r9k3Y= diff --git a/push_common.go b/push_common.go index d59ab23..a78bce3 100644 --- a/push_common.go +++ b/push_common.go @@ -1,5 +1,12 @@ package push_common +import ( + "strconv" + "strings" + + "git.bit5.ru/backend/errors" +) + type PushId uint32 func (id PushId) String() string { @@ -27,6 +34,60 @@ type PushPreview struct { //----------------------------------------------------------------------------- +type PushDef struct { + MaxVersion string `json:"maxVersion"` + Platforms []uint32 `json:"platforms"` + PlayersIds []uint32 `json:"playersIds"` + Stime uint32 `json:"stime"` + TextVariants []*TextVariant `json:"textVariants"` + ForTest bool `json:"forTest"` + ImageUrl string `json:"imageUrl"` +} + +func (pushDef *PushDef) Validate() error { + if len(pushDef.TextVariants) == 0 { + return errors.New("No text variants") + } + return nil +} + +func (pushDef *PushDef) Info() string { + var strs []string + if len(pushDef.Platforms) > 0 { + strs = append(strs, "Platforms: "+sliceUint32ToString(pushDef.Platforms, ",")) + } + if len(pushDef.MaxVersion) > 0 { + strs = append(strs, "Max.ver: "+pushDef.MaxVersion) + } + if len(pushDef.PlayersIds) > 0 { + playersIdsStr := sliceUint32ToString(pushDef.PlayersIds, ",") + strs = append(strs, "Plr.ids: "+playersIdsStr) + } + return strings.Join(strs, "; ") +} + +func sliceUint32ToString(nums []uint32, sep string) string { + strs := make([]string, 0, len(nums)) + for _, num := range nums { + str := strconv.FormatUint(uint64(num), 10) + strs = append(strs, str) + } + return strings.Join(strs, sep) +} + +//----------------------------------------------------------------------------- + +type IncomingPushInfo struct { + Id int `json:"id"` + MaxVersion string `json:"maxVersion"` + Platforms []uint32 `json:"platforms"` + PlayersIds []uint32 `json:"playersIds"` + TextVariants []*TextVariant `json:"textVariants"` + ImageUrl string `json:"imageUrl"` +} + +//----------------------------------------------------------------------------- + type TextVariant struct { Lang int `json:"lang"` Title string `json:"title"`