GameServer.java 5.7 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
package emu.grasscutter.server.game;

import java.net.InetSocketAddress;
KingRainbow44's avatar
KingRainbow44 committed
4
import java.time.OffsetDateTime;
Melledy's avatar
Melledy committed
5
import java.util.*;
Melledy's avatar
Melledy committed
6
7
8
9
import java.util.concurrent.ConcurrentHashMap;

import emu.grasscutter.GenshinConstants;
import emu.grasscutter.Grasscutter;
Jaida Wu's avatar
Jaida Wu committed
10
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.Account;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.game.GenshinPlayer;
14
import emu.grasscutter.game.World;
Melledy's avatar
Melledy committed
15
16
17
18
19
20
21
22
23
import emu.grasscutter.game.dungeons.DungeonManager;
import emu.grasscutter.game.gacha.GachaManager;
import emu.grasscutter.game.managers.ChatManager;
import emu.grasscutter.game.managers.InventoryManager;
import emu.grasscutter.game.managers.MultiplayerManager;
import emu.grasscutter.game.shop.ShopManager;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
import emu.grasscutter.netty.MihoyoKcpServer;
KingRainbow44's avatar
KingRainbow44 committed
24
25
26
27
import emu.grasscutter.server.event.ServerEvent;
import emu.grasscutter.server.event.game.ServerTickEvent;
import emu.grasscutter.server.event.internal.ServerStartEvent;
import emu.grasscutter.server.event.internal.ServerStopEvent;
Melledy's avatar
Melledy committed
28

Melledy's avatar
Melledy committed
29
public final class GameServer extends MihoyoKcpServer {
Melledy's avatar
Melledy committed
30
31
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
Melledy's avatar
Melledy committed
32

Melledy's avatar
Melledy committed
33
	private final Map<Integer, GenshinPlayer> players;
34
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
35
36
37
38
39
40
41
	
	private final ChatManager chatManager;
	private final InventoryManager inventoryManager;
	private final GachaManager gachaManager;
	private final ShopManager shopManager;
	private final MultiplayerManager multiplayerManager;
	private final DungeonManager dungeonManager;
Melledy's avatar
Melledy committed
42
	private final CommandMap commandMap;
Melledy's avatar
Melledy committed
43
44
45
	
	public GameServer(InetSocketAddress address) {
		super(address);
46

Melledy's avatar
Melledy committed
47
48
49
50
		this.setServerInitializer(new GameServerInitializer(this));
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
51
		this.worlds = Collections.synchronizedSet(new HashSet<>());
Melledy's avatar
Melledy committed
52
53
54
55
56
57
58
		
		this.chatManager = new ChatManager(this);
		this.inventoryManager = new InventoryManager(this);
		this.gachaManager = new GachaManager(this);
		this.shopManager = new ShopManager(this);
		this.multiplayerManager = new MultiplayerManager(this);
		this.dungeonManager = new DungeonManager(this);
Melledy's avatar
Melledy committed
59
		this.commandMap = new CommandMap(true);
Melledy's avatar
Melledy committed
60
		
Melledy's avatar
Melledy committed
61
62
63
		// Schedule game loop.
		Timer gameLoop = new Timer();
		gameLoop.scheduleAtFixedRate(new TimerTask() {
Melledy's avatar
Melledy committed
64
65
66
67
68
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
Melledy's avatar
Melledy committed
69
					Grasscutter.getLogger().error("An error occurred during game update.", e);
Melledy's avatar
Melledy committed
70
71
72
73
				}
			}
		}, new Date(), 1000L);
		
Melledy's avatar
Melledy committed
74
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
75
76
77
78
79
80
81
82
83
84
85
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
	
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

	public Map<Integer, GenshinPlayer> getPlayers() {
		return players;
	}

86
87
88
89
	public Set<World> getWorlds() {
		return worlds;
	}

Melledy's avatar
Melledy committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	public ChatManager getChatManager() {
		return chatManager;
	}

	public InventoryManager getInventoryManager() {
		return inventoryManager;
	}

	public GachaManager getGachaManager() {
		return gachaManager;
	}
	
	public ShopManager getShopManager() {
		return shopManager;
	}

	public MultiplayerManager getMultiplayerManager() {
		return multiplayerManager;
	}
	
	public DungeonManager getDungeonManager() {
		return dungeonManager;
	}
	
Melledy's avatar
Melledy committed
114
115
116
117
	public CommandMap getCommandMap() {
		return this.commandMap;
	}
	
Melledy's avatar
Melledy committed
118
	public void registerPlayer(GenshinPlayer player) {
119
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
120
121
	}

122
123
	public GenshinPlayer getPlayerByUid(int id) {
		return this.getPlayerByUid(id, false);
Melledy's avatar
Melledy committed
124
125
	}
	
126
	public GenshinPlayer getPlayerByUid(int id, boolean allowOfflinePlayers) {
Melledy's avatar
Melledy committed
127
128
129
130
131
132
		// Console check
		if (id == GenshinConstants.SERVER_CONSOLE_UID) {
			return null;
		}
		
		// Get from online players
133
		GenshinPlayer player = this.getPlayers().get(id);
134
135
136
137
		
		if (!allowOfflinePlayers) {
			return player;
		}
Melledy's avatar
Melledy committed
138
139
140
141
142
143
144
145
146
		
		// Check database if character isnt here
		if (player == null) {
			player = DatabaseHelper.getPlayerById(id);
		}
		
		return player;
	}
	
147
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
148
		// Get from online players
149
		GenshinPlayer player = this.getPlayerByUid(id, true);
Melledy's avatar
Melledy committed
150
151
152
153
154
155
156
157
	
		if (player == null) {
			return null;
		}
		
		return player.getSocialDetail();
	}
	
Melledy's avatar
Melledy committed
158
159
	public Account getAccountByName(String username) {
		Optional<GenshinPlayer> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
160
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
161
162
163
164
165
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
	
Melledy's avatar
Melledy committed
166
	public void onTick() throws Exception {
167
168
169
170
171
172
173
174
175
176
		Iterator<World> it = this.getWorlds().iterator();
		while (it.hasNext()) {
			World world = it.next();
			
			if (world.getPlayerCount() == 0) {
				it.remove();
			}
			
			world.onTick();
		}
Magix's avatar
Magix committed
177
178
  
    ServerTickEvent event = new ServerTickEvent(); event.call();
Melledy's avatar
Melledy committed
179
	}
180
181
182
183
184
185
186
187
188
	
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
	
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
		
	}
Melledy's avatar
Melledy committed
189
190
191
192

	@Override
	public void onStartFinish() {
		Grasscutter.getLogger().info("Game Server started on port " + address.getPort());
KingRainbow44's avatar
KingRainbow44 committed
193
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
194
195
196
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
197
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
198

Melledy's avatar
Melledy committed
199
200
201
202
203
204
205
206
207
		// Kick and save all players
		List<GenshinPlayer> list = new ArrayList<>(this.getPlayers().size());
		list.addAll(this.getPlayers().values());
		
		for (GenshinPlayer player : list) {
			player.getSession().close();
		}
	}
}