GameServer.java 5.78 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
package emu.grasscutter.server.game;

import java.net.InetSocketAddress;
KingRainbow44's avatar
KingRainbow44 committed
4
import java.time.OffsetDateTime;
Melledy's avatar
Melledy committed
5
import java.util.*;
Melledy's avatar
Melledy committed
6
7
import java.util.concurrent.ConcurrentHashMap;

8
import emu.grasscutter.GameConstants;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.Grasscutter;
Jaida Wu's avatar
Jaida Wu committed
10
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.Account;
Melledy's avatar
Melledy committed
13
14
15
16
17
import emu.grasscutter.game.dungeons.DungeonManager;
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
18
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.game.shop.ShopManager;
Melledy's avatar
Melledy committed
20
import emu.grasscutter.game.world.World;
Melledy's avatar
Melledy committed
21
22
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
23
import emu.grasscutter.netty.KcpServer;
KingRainbow44's avatar
KingRainbow44 committed
24
25
26
27
import emu.grasscutter.server.event.ServerEvent;
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
28
import emu.grasscutter.task.TaskMap;
Melledy's avatar
Melledy committed
29

30
public final class GameServer extends KcpServer {
Melledy's avatar
Melledy committed
31
32
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
Melledy's avatar
Melledy committed
33

34
	private final Map<Integer, Player> players;
35
	private final Set<World> worlds;
Melledy's avatar
Melledy committed
36
37
38
39
40
41
42
	
	private final ChatManager chatManager;
	private final InventoryManager inventoryManager;
	private final GachaManager gachaManager;
	private final ShopManager shopManager;
	private final MultiplayerManager multiplayerManager;
	private final DungeonManager dungeonManager;
Melledy's avatar
Melledy committed
43
	private final CommandMap commandMap;
Yazawazi's avatar
Yazawazi committed
44
	private final TaskMap taskMap;
Melledy's avatar
Melledy committed
45
46
47
	
	public GameServer(InetSocketAddress address) {
		super(address);
48

Melledy's avatar
Melledy committed
49
50
51
52
		this.setServerInitializer(new GameServerInitializer(this));
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
53
		this.worlds = Collections.synchronizedSet(new HashSet<>());
Melledy's avatar
Melledy committed
54
55
56
57
58
59
60
		
		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
61
		this.commandMap = new CommandMap(true);
Yazawazi's avatar
Yazawazi committed
62
		this.taskMap = new TaskMap(true);
Melledy's avatar
Melledy committed
63
		
Melledy's avatar
Melledy committed
64
65
66
		// Schedule game loop.
		Timer gameLoop = new Timer();
		gameLoop.scheduleAtFixedRate(new TimerTask() {
Melledy's avatar
Melledy committed
67
68
69
70
71
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
Melledy's avatar
Melledy committed
72
					Grasscutter.getLogger().error("An error occurred during game update.", e);
Melledy's avatar
Melledy committed
73
74
75
76
				}
			}
		}, new Date(), 1000L);
		
Melledy's avatar
Melledy committed
77
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
78
79
80
81
82
83
84
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
	
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

85
	public Map<Integer, Player> getPlayers() {
Melledy's avatar
Melledy committed
86
87
88
		return players;
	}

89
90
91
92
	public Set<World> getWorlds() {
		return worlds;
	}

Melledy's avatar
Melledy committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	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;
	}
	
	public DungeonManager getDungeonManager() {
		return dungeonManager;
	}
	
Melledy's avatar
Melledy committed
117
118
119
	public CommandMap getCommandMap() {
		return this.commandMap;
	}
Yazawazi's avatar
Yazawazi committed
120
121
122
123

	public TaskMap getTaskMap() {
		return this.taskMap;
	}
Melledy's avatar
Melledy committed
124
	
125
	public void registerPlayer(Player player) {
126
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
127
128
	}

129
	public Player getPlayerByUid(int id) {
130
		return this.getPlayerByUid(id, false);
Melledy's avatar
Melledy committed
131
132
	}
	
133
	public Player getPlayerByUid(int id, boolean allowOfflinePlayers) {
Melledy's avatar
Melledy committed
134
		// Console check
135
		if (id == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
136
137
138
139
			return null;
		}
		
		// Get from online players
140
		Player player = this.getPlayers().get(id);
141
142
143
144
		
		if (!allowOfflinePlayers) {
			return player;
		}
Melledy's avatar
Melledy committed
145
146
147
148
149
150
151
152
153
		
		// Check database if character isnt here
		if (player == null) {
			player = DatabaseHelper.getPlayerById(id);
		}
		
		return player;
	}
	
154
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
155
		// Get from online players
156
		Player player = this.getPlayerByUid(id, true);
Melledy's avatar
Melledy committed
157
158
159
160
161
162
163
164
	
		if (player == null) {
			return null;
		}
		
		return player.getSocialDetail();
	}
	
Melledy's avatar
Melledy committed
165
	public Account getAccountByName(String username) {
166
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
167
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
168
169
170
171
172
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
	
Melledy's avatar
Melledy committed
173
	public void onTick() throws Exception {
174
175
176
177
178
179
180
181
182
183
		Iterator<World> it = this.getWorlds().iterator();
		while (it.hasNext()) {
			World world = it.next();
			
			if (world.getPlayerCount() == 0) {
				it.remove();
			}
			
			world.onTick();
		}
Magix's avatar
Magix committed
184
185
  
    ServerTickEvent event = new ServerTickEvent(); event.call();
Melledy's avatar
Melledy committed
186
	}
187
188
189
190
191
192
193
194
195
	
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
	
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
		
	}
Melledy's avatar
Melledy committed
196
197
198
199

	@Override
	public void onStartFinish() {
		Grasscutter.getLogger().info("Game Server started on port " + address.getPort());
KingRainbow44's avatar
KingRainbow44 committed
200
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
Melledy's avatar
Melledy committed
201
202
203
	}
	
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
204
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
205

Melledy's avatar
Melledy committed
206
		// Kick and save all players
207
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
208
209
		list.addAll(this.getPlayers().values());
		
210
		for (Player player : list) {
Melledy's avatar
Melledy committed
211
212
213
214
			player.getSession().close();
		}
	}
}