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
| // services/activity/ActivityTemplates.ts
class ActivityTemplateManager {
private templates = new Map<string, ActivityTemplate>();
async loadTemplates(templatesDir: string): Promise<void> {
const files = await fs.readdir(templatesDir);
for (const file of files) {
if (file.endsWith('.json')) {
const content = await fs.readFile(
path.join(templatesDir, file),
'utf-8'
);
const template = JSON.parse(content);
this.templates.set(template.id, template);
}
}
}
// 使用模板创建活动
createActivityFromTemplate(
templateId: string,
params: Record<string, any>
): ActivityConfig {
const template = this.templates.get(templateId);
if (!template) {
throw new Error(`Template not found: ${templateId}`);
}
// 填充参数
const config: ActivityConfig = {
id: this.generateId(),
name: this.fillTemplate(template.name, params),
type: template.type,
startTime: new Date(params.startTime),
endTime: new Date(params.endTime),
rewards: this.fillRewards(template.rewards, params),
conditions: this.fillConditions(template.conditions, params),
schedule: params.schedule
};
return config;
}
private fillTemplate(template: string, params: Record<string, any>): string {
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
return params[key] || match;
});
}
private fillRewards(
template: Reward[],
params: Record<string, any>
): Reward[] {
return template.map(reward => ({
...reward,
count: this.evaluateExpression(reward.count, params)
}));
}
// 常用活动模板
getBuiltinTemplates(): ActivityTemplate[] {
return [
{
id: 'daily_login',
name: '每日签到',
type: 'login',
description: '玩家每日登录即可获得奖励',
rewards: [
{ itemId: 'gold', count: 100 },
{ itemId: 'gem', count: 10 }
],
conditions: [],
schedule: {
type: 'daily',
time: '00:00'
}
},
{
id: 'cumulative_login',
name: '累计登录',
type: 'login',
description: '累计登录天数达到要求获得奖励',
rewards: [
{ itemId: 'gem', count: 50 },
{ itemId: 'rare_item', count: 1 }
],
conditions: [
{ type: 'cumulative_login', days: 7 }
]
},
{
id: 'first_purchase',
name: '首充礼包',
type: 'purchase',
description: '首次充值购买特定档位',
rewards: [
{ itemId: 'legendary_weapon', count: 1 },
{ itemId: 'gem', count: 1000 }
],
conditions: [
{ type: 'purchase_count', count: 0 },
{ type: 'purchase_amount', minAmount: 6 }
]
},
{
id: 'consumption_return',
name: '消费返利',
type: 'purchase',
description: '累计消费达到指定金额返利',
rewards: [
{ itemId: 'gem', count: 500 }
],
conditions: [
{ type: 'total_purchase', minAmount: 100 }
]
},
{
id: 'battle_pass',
name: '战斗通行证',
type: 'battle',
description: '完成战斗任务获得积分',
rewards: [
{ itemId: 'exp', count: 1000 },
{ itemId: 'gold', count: 500 }
],
conditions: [
{ type: 'battle_win', count: 10 }
]
},
{
id: 'social_share',
name: '社交分享',
type: 'social',
description: '分享游戏获得奖励',
rewards: [
{ itemId: 'gem', count: 20 }
],
conditions: [
{ type: 'share', platform: 'wechat' }
]
}
];
}
}
|