-
Notifications
You must be signed in to change notification settings - Fork 4
/
activity_default.go
203 lines (189 loc) · 6.36 KB
/
activity_default.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package game
import (
"github.com/fish-tennis/gentity/util"
"github.com/fish-tennis/gserver/cfg"
. "github.com/fish-tennis/gserver/internal"
"github.com/fish-tennis/gserver/logger"
"github.com/fish-tennis/gserver/pb"
"time"
)
func init() {
// 自动注册默认活动模板构造函数
_activityTemplateCtorMap["default"] = func(activities ActivityMgr, activityCfg *cfg.ActivityCfg, args interface{}) Activity {
t := args.(time.Time)
newActivity := &ActivityDefault{
Base: &pb.ActivityDefaultBaseData{
JoinTime: int32(t.Unix()),
},
}
newActivity.Id = activityCfg.CfgId
newActivity.Activities = activities.(*Activities)
newActivity.Reset()
return newActivity
}
}
// 默认活动模板,支持常见的简单活动
type ActivityDefault struct {
ChildActivity
// 基础数据
Base *pb.ActivityDefaultBaseData `db:"Base"`
}
func (this *ActivityDefault) getProgress(cfgId int32) *pb.ActivityProgressData {
if this.Base.Progresses == nil {
return nil
}
return this.Base.Progresses[cfgId]
}
// 添加一个活动进度
func (this *ActivityDefault) addProgress(questCfg *cfg.QuestCfg) *pb.ActivityProgressData {
if !cfg.GetActivityCfgMgr().GetConditionMgr().CheckConditions(&ActivityConditionArg{
Activities: this.Activities,
Activity: this,
}, questCfg.Conditions) {
return nil
}
if this.Base.Progresses == nil {
this.Base.Progresses = make(map[int32]*pb.ActivityProgressData)
}
progress := &pb.ActivityProgressData{
CfgId: questCfg.CfgId,
}
cfg.GetActivityCfgMgr().GetProgressMgr().InitProgress(this.Activities.GetPlayer(), questCfg.ProgressCfg, progress)
this.Base.Progresses[progress.CfgId] = progress
this.SetDirty()
return progress
}
func (this *ActivityDefault) OnEvent(event interface{}) {
activityCfg := this.GetActivityCfg()
if activityCfg == nil {
return
}
switch e := event.(type) {
case *EventDateChange:
this.OnDateChange(e.OldDate, e.CurDate)
return
}
for _, questCfg := range activityCfg.Quests {
progress := this.getProgress(questCfg.CfgId)
if progress == nil {
progress = this.addProgress(questCfg)
if progress == nil {
continue
}
}
// 检查进度更新
if cfg.GetActivityCfgMgr().GetProgressMgr().CheckProgress(event, questCfg.ProgressCfg, progress) {
this.SetDirty()
logger.Debug("Activity %v progress:%v", this.GetId(), progress.GetProgress())
}
}
}
func (this *ActivityDefault) OnDateChange(oldDate time.Time, curDate time.Time) {
activityCfg := this.GetActivityCfg()
if activityCfg == nil {
logger.Debug("%v OnDateChange activityCfg nil %v", this.Activities.GetPlayer().GetId(), this.GetId())
return
}
// 每日刷新
if activityCfg.RefreshType == int32(pb.RefreshType_RefreshType_Day) {
this.Reset()
}
}
// 重置数据
func (this *ActivityDefault) Reset() {
this.Base.Progresses = nil
activityCfg := this.GetActivityCfg()
for _, questCfg := range activityCfg.Quests {
this.addProgress(questCfg)
}
this.Base.ExchangeRecord = nil
this.SetDirty()
logger.Debug("%v Reset %v", this.Activities.GetPlayer().GetId(), this.GetId())
}
func (this *ActivityDefault) OnEnd(t time.Time) {
activityCfg := this.GetActivityCfg()
if activityCfg.RemoveDataWhenEnd {
this.Activities.RemoveActivity(this.GetId())
}
}
// 领取活动任务奖励
func (this *ActivityDefault) ReceiveReward(cfgId int32) {
activityCfg := this.GetActivityCfg()
if activityCfg == nil {
logger.Debug("%v ReceiveRewards activityCfg nil %v", this.Activities.GetPlayer().GetId(), this.GetId())
return
}
progress := this.getProgress(cfgId)
if progress == nil {
logger.Debug("%v ReceiveReward progress nil %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), cfgId)
return
}
if progress.IsReceiveReward {
logger.Debug("%v ReceiveReward repeat %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), cfgId)
return
}
questCfg := activityCfg.GetQuestCfg(cfgId)
if questCfg == nil {
logger.Debug("%v ReceiveReward questCfg nil %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), cfgId)
return
}
if progress.Progress < questCfg.ProgressCfg.GetTotal() {
logger.Debug("%v ReceiveReward progress err %v %v (%v < %v)", this.Activities.GetPlayer().GetId(), this.GetId(), cfgId,
progress.Progress, questCfg.ProgressCfg.GetTotal())
return
}
progress.IsReceiveReward = true
this.Activities.GetPlayer().GetBag().AddItems(questCfg.GetRewards())
this.SetDirty()
logger.Debug("%v ReceiveReward %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), cfgId)
}
// 兑换物品
// 商店也是一种兑换功能
func (this *ActivityDefault) Exchange(exchangeCfgId int32) {
activityCfg := this.GetActivityCfg()
if activityCfg == nil {
logger.Debug("%v Exchange activityCfg nil %v", this.Activities.GetPlayer().GetId(), this.GetId())
return
}
exchangeCfg := activityCfg.GetExchangeCfg(exchangeCfgId)
if exchangeCfg == nil {
logger.Debug("%v Exchange exchangeCfg nil %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), exchangeCfgId)
return
}
exchangeCount := this.getExchangeCount(exchangeCfgId)
if exchangeCount >= exchangeCfg.CountLimit {
logger.Debug("%v Exchange CountLimit nil %v %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), exchangeCfgId, exchangeCount)
return
}
if !this.Activities.GetPlayer().GetBag().IsEnough(exchangeCfg.ConsumeItems) {
logger.Debug("%v Exchange ConsumeItems notEnough %v %v", this.Activities.GetPlayer().GetId(), this.GetId(), exchangeCfgId)
return
}
this.addExchangeCount(exchangeCfgId, 1)
this.Activities.GetPlayer().GetBag().DelItems(exchangeCfg.ConsumeItems)
this.Activities.GetPlayer().GetBag().AddItems(exchangeCfg.Rewards)
}
func (this *ActivityDefault) getExchangeCount(exchangeCfgId int32) int32 {
if this.Base.ExchangeRecord == nil {
return 0
}
return this.Base.ExchangeRecord[exchangeCfgId]
}
func (this *ActivityDefault) addExchangeCount(exchangeCfgId, exchangeCount int32) {
if this.Base.ExchangeRecord == nil {
this.Base.ExchangeRecord = make(map[int32]int32)
}
this.Base.ExchangeRecord[exchangeCfgId] += exchangeCount
this.SetDirty()
}
func (this *ActivityDefault) GetPropertyInt32(propertyName string) int32 {
switch propertyName {
case "DayCount":
// 当前是参加这个活动的第几天,从1开始
days := util.DayCount(this.Activities.GetPlayer().GetTimerEntries().Now(), time.Unix(int64(this.Base.JoinTime), 0))
return int32(days) + 1
default:
logger.Error("Not support property %v %v", this.GetId(), propertyName)
}
return 0
}