DatabaseHelper.java 12.9 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.database;

import java.util.List;

KingRainbow44's avatar
KingRainbow44 committed
5
import com.mongodb.client.result.DeleteResult;
6

7
8
import dev.morphia.query.FindOptions;
import dev.morphia.query.Sort;
KingRainbow44's avatar
KingRainbow44 committed
9
import dev.morphia.query.experimental.filters.Filters;
10
import emu.grasscutter.GameConstants;
11
import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.Account;
Akka's avatar
Akka committed
13
import emu.grasscutter.game.activity.PlayerActivityData;
Akka's avatar
Akka committed
14
import emu.grasscutter.game.activity.musicgame.MusicGameBeatmap;
15
import emu.grasscutter.game.avatar.Avatar;
16
import emu.grasscutter.game.battlepass.BattlePassManager;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.friends.Friendship;
18
import emu.grasscutter.game.gacha.GachaRecord;
19
import emu.grasscutter.game.home.GameHome;
20
import emu.grasscutter.game.inventory.GameItem;
21
import emu.grasscutter.game.mail.Mail;
Melledy's avatar
Melledy committed
22
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
23
import emu.grasscutter.game.quest.GameMainQuest;
Melledy's avatar
Melledy committed
24

25
26
import static com.mongodb.client.model.Filters.eq;

KingRainbow44's avatar
KingRainbow44 committed
27
public final class DatabaseHelper {
Melledy's avatar
Melledy committed
28
	public static Account createAccount(String username) {
29
		return createAccountWithUid(username, 0);
Melledy's avatar
Melledy committed
30
	}
KingRainbow44's avatar
KingRainbow44 committed
31

32
	public static Account createAccountWithUid(String username, int reservedUid) {
Melledy's avatar
Melledy committed
33
		// Unique names only
34
		if (DatabaseHelper.checkIfAccountExists(username)) {
Melledy's avatar
Melledy committed
35
36
			return null;
		}
KingRainbow44's avatar
KingRainbow44 committed
37

Melledy's avatar
Melledy committed
38
		// Make sure there are no id collisions
39
		if (reservedUid > 0) {
Melledy's avatar
Melledy committed
40
			// Cannot make account with the same uid as the server console
41
			if (reservedUid == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
42
43
				return null;
			}
44

45
			if (DatabaseHelper.checkIfAccountExists(reservedUid)) {
Melledy's avatar
Melledy committed
46
47
				return null;
			}
48

49
			// Make sure no existing player already has this id.
50
			if (DatabaseHelper.checkIfPlayerExists(reservedUid)) {
51
52
				return null;
			}
Melledy's avatar
Melledy committed
53
54
55
56
57
58
		}

		// Account
		Account account = new Account();
		account.setUsername(username);
		account.setId(Integer.toString(DatabaseManager.getNextId(account)));
KingRainbow44's avatar
KingRainbow44 committed
59

60
61
		if (reservedUid > 0) {
			account.setReservedPlayerUid(reservedUid);
KingRainbow44's avatar
KingRainbow44 committed
62
		}
Melledy's avatar
Melledy committed
63
64
65
66
67
68
69
70
71
72
73
74

		DatabaseHelper.saveAccount(account);
		return account;
	}

	@Deprecated
	public static Account createAccountWithPassword(String username, String password) {
		// Unique names only
		Account exists = DatabaseHelper.getAccountByName(username);
		if (exists != null) {
			return null;
		}
KingRainbow44's avatar
KingRainbow44 committed
75

Melledy's avatar
Melledy committed
76
77
78
79
80
		// Account
		Account account = new Account();
		account.setId(Integer.toString(DatabaseManager.getNextId(account)));
		account.setUsername(username);
		account.setPassword(password);
KingRainbow44's avatar
KingRainbow44 committed
81
		DatabaseHelper.saveAccount(account);
Melledy's avatar
Melledy committed
82
83
84
85
		return account;
	}

