GameServer.java 8.72 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;
8
import emu.grasscutter.game.battlepass.BattlePassMissionManager;
Akka's avatar
Akka committed
9
import emu.grasscutter.game.combine.CombineManger;
Kengxxiao's avatar
Kengxxiao committed
10
import emu.grasscutter.game.drop.DropManager;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.dungeons.DungeonManager;
AnimeGitB's avatar
AnimeGitB committed
12
import emu.grasscutter.game.dungeons.challenge.DungeonChallenge;
Kinesis's avatar
Kinesis committed
13
import emu.grasscutter.game.expedition.ExpeditionManager;
Melledy's avatar
Melledy committed
14
15
16
import emu.grasscutter.game.gacha.GachaManager;
import emu.grasscutter.game.managers.InventoryManager;
import emu.grasscutter.game.managers.MultiplayerManager;
Melledy's avatar
Melledy committed
17
18
import emu.grasscutter.game.managers.chat.ChatManager;
import emu.grasscutter.game.managers.chat.ChatManagerHandler;
AnimeGitB's avatar
AnimeGitB committed
19
20
import emu.grasscutter.game.managers.energy.EnergyManager;
import emu.grasscutter.game.managers.stamina.StaminaManager;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.game.player.Player;
22
import emu.grasscutter.game.quest.ServerQuestHandler;
Melledy's avatar
Melledy committed
23
import emu.grasscutter.game.shop.ShopManager;
24
import emu.grasscutter.game.tower.TowerScheduleManager;
Melledy's avatar
Melledy committed
25
import emu.grasscutter.game.world.World;
Akka's avatar
Akka committed
26
import emu.grasscutter.game.world.WorldDataManager;
Melledy's avatar
Melledy committed
27
28
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
KingRainbow44's avatar
KingRainbow44 committed
29
import emu.grasscutter.server.event.types.ServerEvent;
KingRainbow44's avatar
KingRainbow44 committed
30
31
32
import emu.grasscutter.server.event.game.ServerTickEvent;
import emu.grasscutter.server.event.internal.ServerStartEvent;
import emu.grasscutter.server.event.internal.ServerStopEvent;
33
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
Yazawazi's avatar
Yazawazi committed
34
import emu.grasscutter.task.TaskMap;
35
36
import kcp.highway.ChannelConfig;
import kcp.highway.KcpServer;
37
import lombok.Getter;
Melledy's avatar
Melledy committed
38

Kengxxiao's avatar
Kengxxiao committed
39
import java.net.InetSocketAddress;
40
import java.time.Instant;
Kengxxiao's avatar
Kengxxiao committed
41
42
43
44
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

45
import static emu.grasscutter.utils.Language.translate;
46
import static emu.grasscutter.Configuration.*;
47

