group.go 3.83 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package rpc

import (
	"encoding/json"
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/consul"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/hashicorp/consul/api"
	"math/rand"
)

const (
	defaultGroupConsulName   = "hiloGroup"
	defaultGroupServerScheme = "http"
hujiebin's avatar
hujiebin committed
17
	defaultGroupServerAddr   = "127.0.0.1:9050" // 默认内网转发,本地回环
hujiebin's avatar
hujiebin committed
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
)

var groupServerHost = []string{defaultGroupServerAddr}

func init() {
	go func() {
		address := api.DefaultConfig().Address // 用consul api的default config
		if err := consul.RegisterWatcher("services", nil, address, func(serviceStatus map[string]map[string][]string) {
			if statusAddrs, ok := serviceStatus[defaultGroupConsulName]; ok {
				healthAddrs, _ := statusAddrs[api.HealthPassing]
				l := len(healthAddrs)
				if l > 0 {
					mylogrus.MyLog.Infof("consul service update state:%v-%v", defaultGroupConsulName, healthAddrs)
					groupServerHost = healthAddrs
				} else {
					mylogrus.MyLog.Warnf("consul service update local state:%v-%v", defaultFinanceConsulName, defaultFinanceServerAddr)
					groupServerHost = []string{defaultGroupServerAddr} // 有其他问题都用默认的
				}
				for status := range statusAddrs {
					if status == api.HealthPassing {
						continue
					}
					mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", defaultGroupConsulName, status, statusAddrs[status])
				}
			}
		}); err != nil {
			mylogrus.MyLog.Errorf("启动 consul 的watch监控失败")
		}
	}()
}

// 家族信息
type CvGroupPowerInfo struct {
	CvGroupPowerBase   `json:",inline"`
	CvGroupPowerMember `json:",inline"`
hujiebin's avatar
hujiebin committed
53
	CvGroupPowerGrade  `json:",inline"`
hujiebin's avatar
hujiebin committed
54 55 56 57
}

// 家族基本信息
type CvGroupPowerBase struct {
hujiebin's avatar
hujiebin committed
58 59 60 61
	Id        mysql.ID `json:"id"`        // 家族id
	Icon      string   `json:"icon"`      // 家族图片
	Name      string   `json:"name"`      // 家族名
	Nameplate string   `json:"nameplate"` // 铭牌
hujiebin's avatar
hujiebin committed
62 63 64 65
}

// 家族成员
type CvGroupPowerMember struct {
hujiebin's avatar
hujiebin committed
66 67
	MemberNum mysql.Num `json:"memberNum"` // 当前成员数
	MemberMax mysql.Num `json:"memberMax"` // 成员上限
hujiebin's avatar
hujiebin committed
68 69
}

hujiebin's avatar
hujiebin committed
70 71 72 73 74 75 76 77 78
// 家族等级
type CvGroupPowerGrade struct {
	Grade    int    `json:"grade"`              // 等级 0:无 1:青铜 2:白银 3:黄金 4:黑金
	Exp      uint32 `json:"exp"`                // 经验值
	NextExp  uint32 `json:"nextExp,omitempty"`  // 升级所需经验值
	ExpireAt string `json:"expireAt,omitempty"` // 有效期
	ShowExp  bool   `json:"showExp"`            // 是否展示经验值
}

hujiebin's avatar
hujiebin committed
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
// 获取家族
func GetGroupPower(model *domain.Model, groupPowerId mysql.ID) (CvGroupPowerInfo, error) {
	infos, err := MGetGroupPower(model, []mysql.ID{groupPowerId})
	if err != nil {
		return CvGroupPowerInfo{}, nil
	}
	return infos[groupPowerId], nil
}

// 批量获取家族
func MGetGroupPower(model *domain.Model, groupPowerIds []mysql.ID) (map[mysql.ID]CvGroupPowerInfo, error) {
	type Response struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Data    map[mysql.ID]CvGroupPowerInfo
	}
	var res = make(map[mysql.ID]CvGroupPowerInfo)
	if len(groupPowerIds) <= 0 {
		return res, nil
	}
	var idsStr []string
	for _, userId := range groupPowerIds {
		idsStr = append(idsStr, fmt.Sprintf("%d", userId))
	}
	_url := fmt.Sprintf("%v://%v/inner/groupPower/infos", defaultGroupServerScheme, getGroupHost())
	resp, err := HttpGet(model, _url, nil, map[string][]string{
		"ids": idsStr,
	})
	if err != nil {
		model.Log.Errorf("MGetGroupPower fail:%v", err)
		return res, err
	}
	response := new(Response)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("MGetUserSvip json fail:%v", err)
		return res, err
	}
	res = response.Data
	return res, nil
}

func getGroupHost() string {
	l := len(groupServerHost)
	r := rand.Intn(l) // 随机一个
	mylogrus.MyLog.Infof("getHostGroup:%v---%v", r, groupServerHost[r])
	return groupServerHost[r]
}