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

3
import emu.grasscutter.GameConstants;
Melledy's avatar
Melledy committed
4
import emu.grasscutter.Grasscutter;
Jaida Wu's avatar
Jaida Wu committed
5
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.Account;
Kengxxiao's avatar
Kengxxiao committed
8
import emu.grasscutter.game.drop.DropManager;
Melledy's avatar
Melledy committed
9
10
11
12
13
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;
Melledy's avatar
Melledy committed
14
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
15
import emu.grasscutter.game.shop.ShopManager;
Melledy's avatar
Melledy committed
16
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
17
18
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
19
import emu.grasscutter.netty.KcpServer;
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
23
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;
Yazawazi's avatar
Yazawazi committed
24
import emu.grasscutter.task.TaskMap;
Melledy's avatar
Melledy committed
25

Kengxxiao's avatar
Kengxxiao committed
26
27
28
29
30
import java.net.InetSocketAddress;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

31
public final class GameServer extends KcpServer {
Melledy's avatar
Melledy committed
32
33
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
Melledy's avatar
Melledy committed
34

35
	private final Map<Integer, Player> players;
36
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
37
38
39
40
41
42
43
	
	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
44
	private final CommandMap commandMap;
Yazawazi's avatar
Yazawazi committed
45
	private final TaskMap taskMap;
Kengxxiao's avatar
Kengxxiao committed
46
	private final DropManager dropManager;
Melledy's avatar
Melledy committed
47
48
49
	
	public GameServer(InetSocketAddress address) {
		super(address);
50

Melledy's avatar
Melledy committed
51
52
53
54
		this.setServerInitializer(new GameServerInitializer(this));
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
55
		this.worlds = Collections.synchronizedSet(new HashSet<>());
Melledy's avatar
Melledy committed
56
57
58
59
60
61
62
		
		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
63
		this.commandMap = new CommandMap(true);
Yazawazi's avatar
Yazawazi committed
64
		this.taskMap = new TaskMap(true);
Kengxxiao's avatar
Kengxxiao committed
65
		this.dropManager = new DropManager(this);
Melledy's avatar
Melledy committed
66
		
Melledy's avatar
Melledy committed
67
68
69
		// Schedule game loop.
		Timer gameLoop = new Timer();
		gameLoop.scheduleAtFixedRate(new TimerTask() {
Melledy's avatar
Melledy committed
70
71
72
73
74
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
Melledy's avatar
Melledy committed
75
					Grasscutter.getLogger().error("An error occurred during game update.", e);
Melledy's avatar
Melledy committed
76
77
78
79
				}
			}
		}, new Date(), 1000L);
		
Melledy's avatar
Melledy committed
80
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
81
82
83
84
85
86
87
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
	
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

88
	public Map<Integer, Player> getPlayers() {
Melledy's avatar
Melledy committed
89
90
91
		return players;
	}

92
93
94
95
	public Set<World> getWorlds() {
		return worlds;
	}

Melledy's avatar
Melledy committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	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;
	}
Kengxxiao's avatar
Kengxxiao committed
115
116
117
118

	public DropManager getDropManager() {
		return dropManager;
	}
Melledy's avatar
Melledy committed
119
120
121
122
123
	
	public DungeonManager getDungeonManager() {
		return dungeonManager;
	}
	
Melledy's avatar
Melledy committed
124
125
126
	public CommandMap getCommandMap() {
		return this.commandMap;
	}
Yazawazi's avatar
Yazawazi committed
127
128
129
130

	public TaskMap getTaskMap() {
		return this.taskMap;
	}
Melledy's avatar
Melledy committed
131
	
132
	public void registerPlayer(Player player) {
133
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
134
135
	}

136
	public Player getPlayerByUid(int id) {
137
		return this.getPlayerByUid(id, false);
Melledy's avatar
Melledy committed
138
139
	}
	
140
	public Player getPlayerByUid(int id, boolean allowOfflinePlayers) {
Melledy's avatar
Melledy committed
141
		// Console check
142
		if (id == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
143
144
145
146
			return null;
		}
		
		// Get from online players
147
		Player player = this.getPlayers().get(id);
148
149
150
151
		
		if (!allowOfflinePlayers) {
			return player;
		}
Melledy's avatar
Melledy committed
152
153
154
155
156
157
158
159
160
		
		// Check database if character isnt here
		if (player == null) {
			player = DatabaseHelper.getPlayerById(id);
		}
		
		return player;
	}
	
161
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
162
		// Get from online players
163
		Player player = this.getPlayerByUid(id, true);
Melledy's avatar
Melledy committed
164
165
166
167
168
169
170
171
	
		if (player == null) {
			return null;
		}
		
		return player.getSocialDetail();
	}
	
Melledy's avatar
Melledy committed
172
	public Account getAccountByName(String username) {
173
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
174
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
175
176
177
178
179
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
	
Melledy's avatar
Melledy committed
180
	public void onTick() throws Exception {
181
182
183
184
185
186
187
188
189
190
		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
191
192
  
    ServerTickEvent event = new ServerTickEvent(); event.call();
Melledy's avatar
Melledy committed
193
	}
194
195
196
197
198
199
200
201
202
	
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
	
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
		
	}
Melledy's avatar
Melledy committed
203
204
205
206

	@Override
	public void onStartFinish() {
		Grasscutter.getLogger().info("Game Server started on port " + address.getPort());
KingRainbow44's avatar
KingRainbow44 committed
207
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
208
209
210
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
211
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
212

Melledy's avatar
Melledy committed
213
		// Kick and save all players
214
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
215
216
		list.addAll(this.getPlayers().values());
		
217
		for (Player player : list) {
Melledy's avatar
Melledy committed
218
219
220
221
			player.getSession().close();
		}
	}
}