Example #1
0
    private async Task DoServerPostProcessing(Server server)
    {
        string serverPath = Path.Combine(_application.EntityPath, server.Name);

        // Read settings files
        server.VanillaSettings =
            new VanillaSettings(
                await _fileReader.ReadVanillaSettingsAsync(serverPath));
        server.ServerPlayers ??= new List <ServerPlayer>();

        // Update changed players by comparing database with files
        // TODO extend this if multiple worlds are possible
        List <string> worlds = new List <string> {
            Path.Combine(serverPath, server.VanillaSettings.LevelName)
        };
        var playersInWorlds = await UidsToPlayersAsync(await _playerService.PlayerUidsForWorldsAsync(worlds));

        foreach (Player player in playersInWorlds)
        {
            var serverPlayer = server.ServerPlayers.FirstOrDefault(s => s.Player.Uid == player.Uid);
            if (serverPlayer != null)
            {
                serverPlayer.Player.Name            = player.Name;
                serverPlayer.Player.Head            = player.Head;
                serverPlayer.Player.LastUpdated     = player.LastUpdated;
                serverPlayer.Player.IsOfflinePlayer = player.IsOfflinePlayer;
            }
            else
            {
                server.ServerPlayers.Add(new ServerPlayer {
                    Player = player, Server = server, ServerId = server.Id
                });
            }
        }

        // Read whitelist, banlist and oplist
        // Version 1.7.5 and earlier have txt files
        if (server.Version.CompareTo(new ServerVersion {
            Version = "1.7.5"
        }) <= 0)
        {
            var whitelistNames = await _fileReader.ReadWhiteListTxt(serverPath);

            server.Whitelist = await NamesToPlayersAsync(whitelistNames);

            var banListNames = await _fileReader.ReadBanListTxt(serverPath);

            server.Banlist = await NamesToPlayersAsync(banListNames);

            var opListNames = await _fileReader.ReadOpListTxt(serverPath);

            foreach (string opName in opListNames)
            {
                var serverPlayer = server.ServerPlayers.FirstOrDefault(p => p.Player.Name == opName);
                if (serverPlayer != null)
                {
                    serverPlayer.IsOp = true;
                }
            }
        }
        else
        {
            var whitelistUids = await _fileReader.ReadWhiteListJson(serverPath);

            server.Whitelist = await UidsToPlayersAsync(whitelistUids);

            var banListUids = await _fileReader.ReadBanListJson(serverPath);

            server.Banlist = await UidsToPlayersAsync(banListUids);

            var opListUids = await _fileReader.ReadOpListJson(serverPath);

            foreach (string opUid in opListUids)
            {
                var serverPlayer = server.ServerPlayers.FirstOrDefault(p => p.Player.Uid == opUid.Replace("-", ""));
                if (serverPlayer != null)
                {
                    serverPlayer.IsOp = true;
                }
            }
        }
    }