game.go 3.05 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6
package game_m

import (
	"git.hilo.cn/hilo-common/domain"
	"gorm.io/gorm"
	"hilo-group/_const/enum/game_e"
7
	"hilo-group/myerr"
hujiebin's avatar
hujiebin committed
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
	"time"
)

type GameInfo struct {
	Id             uint64          `json:"id"`
	MgId           string          `json:"mg_id"`
	GameType       game_e.GameType `json:"game_type"`
	Mode           int32           `json:"mode"`
	Piece          int32           `json:"piece"`
	OnOff1         uint8           `json:"on_off1"`
	Diamond        uint64          `json:"diamond"`
	CreateId       uint64          `json:"create_id"`
	Status         uint8           `json:"status"` // '0.未开始 1.游戏中 2.结束'
	TxGroupId      string          `json:"tx_group_id"`
	GameRoundId    string          `json:"game_round_id"`
	BattleStartAt  uint32          `json:"battle_start_at"`
	BattleEndAt    uint32          `json:"battle_end_at"`
	BattleDuration uint32          `json:"battle_duration"`
	AutoMatch      uint8           `json:"auto_match"` // 是否开启自动匹配,0否1是
	CreatedTime    time.Time       `json:"created_time"`
	UpdatedTime    time.Time       `json:"updated_time"`
}

type GameConfig struct {
	Ludo *GameConfigDiamond `json:"ludo"`
	Uno  *GameConfigDiamond `json:"uno"`
}

type GameConfigDiamond struct {
	Diamond []uint32 `json:"diamond"`
}

func GetGameInfo(model *domain.Model, gameType game_e.GameType, gameId uint64, txGroupId, roundId string, battleEndAt, status int64) (*GameInfo, error) {
	res := new(GameInfo)
	db := model.Db
	if gameType != 0 {
		db = db.Where("game_type = ?", gameType)
	}
	if gameId != 0 {
		db = db.Where("id = ?", gameId)
	}
	if txGroupId != "" {
		db = db.Where("tx_group_id = ?", txGroupId)
	}
	if roundId != "" {
		db = db.Where("game_round_id = ?", roundId)
	}
	if battleEndAt != -1 {
		db = db.Where("battle_end_at = ?", battleEndAt)
	}
	if status != -1 {
		db = db.Where("status = ?", status)
	}
	err := db.First(&res).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return res, nil
}

// 获取所有未结束的游戏
// return map $txGroupId->[]GameTypes
func GetNotEndGamesMap(model *domain.Model) map[string][]game_e.GameType {
	var res = make(map[string][]game_e.GameType)
	var games []GameInfo
	if err := model.Db.Model(GameInfo{}).Where("status in (0,1)").Find(&games).Error; err != nil {
		model.Log.Errorf("GetNotEndGamesMap fail:%v", err)
		return res
	}
	for _, game := range games {
		res[game.TxGroupId] = []game_e.GameType{game.GameType}
	}
	return res
}
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

func IsGaming(model *domain.Model, userId uint64, txGroupId string) (bool, error) {
	ids := make([]uint64, 0)
	err := model.DB().Table("game_info").Where("tx_group_id = ? and battle_end_at = 0 and status in (0, 1)", txGroupId).Limit(1).Pluck("id", &ids).Error
	if err != nil {
		return false, myerr.WrapErr(err)
	}
	if len(ids) > 0 {
		userIds := make([]uint64, 0)
		err = model.DB().Table("game_player").Where("game_id = ? and user_id = ? and status in (0,1)", ids[0], userId).Limit(1).Pluck("id", &userIds).Error
		if err != nil {
			return false, myerr.WrapErr(err)
		}
		if len(userIds) > 0 {
			return true, nil
		}
	}
	return false, nil
}