GameServer.java 7.42 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;
17
18
import emu.grasscutter.game.quest.ServerQuestHandler;
import emu.grasscutter.game.quest.handlers.QuestBaseHandler;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.game.shop.ShopManager;
20
import emu.grasscutter.game.tower.TowerScheduleManager;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
22
23
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
24
import emu.grasscutter.netty.KcpServer;
KingRainbow44's avatar
KingRainbow44 committed
25
import emu.grasscutter.server.event.types.ServerEvent;
KingRainbow44's avatar
KingRainbow44 committed
26
27
28
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
29
import emu.grasscutter.task.TaskMap;
Melledy's avatar
Melledy committed
30

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

36
import static emu.grasscutter.utils.Language.translate;
37
import static emu.grasscutter.Configuration.*;
38

39
public final class GameServer extends KcpServer {
Melledy's avatar
Melledy committed
40
41
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
42
43
	private final ServerQuestHandler questHandler;
	
44
	private final Map<Integer, Player> players;
45
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
46
47
48
49
50
51
52
	
	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
53
	private final ExpeditionManager expeditionManager;
Melledy's avatar
Melledy committed
54
	private final CommandMap commandMap;
Yazawazi's avatar
Yazawazi committed
55
	private final TaskMap taskMap;
Kengxxiao's avatar
Kengxxiao committed
56
	private final DropManager dropManager;
Akka's avatar
Akka committed
57
58

	private final CombineManger combineManger;
59
	private final TowerScheduleManager towerScheduleManager;
Akka's avatar
Akka committed
60

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

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

98
99
100
101
	public ServerQuestHandler getQuestHandler() {
		return questHandler;
	}

102
	public Map<Integer, Player> getPlayers() {
Melledy's avatar
Melledy committed
103
104
105
		return players;
	}

106
107
108
109
	public Set<World> getWorlds() {
		return worlds;
	}

Melledy's avatar
Melledy committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	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
129
130
131
132

	public DropManager getDropManager() {
		return dropManager;
	}
Melledy's avatar
Melledy committed
133
134
135
136
	
	public DungeonManager getDungeonManager() {
		return dungeonManager;
	}
Kinesis's avatar
Kinesis committed
137
138
139
140
141

	public ExpeditionManager getExpeditionManager() {
		return expeditionManager;
	}

Melledy's avatar
Melledy committed
142
143
144
	public CommandMap getCommandMap() {
		return this.commandMap;
	}
Yazawazi's avatar
Yazawazi committed
145

Akka's avatar
Akka committed
146
147
148
	public CombineManger getCombineManger(){
		return this.combineManger;
	}
Kinesis's avatar
Kinesis committed
149

150
151
152
153
	public TowerScheduleManager getTowerScheduleManager() {
		return towerScheduleManager;
	}

Yazawazi's avatar
Yazawazi committed
154
155
156
	public TaskMap getTaskMap() {
		return this.taskMap;
	}
Melledy's avatar
Melledy committed
157
	
158
	public void registerPlayer(Player player) {
159
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
160
161
	}

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

234
235
236
	@Override
	public synchronized void start() {
		// Schedule game loop.
Bwly999's avatar
Bwly999 committed
237
		Timer gameLoop = new Timer();
238
239
240
241
242
243
		gameLoop.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
244
					Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
245
246
				}
			}
Bwly999's avatar
Bwly999 committed
247
248
		}, new Date(), 1000L);

249
250
251
		super.start();
	}

Melledy's avatar
Melledy committed
252
253
	@Override
	public void onStartFinish() {
254
255
		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
256
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
257
258
259
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
260
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
261

Melledy's avatar
Melledy committed
262
		// Kick and save all players
263
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
264
265
		list.addAll(this.getPlayers().values());
		
266
		for (Player player : list) {
Melledy's avatar
Melledy committed
267
268
269
270
			player.getSession().close();
		}
	}
}