logic.go 3.35 KB
package roomrank

import (
	"apigame/configs/confroomrank"
	"apigame/service-common/svconst"
	"apigame/util/util-lx/lxtime"
	"apigame/util/utstring"
	"apigame/util/ztime"
	"fmt"
	"time"
)

func getLockKey(gameId string, topType int) string {
	return fmt.Sprintf("%s:lock:roomrank:%s:%d", svconst.REDIS_CACHEP_REFIX, gameId, topType)
}

// GetActivityTime 根据当前时间获取当前次的结算时间戳
func GetActivityTime(config *confroomrank.ActivityConfig) int64 {
	dtNow := time.Now()
	nowYear, nowMonth, nowDay := dtNow.Date()
	hour, min, sec := ztime.TimeClock(config.ReleaseTime)
	dt := time.Date(nowYear, nowMonth, nowDay, hour, min, sec, 0, time.Local)
	if dtNow.After(dt) {
		dt = dt.AddDate(0, 0, 1)
	}
	dtYear, dtMonth, dtDay := dt.Date()
	activityTime := int64(dtYear)*1_0000 + int64(dtMonth)*100 + int64(dtDay)
	return activityTime
}

func ActivityTimeToDate(activityTime int64) (year int, month time.Month, day int) {
	year = int(activityTime / 1_0000)
	month = time.Month((activityTime % 1_0000) / 100)
	day = int(activityTime % 100)
	return
}

func GetTimeEnd(activityTime int64, releaseTime string) time.Time {
	dtYear, dtMonth, dtDay := ActivityTimeToDate(activityTime)
	hour, min, sec := ztime.TimeClock(releaseTime)
	dt := time.Date(dtYear, dtMonth, dtDay, hour, min, sec, 0, time.Local)
	return dt
}

// TrySettle 尝试判断结算
func TrySettle(gameId string, topType int, player *Player) (hasChange bool) {
	hasChange = false
	if player.ActivityId == 0 {
		return
	}

	if player.SettleHas() {
		return
	}

	confActivity, hasConfActivity := confroomrank.GetConfig(gameId, player.ActivityId)
	if !hasConfActivity {
		return
	}
	activityTime := GetActivityTime(confActivity)

	if player.SettleActivityId == confActivity.Id && player.SettleActivityTime == activityTime {
		return
	}

	// 查找玩家所在的房间
	room, hasRoom := TryGetRoom(gameId, topType, player, confActivity)
	if !hasRoom {
		return
	}

	TrySettleRoom(gameId, room, confActivity)

	if !room.HasSettle {
		return
	}

	// 找到玩家在房间里的名次
	// rankIndex = 0-49  rank = 1-50
	rankIndex, rankScore := room.FindPlayer(player.Uid)
	if rankIndex < 0 {
		return
	}
	player.SettleRank = rankIndex + 1
	player.SettleScore = rankScore
	// 设置玩家奖励等数据
	hasChange = true
	player.SettleActivityId = confActivity.Id
	player.SettleActivityTime = activityTime
	confRoom, hasConfRoom := confActivity.Room[room.ConfigId]
	if !hasConfRoom {
		return
	}
	if confAward, hasConfAward := confRoom.Awards[utstring.IntToString(player.SettleRank)]; hasConfAward {
		player.SettleAward = confAward
	}
	if len(confRoom.SettleScores) > rankIndex {
		player.AddUserScore(confRoom.SettleScores[rankIndex])
		player.SettleUserClass = player.UserClass
	}
	if len(confRoom.SettleUserType) > rankIndex {
		player.UserType = confRoom.SettleUserType[rankIndex]
	}
	// 记录日志
	recordBase := NewRecordBase(player.Uid, topType, confActivity.Id)
	SaveRecordAddSettleAward(gameId, NewRecordAddSettleAward(recordBase, player.SettleAward, player.SettleUserClass, player.UserType, room.Id))
	return
}

// UpdatePlayerScore 房间排行更新积分
func UpdatePlayerScore(room *Room, player *Player) {
	for i := 0; i < len(room.Details.Players); i++ {
		p := room.Details.Players[i]
		if player.Uid == p.Uid {
			p.Name = player.Name
			p.Icon = player.Icon
			p.Score = player.Score
			p.JoinTime = lxtime.NowUninx()
			return
		}
	}
}