event_init.go 14.9 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package event_s

import (
hujiebin's avatar
hujiebin committed
4
	"encoding/json"
hujiebin's avatar
hujiebin committed
5
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
7 8
	"git.hilo.cn/hilo-common/rpc"
	"git.hilo.cn/hilo-common/sdk/tencentyun"
hujiebin's avatar
hujiebin committed
9
	"hilo-group/_const/enum/groupPower_e"
hujiebin's avatar
hujiebin committed
10
	"hilo-group/_const/enum/group_e"
hujiebin's avatar
hujiebin committed
11
	"hilo-group/_const/enum/msg_e"
hujiebin's avatar
hujiebin committed
12
	"hilo-group/_const/enum/task_e"
hujiebin's avatar
hujiebin committed
13
	"hilo-group/domain/event/gift_ev"
14
	"hilo-group/domain/event/group_ev"
hujiebin's avatar
hujiebin committed
15
	"hilo-group/domain/event/group_power_ev"
16
	"hilo-group/domain/model/diamond_m"
hujiebin's avatar
hujiebin committed
17
	"hilo-group/domain/model/groupPower_m"
hujiebin's avatar
hujiebin committed
18
	"hilo-group/domain/model/group_m"
hujiebin's avatar
hujiebin committed
19
	"hilo-group/domain/model/msg_m"
hujiebin's avatar
hujiebin committed
20
	"hilo-group/domain/model/task_m"
hujiebin's avatar
hujiebin committed
21
	"hilo-group/domain/model/user_m"
hujiebin's avatar
hujiebin committed
22
	"hilo-group/myerr"
23
	"strconv"
hujiebin's avatar
hujiebin committed
24
	"time"
hujiebin's avatar
hujiebin committed
25 26 27 28
)

func EventInit() {
	GroupPowerEvents()
29
	GroupSupportEvents()
hujiebin's avatar
hujiebin committed
30 31 32
	GroupEvents()
	GroupImMass()
	GroupTheme()
hujiebin's avatar
hujiebin committed
33
	SendGift()
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
}

func GroupSupportEvents() {
	//群组支持
	group_ev.AddGroupSupportSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupSupportEvent)
		if !ok {
			model.Log.Errorf("AddGroupSupportSync fail data")
			return nil
		}
		model.Log.Infof("diamond AddGroupSupport admin:%v mgrs:%v", event.GroupSupportAwardAdmin, event.GroupSupportAwardMgrs)
		diamondAccountAdmin, err := diamond_m.GetDiamondAccountByUserId(model, event.GroupSupportAwardAdmin.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetailAdmin, err := diamondAccountAdmin.GroupSupportAdmin(event.GroupSupportAwardAdmin.GroupSupportAwardId, event.GroupSupportAwardAdmin.DiamondNum)
		if err != nil {
			return err
		}
		if err := diamondAccountDetailAdmin.Persistent(); err != nil {
			return err
		}
		for i, _ := range event.GroupSupportAwardMgrs {
			diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.GroupSupportAwardMgrs[i].UserId)
			if err != nil {
				return err
			}
			diamondAccountDetail, err := diamondAccount.GroupSupportMgr(event.GroupSupportAwardMgrs[i].GroupSupportAwardId, event.GroupSupportAwardMgrs[i].DiamondNum)
			if err != nil {
				return err
			}
			if err := diamondAccountDetail.Persistent(); err != nil {
				return err
			}
		}
		return nil
	})

	//群组支持奖励
	group_ev.AddGroupSupportAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupSupportEvent)
		if !ok {
			model.Log.Errorf("AddGroupSupportSync fail data")
			return nil
		}
		model.Log.Infof("msg AddGroupSupportAsync admin:%v, mgr:%v", event.GroupSupportAwardAdmin, event.GroupSupportAwardMgrs)
		for i, _ := range event.GroupSupportAwardMgrs {
			user, err := user_m.GetUser(model, event.GroupSupportAwardMgrs[i].UserId)
			if err != nil {
				model.Log.Errorf("msg AddGroupSupportAsync GetUser userId:%v, err:%v", event.GroupSupportAwardMgrs[i].UserId, err)
			}
			if err := msg_m.NewUserRecord(model, user.ID, msg_e.GroupSupport, user.Nick, user.ID, strconv.FormatUint(uint64(event.GroupSupportAwardMgrs[i].DiamondNum), 10), "", "", "", event.GroupCode).Persistent(); err != nil {
				model.Log.Errorf("msg AddGroupSupportAsync NewUserRecord err:%v", err)
				break
			}
			msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
		}
		user, err := user_m.GetUser(model, event.GroupSupportAwardAdmin.UserId)
		if err != nil {
			model.Log.Errorf("msg AddGroupSupportAsync GetUser userId:%v, err:%v", event.GroupSupportAwardAdmin.UserId, err)
		}
		if err := msg_m.NewUserRecord(model, user.ID, msg_e.GroupSupport, user.Nick, user.ID, strconv.FormatUint(uint64(event.GroupSupportAwardAdmin.DiamondNum), 10), "", "", "", event.GroupCode).Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupSupportAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
		return nil
	})

