GameServer.java 8.89 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.BattlePassSystem;
Akka's avatar
Akka committed
9
import emu.grasscutter.game.combine.CombineManger;
10
11
import emu.grasscutter.game.drop.DropSystem;
import emu.grasscutter.game.dungeons.DungeonSystem;
AnimeGitB's avatar
AnimeGitB committed
12
import emu.grasscutter.game.dungeons.challenge.DungeonChallenge;
13
14
15
import emu.grasscutter.game.expedition.ExpeditionSystem;
import emu.grasscutter.game.gacha.GachaSystem;
import emu.grasscutter.game.managers.AnnouncementSystem;
16
import emu.grasscutter.game.managers.CookingManager;
17
18
import emu.grasscutter.game.managers.InventorySystem;
import emu.grasscutter.game.managers.MultiplayerSystem;
Melledy's avatar
Melledy committed
19
20
import emu.grasscutter.game.managers.chat.ChatManager;
import emu.grasscutter.game.managers.chat.ChatManagerHandler;
AnimeGitB's avatar
AnimeGitB committed
21
22
import emu.grasscutter.game.managers.energy.EnergyManager;
import emu.grasscutter.game.managers.stamina.StaminaManager;
Melledy's avatar
Melledy committed
23
import emu.grasscutter.game.player.Player;
24
25
26
import emu.grasscutter.game.quest.QuestSystem;
import emu.grasscutter.game.shop.ShopSystem;
import emu.grasscutter.game.tower.TowerSystem;
Melledy's avatar
Melledy committed
27
import emu.grasscutter.game.world.World;
28
import emu.grasscutter.game.world.WorldDataSystem;
Melledy's avatar
Melledy committed
29
30
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
KingRainbow44's avatar
KingRainbow44 committed
31
import emu.grasscutter.server.event.types.ServerEvent;
KingRainbow44's avatar
KingRainbow44 committed
32
33
34
import emu.grasscutter.server.event.game.ServerTickEvent;
import emu.grasscutter.server.event.internal.ServerStartEvent;
import emu.grasscutter.server.event.internal.ServerStopEvent;
35
import emu.grasscutter.server.scheduler.ServerTaskScheduler;
Yazawazi's avatar
Yazawazi committed
36
import emu.grasscutter.task.TaskMap;
37
38
import kcp.highway.ChannelConfig;
import kcp.highway.KcpServer;
39
import lombok.Getter;
Melledy's avatar
Melledy committed
40

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

47
import static emu.grasscutter.utils.Language.translate;
48
import static emu.grasscutter.Configuration.*;
49

50
@Getter
51
public final class GameServer extends KcpServer {
52
53
    // Game server base
    private final InetSocketAddress address;
Melledy's avatar
Melledy committed
54
	private final GameServerPacketHandler packetHandler;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    private final Map<Integer, Player> players;
    private final Set<World> worlds;

    // Server systems
	private final InventorySystem inventorySystem;
	private final GachaSystem gachaSystem;
	private final ShopSystem shopSystem;
	private final MultiplayerSystem multiplayerSystem;
	private final DungeonSystem dungeonSystem;
	private final ExpeditionSystem expeditionSystem;
	private final DropSystem dropSystem;
	private final WorldDataSystem worldDataSystem;
	private final BattlePassSystem battlePassSystem;
	private final CombineManger combineSystem;
	private final TowerSystem towerSystem;
	private final AnnouncementSystem announcementSystem;
	private final QuestSystem questSystem;
	
	// Extra
	private final ServerTaskScheduler scheduler;
    private final CommandMap commandMap;
    private final TaskMap taskMap;
	
78
	private ChatManagerHandler chatManager;
Akka's avatar
Akka committed
79

80
	public GameServer() {
81
		this(getAdapterInetSocketAddress());
82
	}
83

Melledy's avatar
Melledy committed
84
	public GameServer(InetSocketAddress address) {
85
		ChannelConfig channelConfig = new ChannelConfig();
86
		channelConfig.nodelay(true, 40, 2, true);
87
88
89
		channelConfig.setMtu(1400);
		channelConfig.setSndwnd(256);
		channelConfig.setRcvwnd(256);
90
		channelConfig.setTimeoutMillis(30 * 1000);//30s
91
92
93
94
		channelConfig.setUseConvChannel(true);
		channelConfig.setAckNoDelay(false);

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

AnimeGitB's avatar
AnimeGitB committed
96
97
98
		DungeonChallenge.initialize();
		EnergyManager.initialize();
		StaminaManager.initialize();
99
		CookingManager.initialize();
100
		CombineManger.initialize();
AnimeGitB's avatar
AnimeGitB committed
101

102
		// Game Server base
Melledy's avatar
Melledy committed
103
104
105
		this.address = address;
		this.packetHandler = new GameServerPacketHandler(PacketHandler.class);
		this.players = new ConcurrentHashMap<>();
106
		this.worlds = Collections.synchronizedSet(new HashSet<>());
107
108
109
		
		// Extra
		this.scheduler = new ServerTaskScheduler();
Melledy's avatar
Melledy committed
110
		this.commandMap = new CommandMap(true);
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        this.taskMap = new TaskMap(true);

		// Create game systems
		this.inventorySystem = new InventorySystem(this);
		this.gachaSystem = new GachaSystem(this);
		this.shopSystem = new ShopSystem(this);
		this.multiplayerSystem = new MultiplayerSystem(this);
		this.dungeonSystem = new DungeonSystem(this);
		this.dropSystem = new DropSystem(this);
		this.expeditionSystem = new ExpeditionSystem(this);
		this.combineSystem = new CombineManger(this);
		this.towerSystem = new TowerSystem(this);
		this.worldDataSystem = new WorldDataSystem(this);
		this.battlePassSystem = new BattlePassSystem(this);
		this.announcementSystem = new AnnouncementSystem(this);
		this.questSystem = new QuestSystem(this);
		
		// Chata manager
		this.chatManager = new ChatManager(this);
		
Melledy's avatar
Melledy committed
131
		// Hook into shutdown event.
Melledy's avatar
Melledy committed
132
133
		Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
	}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	
    @Deprecated
    public ChatManagerHandler getChatManager() {
        return chatManager;
    }
    
