GameServer.java 7.14 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;
Akka's avatar
Akka committed
8
import emu.grasscutter.game.combine.CombineManger;
Kengxxiao's avatar
Kengxxiao committed
9
import emu.grasscutter.game.drop.DropManager;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.dungeons.DungeonManager;
Kinesis's avatar
Kinesis committed
11
import emu.grasscutter.game.expedition.ExpeditionManager;
Melledy's avatar
Melledy committed
12
13
14
15
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
16
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.shop.ShopManager;
18
import emu.grasscutter.game.tower.TowerScheduleManager;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
20
21
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
22
import emu.grasscutter.netty.KcpServer;
KingRainbow44's avatar
KingRainbow44 committed
23
import emu.grasscutter.server.event.types.ServerEvent;
KingRainbow44's avatar
KingRainbow44 committed
24
25
26
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
27
import emu.grasscutter.task.TaskMap;
Melledy's avatar
Melledy committed
28

Kengxxiao's avatar
Kengxxiao committed
29
30
31
32
33
import java.net.InetSocketAddress;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

34
import static emu.grasscutter.utils.Language.translate;
35
import static emu.grasscutter.Configuration.*;
36

37
public final class GameServer extends KcpServer {
Melledy's avatar
Melledy committed
38
39
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
Melledy's avatar
Melledy committed
40

41
	private final Map<Integer, Player> players;
42
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
43
44
45
46
47
48
49
	
	private final ChatManager chatManager;
	private final InventoryManager inventoryManager;
	private final GachaManager gachaManager;
	private final ShopManager shopManager;
	private final MultiplayerManager multiplayerManager;
	private final DungeonManager dungeonManager;
Kinesis's avatar
Kinesis committed
50
	private final ExpeditionManager expeditionManager;
Melledy's avatar
Melledy committed
51
	private final CommandMap commandMap;
Yazawazi's avatar
Yazawazi committed
52
	private final TaskMap taskMap;
Kengxxiao's avatar
Kengxxiao committed
53
	private final DropManager dropManager;
Akka's avatar
Akka committed
54
55