hujiebin's avatar
hujiebin committed
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
}

func GroupPowerEvents() {
	//加入势力
	group_power_ev.AddGroupPowerJoinAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_power_ev.GroupPowerJoinEvent)
		if !ok {
			model.Log.Errorf("AddGroupPowerJoinAsync event type err")
			return nil
		}
		model.Log.Infof("msg AddGroupPowerJoinAsync UserId:%v, GroupPowerId:%v", event.UserId, event.GroupPowerId)
		//获取势力主
		mgrUserId, err := groupPower_m.GetGroupPowerMgr(model, event.GroupPowerId)
		if err != nil {
			return err
		}
		mgrUser, err := user_m.GetUser(model, mgrUserId)
		if err != nil {
			return err
		}
		joinUser, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}
		if err := msg_m.NewUserRecord(model, mgrUser.ID, msg_e.GroupPowerUserJoin, joinUser.Nick, joinUser.ID, "", "", "", "", "").Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupPowerJoinAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, mgrUser.ExternalId, mgrUser.DeviceType)
		return nil
	})
	//离开势力
	group_power_ev.AddGroupPowerLeaveAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_power_ev.GroupPowerLeaveEvent)
		if !ok {
			model.Log.Errorf("AddGroupPowerLeaveAsync event type err")
			return nil
		}
		model.Log.Infof("msg AddGroupPowerLeaveAsync UserId:%v, GroupPowerId:%v", event.UserId, event.GroupPowerId)
		//获取势力主
		mgrUserId, err := groupPower_m.GetGroupPowerMgr(model, event.GroupPowerId)
		if err != nil {
			return err
		}
		mgrUser, err := user_m.GetUser(model, mgrUserId)
		if err != nil {
			return err
		}
		joinUser, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}
		if err := msg_m.NewUserRecord(model, mgrUser.ID, msg_e.GroupPowerUserLeave, joinUser.Nick, joinUser.ID, "", "", "", "", "").Persistent(); err != nil {
			model.Log.Errorf("msg AddGroupPowerLeaveAsync NewUserRecord err:%v", err)
			return nil
		}
		msg_m.SendEmasMsgAssistant(model, mgrUser.ExternalId, mgrUser.DeviceType)
		return nil
	})
}
hujiebin's avatar
hujiebin committed
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

