logic.go
3.66 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
129
130
131
132
133
134
135
136
137
package roomrank
import (
"apigame/configs/confroomrank"
"apigame/service-common/svconst"
"apigame/util/util-lx/lxtime"
"apigame/util/zconvert"
"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
}
func GetUserClassMax(config *confroomrank.ActivityConfig) int {
c := 0
for _, roomConfig := range config.Room {
if roomConfig.UserClass > c {
c = roomConfig.UserClass
}
}
if c < 1 {
c = 1
}
return c
}
// 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[zconvert.IntToStr(player.SettleRank)]; hasConfAward {
player.SettleAward = confAward
}
if len(confRoom.SettleScores) > rankIndex {
userClassMax := GetUserClassMax(confActivity)
player.AddUserScore(confRoom.SettleScores[rankIndex], userClassMax)
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, player.SettleRank, player.SettleScore, 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
}
}
}