DatabaseHelper.java 10.1 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;
13
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.game.friends.Friendship;
15
import emu.grasscutter.game.gacha.GachaRecord;
16
import emu.grasscutter.game.inventory.GameItem;
17
import emu.grasscutter.game.mail.Mail;
Melledy's avatar
Melledy committed
18
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.game.quest.GameMainQuest;
Melledy's avatar
Melledy committed
20

21
22
import static com.mongodb.client.model.Filters.eq;

KingRainbow44's avatar
KingRainbow44 committed
23
public final class DatabaseHelper {
Melledy's avatar
Melledy committed
24
25
26
	public static Account createAccount(String username) {
		return createAccountWithId(username, 0);
	}
KingRainbow44's avatar
KingRainbow44 committed
27

Melledy's avatar
Melledy committed
28
29
30
31
32
33
	public static Account createAccountWithId(String username, int reservedId) {
		// Unique names only
		Account exists = DatabaseHelper.getAccountByName(username);
		if (exists != null) {
			return null;
		}
KingRainbow44's avatar
KingRainbow44 committed
34

Melledy's avatar
Melledy committed
35
36
37
		// Make sure there are no id collisions
		if (reservedId > 0) {
			// Cannot make account with the same uid as the server console
38
			if (reservedId == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
39
40
41
42
43
44
45
46
47
48
49
50
				return null;
			}
			exists = DatabaseHelper.getAccountByPlayerId(reservedId);
			if (exists != null) {
				return null;
			}
		}

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

Melledy's avatar
Melledy committed
52
		if (reservedId > 0) {
53
			account.setReservedPlayerUid(reservedId);
KingRainbow44's avatar
KingRainbow44 committed
54
		}
Melledy's avatar
Melledy committed
55
56
57
58
59
60
61
62
63
64
65
66

		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
67

Melledy's avatar
Melledy committed
68
69
70
71
72
		// Account
		Account account = new Account();
		account.setId(Integer.toString(DatabaseManager.getNextId(account)));
		account.setUsername(username);
		account.setPassword(password);
KingRainbow44's avatar
KingRainbow44 committed
73
		DatabaseHelper.saveAccount(account);
Melledy's avatar
Melledy committed
74
75
76
77
		return account;
	}

	public static void saveAccount(Account account) {
78
		DatabaseManager.getAccountDatastore().save(account);
Melledy's avatar
Melledy committed
79
	}
KingRainbow44's avatar
KingRainbow44 committed
80

Melledy's avatar
Melledy committed
81
	public static Account getAccountByName(String username) {
82
		return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("username", username)).first();
Melledy's avatar
Melledy committed
83
	}
KingRainbow44's avatar
KingRainbow44 committed
84

Melledy's avatar
Melledy committed
85
	public static Account getAccountByToken(String token) {
KingRainbow44's avatar
KingRainbow44 committed
86
		if(token == null) return null;
87
		return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("token", token)).first();
Melledy's avatar
Melledy committed
88
	}
KingRainbow44's avatar
KingRainbow44 committed
89

90
91
	public static Account getAccountBySessionKey(String sessionKey) {
		if(sessionKey == null) return null;
92
		return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("sessionKey", sessionKey)).first();
93
94
	}

Melledy's avatar
Melledy committed
95
	public static Account getAccountById(String uid) {
96
		return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("_id", uid)).first();
Melledy's avatar
Melledy committed
97
	}
KingRainbow44's avatar
KingRainbow44 committed
98

99
	public static Account getAccountByPlayerId(int playerId) {
100
		return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first();
Melledy's avatar
Melledy committed
101
	}
KingRainbow44's avatar
KingRainbow44 committed
102

103
104
105
106
	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 ...
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		Player player = Grasscutter.getGameServer().getPlayerByAccountId(target.getId());
		
        if (player != null) {
        	// Close session first
            player.getSession().close();
            
            // Delete data from collections
    		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()));

    		// 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();
        }
130
131

		// Finally, delete the account itself.
132
		DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("id", target.getId())).delete();
Melledy's avatar
Melledy committed
133
	}
KingRainbow44's avatar
KingRainbow44 committed
134

135
	public static List<Player> getAllPlayers() {
136
		return DatabaseManager.getGameDatastore().find(Player.class).stream().toList();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
137
138
	}

139
	public static Player getPlayerByUid(int id) {
140
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", id)).first();
Melledy's avatar
Melledy committed
141
	}
142
143
144
145
146
147
148
	
	public static Player getPlayerByAccount(Account account) {
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first();
	}
	
	public static boolean checkPlayerExists(int uid) {
		return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0;
Melledy's avatar
Melledy committed
149
	}
KingRainbow44's avatar
KingRainbow44 committed
150

151
	public static synchronized Player generatePlayerUid(Player character, int reservedId) {
Melledy's avatar
Melledy committed
152
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
153
		int id;
Melledy's avatar
Melledy committed
154
155
		if (reservedId > 0 && !checkPlayerExists(reservedId)) {
			id = reservedId;
156
			character.setUid(id);
Melledy's avatar
Melledy committed
157
158
159
160
161
		} else {
			do {
				id = DatabaseManager.getNextId(character);
			}
			while (checkPlayerExists(id));
162
			character.setUid(id);
Melledy's avatar
Melledy committed
163
164
		}
		// Save to database
165
		DatabaseManager.getGameDatastore().save(character);
Melledy's avatar
Melledy committed
166
167
		return character;
	}