	public static void saveAccount(Account account) {
86
		DatabaseManager.getAccountDatastore().save(account);
Melledy's avatar
Melledy committed
87
	}
KingRainbow44's avatar
KingRainbow44 committed
88

Melledy's avatar
Melledy committed
89
	public static Account getAccountByName(String username) {
yuyuko's avatar
yuyuko committed
90
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
Melledy's avatar
Melledy committed
91
	}
KingRainbow44's avatar
KingRainbow44 committed
92

Melledy's avatar
Melledy committed
93
	public static Account getAccountByToken(String token) {
KingRainbow44's avatar
KingRainbow44 committed
94
		if(token == null) return null;
yuyuko's avatar
yuyuko committed
95
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
Melledy's avatar
Melledy committed
96
	}
KingRainbow44's avatar
KingRainbow44 committed
97

98
99
	public static Account getAccountBySessionKey(String sessionKey) {
		if(sessionKey == null) return null;
yuyuko's avatar
yuyuko committed
100
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
101
102
	}

Melledy's avatar
Melledy committed
103
	public static Account getAccountById(String uid) {
yuyuko's avatar
yuyuko committed
104
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
Melledy's avatar
Melledy committed
105
	}
KingRainbow44's avatar
KingRainbow44 committed
106

107
	public static Account getAccountByPlayerId(int playerId) {
yuyuko's avatar
yuyuko committed
108
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first();
Melledy's avatar
Melledy committed
109
	}
110

111
	public static boolean checkIfAccountExists(String name) {
yuyuko's avatar
yuyuko committed
112
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("username", name)).count() > 0;
113
	}
114

115
	public static boolean checkIfAccountExists(int reservedUid) {
yuyuko's avatar
yuyuko committed
116
		return DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", reservedUid)).count() > 0;
117
	}
KingRainbow44's avatar
KingRainbow44 committed
118

119
120
121
122
	public static void deleteAccount(Account target) {
		// To delete an account, we need to also delete all the other documents in the database that reference the account.
		// This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the
		// database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ...
123

124
		Player player = Grasscutter.getGameServer().getPlayerByAccountId(target.getId());
125

126
127
128
        if (player != null) {
        	// Close session first
            player.getSession().close();
129

130
            // Delete data from collections
dragon's avatar
dragon committed
131
132
            DatabaseManager.getGameDatabase().getCollection("activities").deleteMany(eq("uid",player.getUid()));
            DatabaseManager.getGameDatabase().getCollection("homes").deleteMany(eq("ownerUid",player.getUid()));
133
134
135
136
137
    		DatabaseManager.getGameDatabase().getCollection("mail").deleteMany(eq("ownerUid", player.getUid()));
    		DatabaseManager.getGameDatabase().getCollection("avatars").deleteMany(eq("ownerId", player.getUid()));
    		DatabaseManager.getGameDatabase().getCollection("gachas").deleteMany(eq("ownerId", player.getUid()));
    		DatabaseManager.getGameDatabase().getCollection("items").deleteMany(eq("ownerId", player.getUid()));
    		DatabaseManager.getGameDatabase().getCollection("quests").deleteMany(eq("ownerUid", player.getUid()));
138
    		DatabaseManager.getGameDatabase().getCollection("battlepass").deleteMany(eq("ownerUid", player.getUid()));
139
140
141
142
143
144
145
146
147
148

    		// Delete friendships.
    		// Here, we need to make sure to not only delete the deleted account's friendships,
    		// but also all friendship entries for that account's friends.
    		DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("ownerId", player.getUid()));
    		DatabaseManager.getGameDatabase().getCollection("friendships").deleteMany(eq("friendId", player.getUid()));

    		// Delete the player last.
    		DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("id", player.getUid())).delete();
        }
149
150

		// Finally, delete the account itself.
yuyuko's avatar
yuyuko committed
151
		DatabaseManager.getAccountDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
Melledy's avatar
Melledy committed
152
	}
KingRainbow44's avatar
KingRainbow44 committed
153

154
	public static List<Player> getAllPlayers() {
155
		return DatabaseManager.getGameDatastore().find(Player.class).stream().toList();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
156
157
	}

158
	public static Player getPlayerByUid(int id) {
159
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first();
Melledy's avatar
Melledy committed
160
	}
161
162

    @Deprecated
163
164
165
	public static Player getPlayerByAccount(Account account) {
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first();
	}
