publicScreenMsg.go 5.3 KB
Newer Older
hujiebin's avatar
hujiebin committed
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
package group_m

import (
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/config"
	"git.hilo.cn/hilo-common/rpc"
	"git.hilo.cn/hilo-common/sdk/tencentyun"
	"hilo-group/_const/enum/group_e"
	"hilo-group/domain/model/noble_m"
	"hilo-group/domain/model/user_m"
	"runtime/debug"
	"time"
)

//公屏消息
type PublicScreenMsg struct {
	Code uint8
	Data map[string]string
}

// 信令消息
type GroupSystemMsg struct {
	MsgId   group_e.TypeSignalMsg `json:"msgId"`
	Source  string                `json:"source"`
	Target  string                `json:"target"`
	Content string                `json:"content"`
}

type UserJoinPublicMsg struct {
	Type       uint8  `json:"type"`
	ExternalId string `json:"externalId"`
	Nick       string `json:"nick"`
}

type CommonPublicMsg struct {
	Type               uint8      `json:"type"`
	OperatorExternalId string     `json:"operatorExternalId"`
	OperatorNick       string     `json:"operatorNick"`
	OperatorAvatar     string     `json:"operatorAvatar"`
	ExternalId         string     `json:"externalId"`
	Nick               string     `json:"nick"`
	Avatar             string     `json:"avatar"`
	IsVip              bool       `json:"is_vip"`
	Role               uint16     `json:"role"`
	NobleLevel         uint16     `json:"nobleLevel"`
	SvipLevel          int        `json:"svipLevel"`
	Svip               rpc.CvSvip `json:"svip"`
}

type RollDiceMsg struct {
	CommonPublicMsg
	Dices []int `json:"dices"`
}

type GroupSupportH5 struct {
	CommonPublicMsg
	H5 string `json:"h5"`
}

type RocketAwardMsg struct {
	CommonPublicMsg
	AwardType uint16 `json:"awardType"` // 奖励类型
	Number    uint32 `json:"number"`    // 数量(天/个数)
}

type LuckyboxAwardMsg struct {
	CommonPublicMsg
	Number uint32 `json:"number"` //前端要求,含义钻石数量
}

type FruitMachineAwardMsg struct {
	CommonPublicMsg
	DiamondNum uint `json:"diamondNum"`
}

type GroupCustomMsgContentType = int

const (
	// 跳转url
	GroupCustomMsgH5 GroupCustomMsgContentType = 1
	// 跳转房间
	GroupCustomMsgJump GroupCustomMsgContentType = 2
	//

)

type GroupCustomMsg struct {
	CommonPublicMsg
	ContentType int    `json:"contentType"`
	Content     string `json:"content"`
	H5          string `json:"h5"`
	GroupId     string `json:"groupId"`
}

//

type HiloUserInfo struct {
	WealthGrade uint32   `json:"wealthGrade"`
	CharmGrade  uint32   `json:"charmGrade"`
	IsVip       bool     `json:"isVip"`
	IsPretty    bool     `json:"isPretty"`
	Medals      []uint32 `json:"medals"`
	PowerName   string   `json:"powerName"` // 用户加入的国家势力的绑定群组的名称
	NobleLevel  uint16   `json:"nobleLevel"`
}

//不用返回错误
func GetHiloUserInfo(model *domain.Model, extId string) string {
	user, err := user_m.GetUserByExtId(model, extId)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%+v", extId, err)
		return ""
	}

	wealthGrade, _, err := user_m.GetWealthGrade(model, user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}
	charmGrade, _, err := user_m.GetCharmGrade(model, user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}
	isVip, _, err := user_m.IsVip(user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}
	//

	isPretty := user.IsPrettyCode()
	//
	medals, err := user_m.GetUserMedalMerge(model.Log, model.Db, user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}

	_, powerName, err := GetGroupPowerNameByUserId(model, user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}

	nobleLevel, err := noble_m.GetNobleLevel(model.Db, user.ID)
	if err != nil {
		model.Log.Errorf("extId:%v, err:%v", extId, err)
		return ""
	}

	hilo := HiloUserInfo{
		WealthGrade: wealthGrade,
		CharmGrade:  charmGrade,
		IsVip:       isVip,
		IsPretty:    isPretty,
		Medals:      medals,
		PowerName:   powerName,
		NobleLevel:  nobleLevel,
	}
	buf, err := json.Marshal(hilo)
	if err != nil {
		model.Log.Errorf("hilo:%+v, err:%v", hilo, err)
	}
	return string(buf)
}

//
var GetGroupPowerNameByUserId func(model *domain.Model, userId uint64) (uint64, string, error)

// 多协程并行发送群消息
func BroadcastGroupMessage(model *domain.Model, groupIds []string, content string, hiloUserInfo string) {
	n := config.GetGroupImConfig().MSG_PARALLEL_SIZE
	if n <= 0 {
		n = group_e.DefaultMsgParallelSize
	}
	buckets := make([][]string, n, n)
	for i := 0; i < n; i++ {
		buckets[i] = make([]string, 0)
	}
	for i := 0; i < len(groupIds); i++ {
		buckets[i%n] = append(buckets[i%n], groupIds[i])
	}
	model.Log.Infof("%d groups split into %d buckets", len(groupIds), n)

	for i, _ := range buckets {
		go func(model *domain.Model, bucketId int, groupIds []string, content string) {
			defer func() {
				if r := recover(); r != nil {
					model.Log.Errorf("BroadcastGroupMessage SYSTEM ACTION PANIC: %v, stack: %v", r, string(debug.Stack()))
				}
			}()
			for j, g := range groupIds {
				txGroupId, err := ToTxGroupId(model, g)
				if err == nil {
					model.Log.Infof("Bucket %d, sending message to groupId %d: %s", bucketId, j, txGroupId)
					tencentyun.SendCustomMsg(model.Log, txGroupId, nil, content, hiloUserInfo)
				}

				if j%10 == 0 {
					time.Sleep(time.Second)
				}
			}
		}(domain.CreateModelContext(model.MyContext), i, buckets[i], content)
	}
}