KingRainbow44's avatar
KingRainbow44 committed
168

Melledy's avatar
Melledy committed
169
170
	public static synchronized int getNextPlayerId(int reservedId) {
		// Check if reserved id
KingRainbow44's avatar
KingRainbow44 committed
171
		int id;
Melledy's avatar
Melledy committed
172
173
174
175
		if (reservedId > 0 && !checkPlayerExists(reservedId)) {
			id = reservedId;
		} else {
			do {
176
				id = DatabaseManager.getNextId(Player.class);
Melledy's avatar
Melledy committed
177
178
179
180
181
			}
			while (checkPlayerExists(id));
		}
		return id;
	}
KingRainbow44's avatar
KingRainbow44 committed
182

183
	public static void savePlayer(Player character) {
184
		DatabaseManager.getGameDatastore().save(character);
Melledy's avatar
Melledy committed
185
	}
KingRainbow44's avatar
KingRainbow44 committed
186

187
	public static void saveAvatar(Avatar avatar) {
188
		DatabaseManager.getGameDatastore().save(avatar);
Melledy's avatar
Melledy committed
189
	}
KingRainbow44's avatar
KingRainbow44 committed
190

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

195
	public static void saveItem(GameItem item) {
196
		DatabaseManager.getGameDatastore().save(item);
Melledy's avatar
Melledy committed
197
	}
KingRainbow44's avatar
KingRainbow44 committed
198

199
	public static boolean deleteItem(GameItem item) {
200
		DeleteResult result = DatabaseManager.getGameDatastore().delete(item);
Melledy's avatar
Melledy committed
201
202
		return result.wasAcknowledged();
	}
KingRainbow44's avatar
KingRainbow44 committed
203

204
	public static List<GameItem> getInventoryItems(Player player) {
205
		return DatabaseManager.getGameDatastore().find(GameItem.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
206
	}
207
	
208
	public static List<Friendship> getFriends(Player player) {
209
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("ownerId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
210
	}
KingRainbow44's avatar
KingRainbow44 committed
211

212
	public static List<Friendship> getReverseFriends(Player player) {
213
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.eq("friendId", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
214
215
216
	}

	public static void saveFriendship(Friendship friendship) {
217
		DatabaseManager.getGameDatastore().save(friendship);
Melledy's avatar
Melledy committed
218
219
220
	}

	public static void deleteFriendship(Friendship friendship) {
221
		DatabaseManager.getGameDatastore().delete(friendship);
Melledy's avatar
Melledy committed
222
223
224
	}

	public static Friendship getReverseFriendship(Friendship friendship) {
225
		return DatabaseManager.getGameDatastore().find(Friendship.class).filter(Filters.and(
KingRainbow44's avatar
KingRainbow44 committed
226
227
228
				Filters.eq("ownerId", friendship.getFriendId()),
				Filters.eq("friendId", friendship.getOwnerId())
		)).first();
Melledy's avatar
Melledy committed
229
	}
memetrollsXD's avatar
memetrollsXD committed
230

231
232
233
234
235
	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){
236
		return DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
237
238
239
240
241
242
243
244
245
246
247
248
249
250
			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){
251
		long count = DatabaseManager.getGameDatastore().find(GachaRecord.class).filter(
252
253
254
255
256
257
258
			Filters.eq("ownerId", ownerId),
			Filters.eq("gachaType", gachaType)
		).count();
		return count / 10 + (count % 10 > 0 ? 1 : 0 );
	}

	public static void saveGachaRecord(GachaRecord gachaRecord){
259
		DatabaseManager.getGameDatastore().save(gachaRecord);
260
	}
261
262
	
	public static List<Mail> getAllMail(Player player) {
263
		return DatabaseManager.getGameDatastore().find(Mail.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
264
265
266
	}
	
	public static void saveMail(Mail mail) {
267
		DatabaseManager.getGameDatastore().save(mail);
268
269
270
	}
	
	public static boolean deleteMail(Mail mail) {
271
		DeleteResult result = DatabaseManager.getGameDatastore().delete(mail);
272
273
		return result.wasAcknowledged();
	}
Melledy's avatar
Melledy committed
274
275
	
	public static List<GameMainQuest> getAllQuests(Player player) {
276
		return DatabaseManager.getGameDatastore().find(GameMainQuest.class).filter(Filters.eq("ownerUid", player.getUid())).stream().toList();
Melledy's avatar
Melledy committed
277
278
279
	}
	
	public static void saveQuest(GameMainQuest quest) {
280
		DatabaseManager.getGameDatastore().save(quest);
Melledy's avatar
Melledy committed
281
282
283
	}
	
	public static boolean deleteQuest(GameMainQuest quest) {
284
		return DatabaseManager.getGameDatastore().delete(quest).wasAcknowledged();
Melledy's avatar
Melledy committed
285
	}
Melledy's avatar
Melledy committed
286
}