DatabaseManager.java 3.98 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
package emu.grasscutter.database;

import com.mongodb.MongoCommandException;
KingRainbow44's avatar
KingRainbow44 committed
4
5
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
Melledy's avatar
Melledy committed
6
7
8
9
10
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;

import dev.morphia.Datastore;
import dev.morphia.Morphia;
KingRainbow44's avatar
KingRainbow44 committed
11
12
import dev.morphia.mapping.MapperOptions;
import dev.morphia.query.experimental.filters.Filters;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.Grasscutter;
14
import emu.grasscutter.Grasscutter.ServerRunMode;
Melledy's avatar
Melledy committed
15
import emu.grasscutter.game.Account;
16
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.friends.Friendship;
18
import emu.grasscutter.game.gacha.GachaRecord;
19
import emu.grasscutter.game.inventory.GameItem;
20
import emu.grasscutter.game.mail.Mail;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
22
23
import emu.grasscutter.game.quest.GameMainQuest;
import emu.grasscutter.game.quest.GameQuest;
Melledy's avatar
Melledy committed
24

25
import static emu.grasscutter.Configuration.*;
26

27
public final class DatabaseManager {
28
	private static Datastore gameDatastore;
29
	private static Datastore dispatchDatastore;
Melledy's avatar
Melledy committed
30
	
KingRainbow44's avatar
KingRainbow44 committed
31
	private static final Class<?>[] mappedClasses = new Class<?>[] {
Melledy's avatar
Melledy committed
32
33
		DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, 
		GachaRecord.class, Mail.class, GameMainQuest.class
Melledy's avatar
Melledy committed
34
35
	};
    
36
37
    public static Datastore getGameDatastore() {
    	return gameDatastore;
Melledy's avatar
Melledy committed
38
39
    }
    
40
41
    public static MongoDatabase getGameDatabase() {
    	return getGameDatastore().getDatabase();
Melledy's avatar
Melledy committed
42
    }
43

Benjamin Elsdon's avatar
Benjamin Elsdon committed
44
45
	// Yes. I very dislike this method. However, this will be good for now.
	// TODO: Add dispatch routes for player account management
46
	public static Datastore getAccountDatastore() {
47
		if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
48
49
			return dispatchDatastore;
		} else {
50
			return gameDatastore;
51
52
		}
	}
Melledy's avatar
Melledy committed
53
54
55
	
	public static void initialize() {
		// Initialize
56
		MongoClient gameMongoClient = MongoClients.create(DATABASE.game.connectionUri);
Melledy's avatar
Melledy committed
57
		
KingRainbow44's avatar
KingRainbow44 committed
58
59
60
61
		// Set mapper options.
		MapperOptions mapperOptions = MapperOptions.builder()
				.storeEmpties(true).storeNulls(false).build();
		// Create data store.
62
		gameDatastore = Morphia.createDatastore(gameMongoClient, DATABASE.game.collection, mapperOptions);
KingRainbow44's avatar
KingRainbow44 committed
63
		// Map classes.
64
		gameDatastore.getMapper().map(mappedClasses);
Melledy's avatar
Melledy committed
65
66
67
		
		// Ensure indexes
		try {
68
			gameDatastore.ensureIndexes();
KingRainbow44's avatar
KingRainbow44 committed
69
70
		} catch (MongoCommandException exception) {
			Grasscutter.getLogger().info("Mongo index error: ", exception);
Melledy's avatar
Melledy committed
71
			// Duplicate index error
KingRainbow44's avatar
KingRainbow44 committed
72
			if (exception.getCode() == 85) {
Melledy's avatar
Melledy committed
73
				// Drop all indexes and re add them
74
				MongoIterable<String> collections = gameDatastore.getDatabase().listCollectionNames();
Melledy's avatar
Melledy committed
75
				for (String name : collections) {
76
					gameDatastore.getDatabase().getCollection(name).dropIndexes();
Melledy's avatar
Melledy committed
77
78
				}
				// Add back indexes
79
				gameDatastore.ensureIndexes();
Melledy's avatar
Melledy committed
80
81
			}
		}
82

83
		if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
84
85
			MongoClient dispatchMongoClient = MongoClients.create(DATABASE.server.connectionUri);
			dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, DATABASE.server.collection);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
86
87
88

			// Ensure indexes for dispatch server
			try {
89
				dispatchDatastore.ensureIndexes();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
90
91
92
93
94
95
96
97
98
99
100
101
			} catch (MongoCommandException e) {
				Grasscutter.getLogger().info("Mongo index error: ", e);
				// Duplicate index error
				if (e.getCode() == 85) {
					// Drop all indexes and re add them
					MongoIterable<String> collections = dispatchDatastore.getDatabase().listCollectionNames();
					for (String name : collections) {
						dispatchDatastore.getDatabase().getCollection(name).dropIndexes();
					}
					// Add back indexes
					dispatchDatastore.ensureIndexes();
				}
102
103
			}
		}
Melledy's avatar
Melledy committed
104
	}
KingRainbow44's avatar
KingRainbow44 committed
105

Melledy's avatar
Melledy committed
106
	public static synchronized int getNextId(Class<?> c) {
107
		DatabaseCounter counter = getGameDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first();
Melledy's avatar
Melledy committed
108
109
110
111
112
113
		if (counter == null) {
			counter = new DatabaseCounter(c.getSimpleName());
		}
		try {
			return counter.getNextId();
		} finally {
114
			getGameDatastore().save(counter);
Melledy's avatar
Melledy committed
115
116
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
117

Melledy's avatar
Melledy committed
118
119
120
121
	public static synchronized int getNextId(Object o) {
		return getNextId(o.getClass());
	}
}