game.go 1.6 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package game_c

import (
	"context"
	"encoding/json"
chenweijian's avatar
chenweijian committed
6
	"git.hilo.cn/hilo-common/mylogrus"
hujiebin's avatar
hujiebin committed
7 8 9 10 11 12
	"git.hilo.cn/hilo-common/resource/redisCli"
	"hilo-group/_const/enum/game_e"
	"time"
)

type gameAutoJoinMsg struct {
chenweijian's avatar
chenweijian committed
13 14 15 16 17 18 19
	TraceId    string
	Token      string
	EnterType  string
	GameCode   string
	Is1V1      string
	GameMode   string
	Is1V1Robot string
hujiebin's avatar
hujiebin committed
20 21
}

chenweijian's avatar
chenweijian committed
22
func SetAutoMathEnterRoom(userId uint64, imGroupId, traceId, token, enterType, gameCode, is1V1, gameMode, is1V1Robot string) error {
hujiebin's avatar
hujiebin committed
23
	key := game_e.GetAutoMathEnterRoom(userId, imGroupId)
chenweijian's avatar
chenweijian committed
24
	mylogrus.MyLog.Infof("Start1V1AfterEnterRoom is1V1Robot:%s, userId:%d, imGroupId:%v", is1V1Robot, userId, imGroupId)
chenweijian's avatar
chenweijian committed
25
	info := gameAutoJoinMsg{traceId, token, enterType, gameCode, is1V1, gameMode, is1V1Robot}
hujiebin's avatar
hujiebin committed
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
	data, err := json.Marshal(info)
	if err != nil {
		return err
	}
	err = redisCli.GetRedis().LPush(context.Background(), key, data).Err()
	if err != nil {
		return err
	}
	redisCli.GetRedis().Expire(context.Background(), key, time.Second*3)
	return nil
}

func IsAutoMathEnterRoom(userId uint64, imGroupId string) (bool, string, string, string, string) {
	key := game_e.GetAutoMathEnterRoom(userId, imGroupId)
	data, err := redisCli.GetRedis().RPop(context.Background(), key).Bytes()
	if err != nil {
		return false, "", "", "", ""
	}
	info := gameAutoJoinMsg{}
	err = json.Unmarshal(data, &info)
	if err != nil {
		return false, "", "", "", ""
	}
	if info.Token != "" && info.TraceId != "" && info.EnterType != "" && info.GameCode != "" {
		redisCli.GetRedis().Del(context.Background(), key)
		return true, info.TraceId, info.Token, info.EnterType, info.GameCode
	}
	return false, "", "", "", ""
}