BattlePassManager.java 9.22 KB
Newer Older
1
2
package emu.grasscutter.game.battlepass;

3
4
5
6
7
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

8
9
10
11
12
13
import org.bson.types.ObjectId;

import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.Transient;
14
15
16
17
18
import emu.grasscutter.GameConstants;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.BattlePassRewardData;
import emu.grasscutter.data.excels.RewardData;
19
import emu.grasscutter.database.DatabaseHelper;
20
import emu.grasscutter.game.player.Player;
21
import emu.grasscutter.game.props.ActionReason;
22
import emu.grasscutter.game.props.BattlePassMissionRefreshType;
23
24
25
import emu.grasscutter.game.props.BattlePassMissionStatus;
import emu.grasscutter.game.props.WatcherTriggerType;
import emu.grasscutter.net.proto.BattlePassCycleOuterClass.BattlePassCycle;
26
import emu.grasscutter.net.proto.BattlePassProductOuterClass.BattlePassProduct;
27
28
29
30
import emu.grasscutter.net.proto.BattlePassRewardTagOuterClass.BattlePassRewardTag;
import emu.grasscutter.net.proto.BattlePassUnlockStatusOuterClass.BattlePassUnlockStatus;
import emu.grasscutter.net.proto.BattlePassRewardTakeOptionOuterClass.BattlePassRewardTakeOption;
import emu.grasscutter.net.proto.BattlePassScheduleOuterClass.BattlePassSchedule;
31
import emu.grasscutter.server.packet.send.PacketBattlePassCurScheduleUpdateNotify;
32
33
import emu.grasscutter.server.packet.send.PacketBattlePassMissionUpdateNotify;
import emu.grasscutter.server.packet.send.PacketTakeBattlePassRewardRsp;
34

35
@Entity(value = "battlepass", useDiscriminator = false)
36
public class BattlePassManager {
37
38
39
40
	@Id private ObjectId id;
	@Transient private Player player;
	
	@Indexed private int ownerUid;
41
    private int point;
42
    private int cyclePoints; // Weekly maximum cap
43
44
45
46
47
48
49
    private int level;
    
    private boolean viewed;
    private boolean paid;
    
    private Map<Integer, BattlePassMission> missions;
    private Map<Integer, BattlePassReward> takenRewards;
50
51
52
    
    @Deprecated // Morphia only
    public BattlePassManager() {}
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    public BattlePassManager(Player player) {
        this.setPlayer(player);
    }
    
    public ObjectId getId() {
		return id;
	}

	public Player getPlayer() {
    	return this.player;
    }
    
    public void setPlayer(Player player) {
    	this.player = player;
    	this.ownerUid = player.getUid();
    }
    
    public int getPoint() {
72
73
74
        return this.point;
    }
    
75
76
77
78
79
    public int getCyclePoints() {
		return cyclePoints;
	}

	public int getLevel() {
80
        return this.level;
81
82
    }

83
84
85
86
87
88
    public boolean isViewed() {
		return viewed;
	}
    
    public void updateViewed() {
		this.viewed = true;
89
90
    }

91
92
93
94
95
	public boolean isPaid() {
		return paid;
	}

