public void DeleteWhiteListEntry(string name)
        {
            try
            {
                var entries = GetWhiteListEntries();
                entries = entries.Where(e => e.name != name).ToList();
                var jsonString = JsonConvert.SerializeObject(entries);

                File.WriteAllText($"{_applicationSettings.BdsPath}{Path.DirectorySeparatorChar}whitelist.json", jsonString);

                _wrapper.SendInput("whitelist reload", null);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An unexpected error occured while deleting from whitelist");
            }
        }
示例#2
0
        public async Task CreateWorldBackup(ConsoleApplicationWrapper <MinecraftMessageParser> wrapper, string worldPath, string tempPath, bool fullCopy, bool archive = true)
        {
            if (fullCopy)
            {
                if (Directory.Exists(tempPath))
                {
                    wrapper.AddEphemeralMessage("Clearing local world backup directory...\t", null);

                    Directory.Delete(tempPath, true);
                    Directory.CreateDirectory(tempPath);

                    Console.WriteLine("Done!");
                }
                else
                {
                    Directory.CreateDirectory(tempPath);
                }


                if (Directory.Exists(worldPath))
                {
                    wrapper.AddEphemeralMessage("Attempting to create full world backup...\t", null);

                    CopyDirectory(worldPath, tempPath);

                    Console.WriteLine("Done!");
                }
                else
                {
                    wrapper.AddEphemeralMessage("Invalid world directory. Could not create full world backup!", null);
                }
            }
            else
            {
                wrapper.AddEphemeralMessage("Creating backup...", null);
                wrapper.AddEphemeralMessage("Holding world saving...", null);

                // Send tellraw message 1/2
                wrapper.SendInput("say Creating backup...", null);

                wrapper.SendInput("save hold", null);
                var holdStart = DateTime.UtcNow;

                var matchPattern = new Regex("^(" + Path.GetFileName(worldPath) + @"[\/]{1})");
                var files        = new List <string> ();

                while (files.Count == 0)
                {
                    wrapper.SendInput("save query", null);
                    await Task.Delay(2000);

                    files = wrapper.GetStdoutSinceTime(holdStart)
                            .Where(item => matchPattern.IsMatch(item))
                            .ToList();
                }

                Regex           fileListRegex = new Regex("(" + Path.GetFileName(worldPath) + @"[\/]{1}.+?)\:{1}(\d+)");
                MatchCollection matches       = fileListRegex.Matches(string.Join(Environment.NewLine, files));

                string[,] sourceFiles = new string[matches.Count, 2];

                for (int i = 0; i < matches.Count; i++)
                {
                    sourceFiles[i, 0] = matches[i].Groups[1].Value.Replace(Path.GetFileName(worldPath), "");
                    sourceFiles[i, 1] = matches[i].Groups[2].Value;
                }

                wrapper.AddEphemeralMessage($"Copying {sourceFiles.GetLength ( 0 )} files... ", null);

                // ACTUAL COPYING BEGINS HERE
                if (!Directory.Exists(Path.Combine(tempPath, "db")))
                {
                    Directory.CreateDirectory(Path.Combine(tempPath, "db"));
                }

                for (uint i = 0; i < sourceFiles.GetLength(0); i++)
                {
                    var subdir =
                        File.Exists(Path.Join(worldPath, "db", Path.GetFileName(sourceFiles[i, 0])))
                        ? $"{Path.DirectorySeparatorChar}db" : "";

                    string filePath   = Path.Join(worldPath, subdir, Path.GetFileName(sourceFiles[i, 0]));
                    string targetPath = tempPath + sourceFiles[i, 0];

                    wrapper.AddEphemeralMessage($"Copying from '{filePath}' to '{targetPath}'", null);

                    WriteFileCopy(filePath, targetPath, int.Parse(sourceFiles[i, 1]));
                }

                #region FILE INTEGRITY CHECK

                wrapper.AddEphemeralMessage("Veryfing file-integrity... ", null);

                string[] sourceDbFiles = Directory.GetFiles(worldPath + "/db/");
                string[] targetDbFiles = Directory.GetFiles(tempPath + "/db/");

                foreach (string tFile in targetDbFiles)
                {
                    bool found = false;
                    foreach (string sFile in sourceDbFiles)
                    {
                        if (Path.GetFileName(tFile) == Path.GetFileName(sFile))
                        {
                            found = true;
                            break;
                        }
                    }

                    // File isn't in the source world directory anymore, delete!
                    if (!found)
                    {
                        File.Delete(tFile);
                    }
                }

                #endregion

                Console.WriteLine("Resuming world saving...");

                wrapper.SendInput("save resume", null);
                var resumeRegex = new Regex("^Changes to the (level|world) are resumed.?");

                while (!wrapper.StandardOutput.Any(item => resumeRegex.IsMatch(item)))
                {
                    await Task.Delay(500);
                }

                string tellrawMsg = "Finished creating backup!";

                // Archive
                if (archive)
                {
                    wrapper.AddEphemeralMessage("Archiving world backup...", null);
                    if (ArchiveBackup(tempPath, _applicationSettings.ArchivePath, _applicationSettings.BackupsToKeep, _applicationSettings.WorldName, fullCopy ? "FULL" : ""))
                    {
                        wrapper.AddEphemeralMessage("Archiving done!", null);
                    }
                    else
                    {
                        wrapper.AddEphemeralMessage("Archiving failed!", null);
                        tellrawMsg = "Could not archive backup!";
                    }
                }

                // Send tellraw message 2/2
                wrapper.SendInput($"say {tellrawMsg}", null);

                wrapper.AddEphemeralMessage("Backup done!", null);
            }
        }
