GameServer.java 7.03 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;
Melledy's avatar
Melledy committed
18
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
19
20
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
21
import emu.grasscutter.netty.KcpServer;
KingRainbow44's avatar
KingRainbow44 committed
22
import emu.grasscutter.server.event.types.ServerEvent;
KingRainbow44's avatar
KingRainbow44 committed
23
24
25
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
26
import emu.grasscutter.task.TaskMap;
Melledy's avatar
Melledy committed
27

Kengxxiao's avatar
Kengxxiao committed
28
29
30
31
import java.net.InetSocketAddress;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
32
33
34
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Kengxxiao's avatar
Kengxxiao committed
35

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

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

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

	private final CombineManger combineManger;

58
59
60
61
62
63
64
	public GameServer() {
		this(new InetSocketAddress(
				Grasscutter.getConfig().getGameServerOptions().Ip, 
				Grasscutter.getConfig().getGameServerOptions().Port
		));
	}
	
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);
Melledy's avatar
Melledy committed
85
		
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

Yazawazi's avatar
Yazawazi committed
142
143
144
	public TaskMap getTaskMap() {
		return this.taskMap;
	}
Melledy's avatar
Melledy committed
145
	
146
	public void registerPlayer(Player player) {
147
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
148
149
	}

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

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

237
238
239
		super.start();
	}

Melledy's avatar
Melledy committed
240
241
	@Override
	public void onStartFinish() {
242
243
		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
244
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
245
246
247
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
248
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
249

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