Beispiel #1
0
 public static ServerBans CreatePavlovServerBans(ServerBansService serverBansService, PavlovServer pavlovServer)
 {
     serverBansService.Upsert(
         new ServerBans
     {
         SteamId        = "test",
         SteamName      = "test",
         BannedDateTime = DateTime.Now,
         BanSpan        = new TimeSpan(0, 0, 1),
         Comment        = "test",
         PavlovServer   = pavlovServer
     }).GetAwaiter().GetResult();
     return(serverBansService.FindOne(pavlovServer.Id).GetAwaiter().GetResult());
 }
        public async Task <IActionResult> ImportBans(PavlovServerImportBansListViewModel viewModel)
        {
            if (viewModel.ServerId <= 0)
            {
                return(BadRequest("Please choose a server!"));
            }

            var user = await _userservice.getUserFromCp(HttpContext.User);

            if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User, user
                                                                 , viewModel.ServerId, _service,
                                                                 _pavlovServerService))
            {
                return(Forbid());
            }
            var server = await _pavlovServerService.FindOne(viewModel.ServerId);

            await using var ms = new MemoryStream();
            await viewModel.BansFromFile.CopyToAsync(ms);

            var fileBytes = ms.ToArray();

            var table = (Encoding.Default.GetString(
                             fileBytes,
                             0,
                             fileBytes.Length - 1)).Split(new string[] { "\r\n", "\r", "\n" },
                                                          StringSplitOptions.None);
            var timespan = "unlimited";

            Statics.BanList.TryGetValue(timespan, out var timespans);
            if (string.IsNullOrEmpty(timespan))
            {
                return(BadRequest("TimeSpan  must be set!"));
            }
            var banlist = Array.Empty <ServerBans>();

            if (server.GlobalBan)
            {
                ViewBag.GlobalBan = true;
                banlist           = await _serverBansService.FindAllGlobal(true);
            }
            else
            {
                banlist = await _serverBansService.FindAllFromPavlovServerId(viewModel.ServerId, true);
            }

            foreach (var steamId in table)
            {
                var singleSteamId = steamId.Replace(";", "").Trim();
                var ban           = new ServerBans
                {
                    SteamId        = singleSteamId,
                    BanSpan        = timespans,
                    BannedDateTime = DateTime.Now,
                    PavlovServer   = server
                };

                if (banlist.FirstOrDefault(x => x.SteamId == ban.SteamId) == null)
                {
                    await _serverBansService.Upsert(ban);
                }
            }
            return(new ObjectResult(true));
        }