func GroupEvents() {
	// 进房事件
	group_ev.AddGroupInAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupInEvent)
		if !ok {
			model.Log.Errorf("AddGroupInAsync for room: %+v", event)
			return nil
		}
		model.Log.Infof("AddGroupInAsync for room: %+v", event)

		uer := group_m.UserEnterRoom{
			UserId:    event.UserId,
			GroupId:   event.GroupId,
			EnterTime: time.Now(),
		}
		err := uer.Save(model.Db)
		model.Log.Infof("AddGroupInAsync, UserEnterRoom err: %v", err)
		return err
	})

	// 进入房间时,
	group_ev.AddGroupInAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupInEvent)
		if !ok {
			model.Log.Errorf("AddGroupInAsync for room: %+v", event)
			return nil
		}
		model.Log.Infof("AddGroupInAsync for user: %+v", event)

		user, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}

		medals, err := user_m.GetUserMedalMerge(model.Log, model.Db, event.UserId)
		if err != nil {
			model.Log.Errorf("tim_m user AddGroupInAsync GetUserMedal err:%v, userId:%v", err, event.UserId)
			return err
		}

		wealthGrade, _, err := user_m.GetWealthGrade(model, event.UserId)
		if err != nil {
			return err
		}

		charmGrade, _, err := user_m.GetCharmGrade(model, event.UserId)
		if err != nil {
			return err
		}

		if err = FlushGrades(event.ExternalId, wealthGrade, charmGrade); err != nil {
			model.Log.Info("AddGroupInAsync, FlushGrades failed: ", err)
		}

		_, powerName, err := groupPower_m.GetUserGroupPower(model, event.UserId)
		if err != nil {
			return err
		}

		if err = FlushHiloInfo(user.ExternalId, event.IsVip, user.Code != user.OriginCode, medals, powerName, event.NobleLevel); err != nil {
			model.Log.Info("AddGroupInAsync, FlushHiloInfo failed: ", err)
		}
		return nil
	})
	//被踢出
	group_ev.AddGroupKickOutAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupKickOutEvent)
		if !ok {
			model.Log.Errorf("AddGroupKickOutAsync fail data")
			return nil
		}
		model.Log.Infof("publicScreenMsg AddGroupKickOutAsync GroupId:%v, OperatorExternalId:%v, MemberExternalId:%v", event.GroupId, event.OperatorExternalId, event.MemberExternalId)

		groupKickOutMsg := group_m.CommonPublicMsg{
			Type:               group_e.UserKickPublicScreenMsg,
			OperatorExternalId: event.OperatorExternalId,
			OperatorNick:       event.OperatorName,
			OperatorAvatar:     event.OperatorFaceUrl,
			ExternalId:         event.MemberExternalId,
			Nick:               event.MemberName,
			Avatar:             event.MemberAvatar,
		}
		//
		body, err := json.Marshal(groupKickOutMsg)
		if err != nil {
			return myerr.WrapErr(err)
		}

		txGroupId, err := group_m.ToTxGroupId(model, event.GroupId)
		if err != nil {
			return err
		}
		//发送公屏消息,
		u, err := tencentyun.SendCustomMsg(model.Log, txGroupId, nil, string(body), "")
		model.Log.Infof("publicScreenMsg AddGroupKickOutAsync result response.MsgSeq:%v, err:%v", u, err)
		return err
	})
}

func GroupImMass() {
	//支付群主群发IM
	group_ev.AddGroupImMassSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("diamond AddGroupImMass groupId:%v userId:%v", event.GroupId, event.UserId)
		diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetail, err := diamondAccount.GroupImMass()
		if err != nil {
			return err
		}
		return diamondAccountDetail.Persistent()
	})

	// 任务
	group_ev.AddGroupImMassAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("task AddGroupImMassAsync %+v", event)
		_, err := task_m.AddTaskUser(model, task_e.GroupImMass, event.UserId, 0)
		return err
	})

	// 麦上的人/管理员群发消息,弹窗 fixme:放在这里已经不合适了
	group_ev.AddGroupImMassAsync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.GroupImMassEvent)
		if !ok {
			model.Log.Errorf("AddGroupImMassSync data fail")
			return nil
		}
		model.Log.Infof("AddGroupImMassAsync begin groupId = %s, userId %d, members: %v, content: %s",
			event.GroupId, event.UserId, event.Members, event.Content)

		flag, err := group_m.IsHiddenGroup(model.Db, event.GroupId)
		if err != nil {
			return err
		}
		if flag {
			model.Log.Infof("AddGroupImMassAsync, skip hidden group %s", event.GroupId)
			return nil
		}

		if len(event.Members) <= 0 {
			return nil
		}
		user, err := user_m.GetUser(model, event.UserId)
		if err != nil {
			return err
		}

		//过滤用户黑名单,只要单方面拉黑就不发
		memberUserIds, err := user_m.FilterBlock(model, event.UserId, event.Members)
		if err != nil {
			return err
		}

		groupInfo, err := group_m.GetGroupInfo(model, event.GroupId)
		if err != nil {
			return err
		}

		userInCount, err := group_m.GetRoomVisitCount(event.GroupId)
		if err != nil {
			return err
		}
		if userInCount < 0 {
			userInCount = 0
		}

		// 注意发消息使用了TxGroupId
		err = rpc.SendGroupChatNotice(event.UserId, memberUserIds, user.ExternalId, user.Code, uint32(user.Sex), user.Avatar,
			event.Content, groupInfo.TxGroupId, groupInfo.Name, groupInfo.Code, groupInfo.FaceUrl, uint32(userInCount))
		model.Log.Infof("AddGroupImMassAsync, groupId = %s ended, err = %v", event.GroupId, err)
		return nil
	})

}