166
167
168
169
170

    public static Player getPlayerByAccount(Account account, Class<? extends Player> playerClass) {
        return DatabaseManager.getGameDatastore().find(playerClass).filter(Filters.eq("accountId", account.getId())).first();
    }

171
	public static boolean checkIfPlayerExists(int uid) {
172
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0;
Melledy's avatar
Melledy committed
173
	}
KingRainbow44's avatar
KingRainbow44 committed
174

175
	public static synchronized Player generatePlayerUid(Player character, int reservedId) {
Melledy's avatar
Melledy committed
176
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
177
		int id;
178
		if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
Melledy's avatar
Melledy committed
179
			id = reservedId;
180
			character.setUid(id);
Melledy's avatar
Melledy committed
181
182
183
184
		} else {
			do {
				id = DatabaseManager.getNextId(character);
			}
185
			while (checkIfPlayerExists(id));
186
			character.setUid(id);
Melledy's avatar
Melledy committed
187
188
		}
		// Save to database
189
		DatabaseManager.getGameDatastore().save(character);
Melledy's avatar
Melledy committed
190
191
		return character;
	}
KingRainbow44's avatar
KingRainbow44 committed
192

Melledy's avatar
Melledy committed
193
194
	public static synchronized int getNextPlayerId(int reservedId) {
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
195
		int id;
196
		if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
Melledy's avatar
Melledy committed
197
198
199
			id = reservedId;
		} else {
			do {
200
				id = DatabaseManager.getNextId(Player.class);
Melledy's avatar
Melledy committed
201
			}
202
			while (checkIfPlayerExists(id));
Melledy's avatar
Melledy committed
203
204
205
		}
		return id;
	}
KingRainbow44's avatar
KingRainbow44 committed
206

207
	public static void savePlayer(Player character) {
208
		DatabaseManager.getGameDatastore().save(character);
Melledy's avatar
Melledy committed
209
	}
KingRainbow44's avatar
KingRainbow44 committed
210

211
	public static void saveAvatar(Avatar avatar) {
212
		DatabaseManager.getGameDatastore().save(avatar);
Melledy's avatar
Melledy committed
213
	}
KingRainbow44's avatar
KingRainbow44 committed
214

215
	public static List<Avatar> getAvatars(Player player) {
216
		return DatabaseManager.getGameDatastore().find(Avatar.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
217
	}
KingRainbow44's avatar
KingRainbow44 committed
218

219
	public static void saveItem(GameItem item) {
220
		DatabaseManager.getGameDatastore().save(item);
Melledy's avatar
Melledy committed
221
	}
KingRainbow44's avatar
KingRainbow44 committed
222

223
	public static boolean deleteItem(GameItem item) {
224
		DeleteResult result = DatabaseManager.getGameDatastore().delete(item);
Melledy's avatar
Melledy committed
225
226
		return result.wasAcknowledged();
	}
KingRainbow44's avatar
KingRainbow44 committed
227

228
	public static List<GameItem> getInventoryItems(Player player) {
229
		return DatabaseManager.getGameDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
230
	}
231

232
	public static List<Friendship> getFriends(Player player) {
233
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
234
	}
KingRainbow44's avatar
KingRainbow44 committed
235

236
	public static List<Friendship> getReverseFriends(Player player) {
237
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
238
239
240
	}

	public static void saveFriendship(Friendship friendship) {
241
		DatabaseManager.getGameDatastore().save(friendship);
Melledy's avatar
Melledy committed
242
243
244
	}

	public static void deleteFriendship(Friendship friendship) {
245
		DatabaseManager.getGameDatastore().delete(friendship);
Melledy's avatar
Melledy committed
246
247
248
	}

	public static Friendship getReverseFriendship(Friendship friendship) {
249
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.and(
KingRainbow44's avatar
KingRainbow44 committed
250
251
252
				Filters.eq("ownerId", friendship.getFriendId()),
				Filters.eq("friendId", friendship.getOwnerId())
		)).first();
Melledy's avatar
Melledy committed
253
	}
memetrollsXD's avatar
memetrollsXD committed
254