	private final CombineManger combineManger;
56
	private final TowerScheduleManager towerScheduleManager;
Akka's avatar
Akka committed
57

58
59
	public GameServer() {
		this(new InetSocketAddress(
60
61
				GAME_INFO.bindAddress,
				GAME_INFO.bindPort
62
63
64
		));
	}
	
Melledy's avatar
Melledy committed
65
66
	public GameServer(InetSocketAddress address) {
		super(address);
67

Melledy's avatar
Melledy committed
68
69
70
71
		this.setServerInitializer(new GameServerInitializer(this));
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
72
		this.worlds = Collections.synchronizedSet(new HashSet<>());
Melledy's avatar
Melledy committed
73
74
75
76
77
78
79
		
		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
80
		this.commandMap = new CommandMap(true);
Yazawazi's avatar
Yazawazi committed
81
		this.taskMap = new TaskMap(true);
Kengxxiao's avatar
Kengxxiao committed
82
		this.dropManager = new DropManager(this);
Kinesis's avatar
Kinesis committed
83
		this.expeditionManager = new ExpeditionManager(this);
Akka's avatar
Akka committed
84
		this.combineManger = new CombineManger(this);
85
		this.towerScheduleManager = new TowerScheduleManager(this);
Melledy's avatar
Melledy committed
86
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
87
88
89
90
91
92
93
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
	
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

94
	public Map<Integer, Player> getPlayers() {
Melledy's avatar
Melledy committed
95
96
97
		return players;
	}

98
99
100
101
	public Set<World> getWorlds() {
		return worlds;
	}

Melledy's avatar
Melledy committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	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
121
122
123
124

	public DropManager getDropManager() {
		return dropManager;
	}
Melledy's avatar
Melledy committed
125
126
127
128
	
	public DungeonManager getDungeonManager() {
		return dungeonManager;
	}
Kinesis's avatar
Kinesis committed
129
130
131
132
133

	public ExpeditionManager getExpeditionManager() {
		return expeditionManager;
	}

Melledy's avatar
Melledy committed
134
135
136
	public CommandMap getCommandMap() {
		return this.commandMap;
	}
Yazawazi's avatar
Yazawazi committed
137

Akka's avatar
Akka committed
138
139
140
	public CombineManger getCombineManger(){
		return this.combineManger;
	}
Kinesis's avatar
Kinesis committed
141

142
143
144
145
	public TowerScheduleManager getTowerScheduleManager() {
		return towerScheduleManager;
	}

Yazawazi's avatar
Yazawazi committed
146
147
148
	public TaskMap getTaskMap() {
		return this.taskMap;
	}
Melledy's avatar
Melledy committed
149
	
150
	public void registerPlayer(Player player) {
151
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
152
153
	}

154
	public Player getPlayerByUid(int id) {
155
		return this.getPlayerByUid(id, false);
Melledy's avatar
Melledy committed
156
157
	}
	
158
	public Player getPlayerByUid(int id, boolean allowOfflinePlayers) {
Melledy's avatar
Melledy committed
159
		// Console check
160
		if (id == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
161
162
163
164
			return null;
		}
		
		// Get from online players
165
		Player player = this.getPlayers().get(id);
166
167
168
169
		
		if (!allowOfflinePlayers) {
			return player;
		}
Melledy's avatar
Melledy committed
170
171
172
173
174
175
176
177
178
		
		// Check database if character isnt here
		if (player == null) {
			player = DatabaseHelper.getPlayerById(id);
		}
		
		return player;
	}
	
179
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
180
		// Get from online players
181
		Player player = this.getPlayerByUid(id, true);
Melledy's avatar
Melledy committed
182
183
184
185
186
187
188
189
	
		if (player == null) {
			return null;
		}
		
		return player.getSocialDetail();
	}
	
Melledy's avatar
Melledy committed
190
	public Account getAccountByName(String username) {
191
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
192
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
193
194
195
196
197
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
	
Melledy's avatar
Melledy committed
198
	public void onTick() throws Exception {
199
200
201
202
203
204
205
206
207
208
		Iterator<World> it = this.getWorlds().iterator();
		while (it.hasNext()) {
			World world = it.next();
			
			if (world.getPlayerCount() == 0) {
				it.remove();
			}
			
			world.onTick();
		}
Melledy's avatar
Melledy committed
209
210
211
212
		
		for (Player player : this.getPlayers().values()) {
			player.onTick();
		}
Magix's avatar
Magix committed
213
  
Melledy's avatar
Melledy committed
214
		ServerTickEvent event = new ServerTickEvent(); event.call();
Melledy's avatar
Melledy committed
215
	}
216
217
218
219
220
221
222
223
224
	
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
	
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
		
	}
Melledy's avatar
Melledy committed
225

226
227
228
	@Override
	public synchronized void start() {
		// Schedule game loop.
Bwly999's avatar
Bwly999 committed
229
		Timer gameLoop = new Timer();
230
231
232
233
234
235
		gameLoop.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
236
					Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
237
238
				}
			}
Bwly999's avatar
Bwly999 committed
239
240
		}, new Date(), 1000L);

241
242
243
		super.start();
	}

Melledy's avatar
Melledy committed
244
245
	@Override
	public void onStartFinish() {
246
247
		Grasscutter.getLogger().info(translate("messages.status.free_software"));
		Grasscutter.getLogger().info(translate("messages.game.port_bind", Integer.toString(address.getPort())));
KingRainbow44's avatar
KingRainbow44 committed
248
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
249
250
251
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
252
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
253

Melledy's avatar
Melledy committed
254
		// Kick and save all players
255
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
256
257
		list.addAll(this.getPlayers().values());
		
258
		for (Player player : list) {
Melledy's avatar
Melledy committed
259
260
261
262
			player.getSession().close();
		}
	}
}