activity.go 2.75 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
package rpc

import (
	"encoding/json"
	"errors"
	"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"
	"git.hilo.cn/hilo-common/utils"
	"github.com/hashicorp/consul/api"
	"math/rand"
)

const (
	defaultActivityConsulName   = "hiloActivity"
	defaultActivityServerScheme = "http"
	defaultActivityServerAddr   = "127.0.0.1:9010" // 默认内网转发,本地回环
)

var activityServerHost = []string{defaultActivityServerAddr}

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[defaultActivityConsulName]; ok {
				healthAddrs, _ := statusAddrs[api.HealthPassing]
				l := len(healthAddrs)
				if l > 0 {
					mylogrus.MyLog.Infof("consul service update state:%v-%v", defaultActivityConsulName, healthAddrs)
					activityServerHost = healthAddrs
				} else {
					mylogrus.MyLog.Warnf("consul service update local state:%v-%v", defaultActivityConsulName, defaultActivityServerAddr)
					activityServerHost = []string{defaultActivityServerAddr} // 有其他问题都用默认的
				}
				for status := range statusAddrs {
					if status == api.HealthPassing {
						continue
					}
					mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", defaultActivityConsulName, status, statusAddrs[status])
				}
			}
		}); err != nil {
			mylogrus.MyLog.Errorf("启动 consul 的watch监控失败")
		}
	}()
}

// 活动积分增加 fType: 1.上麦 2.ludo游戏完成 3.slots游戏完成 4.fruit游戏完成 5.特定座驾进入房间
chenweijian's avatar
chenweijian committed
52
func AddActPoint(model *domain.Model, userId mysql.ID, fType mysql.Type, roomId int64) error {
chenweijian's avatar
chenweijian committed
53 54 55 56 57 58 59 60 61
	defer utils.CheckGoPanic()
	type Response struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
	}
	_url := fmt.Sprintf("%v://%v/inner/act/addPoint", defaultActivityServerScheme, getActivityHost())
	resp, err := HttpPostForm(model, _url, nil, map[string]string{
		"userId": fmt.Sprintf("%d", userId),
		"fType":  fmt.Sprintf("%d", fType),
chenweijian's avatar
chenweijian committed
62
		"roomId": fmt.Sprintf("%d", roomId),
chenweijian's avatar
chenweijian committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	})
	if err != nil {
		model.Log.Errorf("AddActPoint fail:%v", err)
		return err
	}
	response := new(Response)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("AddActPoint json fail:%v", err)
		return err
	}
	if response.Code != 200 {
		model.Log.Errorf("AddActPoint fail:%v", *response)
		return errors.New(response.Message)
	}
	return nil
}

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