255
256
257
258
259
	public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType){
		return getGachaRecords(ownerId, page, gachaType, 10);
	}

	public static List<GachaRecord> getGachaRecords(int ownerId, int page, int gachaType, int pageSize){
260
		return DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
261
262
263
264
265
266
267
268
269
270
271
272
273
274
			Filters.eq("ownerId", ownerId),
			Filters.eq("gachaType", gachaType)
		).iterator(new FindOptions()
				.sort(Sort.descending("transactionDate"))
				.skip(pageSize * page)
				.limit(pageSize)
		).toList();
	}

	public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType){
		return getGachaRecordsMaxPage(ownerId, page, gachaType, 10);
	}

	public static long getGachaRecordsMaxPage(int ownerId, int page, int gachaType, int pageSize){
275
		long count = DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
276
277
278
279
280
281
282
			Filters.eq("ownerId", ownerId),
			Filters.eq("gachaType", gachaType)
		).count();
		return count / 10 + (count % 10 > 0 ? 1 : 0 );
	}

	public static void saveGachaRecord(GachaRecord gachaRecord){
283
		DatabaseManager.getGameDatastore().save(gachaRecord);
284
	}
285

286
	public static List<Mail> getAllMail(Player player) {
287
		return DatabaseManager.getGameDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
288
	}
289

290
	public static void saveMail(Mail mail) {
291
		DatabaseManager.getGameDatastore().save(mail);
292
	}
293

294
	public static boolean deleteMail(Mail mail) {
295
		DeleteResult result = DatabaseManager.getGameDatastore().delete(mail);
296
297
		return result.wasAcknowledged();
	}
298

Melledy's avatar
Melledy committed
299
	public static List<GameMainQuest> getAllQuests(Player player) {
300
		return DatabaseManager.getGameDatastore().find(GameMainQuest.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
301
	}
302

Melledy's avatar
Melledy committed
303
	public static void saveQuest(GameMainQuest quest) {
304
		DatabaseManager.getGameDatastore().save(quest);
Melledy's avatar
Melledy committed
305
	}
306

Melledy's avatar
Melledy committed
307
	public static boolean deleteQuest(GameMainQuest quest) {
308
		return DatabaseManager.getGameDatastore().delete(quest).wasAcknowledged();
Melledy's avatar
Melledy committed
309
	}
310

311
312
313
	public static GameHome getHomeByUid(int id) {
		return DatabaseManager.getGameDatastore().find(GameHome.class).filter(Filters.eq("ownerUid", id)).first();
	}
314

315
316
317
	public static void saveHome(GameHome gameHome) {
		DatabaseManager.getGameDatastore().save(gameHome);
	}
318

319
320
321
322
323
324
325
326
327
328
	public static BattlePassManager loadBattlePass(Player player) {
		BattlePassManager manager = DatabaseManager.getGameDatastore().find(BattlePassManager.class).filter(Filters.eq("ownerUid", player.getUid())).first();
		if (manager == null) {
			manager = new BattlePassManager(player);
			manager.save();
		} else {
			manager.setPlayer(player);
		}
		return manager;
	}
329

330
331
332
	public static void saveBattlePass(BattlePassManager manager) {
		DatabaseManager.getGameDatastore().save(manager);
	}
Akka's avatar
Akka committed
333
334
335
336
337
338
339
340
341
342

    public static PlayerActivityData getPlayerActivityData(int uid, int activityId) {
        return DatabaseManager.getGameDatastore().find(PlayerActivityData.class)
            .filter(Filters.and(Filters.eq("uid", uid),Filters.eq("activityId", activityId)))
            .first();
    }

    public static void savePlayerActivityData(PlayerActivityData playerActivityData) {
        DatabaseManager.getGameDatastore().save(playerActivityData);
    }
Akka's avatar
Akka committed
343
344
345
346
347
348
349
350
351
    public static MusicGameBeatmap getMusicGameBeatmap(long musicShareId) {
        return DatabaseManager.getGameDatastore().find(MusicGameBeatmap.class)
            .filter(Filters.eq("musicShareId", musicShareId))
            .first();
    }

    public static void saveMusicGameBeatmap(MusicGameBeatmap musicGameBeatmap) {
        DatabaseManager.getGameDatastore().save(musicGameBeatmap);
    }
Melledy's avatar
Melledy committed
352
}