48
public final class GameServer extends KcpServer {
Melledy's avatar
Melledy committed
49
50
	private final InetSocketAddress address;
	private final GameServerPacketHandler packetHandler;
51
	private final ServerQuestHandler questHandler;
52
53
    @Getter private final ServerTaskScheduler scheduler;

54
	private final Map<Integer, Player> players;
55
	private final Set<World> worlds;
56

57
	private ChatManagerHandler chatManager;
58
59
60
61
62
63
64
65
66
67
68
69
70
	@Getter private final InventoryManager inventoryManager;
	@Getter private final GachaManager gachaManager;
	@Getter private final ShopManager shopManager;
	@Getter private final MultiplayerManager multiplayerManager;
	@Getter private final DungeonManager dungeonManager;
	@Getter private final ExpeditionManager expeditionManager;
	@Getter private final CommandMap commandMap;
	@Getter private final TaskMap taskMap;
	@Getter private final DropManager dropManager;
	@Getter private final WorldDataManager worldDataManager;
	@Getter private final BattlePassMissionManager battlePassMissionManager;
	@Getter private final CombineManger combineManger;
	@Getter private final TowerScheduleManager towerScheduleManager;
Akka's avatar
Akka committed
71

72
	public GameServer() {
73
		this(getAdapterInetSocketAddress());
74
	}
75

Melledy's avatar
Melledy committed
76
	public GameServer(InetSocketAddress address) {
77
78
79
80
81
82
83
84
85
86
		ChannelConfig channelConfig = new ChannelConfig();
		channelConfig.nodelay(true,40,2,true);
		channelConfig.setMtu(1400);
		channelConfig.setSndwnd(256);
		channelConfig.setRcvwnd(256);
		channelConfig.setTimeoutMillis(30*1000);//30s
		channelConfig.setUseConvChannel(true);
		channelConfig.setAckNoDelay(false);

		this.init(GameSessionManager.getListener(),channelConfig,address);
87

AnimeGitB's avatar
AnimeGitB committed
88
89
90
91
		DungeonChallenge.initialize();
		EnergyManager.initialize();
		StaminaManager.initialize();

Melledy's avatar
Melledy committed
92
93
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
94
		this.questHandler = new ServerQuestHandler();
95
        this.scheduler = new ServerTaskScheduler();
Melledy's avatar
Melledy committed
96
		this.players = new ConcurrentHashMap<>();
97
		this.worlds = Collections.synchronizedSet(new HashSet<>());
98

Melledy's avatar
Melledy committed
99
100
101
102
103
104
		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
105
		this.commandMap = new CommandMap(true);
Yazawazi's avatar
Yazawazi committed
106
		this.taskMap = new TaskMap(true);
Kengxxiao's avatar
Kengxxiao committed
107
		this.dropManager = new DropManager(this);
Kinesis's avatar
Kinesis committed
108
		this.expeditionManager = new ExpeditionManager(this);
Akka's avatar
Akka committed
109
		this.combineManger = new CombineManger(this);
110
		this.towerScheduleManager = new TowerScheduleManager(this);
Akka's avatar
Akka committed
111
		this.worldDataManager = new WorldDataManager(this);
112
113
		this.battlePassMissionManager = new BattlePassMissionManager(this);
		
Melledy's avatar
Melledy committed
114
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
115
116
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
117

Melledy's avatar
Melledy committed
118
119
120
121
	public GameServerPacketHandler getPacketHandler() {
		return packetHandler;
	}

122
123
124
125
	public ServerQuestHandler getQuestHandler() {
		return questHandler;
	}

126
	public Map<Integer, Player> getPlayers() {
Melledy's avatar
Melledy committed
127
128
129
		return players;
	}

130
131
132
133
	public Set<World> getWorlds() {
		return worlds;
	}

134
	public ChatManagerHandler getChatManager() {
Melledy's avatar
Melledy committed
135
136
		return chatManager;
	}
137

138
139
140
	public void setChatManager(ChatManagerHandler chatManager) {
		this.chatManager = chatManager;
	}
Melledy's avatar
Melledy committed
141

142

143
144
145
146
147
148
149
150
151
152
153
154
	private static InetSocketAddress getAdapterInetSocketAddress(){
		InetSocketAddress inetSocketAddress;
		if(GAME_INFO.bindAddress.equals("")){
			inetSocketAddress=new InetSocketAddress(GAME_INFO.bindPort);
		}else{
			inetSocketAddress=new InetSocketAddress(
					GAME_INFO.bindAddress,
					GAME_INFO.bindPort
			);
		}
		return inetSocketAddress;
	}
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
			return null;
		}
169

Melledy's avatar
Melledy committed
170
		// Get from online players
171
		Player player = this.getPlayers().get(id);
172

173
174
175
		if (!allowOfflinePlayers) {
			return player;
		}
176

Melledy's avatar
Melledy committed
177
178
		// Check database if character isnt here
		if (player == null) {
179
			player = DatabaseHelper.getPlayerByUid(id);
Melledy's avatar
Melledy committed
180
		}
181

Melledy's avatar
Melledy committed
182
183
		return player;
	}
184

185
186
187
188
	public Player getPlayerByAccountId(String accountId) {
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getId().equals(accountId)).findFirst();
		return playerOpt.orElse(null);
	}
189

190
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
191
		// Get from online players
192
		Player player = this.getPlayerByUid(id, true);
193

Melledy's avatar
Melledy committed
194
195
196
		if (player == null) {
			return null;
		}
197

Melledy's avatar
Melledy committed
198
199
		return player.getSocialDetail();
	}
200

Melledy's avatar
Melledy committed
201
	public Account getAccountByName(String username) {
202
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
203
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
204
205
206
207
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
208

209
210
    public synchronized void onTick() {
        var tickStart = Instant.now();
211

212
213
214
215
        // Tick worlds.
        Iterator<World> it = this.getWorlds().iterator();
        while (it.hasNext()) {
            World world = it.next();
216

217
218
219
            if (world.getPlayerCount() == 0) {
                it.remove();
            }
220

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
            world.onTick();
        }

        // Tick players.
        for (Player player : this.getPlayers().values()) {
            player.onTick();
        }

        // Tick scheduler.
        this.getScheduler().runTasks();

        // Call server tick event.
        ServerTickEvent event = new ServerTickEvent(tickStart, Instant.now());
        event.call();
    }
236

237
238
239
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
240

241
242
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
243

244
	}
Melledy's avatar
Melledy committed
245

246
	public void start() {
247
		// Schedule game loop.
Bwly999's avatar
Bwly999 committed
248
		Timer gameLoop = new Timer();
249
250
251
252
253
254
		gameLoop.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
255
					Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
256
257
				}
			}
Bwly999's avatar
Bwly999 committed
258
		}, new Date(), 1000L);
259
260
		Grasscutter.getLogger().info(translate("messages.status.free_software"));
		Grasscutter.getLogger().info(translate("messages.game.port_bind", Integer.toString(address.getPort())));
261
262
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now());
		event.call();
Melledy's avatar
Melledy committed
263
	}
264

Melledy's avatar
Melledy committed
265
	public void onServerShutdown() {
KingRainbow44's avatar
KingRainbow44 committed
266
		ServerStopEvent event = new ServerStopEvent(ServerEvent.Type.GAME, OffsetDateTime.now()); event.call();
267

Melledy's avatar
Melledy committed
268
		// Kick and save all players
269
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
270
		list.addAll(this.getPlayers().values());
271

272
		for (Player player : list) {
Melledy's avatar
Melledy committed
273
274
275
276
			player.getSession().close();
		}
	}
}