GameServer.java 6.83 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
32
import java.net.InetSocketAddress;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

33
34
import static emu.grasscutter.utils.Language.translate;

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

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

	private final CombineManger combineManger;

55
56
57
58
59
60
61
	public GameServer() {
		this(new InetSocketAddress(
				Grasscutter.getConfig().getGameServerOptions().Ip, 
				Grasscutter.getConfig().getGameServerOptions().Port
		));
	}
	
Melledy's avatar
Melledy committed
62
63
	public GameServer(InetSocketAddress address) {
		super(address);
64

Melledy's avatar
Melledy committed
65
66
67
68
		this.setServerInitializer(new GameServerInitializer(this));
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
69
		this.worlds = Collections.synchronizedSet(new HashSet<>());
Melledy's avatar
Melledy committed
70
71
72
73
74
75
76
		
		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
77
		this.commandMap = new CommandMap(true);
Yazawazi's avatar
Yazawazi committed
78
		this.taskMap = new TaskMap(true);
Kengxxiao's avatar
Kengxxiao committed
79
		this.dropManager = new DropManager(this);
Kinesis's avatar
Kinesis committed
80
		this.expeditionManager = new ExpeditionManager(this);
Akka's avatar
Akka committed
81
82
		this.combineManger = new CombineManger(this);

Melledy's avatar
Melledy committed
83
84
85
		// Schedule game loop.
		Timer gameLoop = new Timer();
		gameLoop.scheduleAtFixedRate(new TimerTask() {
Melledy's avatar
Melledy committed
86
87
88
89
90
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
91
					Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
Melledy's avatar
Melledy committed
92
93
94
95
				}
			}
		}, new Date(), 1000L);
		
Melledy's avatar
Melledy committed
96
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
97
98
99
100
101
102
103
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
	
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

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

108
109
110
111
	public Set<World> getWorlds() {
		return worlds;
	}

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

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

	public ExpeditionManager getExpeditionManager() {
		return expeditionManager;
	}

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

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

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

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

	@Override
	public void onStartFinish() {
234
235
		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
236
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
237
238
239
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
240
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
241

Melledy's avatar
Melledy committed
242
		// Kick and save all players
243
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
244
245
		list.addAll(this.getPlayers().values());
		
246
		for (Player player : list) {
Melledy's avatar
Melledy committed
247
248
249
250
			player.getSession().close();
		}
	}
}