	public void addPoints(int point){
96
        this.addPointsDirectly(point, false);
97
   
98
        player.getSession().send(new PacketBattlePassCurScheduleUpdateNotify(player.getSession().getPlayer()));
99
        this.save();
100
101
    }

102
103
104
105
106
107
108
109
110
111
112
113
114
    public void addPointsDirectly(int point, boolean isWeekly) {
    	int amount = point;
    	
    	if (isWeekly) {
    		amount = Math.min(amount, GameConstants.BATTLE_PASS_POINT_PER_WEEK - this.cyclePoints);
    	}
    	
    	if (amount <= 0) {
    		return;
    	}
    	
        this.point += amount;
        this.cyclePoints += amount;
115
116
117
118
119
120
121
122
123
124
125
        
        if (this.point >= GameConstants.BATTLE_PASS_POINT_PER_LEVEL && this.getLevel() < GameConstants.BATTLE_PASS_MAX_LEVEL) {
        	int levelups = (int) Math.floor((float) this.point / GameConstants.BATTLE_PASS_POINT_PER_LEVEL);
        	
        	// Make sure player cant go above max BP level
        	levelups = Math.min(levelups, GameConstants.BATTLE_PASS_MAX_LEVEL - levelups);
        	
        	// Set new points after level up
        	this.point = this.point - (levelups * GameConstants.BATTLE_PASS_POINT_PER_LEVEL);
        	this.level += levelups;
        }
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
	public Map<Integer, BattlePassMission> getMissions() {
		if (this.missions == null) this.missions = new HashMap<>();
		return this.missions;
	}

	// Will return a new empty mission if the mission id is not found
	public BattlePassMission loadMissionById(int id) {
		return getMissions().computeIfAbsent(id, i -> new BattlePassMission(i));
	}
	
	public boolean hasMission(int id) {
		return getMissions().containsKey(id);
	}

	public Map<Integer, BattlePassReward> getTakenRewards() {
		if (this.takenRewards == null) this.takenRewards = new HashMap<>();
		return this.takenRewards;
	}
	
	// Mission triggers
	public void triggerMission(WatcherTriggerType triggerType) {
		getPlayer().getServer().getBattlePassMissionManager().triggerMission(getPlayer(), triggerType);
	}
	
	public void triggerMission(WatcherTriggerType triggerType, int param, int progress) {
		getPlayer().getServer().getBattlePassMissionManager().triggerMission(getPlayer(), triggerType, param, progress);
	}
	
	// Handlers
	public void takeMissionPoint(List<Integer> missionIdList) {
		// Obvious exploit check
		if (missionIdList.size() > GameData.getBattlePassMissionDataMap().size()) {
			return;
		}
		
		List<BattlePassMission> updatedMissions = new ArrayList<>(missionIdList.size());
		
		for (int id : missionIdList) {
			// Skip if we dont have this mission
			if (!this.hasMission(id)) {
				continue;
			}
			
			BattlePassMission mission = this.loadMissionById(id);
			
			if (mission.getData() == null) {
				this.getMissions().remove(mission.getId());
				continue;
			}
			
			// Take reward
			if (mission.getStatus() == BattlePassMissionStatus.MISSION_STATUS_FINISHED) {
180
				this.addPointsDirectly(mission.getData().getAddPoint(), mission.getData().isCycleRefresh());
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
				mission.setStatus(BattlePassMissionStatus.MISSION_STATUS_POINT_TAKEN);
				
				updatedMissions.add(mission);
			}
		}
		
		if (updatedMissions.size() > 0) {
			// Save to db
			this.save();
			
			// Packet
			getPlayer().sendPacket(new PacketBattlePassMissionUpdateNotify(updatedMissions));
			getPlayer().sendPacket(new PacketBattlePassCurScheduleUpdateNotify(getPlayer()));
		}
	}
	
	public void takeReward(List<BattlePassRewardTakeOption> takeOptionList) {
		List<BattlePassRewardTag> rewardList = new ArrayList<>();
		
		for (BattlePassRewardTakeOption option : takeOptionList) {
			// Duplicate check
			if (option.getTag().getRewardId() == 0 || getTakenRewards().containsKey(option.getTag().getRewardId())) {
				continue;
			}
			
			// Level check
			if (option.getTag().getLevel() > this.getLevel()) {
				continue;
			}
			
			BattlePassRewardData rewardData = GameData.getBattlePassRewardDataMap().get(option.getTag().getLevel());
			
			// Sanity check with excel data
			if (rewardData.getFreeRewardIdList().contains(option.getTag().getRewardId())) {
				rewardList.add(option.getTag());
			} else if (this.isPaid() && rewardData.getPaidRewardIdList().contains(option.getTag().getRewardId())) {
				rewardList.add(option.getTag());
			}
		}
		
		// Get rewards
		List<ItemParamData> rewardItems = null;
		
		if (rewardList.size() > 0) {
			rewardItems = new ArrayList<>();
			
			for (BattlePassRewardTag tag : rewardList) {
				RewardData reward = GameData.getRewardDataMap().get(tag.getRewardId());
				
				if (reward == null) continue;
				
				BattlePassReward bpReward = new BattlePassReward(tag.getLevel(), tag.getRewardId(), tag.getUnlockStatus() == BattlePassUnlockStatus.BATTLE_PASS_UNLOCK_STATUS_PAID);
				this.getTakenRewards().put(bpReward.getRewardId(), bpReward);
				
				rewardItems.addAll(reward.getRewardItemList());
			}
			
			// Save to db
			this.save();
			
			// Add items and send battle pass schedule packet
			getPlayer().getInventory().addItemParamDatas(rewardItems);
			getPlayer().sendPacket(new PacketBattlePassCurScheduleUpdateNotify(getPlayer()));
		}
		
		getPlayer().sendPacket(new PacketTakeBattlePassRewardRsp(takeOptionList, rewardItems));
	}
	
	public int buyLevels(int buyLevel) {
		int boughtLevels = Math.min(buyLevel, GameConstants.BATTLE_PASS_MAX_LEVEL - buyLevel);
		
		if (boughtLevels > 0) {
			int price = GameConstants.BATTLE_PASS_LEVEL_PRICE * boughtLevels;
			
			if (getPlayer().getPrimogems() < price) {
				return 0;
			}
			
			this.level += boughtLevels;
			this.save();
			
			getPlayer().sendPacket(new PacketBattlePassCurScheduleUpdateNotify(getPlayer()));
		}
		
		return boughtLevels;
	}
	
	public void resetDailyMissions() {
		// TODO
	}
	
	public void resetWeeklyMissions() {
		// TODO
	}
	
	//
	public BattlePassSchedule getScheduleProto() {
		BattlePassSchedule.Builder schedule = BattlePassSchedule.newBuilder()
                .setScheduleId(2700)
                .setLevel(this.getLevel())
                .setPoint(this.getPoint())
                .setBeginTime(0)
                .setEndTime(2059483200)
                .setIsViewed(this.isViewed())
                .setUnlockStatus(this.isPaid() ? BattlePassUnlockStatus.BATTLE_PASS_UNLOCK_STATUS_PAID : BattlePassUnlockStatus.BATTLE_PASS_UNLOCK_STATUS_FREE)
286
                .setCurCyclePoints(this.getCyclePoints())
287
288
289
290
291
292
293
294
295
                .setCurCycle(BattlePassCycle.newBuilder().setBeginTime(0).setEndTime(2059483200).setCycleIdx(3));
		
		for (BattlePassReward reward : getTakenRewards().values()) {
			schedule.addRewardTakenList(reward.toProto());
		}
		
		return schedule.build();
	}
	
296
297
    public void save() {
    	DatabaseHelper.saveBattlePass(this);
298
299
    }
}