GameServer.java 7.29 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
import java.net.InetSocketAddress;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
33
34
35
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Kengxxiao's avatar
Kengxxiao committed
36

37
38
import static emu.grasscutter.utils.Language.translate;

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

43
	private final Map<Integer, Player> players;
44
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
45
46
47
48
49
50
51
	
	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
52
	private final ExpeditionManager expeditionManager;
Melledy's avatar
Melledy committed
53
	private final CommandMap commandMap;
Yazawazi's avatar
Yazawazi committed
54
	private final TaskMap taskMap;
Kengxxiao's avatar
Kengxxiao committed
55
	private final DropManager dropManager;
Akka's avatar
Akka committed
56
57

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

60
61
62
63
64
65
66
	public GameServer() {
		this(new InetSocketAddress(
				Grasscutter.getConfig().getGameServerOptions().Ip, 
				Grasscutter.getConfig().getGameServerOptions().Port
		));
	}
	
Melledy's avatar
Melledy committed
67
68
	public GameServer(InetSocketAddress address) {
		super(address);
69

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

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

100
101
102
103
	public Set<World> getWorlds() {
		return worlds;
	}

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

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

	public ExpeditionManager getExpeditionManager() {
		return expeditionManager;
	}

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

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

144
145
146
147
	public TowerScheduleManager getTowerScheduleManager() {
		return towerScheduleManager;
	}

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

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

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

243
244
245
		super.start();
	}

Melledy's avatar
Melledy committed
246
247
	@Override
	public void onStartFinish() {
248
249
		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
250
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
251
252
253
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
254
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
255

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