示例#3
0
        public async Task PurchaseItemAsync(StoreItem item, ApplicationUser user)
        {
            var payment = new UserCurrency
            {
                Amount = GetDiscountedValueForUser(-item.Price, user),
                CurrencyTransactionReasonId = CurrencyTransactionReason.Purchase,
                CurrencyTypeId = CurrencyType.Normal,
                DateNoted      = DateTime.UtcNow,
                UserId         = user.Id,
                StoreItemId    = item.StoreItemId
            };

            switch (item.StoreItemTypeId)
            {
            case StoreItemType.Command:
                _wrapper.SendInput(ParseCommand(item.Effect, user), null);
                break;

            case StoreItemType.Membership:
                int hours;

                if (int.TryParse(item.Effect, out hours))
                {
                    DateTime baseTime = (user.MembershipExpirationTime ?? DateTime.UtcNow) > DateTime.UtcNow ? user.MembershipExpirationTime.Value : DateTime.UtcNow;
                    user.MembershipExpirationTime = baseTime.AddHours(hours);
                    await _userRepository.SaveUserAsync(user);
                }
                else
                {
                    throw new ArgumentException("The 'Effect' field must be an integer number of hours when using the 'Membership' StoreItemType");
                }
                break;

            case StoreItemType.Rank:
                int rankUp;

                if (int.TryParse(item.Effect, out rankUp))
                {
                    user.Rank += rankUp;
                    await _userRepository.SaveUserAsync(user);
                }
                else
                {
                    throw new ArgumentException("The 'Effect' field must be an integer number indicating how much Rank should increase by when using the 'Rank' StoreItemType");
                }
                break;

            default:
                throw new NotImplementedException($"You have tried using an unsupported StoreItemTypeId of {item.StoreItemTypeId}");
            }

            await _storeRepository.SaveUserCurrency(payment);

            var gift = new UserCurrency
            {
                Amount = item.Price * _applicationSettings.GiftPointPercentage,
                CurrencyTransactionReasonId = CurrencyTransactionReason.Purchase,
                CurrencyTypeId = CurrencyType.Gift,
                DateNoted      = DateTime.UtcNow,
                UserId         = user.Id,
                StoreItemId    = null
            };

            await _storeRepository.SaveUserCurrency(gift);
        }