event_init.go 3.71 KB
Newer Older
chenweijian's avatar
chenweijian 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
package event_s

import (
	"errors"
	"hilo-user/_const/enum/user_e"
	"hilo-user/domain"
	"hilo-user/domain/event/group_ev"
	"hilo-user/domain/event/user_ev"
	"hilo-user/domain/model/groupPower_m"
	"hilo-user/domain/model/group_m"
	"hilo-user/domain/model/user_m"
	"hilo-user/domain/service/user_s"
	"hilo-user/mycontext"
	"hilo-user/req/jwt"
	"hilo-user/resource/config"
	"hilo-user/resource/mysql"
)

func EventInit() {
	GroupInEvents()
	GameEventInit()
	GroupLeaveEvents()
	ModelFuncInit()
}

// 用户进房事件
func GroupInEvents() {
	group_ev.AddGroupInEventSync(func(model *domain.Model, event interface{}) error {
		e, ok := event.(*group_ev.GroupInEvent)
		if !ok {
			model.Log.Errorf("AddGroupInEventSync event type err")
			return nil
		}
		txGroupId, err := group_m.ToTxGroupId(model, e.GroupId)
		if err != nil {
			model.Log.Errorf("AddGroupInEventSync err:%v, imGroupId:%v", err, e.GroupId)
			return nil
		}
		return user_s.NewGameService(mycontext.CreateMyContext(model.Cxt)).PushGameInfo("", e.ExternalId, txGroupId, 0)
	})
}

// 用户退房事件
func GroupLeaveEvents() {
	group_ev.AddGroupLeaveEventAsync(func(model *domain.Model, event interface{}) error {
		e, ok := event.(*group_ev.GroupLeaveEvent)
		if !ok || e == nil {
			model.Log.Errorf("AddGroupLeaveEventAsync event type err")
			return nil
		}
		txGroupId, err := group_m.ToTxGroupId(model, e.GroupId)
		if err != nil {
			model.Log.Errorf("ToTxGroupId fail:%v-%v", e.GroupId, err)
		}
		if err := user_s.NewGameService(model.MyContext).GameOpt(e.UserId, 0, "", e.ExternalId, "", "", txGroupId, user_e.GameOptExit, 0, false); err != nil {
			model.Log.Warnf("AddGroupLeaveEventAsync GameOpt fail,e%v,err:%v", *e, err)
		}
		return nil
	})
}

// 游戏上报事件
func GameEventInit() {
	user_ev.AddReportGameInfoEventSync(func(model *domain.Model, event interface{}) error {
		e, ok := event.(*user_ev.ReportGameInfoEvent)
		if !ok {
			model.Log.Errorf("AddReportGameInfoEventSync 消息类型错误!event:%+v", event)
			return nil
		}
		switch e.ReportType {
		case user_e.ReportTypeGameStart:
			// 更新游戏信息
			return user_s.NewGameService(model.MyContext).GameStart(e.GameStartObject)
			//return user_m.GameStartUpdate(model, e.GameStartObject)
		case user_e.ReportTypeGameSettle:
			return user_s.NewGameService(model.MyContext).GameSettle(e.GameSettleObject)
		}
		return nil
	})
}

func ModelFuncInit() {
	group_m.GetGroupPowerNameByUserId = func(model *domain.Model, userId uint64) (uint64, string, error) {
		return groupPower_m.GetUserGroupPower(model, userId)
	}
	// 编辑游戏-清理机器人
	user_ev.AddGameEditEventAsync(func(model *domain.Model, event interface{}) error {
		e, ok := event.(*user_ev.GameEditEvent)
		if !ok {
			return errors.New("GameEditEvent asset fail")
		}
		robots, err := user_m.GetRoomRobots(model, e.TxGroupId)
		if err != nil {
			model.Log.Errorf("GetRoomRobots fail:%v", err)
			return err
		}
		model.Log.Infof("AddGameEditEventAsync event:%v,robots:%v", *e, robots)
		var userIds []mysql.ID
		for _, robot := range robots {
			userIds = append(userIds, robot.UserId)
		}
		users, err := user_m.GetUserMapByIds(model, userIds)
		if err != nil {
			model.Log.Errorf("GetUserMapByIds fail:%v", err)
			return err
		}
		userService := user_s.NewGameService(model.MyContext)
		for i, robot := range robots {
			user, ok := users[robot.UserId]
			if !ok {
				model.Log.Errorf("robot userId not exits:%v", robot)
				continue
			}
			// filled
			robots[i].GameOpt = userService.GameOpt // service func -> model func
			robots[i].User = users[robot.UserId]    // user info
			robots[i].Token, _ = jwt.GenerateToken(user.ID, user.ExternalId,
				config.GetConfigJWT().ISSUER_API) // jwt
			// 离开
			robots[i].Leave("GameEdit")
		}
		return nil
	})
}