    @Deprecated
    public void setChatManager(ChatManagerHandler chatManager) {
        this.chatManager = chatManager;
    }
    
    public ChatManagerHandler getChatSystem() {
        return chatManager;
    }

    public void setChatSystem(ChatManagerHandler chatManager) {
        this.chatManager = chatManager;
    }
152

153
154
155
156
157
158
159
160
161
162
163
164
	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;
	}
165

166
	public void registerPlayer(Player player) {
167
		getPlayers().put(player.getUid(), player);
Melledy's avatar
Melledy committed
168
169
	}

170
	public Player getPlayerByUid(int id) {
171
		return this.getPlayerByUid(id, false);
Melledy's avatar
Melledy committed
172
	}
173

174
	public Player getPlayerByUid(int id, boolean allowOfflinePlayers) {
Melledy's avatar
Melledy committed
175
		// Console check
176
		if (id == GameConstants.SERVER_CONSOLE_UID) {
Melledy's avatar
Melledy committed
177
178
			return null;
		}
179

Melledy's avatar
Melledy committed
180
		// Get from online players
181
		Player player = this.getPlayers().get(id);
182

183
184
185
		if (!allowOfflinePlayers) {
			return player;
		}
186

Melledy's avatar
Melledy committed
187
188
		// Check database if character isnt here
		if (player == null) {
189
			player = DatabaseHelper.getPlayerByUid(id);
Melledy's avatar
Melledy committed
190
		}
191

Melledy's avatar
Melledy committed
192
193
		return player;
	}
194

195
196
197
198
	public Player getPlayerByAccountId(String accountId) {
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getId().equals(accountId)).findFirst();
		return playerOpt.orElse(null);
	}
199

200
	public SocialDetail.Builder getSocialDetailByUid(int id) {
Melledy's avatar
Melledy committed
201
		// Get from online players
202
		Player player = this.getPlayerByUid(id, true);
203

Melledy's avatar
Melledy committed
204
205
206
		if (player == null) {
			return null;
		}
207

Melledy's avatar
Melledy committed
208
209
		return player.getSocialDetail();
	}
210

Melledy's avatar
Melledy committed
211
	public Account getAccountByName(String username) {
212
		Optional<Player> playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst();
213
		if (playerOpt.isPresent()) {
Melledy's avatar
Melledy committed
214
215
216
217
			return playerOpt.get().getAccount();
		}
		return DatabaseHelper.getAccountByName(username);
	}
218

219
220
    public synchronized void onTick() {
        var tickStart = Instant.now();
221

222
223
224
225
        // Tick worlds.
        Iterator<World> it = this.getWorlds().iterator();
        while (it.hasNext()) {
            World world = it.next();
226

227
228
229
            if (world.getPlayerCount() == 0) {
                it.remove();
            }
230

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
            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();
    }
246

247
248
249
	public void registerWorld(World world) {
		this.getWorlds().add(world);
	}
250

251
252
	public void deregisterWorld(World world) {
		// TODO Auto-generated method stub
253

254
	}
Melledy's avatar
Melledy committed
255

256
	public void start() {
257
		// Schedule game loop.
Bwly999's avatar
Bwly999 committed
258
		Timer gameLoop = new Timer();
259
260
261
262
263
264
		gameLoop.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				try {
					onTick();
				} catch (Exception e) {
265
					Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
266
267
				}
			}
Bwly999's avatar
Bwly999 committed
268
		}, new Date(), 1000L);
269
270
		Grasscutter.getLogger().info(translate("messages.status.free_software"));
		Grasscutter.getLogger().info(translate("messages.game.port_bind", Integer.toString(address.getPort())));
271
272
		ServerStartEvent event = new ServerStartEvent(ServerEvent.Type.GAME, OffsetDateTime.now());
		event.call();
Melledy's avatar
Melledy committed
273
	}
274

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

Melledy's avatar
Melledy committed
278
		// Kick and save all players
279
		List<Player> list = new ArrayList<>(this.getPlayers().size());
Melledy's avatar
Melledy committed
280
		list.addAll(this.getPlayers().values());
281

282
		for (Player player : list) {
Melledy's avatar
Melledy committed
283
284
285
286
			player.getSession().close();
		}
	}
}