func GroupTheme() {
	//购买自定义主题
	group_ev.AddBuyGroupCustomThemeSync(func(model *domain.Model, e interface{}) error {
		event, ok := e.(*group_ev.BuyGroupCustomThemeEvent)
		if !ok {
			model.Log.Errorf("AddBuyGroupCustomThemeSync data fail")
			return nil
		}
		model.Log.Infof("diamond AddBuyGroupCustomTheme userId:%v, GroupCustomThemeId:%v", event.UserId, event.GroupCustomThemeId)
		diamondAccount, err := diamond_m.GetDiamondAccountByUserId(model, event.UserId)
		if err != nil {
			return err
		}
		diamondAccountDetail, err := diamondAccount.BuyGroupCustomTheme(event.GroupCustomThemeId)
		if err != nil {
			return err
		}
		if err := diamondAccountDetail.Persistent(); err != nil {
			return err
		}
		return nil
	})
}

func FlushGrades(userExtId string, wealthGrade uint32, charmGrade uint32) error {
	level := (charmGrade & 0x000000FF << 8) | wealthGrade&0x000000FF

	return tencentyun.SetUserLevel(userExtId, level)
}

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

func FlushHiloInfo(extId string, isVip bool, isPrettyCode bool, medals []uint32, groupPowerName string, nobleLevel uint16) error {
	info := TimHiloInfo{IsVip: isVip, IsPretty: isPrettyCode, Medals: medals, PowerName: groupPowerName, NobleLevel: nobleLevel}
	buf, err := json.Marshal(info)
	if err != nil {
		return err
	}

	if err = tencentyun.SetUserHiloInfo(extId, string(buf)); err != nil {
		return err
	}
	return nil
}
hujiebin's avatar
hujiebin committed
400 401

func SendGift() {
hujiebin's avatar
hujiebin committed
402 403
	// 送礼事件-势力经验
	gift_ev.AddSendGiftEventSync(func(model *domain.Model, event interface{}) error {
hujiebin's avatar
hujiebin committed
404 405 406 407 408 409
		sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
		if !ok {
			model.Log.Errorf("AddSendGiftEventAsync event type err")
			return nil
		}
		model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent)
hujiebin's avatar
hujiebin committed
410 411 412 413 414 415
		exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId)
		if err != nil {
			model.Log.Infof("CheckGroupPowerUser fail %+v", err)
			return err
		}
		if exist {
hujiebin's avatar
hujiebin committed
416 417
			exp := sendGiftEvent.GiftN * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) * sendGiftEvent.ResGift.DiamondNum
			return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼")
hujiebin's avatar
hujiebin committed
418
		}
hujiebin's avatar
hujiebin committed
419 420
		return nil
	})
hujiebin's avatar
hujiebin committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	// 家族之星-送礼/收礼
	gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error {
		sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent)
		if !ok {
			model.Log.Errorf("AddSendGiftEventAsync event type err")
			return nil
		}
		var userIds = []mysql.ID{sendGiftEvent.SendUserId}
		userIds = append(userIds, sendGiftEvent.ReceiveUserIds...)
		groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds)
		if err != nil {
			model.Log.Errorf("AddSendGiftEventAsync fail:%v", err)
			return err
		}
		// 送礼加分
		if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok {
			diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds))
			if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds); err != nil {
				model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err)
			}
		}
		// 收礼加分
		for _, userId := range sendGiftEvent.ReceiveUserIds {
			if data, ok := groupPowers[userId]; ok {
				diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
				if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds); err != nil {
					model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err)
				}
			}
		}
		return nil
	})
hujiebin's avatar
hujiebin committed
453
}