logic.go
3.49 KB
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
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
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
126
127
128
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, activityId int64) string {
return fmt.Sprintf("%s:lock:roomrank:%s:%d:%d", svconst.REDIS_CACHEP_REFIX, gameId, topType, activityId)
}
// 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, config *confroomrank.ActivityConfig, activityTime int64) (hasChange bool) {
hasChange = false
if player.ActivityId == 0 {
return
}
if player.SettleActivityId == config.Id && player.SettleActivityTime == activityTime {
return
}
//if player.ActivityId == config.Id && player.ActivityTime == activityTime {
// return
//}
//if player.ActivityId == 0 {
// return
//}
//if player.ActivityId == config.Id {
// return
//}
//fmt.Println(zjson.Str(player))
// 查找玩家所在的房间
room, hasRoom := LoadRoom(gameId, topType, player.ActivityId, player.RoomUid)
if !hasRoom {
return
}
confActivity, hasConfActivity := confroomrank.GetConfig(gameId, room.ActivityId)
if !hasConfActivity {
return
}
if hasRoom {
if confRoom, hasConfRoom := config.Room[room.ConfigId]; hasConfRoom {
TryCloseRoom(gameId, room, config, confRoom)
}
}
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 = config.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]
}
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
}
}
}