public void ExecuteGoToMessage(
            ICommandContext context, int? messageId)
        {
            var parentWindow = context
                .GetRequiredService<IUIShell>()
                .GetMainWindowParent();

            if (Config.Instance.ConfirmationConfig.ConfirmJump
                    && MessageBox.Show(
                            parentWindow,
                            SR.Search.JumpRequest,
                            SR.Search.Confirmation,
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) != DialogResult.Yes)
                return;

            if (ApplicationManager.Instance.ForumNavigator.SelectMessage(
                    ForumCommandHelper.GetMessageId(context, messageId)))
            {
                var mainWindowSvc = context.GetService<IMainWindowService>();
                if (mainWindowSvc != null)
                    mainWindowSvc.EnsureVisible();
            }
            else
                MessageBox.Show(
                    parentWindow,
                    SR.Search.NotFound,
                    SR.Search.Error,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
        }
Beispiel #2
0
        public void ExecuteCompactDb(ICommandContext context)
        {
            ProgressWorker.Run(
                context,
                false,
                progressVisualizer =>
                    {
                        progressVisualizer.SetProgressText(Resources.CompactDbProgressText);

                        var janusDatabaseManager = context.GetRequiredService<IJanusDatabaseManager>();

                        using(janusDatabaseManager.GetLock().GetWriterLock())
                        using (var con = new SQLiteConnection(janusDatabaseManager.GetCurrentConnectionString()))
                        using (var cmd = con.CreateCommand())
                        {
                            con.Open();

                            // This is a REALLY long operation
                            cmd.CommandTimeout = Int32.MaxValue;

                            // Clean up the backend database file
                            cmd.CommandText = @"pragma page_size=" + SqliteSchemaDriver.PageSize + @"; VACUUM; ANALYZE;";
                            cmd.ExecuteNonQuery();
                        }
                    });
        }
 protected override Rendering.Model LoadModel(ICommandContext commandContext, AssetManager assetManager)
 {
     var meshConverter = CreateMeshConverter(commandContext);
     var materialMapping = Materials.Select((s, i) => new { Value = s, Index = i }).ToDictionary(x => x.Value.Name, x => x.Index);
     var sceneData = meshConverter.Convert(SourcePath, Location, materialMapping);
     return sceneData;
 }
        protected override Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            logger = commandContext.Logger;
            if (!File.Exists(ProcessPath))
            {
                logger.Error("Unable to find binary file " + ProcessPath);
                return Task.FromResult(ResultStatus.Failed);
            }

            var startInfo = new ProcessStartInfo
            {
                FileName = ProcessPath,
                Arguments = Arguments,
                WorkingDirectory = ".",
                UseShellExecute = false,
                RedirectStandardOutput = true
            };
            process = Process.Start(startInfo);
            process.OutputDataReceived += OnOutputDataReceived;
            process.BeginOutputReadLine();
            process.WaitForExit();

            ExitCode = process.ExitCode;

            return Task.FromResult(CancellationToken.IsCancellationRequested ? ResultStatus.Cancelled : (ExitCode == 0 ? ResultStatus.Successful : ResultStatus.Failed));
        }
 public void ExecuteModerating(ICommandContext context, int? messageId)
 {
     using (var frm = new ModeratingForm(
             context,
             ForumCommandHelper.GetMessageId(context, messageId)))
         frm.ShowDialog(context.GetRequiredService<IUIShell>().GetMainWindowParent());
 }
 public override void Do(ICommandContext context)
 {
     //if (dimension == 1)
     //{
         if (initFromStack)
         {
             if (type.TypeEnum == TypeEnum.Array && readSizesFromStack)
             {
                 type.Dimensions = context.RunStack.Pop(dimension).Select(value => value.ToInt32()).ToList();
             }
             var list = new Types.List();
             for (int i = 0; i < size; i++)
                 list.Insert(context.RunStack.Pop());
             context.RunStack.Push(list.ConvertTo(type));
         }
         else
         {
             // ???
             context.RunStack.Push(new Types.Array(type, context.RunStack.Pop().ToInt32()));
         }
     //}
     //else
     //{
     //    throw new Psimulex.Core.Exceptions.PsimulexException("More than one dimensional array is not implemented yet!");
     //}
 }
        public void ExecuteGoToMessageWithPrompt(ICommandContext context)
        {
            var parentWindow = context.GetRequiredService<IUIShell>().GetMainWindowParent();

            int mid;
            using (var etf = new EnterTopicMessageIdForm())
                if (etf.ShowDialog(parentWindow) == DialogResult.OK)
                    mid = etf.MessageId;
                else
                    return;

            if (ApplicationManager.Instance.ForumNavigator.SelectMessage(mid))
            {
                var mainWindowSvc = context.GetService<IMainWindowService>();
                if (mainWindowSvc != null)
                    mainWindowSvc.EnsureVisible();
            }
            else if (MessageBox.Show(
                parentWindow,
                SR.Forum.GoToMessage.NotFound.FormatStr(mid),
                SR.Search.Error,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                context
                    .GetRequiredService<IOutboxManager>()
                    .AddTopicForDownload(mid);
        }
Beispiel #8
0
        /// <summary>
        /// The method that indirectly call <see cref="DoCommandOverride"/> to execute the actual command code. 
        /// It is called by the current <see cref="Builder"/> when the command is triggered
        /// </summary>
        /// <param name="commandContext"></param>
        public Task<ResultStatus> DoCommand(ICommandContext commandContext)
        {
            if (CancellationToken.IsCancellationRequested)
                return Task.FromResult(ResultStatus.Cancelled);

            return DoCommandOverride(commandContext);
        }
        public override void PreCommand(ICommandContext commandContext)
        {
            base.PreCommand(commandContext);

            buildTransaction = MicrothreadLocalDatabases.CreateTransaction(commandContext.GetOutputObjectsGroups());
            MicrothreadLocalDatabases.MountDatabase(buildTransaction);
        }
        protected override Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            // This path for effects xml is now part of this tool, but it should be done in a separate exporter?
            using (var inputStream = File.OpenRead(SourcePath))
            using (var outputStream = AssetManager.FileProvider.OpenStream(Location, VirtualFileMode.Create, VirtualFileAccess.Write))
            {
                inputStream.CopyTo(outputStream);

                var objectURL = new ObjectUrl(UrlType.ContentLink, Location);

                if (DisableCompression)
                    commandContext.AddTag(objectURL, DisableCompressionSymbol);
            }

            if (SaveSourcePath)
            {
                // store absolute path to source
                // TODO: the "/path" is hardcoded, used in EffectSystem and ShaderSourceManager. Find a place to share this correctly.
                var pathLocation = new UFile(Location.FullPath + "/path");
                using (var outputStreamPath = AssetManager.FileProvider.OpenStream(pathLocation, VirtualFileMode.Create, VirtualFileAccess.Write))
                {
                    using (var sw = new StreamWriter(outputStreamPath))
                    {
                        sw.Write(SourcePath.FullPath);
                    }
                }
            }

            return Task.FromResult(ResultStatus.Successful);
        }
 /// <summary>
 /// The method accepts ICommandContext to execute undo command.
 /// </summary>
 /// <param name="context">Context of type ICommandContext.</param>
 public void Execute(ICommandContext context)
 {
     if (context.Moves > 0)
     {
         context.Game.Frame = context.BoardHistory.Undo();                
     }
 }
Beispiel #12
0
        public void Run(ICommandContext context)
        {
            var parameters = context.Parameters;

            var allAttributeNames = new[]{
                COMMAND_GET_ALL,
                COMMAND_GET_SINGLE,
                COMMAND_GET_MIN,
                COMMAND_GET_MAX
            };

            if (_setupParameters.Directory == null)
            {
                throw new ApplicationException("No working directory set.  Use 'setup --dir [workingdirectory] to continue.");
            }
            else if (!parameters.Attributes.Select(s => s.Key).Intersect(allAttributeNames).Any())
            {
                throw new ApplicationException("Command parameters required.");
            }

            var versions = _versionRepository.Get(new GetVersionsParameters {
                                Path = _setupParameters.Directory,
                                Single = parameters.GetAttribute(COMMAND_GET_SINGLE, required: false),
                                Min = parameters.GetAttribute(COMMAND_GET_MIN, required: false),
                                Max = parameters.GetAttribute(COMMAND_GET_MAX, required: false)
                                });

            foreach (var version in versions)
            {
                _utility.WriteLine(String.Format(   "Version # {0} \t Down Script: {1} \t Up Script: {2}",
                                                version.Name,
                                                version.DownScript != null,
                                                version.UpScript != null));
            }
        }
Beispiel #13
0
		public void ExecuteShowAppOptions(ICommandContext context)
		{
			var oldValue = Config.Instance.ForumDisplayConfig.ShowUnreadThreadsOnly;
			using (var of = new OptionsForm())
			{
				var owner = context.GetRequiredService<IUIShell>().GetMainWindowParent();

				var res = of.ShowDialog(owner);

				if ((of.ActionType & ChangeActionType.Refresh) == ChangeActionType.Refresh
						&& res == DialogResult.OK)
				{
					if (oldValue != Config.Instance.ForumDisplayConfig.ShowUnreadThreadsOnly)
						Forums.Instance.Refresh();

					Features.Instance.ConfigChanged();

					if (Config.Instance.TickerConfig.ShowTicker)
						Ticker.ShowTicker(context);
					else
						Ticker.HideTicker();

					context.GetRequiredService<IMainWindowService>().Refresh();
				}

				if ((of.ActionType & ChangeActionType.Restart) == ChangeActionType.Restart
						&& res == DialogResult.OK)
					MessageBox.Show(
						owner,
						SR.MainForm.AppNeedRestart,
						ApplicationInfo.ApplicationName,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
			}
		}
Beispiel #14
0
 public override void Execute(ICommandContext context)
 {
     if (context.Memory.Memento != null)
     {
         context.Board.RestoreMemento(context.Memory.Memento);
     }
 }
Beispiel #15
0
        public virtual void PostCommand(ICommandContext commandContext, ResultStatus status)
        {
            // Safeguard, will throw an exception if a inherited command does not call base.PostCommand
            BasePostCommandCalled = true;

            commandContext.RegisterCommandLog(commandContext.Logger.Messages);
        }
        /// <summary>
        /// Invokes the current command with the specified context as input.
        /// </summary>
        /// <param name="context">The context for the command.</param>
        public void Invoke(ICommandContext context)
        {
            var commandContext = context as ShutdownApplicationContext;
            Debug.Assert(commandContext != null, "Incorrect command context specified.");

            m_Action();
        }
 public void Setup()
 {
     var mock = new Mock<ICommandContext>();
     mock.SetupGet(x => x.Messages).Returns(this.messages);
     mock.SetupProperty(x => x.CurrentMessage, string.Empty);
     this.ctx = mock.Object;
 }
Beispiel #18
0
        public void Execute(ICommandContext context, IList<string> arguments)
        {
            int maxNameLength = context.Service.Commands.Select(i => i.Name.Length).Max();

            foreach (var i in context.Service.Commands.OrderBy(i => i.Name))
                context.NormalWriter.WriteLine("{0,-" + maxNameLength + "} - {1}", i.Name, i.Description);
        }
        public void ExecuteInsertPairTag(
            ICommandContext context, string start, string end, bool newLine)
        {
            if (start == "url=" && Clipboard.ContainsText())
            {
                var clipboardText = Clipboard.GetText();
                if (clipboardText.StartsWith("www."))
                    start += "http://" + clipboardText;
                else if (Uri.IsWellFormedUriString(clipboardText, UriKind.Absolute))
                    start += clipboardText;
            }

            switch (start)
            {
                case "c#":
                case "nemerle":
                case "msil":
                case "midl":
                case "asm":
                case "ccode":
                case "code":
                case "pascal":
                case "vb":
                case "sql":
                case "java":
                case "perl":
                case "php":
                    Config.Instance.LastLanguageTag = start;
                    break;
            }

            context.GetRequiredService<IMessageEditorService>().SurroundText(
                "[" + start + "]", "[/" + end + "]", newLine);
        }
Beispiel #20
0
 public void ExecuteShowForumArticles(ICommandContext context, int? forumId)
 {
     context.OpenUrlInBrowser(
         JanusProtocolDispatcher.FormatURI(
             JanusProtocolResourceType.ArticleList,
             GetForumId(context, forumId).ToString()));
 }
Beispiel #21
0
		public void ExecuteEditMessage(ICommandContext context, int? messageId)
		{
			context.OpenUrlInBrowser(
				_editUrlTemplate.FormatWith(
					_siteUrl(),
					ForumCommandHelper.GetMessageId(context, messageId)));
		}
Beispiel #22
0
		public void ExecuteAddForumMessageToFavorites(
			ICommandContext context, int? messageId)
		{
			var manager = context.GetRequiredService<IFavoritesManager>();
			var activeMsg = ForumMessageCommandHelper.GetMessage(context, messageId);

			using (var selForm =
				new FavoritesSelectFolderForm(
					context,
					manager.RootFolder,
					true))
			{
				selForm.Comment = activeMsg.Subject;

				var windowParent = context.GetRequiredService<IUIShell>().GetMainWindowParent();

				if (selForm.ShowDialog(windowParent) == DialogResult.OK)
				{
					var folder = selForm.SelectedFolder ?? manager.RootFolder;
					if (!manager.AddMessageLink(activeMsg.ID, selForm.Comment, folder))
						//сообщения уже есть в разделе
						MessageBox.Show(
							windowParent,
							SR.Favorites.ItemExists.FormatWith(
								activeMsg.ID, folder.Name),
							ApplicationInfo.ApplicationName,
							MessageBoxButtons.OK,
							MessageBoxIcon.Information);
				}
			}
		}
            public override void Execute(ICommandContext context)
            {
                var root = context.TryLoad<SomeRoot>(SomeId)
                           ?? context.Create<SomeRoot>(SomeId);

                root.PossiblyDoSomething();
            }
Beispiel #24
0
 public override void Execute(ICommandContext context)
 {
     var potato = context.TryLoad<Potato>(PotatoId)
                  ?? context.Create<Potato>(PotatoId);
     
     potato.Bite(FractionToBiteOff);
 }
 private Paradox.Importer.AssimpNET.MeshConverter CreateMeshConverter(ICommandContext commandContext)
 {
     return new Paradox.Importer.AssimpNET.MeshConverter(commandContext.Logger)
     {
         AllowUnsignedBlendIndices = this.AllowUnsignedBlendIndices
     };
 }
Beispiel #26
0
        protected override async Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
        {
            try
            {
                switch (Type)
                {
                    case Operation.Move:
                        if (File.Exists(Target))
                            File.Delete(Target);

                        File.Move(Source, Target);
                        commandContext.RegisterOutput(new ObjectUrl(UrlType.File, Target), ObjectId.Empty);
                        break;
                    case Operation.Copy:
                        var sourceStream = File.OpenRead(Source);
                        var destStream = File.OpenWrite(Target);
                        await sourceStream.CopyToAsync(destStream);
                        commandContext.RegisterOutput(new ObjectUrl(UrlType.File, Target), ObjectId.Empty);
                        break;
                    case Operation.Delete:
                        File.Delete(Source);
                        break;
                }

                return ResultStatus.Successful;
            }
            catch (Exception e)
            {
                commandContext.Logger.Error(e.Message);
                return ResultStatus.Failed;
            }          
        }
        /// <summary>
        /// Executes the ShowTopScore Command on the passed context.
        /// </summary>
        /// <param name="ctx">The context that contains the Score object</param>
        public void Execute(ICommandContext ctx)
        {
            char separator = '-';
            int sepCount = 15;

            string header = new string(separator, sepCount) + " Top 5 players: " + new string(separator, sepCount) + Environment.NewLine;

            string footer = Environment.NewLine + new string(separator, 2) + " " + new string(separator, 40) + " " + new string(separator, 2) + Environment.NewLine;

            var highscores = ctx.HighscoreProcessor.GetTopHighscores();

            if (highscores.Count() == 0)
            {
                ctx.CurrentMessage = ctx.Messages["noscores"];
            }
            else
            {
                ctx.CurrentMessage = header + string.Join(
                    Environment.NewLine,
                    highscores.Select(x =>
                    {
                        if (x.Item1.Length > 18)
                        {
                            return new Tuple<string, int>(x.Item1.Substring(0, 15) + "...", x.Item2);
                        }

                        return x;
                    }).Select(x => string.Format("{0,-20} {1}", x.Item1, x.Item2))) + footer;
            }
        }
Beispiel #28
0
        public DefaultCommandExecutor(
            IProcessingCommandCache processingCommandCache,
            ICommandAsyncResultManager commandAsyncResultManager,
            ICommandHandlerProvider commandHandlerProvider,
            IAggregateRootTypeProvider aggregateRootTypeProvider,
            IMemoryCache memoryCache,
            IRepository repository,
            IRetryCommandService retryCommandService,
            IEventStore eventStore,
            IEventPublisher eventPublisher,
            IEventPersistenceSynchronizerProvider eventPersistenceSynchronizerProvider,
            ICommandContext commandContext,
            ILoggerFactory loggerFactory)
        {
            _processingCommandCache = processingCommandCache;
            _commandAsyncResultManager = commandAsyncResultManager;
            _commandHandlerProvider = commandHandlerProvider;
            _aggregateRootTypeProvider = aggregateRootTypeProvider;
            _memoryCache = memoryCache;
            _repository = repository;
            _retryCommandService = retryCommandService;
            _eventStore = eventStore;
            _eventPublisher = eventPublisher;
            _eventPersistenceSynchronizerProvider = eventPersistenceSynchronizerProvider;
            _commandContext = commandContext;
            _trackingContext = commandContext as ITrackingContext;
            _logger = loggerFactory.Create(GetType().Name);

            if (_trackingContext == null)
            {
                throw new Exception("command context must also implement ITrackingContext interface.");
            }
        }
 protected override Dictionary<string, AnimationClip> LoadAnimation(ICommandContext commandContext, ContentManager contentManager, out TimeSpan duration)
 {
     var meshConverter = this.CreateMeshConverter(commandContext);
     var sceneData = meshConverter.ConvertAnimation(SourcePath, Location);
     duration = sceneData.Duration;
     return sceneData.AnimationClips;
 }
Beispiel #30
0
        public override void Do(ICommandContext context)
        {
            BaseType op2 = null; //context.RunStack.Pop().Clone();
            BaseType op1 = null; //context.RunStack.Pop().Clone();

            BaseType result = null;
            BaseType first = null;
            BaseType second = null;

            using (var listener = new Memory.AutoCleanup())
            {
                op2 = context.RunStack.Pop().Clone();
                op1 = context.RunStack.Pop().Clone();

                TypeEnum biggerType = TypeHierarchy.GetBiggerType(op1.Type, op2.Type);
                first = op1.ConvertTo(biggerType);
                second = op2.ConvertTo(biggerType);

                // Note: Dereference has no effect on tests.
                result = first.Dereference().Clone();

                switch (operation)
                {
                    case Operations.Addition:
                        result.Add(second);
                        break;
                    case Operations.Subtraction:
                        result.Subtract(second);
                        break;
                    case Operations.Multiplication:
                        result.Multiply(second);
                        break;
                    case Operations.Division:
                        result.Divide(second);
                        break;
                    case Operations.Power:
                        result.Power(second);
                        break;
                    case Operations.Modulo:
                        result.Modulo(second);
                        break;
                    case Operations.LogicalXor:
                        result = ValueFactory.Create(first && !second || !first && second);
                        break;
                    case Operations.LogicalOr:
                        result = ValueFactory.Create(first || second);
                        break;
                    case Operations.LogicalAnd:
                        result = ValueFactory.Create(first && second);
                        break;
                    default:
                        break;
                }
            }

            //op1.Delete();
            //op2.Delete();

            context.RunStack.Push(result.Clone());
        }
Beispiel #31
0
 public CommandStatus QueryReplyMessageStatus(ICommandContext context, int?messageId)
 {
     return(QueryMessageCommandStatus(context, messageId).DisabledIfNot(
                () => !ForumMessageCommandHelper.GetMessage(context, messageId).Closed));
 }
Beispiel #32
0
 public void ExecuteShowMessageRating(ICommandContext context, int?messageId)
 {
     context.OpenUrlInBrowser(
         SiteUrlHelper.GetRatingUrl(
             ForumCommandHelper.GetMessageId(context, messageId)));
 }
Beispiel #33
0
        public void Execute(ICommandContext context)
        {
            UnturnedUser user   = (UnturnedUser)context.User;
            var          player = user.Player;

            var translations = ((RocketUnturnedHost)context.Container.Resolve <IHost>()).ModuleTranslations;

            float currentDirection = player.Entity.Rotation;

            string targetDirection = context.Parameters.Length > 0 ? context.Parameters.Get <string>(0) : null;

            if (targetDirection != null)
            {
                switch (targetDirection.ToLowerInvariant())
                {
                case "north":
                    currentDirection = 0;
                    break;

                case "east":
                    currentDirection = 90;
                    break;

                case "south":
                    currentDirection = 180;
                    break;

                case "west":
                    currentDirection = 270;
                    break;

                default:
                    throw new CommandWrongUsageException();
                }

                player.Entity.Teleport(player.Entity.Position, currentDirection);
            }

            string directionName = "Unknown";

            if (currentDirection > 30 && currentDirection < 60)
            {
                directionName = translations.Get("command_compass_northeast");
            }
            else if (currentDirection > 60 && currentDirection < 120)
            {
                directionName = translations.Get("command_compass_east");
            }
            else if (currentDirection > 120 && currentDirection < 150)
            {
                directionName = translations.Get("command_compass_southeast");
            }
            else if (currentDirection > 150 && currentDirection < 210)
            {
                directionName = translations.Get("command_compass_south");
            }
            else if (currentDirection > 210 && currentDirection < 240)
            {
                directionName = translations.Get("command_compass_southwest");
            }
            else if (currentDirection > 240 && currentDirection < 300)
            {
                directionName = translations.Get("command_compass_west");
            }
            else if (currentDirection > 300 && currentDirection < 330)
            {
                directionName = translations.Get("command_compass_northwest");
            }
            else if (currentDirection > 330 || currentDirection < 30)
            {
                directionName = translations.Get("command_compass_north");
            }

            user.SendLocalizedMessage(translations, "command_compass_facing_private", null, directionName);
        }
Beispiel #34
0
        public virtual object Execute(ICommandContext commandContext)
        {
            commandContext.EventLogEntryEntityManager.DeleteEventLogEntry(logNr);

            return(null);
        }
Beispiel #35
0
 public GetVersionCommand(ICommandContext commandContext, ILogger logger)
     : base(commandContext, logger)
 {
 }
Beispiel #36
0
        private static string GetPermission(ICommandContext context, string permission)
        {
            var permissionBuilder = context.ServiceProvider.GetRequiredService <ICommandPermissionBuilder>();

            return($"{permissionBuilder.GetPermission(context.CommandRegistration!)}.{permission}");
        }
Beispiel #37
0
        internal static async Task SearchCharacter(ICommandContext context, string name)
        {
            var channel = context.Channel;

            var charSearch = await APIHelper.ESIAPI.SearchCharacterId(LogCat.CharSearch.ToString(), name);

            if (charSearch == null)
            {
                await APIHelper.DiscordAPI.ReplyMessageAsync(context, LM.Get("charNotFound"), true);

                return;
            }

            var characterId = charSearch.character[0];

            var characterData = await APIHelper.ESIAPI.GetCharacterData(LogCat.CharSearch.ToString(), characterId, true);

            if (characterData == null)
            {
                await APIHelper.DiscordAPI.ReplyMessageAsync(context, LM.Get("charNotFound"), true);

                return;
            }

            var corporationData = await APIHelper.ESIAPI.GetCorporationData(LogCat.CharSearch.ToString(), characterData.corporation_id);

            var zkillContent = await APIHelper.ZKillAPI.GetCharacterKills(characterId);

            var characterStats = await APIHelper.ZKillAPI.GetCharacterStats(characterId);

            var zkillLosses = await APIHelper.ZKillAPI.GetCharacterLosses(characterId);

            var zkillLast  = zkillContent.Count > 0 ? zkillContent[0] : new JsonZKill.Kill();
            var systemData = await APIHelper.ESIAPI.GetSystemData("", zkillLast.solar_system_id);

            var lastShipType = LM.Get("Unknown");

            if (zkillLast.victim != null && zkillLast.victim.character_id == characterId)
            {
                lastShipType = zkillLast.victim.ship_type_id.ToString();
            }
            else if (zkillLast.victim != null)
            {
                foreach (var attacker in zkillLast.attackers)
                {
                    if (attacker.character_id == characterId)
                    {
                        lastShipType = attacker.ship_type_id.ToString();
                    }
                }
            }

            var lastShip = await APIHelper.ESIAPI.GetTypeId("", lastShipType);

            var lastSeen     = zkillLast.killmail_time;
            var allianceData = await APIHelper.ESIAPI.GetAllianceData("", characterData.alliance_id);

            var alliance       = allianceData?.name ?? LM.Get("None");
            var lastSeenSystem = systemData?.name ?? LM.Get("None");
            var lastSeenShip   = lastShip?.name ?? LM.Get("None");
            var lastSeenTime   = lastSeen == DateTime.MinValue ? LM.Get("longTimeAgo") : $"{lastSeen}";
            var dangerous      = characterStats.dangerRatio > 75 ? LM.Get("Dangerous") : LM.Get("Snuggly");
            var gang           = characterStats.gangRatio > 70 ? LM.Get("fleetChance") : LM.Get("soloChance");

            var cynoCount   = 0;
            var covertCount = 0;

            foreach (var kill in zkillLosses)
            {
                if (kill.victim.character_id == characterId)
                {
                    foreach (var item in kill.victim.items)
                    {
                        if (item.item_type_id == 21096)
                        {
                            cynoCount++;
                        }
                        if (item.item_type_id == 28646)
                        {
                            covertCount++;
                        }
                    }
                }
            }

            var text1 = characterStats.dangerRatio == 0 ? LM.Get("Unavailable") : HelpersAndExtensions.GenerateUnicodePercentage(characterStats.dangerRatio);
            var text2 = characterStats.gangRatio == 0 ? LM.Get("Unavailable") : HelpersAndExtensions.GenerateUnicodePercentage(characterStats.gangRatio);

            var builder = new EmbedBuilder()
                          .WithDescription(
                $"[zKillboard](https://zkillboard.com/character/{characterId}/) / [EVEWho](https://evewho.com/pilot/{HttpUtility.UrlEncode(characterData.name)})")
                          .WithColor(new Color(0x4286F4))
                          .WithThumbnailUrl($"https://image.eveonline.com/Character/{characterId}_64.jpg")
                          .WithAuthor(author =>
            {
                author
                .WithName($"{characterData.name}");
            })
                          .AddField(LM.Get("Additionaly"), "\u200b")
                          .AddInlineField($"{LM.Get("Corporation")}:", $"{corporationData.name}")
                          .AddInlineField($"{LM.Get("Alliance")}:", $"{alliance}")
                          .AddInlineField($"{LM.Get("HasBeenSeen")}:", $"{lastSeenSystem}")
                          .AddInlineField($"{LM.Get("OnShip")}:", $"{lastSeenShip}")
                          .AddInlineField($"{LM.Get("Seen")}:", $"{lastSeenTime}")
                          .AddField("\u200b", "\u200b")
                          .AddInlineField(LM.Get("CommonCyno"), $"{cynoCount}")
                          .AddInlineField(LM.Get("CovertCyno"), $"{covertCount}")
                          .AddInlineField(LM.Get("Dangerous"), $"{text1}{Environment.NewLine}{Environment.NewLine}**{dangerous} {characterStats.dangerRatio}%**")
                          .AddInlineField(LM.Get("FleetChance2"), $"{text2}{Environment.NewLine}{Environment.NewLine}**{characterStats.gangRatio}% {gang}**");

            var embed = builder.Build();

            await APIHelper.DiscordAPI.SendMessageAsync(channel, "", embed).ConfigureAwait(false);

            await LogHelper.LogInfo($"Sending {context.Message.Author} Character Info Request", LogCat.CharSearch).ConfigureAwait(false);

            await Task.CompletedTask;
        }
Beispiel #38
0
 protected abstract Dictionary <string, AnimationClip> LoadAnimation(ICommandContext commandContext, ContentManager contentManager, out TimeSpan duration);
Beispiel #39
0
 protected abstract Model LoadModel(ICommandContext commandContext, ContentManager contentManager);
 public void Handle(ICommandContext context, SetCashTransferSuccessCommand command)
 {
     context.Get <CashTransfer>(command.AggregateRootId).SetStateSuccess();
 }
            private void ScaleToHeightRange <T>(T[] heights, float minT, float maxT, Vector2 heightRange, float heightScale, ICommandContext commandContext) where T : struct
            {
                float min;
                float max;

                var typeOfT = typeof(T);

                if (typeOfT == typeof(float))
                {
                    min = heightRange.X;
                    max = heightRange.Y;
                }
                else if (typeOfT == typeof(short) || typeOfT == typeof(byte))
                {
                    CalculateByteOrShortRange(heightRange, heightScale, out min, out max);

                    if (!MathUtil.IsInRange(min, minT, maxT) ||
                        !MathUtil.IsInRange(max, minT, maxT))
                    {
                        throw new Exception(
                                  $"{ nameof(ScaleToHeightRange) } failed to scale { minT }..{ maxT } to { min }..{ max }. Check HeightScale and HeightRange are proper.");
                    }
                }
                else
                {
                    throw new NotSupportedException($"{ typeof(T[]) } type is not supported.");
                }

                commandContext?.Logger.Info($"[{Url}] ScaleToHeightRange : { minT }..{ maxT } -> { min }..{ max }");

                if (typeOfT == typeof(float))
                {
                    float[] floats = heights as float[];
                    for (var i = 0; i < floats.Length; ++i)
                    {
                        floats[i] = MathUtil.Clamp(MathUtil.Lerp(min, max, MathUtil.InverseLerp(minT, maxT, floats[i])), min, max);
                    }
                }
                else if (typeOfT == typeof(short))
                {
                    short[] shorts = heights as short[];
                    for (var i = 0; i < shorts.Length; ++i)
                    {
                        shorts[i] = (short)MathUtil.Clamp(MathUtil.Lerp(min, max, MathUtil.InverseLerp(minT, maxT, shorts[i])), min, max);
                    }
                }
                else if (typeOfT == typeof(byte))
                {
                    byte[] bytes = heights as byte[];
                    for (var i = 0; i < bytes.Length; ++i)
                    {
                        bytes[i] = (byte)MathUtil.Clamp(MathUtil.Lerp(min, max, MathUtil.InverseLerp(minT, maxT, bytes[i])), min, max);
                    }
                }
            }
            protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);

                Heightmap heightmap = null;

                var heightType  = Parameters.HeightConversionParameters.HeightType;
                var heightScale = Parameters.HeightConversionParameters.HeightScale;
                var heightRange = Parameters.HeightConversionParameters.HeightRange;

                // Heights

                var source = Parameters.Source;

                using (var textureTool = new TextureTool())
                    using (var texImage = textureTool.Load(source, Parameters.IsSRgb))
                    {
                        // Resize if needed.

                        var size = Parameters.Resizing.Enabled ?
                                   Parameters.Resizing.Size :
                                   new Int2(texImage.Width, texImage.Height);

                        HeightmapUtils.CheckHeightParameters(size, heightType, heightRange, heightScale, true);

                        // Convert the pixel format to single component one.

                        var isConvertedR16 = false;

                        switch (texImage.Format)
                        {
                        case PixelFormat.R32_Float:
                        case PixelFormat.R16_SNorm:
                        case PixelFormat.R8_UNorm:
                            break;

                        case PixelFormat.R32G32B32A32_Float:
                        case PixelFormat.R16G16B16A16_Float:
                        case PixelFormat.R16_Float:
                            textureTool.Convert(texImage, PixelFormat.R32_Float);
                            break;

                        case PixelFormat.R16_UNorm:
                        case PixelFormat.R16G16B16A16_UNorm:
                        case PixelFormat.R16G16_UNorm:
                            textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                            isConvertedR16 = true;
                            break;

                        case PixelFormat.R16G16B16A16_SNorm:
                        case PixelFormat.R16G16_SNorm:
                            textureTool.Convert(texImage, PixelFormat.R16_SNorm);
                            break;

                        case PixelFormat.R8_SNorm:
                        case PixelFormat.B8G8R8A8_UNorm:
                        case PixelFormat.B8G8R8X8_UNorm:
                        case PixelFormat.R8G8B8A8_UNorm:
                        case PixelFormat.R8G8_UNorm:
                            textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                            break;

                        case PixelFormat.R8G8B8A8_SNorm:
                        case PixelFormat.R8G8_SNorm:
                            textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                            break;

                        case PixelFormat.B8G8R8A8_UNorm_SRgb:
                        case PixelFormat.B8G8R8X8_UNorm_SRgb:
                        case PixelFormat.R8G8B8A8_UNorm_SRgb:
                            textureTool.Convert(texImage, PixelFormat.R8_UNorm);
                            break;

                        default:
                            throw new Exception($"{ texImage.Format } format is not supported.");
                        }

                        // Convert pixels to heights

                        using (var image = textureTool.ConvertToStrideImage(texImage))
                        {
                            var pixelBuffer     = image.PixelBuffer[0];
                            var pixelBufferSize = new Int2(pixelBuffer.Width, pixelBuffer.Height);

                            var minFloat = Parameters.FloatingPointComponentRange.X;
                            var maxFloat = Parameters.FloatingPointComponentRange.Y;
                            var isSNorm  = (Math.Abs(-1 - minFloat) < float.Epsilon) && (Math.Abs(1 - maxFloat) < float.Epsilon);

                            if (maxFloat < minFloat)
                            {
                                throw new Exception($"{ nameof(Parameters.FloatingPointComponentRange) }.{ nameof(Parameters.FloatingPointComponentRange.Y) } should be greater than { nameof(Parameters.FloatingPointComponentRange.X) }.");
                            }

                            var useScaleToRange = Parameters.ScaleToHeightRange;

                            switch (heightType)
                            {
                            case HeightfieldTypes.Float:
                            {
                                float[] floats = null;

                                switch (image.Description.Format)
                                {
                                case PixelFormat.R32_Float:
                                    floats = HeightmapUtils.Resize(pixelBuffer.GetPixels <float>(), pixelBufferSize, size);
                                    floats = isSNorm ?
                                             floats :
                                             HeightmapUtils.ConvertToFloatHeights(floats, minFloat, maxFloat);
                                    break;

                                case PixelFormat.R16_SNorm:
                                    var shorts = HeightmapUtils.Resize(pixelBuffer.GetPixels <short>(), pixelBufferSize, size);
                                    floats = !isConvertedR16 && Parameters.IsSymmetricShortComponent ?
                                             HeightmapUtils.ConvertToFloatHeights(shorts, -short.MaxValue, short.MaxValue) :
                                             HeightmapUtils.ConvertToFloatHeights(shorts);
                                    break;

                                case PixelFormat.R8_UNorm:
                                    var bytes = HeightmapUtils.Resize(pixelBuffer.GetPixels <byte>(), pixelBufferSize, size);
                                    floats = HeightmapUtils.ConvertToFloatHeights(bytes);
                                    break;
                                }

                                if (useScaleToRange)
                                {
                                    ScaleToHeightRange(floats, -1, 1, heightRange, heightScale, commandContext);
                                }

                                heightmap = Heightmap.Create(size, heightType, heightRange, heightScale, floats);
                            }
                            break;

                            case HeightfieldTypes.Short:
                            {
                                short[] shorts = null;

                                switch (image.Description.Format)
                                {
                                case PixelFormat.R32_Float:
                                    var floats = HeightmapUtils.Resize(pixelBuffer.GetPixels <float>(), pixelBufferSize, size);
                                    shorts = HeightmapUtils.ConvertToShortHeights(floats, minFloat, maxFloat);
                                    break;

                                case PixelFormat.R16_SNorm:
                                    shorts = HeightmapUtils.Resize(pixelBuffer.GetPixels <short>(), pixelBufferSize, size);
                                    shorts = !isConvertedR16 && Parameters.IsSymmetricShortComponent ?
                                             shorts :
                                             HeightmapUtils.ConvertToShortHeights(shorts);
                                    break;

                                case PixelFormat.R8_UNorm:
                                    var bytes = HeightmapUtils.Resize(pixelBuffer.GetPixels <byte>(), pixelBufferSize, size);
                                    shorts = HeightmapUtils.ConvertToShortHeights(bytes);
                                    break;
                                }

                                if (useScaleToRange)
                                {
                                    ScaleToHeightRange(shorts, short.MinValue, short.MaxValue, heightRange, heightScale, commandContext);
                                }

                                heightmap = Heightmap.Create(size, heightType, heightRange, heightScale, shorts);
                            }
                            break;

                            case HeightfieldTypes.Byte:
                            {
                                byte[] bytes = null;

                                switch (image.Description.Format)
                                {
                                case PixelFormat.R32_Float:
                                    var floats = HeightmapUtils.Resize(pixelBuffer.GetPixels <float>(), pixelBufferSize, size);
                                    bytes = HeightmapUtils.ConvertToByteHeights(floats, minFloat, maxFloat);
                                    break;

                                case PixelFormat.R16_SNorm:
                                    var shorts = HeightmapUtils.Resize(pixelBuffer.GetPixels <short>(), pixelBufferSize, size);
                                    bytes = !isConvertedR16 && Parameters.IsSymmetricShortComponent ?
                                            HeightmapUtils.ConvertToByteHeights(shorts, -short.MaxValue, short.MaxValue) :
                                            HeightmapUtils.ConvertToByteHeights(shorts);
                                    break;

                                case PixelFormat.R8_UNorm:
                                    bytes = HeightmapUtils.Resize(pixelBuffer.GetPixels <byte>(), pixelBufferSize, size);
                                    break;
                                }

                                if (useScaleToRange)
                                {
                                    ScaleToHeightRange(bytes, byte.MinValue, byte.MaxValue, heightRange, heightScale, commandContext);
                                }

                                heightmap = Heightmap.Create(size, heightType, heightRange, heightScale, bytes);
                            }
                            break;

                            default:
                                throw new Exception($"{ heightType } height type is not supported.");
                            }

                            commandContext.Logger.Info($"[{Url}] Convert Image(Format={ texImage.Format }, Width={ texImage.Width }, Height={ texImage.Height }) " +
                                                       $"to Heightmap(HeightType={ heightType }, MinHeight={ heightRange.X }, MaxHeight={ heightRange.Y }, HeightScale={ heightScale }, Size={ size }).");
                        }
                    }

                if (heightmap == null)
                {
                    throw new Exception($"Failed to compile { Url }.");
                }

                assetManager.Save(Url, heightmap);

                return(Task.FromResult(ResultStatus.Successful));
            }
 public override Task <PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command,
                                                            IServiceProvider map) => Task.FromResult(PreconditionResult.FromSuccess());
Beispiel #44
0
            /// <inheritdoc />
            protected override async Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
            {
                VideoAsset videoAsset = Parameters.Video;

                try
                {
                    // Get path to ffmpeg
                    var installationDir = DirectoryHelper.GetPackageDirectory("Xenko");
                    var binDir          = UPath.Combine(installationDir, new UDirectory("Bin"));
                    binDir = UPath.Combine(binDir, new UDirectory("Windows"));
                    var ffmpeg = UPath.Combine(binDir, new UFile("ffmpeg.exe")).ToWindowsPath();
                    if (!File.Exists(ffmpeg))
                    {
                        throw new AssetException("Failed to compile a video asset. ffmpeg was not found.");
                    }

                    // Get absolute path of asset source on disk
                    var assetDirectory = videoAsset.Source.GetParent();
                    var assetSource    = UPath.Combine(assetDirectory, videoAsset.Source);

                    //=====================================================================================
                    //Get the info from the video codec

                    //Check if we need to reencode the video
                    var mustReEncodeVideo    = false;
                    var sidedataStripCommand = "";

                    // check that the video file format is supported
                    if (Parameters.Platform == PlatformType.Windows && videoAsset.Source.GetFileExtension() != ".mp4")
                    {
                        mustReEncodeVideo = true;
                    }

                    //Use FFmpegMedia object (need to check more details first before I can use it)
                    VideoStream videoStream = null;
                    AudioStream audioStream = null;
                    FFmpegUtils.PreloadLibraries();
                    FFmpegUtils.Initialize();
                    using (var media = new FFmpegMedia())
                    {
                        media.Open(assetSource.ToWindowsPath());

                        // Get the first video stream
                        videoStream = media.Streams.OfType <VideoStream>().FirstOrDefault();
                        if (videoStream == null)
                        {
                            throw new AssetException("Failed to compile a video asset. Did not find the VideoStream from the media.");
                        }

                        // On windows MediaEngineEx player only decode the first video if the video is detected as a stereoscopic video,
                        // so we remove the tags inside the video in order to ensure the same behavior as on other platforms (side by side decoded texture)
                        // Unfortunately it does seem possible to disable this behavior from the MediaEngineEx API.
                        if (Parameters.Platform == PlatformType.Windows && media.IsStereoscopicVideo(videoStream))
                        {
                            mustReEncodeVideo    = true;
                            sidedataStripCommand = "-vf sidedata=delete";
                        }

                        // Get the first audio stream
                        audioStream = media.Streams.OfType <AudioStream>().FirstOrDefault();
                    }
                    Size2 videoSize = new Size2(videoStream.Width, videoStream.Height);

                    //check the format
                    if (ListSupportedCodecNames != null)
                    {
                        if (Array.IndexOf(ListSupportedCodecNames, videoStream.Codec) < 0)
                        {
                            mustReEncodeVideo = true;
                        }
                    }

                    // check if video need to be trimmed
                    var videoDuration = videoAsset.VideoDuration;
                    if (videoDuration.Enabled && (videoDuration.StartTime != TimeSpan.Zero ||
                                                  videoDuration.EndTime.TotalSeconds < videoStream.Duration.TotalSeconds - MathUtil.ZeroToleranceDouble))
                    {
                        mustReEncodeVideo = true;
                    }

                    //check the video target and source resolution
                    Size2 targetSize;
                    if (videoAsset.IsSizeInPercentage)
                    {
                        targetSize = new Size2((int)(videoSize.Width * videoAsset.Width / 100.0f), (int)(videoSize.Height * videoAsset.Height / 100.0f));
                    }
                    else
                    {
                        targetSize = new Size2((int)(videoAsset.Width), (int)(videoAsset.Height));
                    }

                    // ensure that the size is a multiple of 2 (ffmpeg cannot output video not multiple of 2, at least with this codec)
                    if (targetSize.Width % 2 == 1)
                    {
                        targetSize.Width += 1;
                    }
                    if (targetSize.Height % 2 == 1)
                    {
                        targetSize.Height += 1;
                    }

                    if (targetSize.Width != videoSize.Width || targetSize.Height != videoSize.Height)
                    {
                        mustReEncodeVideo = true;
                    }

                    //check the audio settings
                    int  audioChannelsTarget       = audioStream == null? 0: audioStream.ChannelCount;
                    bool mustReEncodeAudioChannels = false;
                    if (videoAsset.IsAudioChannelMono)
                    {
                        audioChannelsTarget = 1;
                        if (audioStream != null && audioStream.ChannelCount != audioChannelsTarget)
                        {
                            mustReEncodeAudioChannels = true;
                            mustReEncodeVideo         = true;
                        }
                    }

                    // Execute ffmpeg to convert source to H.264
                    string tempFile = null;
                    try
                    {
                        if (mustReEncodeVideo)
                        {
                            string targetCodecFormat = "h264";  //hardcodec for now
                            commandContext.Logger.Info(string.Format("Video Asset Compiler: \"{0}\". Re-encode the Video. Format:{1}, Size:{2}x{3}. Audio Channels:{4}",
                                                                     videoAsset.Source.GetFileName(), targetCodecFormat, targetSize.Width, targetSize.Height, audioChannelsTarget));

                            tempFile = Path.GetTempFileName();
                            string channelFlag = "";
                            if (mustReEncodeAudioChannels)
                            {
                                channelFlag = string.Format(" -ac {0}", audioChannelsTarget);
                            }

                            var startTime       = videoDuration.StartTime;
                            var duration        = videoDuration.EndTime - videoDuration.StartTime;
                            var trimmingOptions = videoDuration.Enabled ?
                                                  $" -ss {startTime.Hours:D2}:{startTime.Minutes:D2}:{startTime.Seconds:D2}.{startTime.Milliseconds:D3}" +
                                                  $" -t {duration.Hours:D2}:{duration.Minutes:D2}:{duration.Seconds:D2}.{duration.Milliseconds:D3}":
                                                  "";

                            var commandLine = "  -hide_banner -loglevel error" +                       // hide most log output
                                              "  -nostdin" +                                           // no interaction (background process)
                                              $" -i \"{assetSource.ToWindowsPath()}\"" +               // input file
                                              $"{trimmingOptions}" +
                                              "  -f mp4 -vcodec " + targetCodecFormat +                // codec
                                              channelFlag +                                            // audio channels
                                              $"  -vf scale={targetSize.Width}:{targetSize.Height} " + // adjust the resolution
                                              sidedataStripCommand +                                   // strip of stereoscopic sidedata tag
                                                                                                       //" -an" + // no audio
                                                                                                       //" -pix_fmt yuv422p" + // pixel format (planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples))
                                              $" -y \"{tempFile}\"";                                   // output file (always overwrite)
                            var ret = await ShellHelper.RunProcessAndGetOutputAsync(ffmpeg, commandLine, commandContext.Logger);

                            if (ret != 0 || commandContext.Logger.HasErrors)
                            {
                                throw new AssetException($"Failed to compile a video asset. ffmpeg failed to convert {assetSource}.");
                            }
                        }
                        else
                        {
                            commandContext.Logger.Info(string.Format("Video Asset Compiler: \"{0}\". No Re-encoding necessary",
                                                                     videoAsset.Source.GetFileName()));

                            // Use temporary file
                            tempFile = assetSource.ToWindowsPath();
                        }

                        var dataUrl = Url + "_Data";
                        var video   = new Video.Video
                        {
                            CompressedDataUrl = dataUrl,
                        };

                        // Make sure we don't compress h264 data
                        commandContext.AddTag(new ObjectUrl(UrlType.Content, dataUrl), Builder.DoNotCompressTag);

                        // Write the data
                        using (var reader = new BinaryReader(new FileStream(tempFile, FileMode.Open, FileAccess.Read)))
                            using (var outputStream = MicrothreadLocalDatabases.DatabaseFileProvider.OpenStream(dataUrl, VirtualFileMode.Create, VirtualFileAccess.Write, VirtualFileShare.Read, StreamFlags.Seekable))
                            {
                                // For now write everything at once, 1MB at a time
                                var length = reader.BaseStream.Length;
                                for (var position = 0L; position < length; position += 2 << 20)
                                {
                                    var buffer = reader.ReadBytes(2 << 20);
                                    outputStream.Write(buffer, 0, buffer.Length);
                                }
                            }

                        var assetManager = new ContentManager(MicrothreadLocalDatabases.ProviderService);
                        assetManager.Save(Url, video);

                        return(ResultStatus.Successful);
                    }
                    finally
                    {
                        if (mustReEncodeVideo)
                        {
                            if (tempFile != null)
                            {
                                File.Delete(tempFile);
                            }
                        }
                    }
                }
                catch (AssetException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new AssetException("Failed to compile a video asset. Unexpected exception.", ex);
                }
            }
Beispiel #45
0
 /// <summary>
 ///     The overridable method for type parsing logic.
 /// </summary>
 /// <param name="parameter"> The currently parsed <see cref="Parameter"/>. </param>
 /// <param name="value"> The raw argument to parse. </param>
 /// <param name="context"> The <see cref="ICommandContext"/> used during execution. </param>
 /// <param name="provider"> The <see cref="IServiceProvider"/> used during execution. </param>
 /// <returns> A <see cref="TypeParserResult{T}"/>. </returns>
 public abstract Task <TypeParserResult <T> > ParseAsync(Parameter parameter, string value, ICommandContext context, IServiceProvider provider);
Beispiel #46
0
        async Task <TypeParserResult <object> > ITypeParser.ParseAsync(Parameter parameter, string value, ICommandContext context, IServiceProvider provider)
        {
            var result = await ParseAsync(parameter, value, context, provider).ConfigureAwait(false);

            return(result.IsSuccessful
                ? result.HasValue
                    ? new TypeParserResult <object>(result.Value)
                    : new TypeParserResult <object>(false)
                : new TypeParserResult <object>(result.Reason));
        }
Beispiel #47
0
        /// <summary>
        /// Handles the result of an internal error, generating a short error report for the user and instructing them
        /// on how to proceed.
        /// </summary>
        /// <param name="context">The context of the command.</param>
        /// <param name="result">The result of the command.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task HandleInternalErrorAsync
        (
            ICommandContext context,
            ExecuteResult result
        )
        {
            // Log the exception for later debugging purposes
            this.Log.LogError(result.Exception.ToString());

            // Alert the user, explain what happened, and ask them to make a bug report.
            var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

            try
            {
                var eb = _feedback.CreateEmbedBase(Color.Red);
                eb.WithTitle("Internal Error");
                eb.WithDescription
                (
                    "Oops! Looks like you've found a bug in the bot - fear not, though. If we work together, we'll " +
                    "have it licked in no time at all.\n" +
                    "\n" +
                    "I've prepared a short report of the technical details of what happened, but it's not going " +
                    "to be worth much without your help. In order to fix this problem, it would be extremely " +
                    "helpful if you wrote down the exact steps you did to encounter this error, and post them along " +
                    "with the generated report on the GitHub repository. You can go there by clicking the link " +
                    "in this message.\n" +
                    "\n" +
                    "The report contains some information about you, so please check it and erase anything you don't" +
                    " want to share before passing it on.\n" +
                    "\n" +
                    "Your assistance is essential, and very much appreciated!"
                );

                eb.WithAuthor(context.Client.CurrentUser);
                eb.WithCurrentTimestamp();
                eb.WithFooter
                (
                    "If you don't have an account on github, you can also send a DM to Jax#7487, who is the main" +
                    " developer of the bot."
                );
                eb.WithThumbnailUrl(_portraits.BrokenAmbyUri.ToString());

                var reportEmbed = _feedback.CreateEmbedBase(Color.Red);
                reportEmbed.WithTitle("Click here to create a new issue");
                reportEmbed.WithUrl(_content.AutomaticBugReportCreationUri.ToString());

                await using var ms = new MemoryStream();
                var now = DateTime.UtcNow;

                await using (var sw = new StreamWriter(ms, Encoding.Default, 1024, true))
                {
                    await sw.WriteLineAsync("Automatic bug report");

                    await sw.WriteLineAsync("====================");

                    await sw.WriteLineAsync();

                    await sw.WriteLineAsync($"Generated at: {now}");

                    var entryAssembly = Assembly.GetEntryAssembly();
                    if (!(entryAssembly is null))
                    {
                        await sw.WriteLineAsync($"Bot version: {entryAssembly.GetName().Version}");
                    }

                    await sw.WriteLineAsync($"Command message link: {context.Message.GetJumpUrl()}");

                    await sw.WriteLineAsync
                    (
                        $"Ran by: {context.User.Username}#{context.User.Discriminator} ({context.User.Id})"
                    );

                    await sw.WriteLineAsync
                    (
                        $"In: {(context.Guild is null ? "DM" : $"{context.Guild.Name} ({context.Guild.Id})")}"
                    );

                    await sw.WriteLineAsync($"Full command: {context.Message.Content}");

                    await sw.WriteLineAsync();

                    await sw.WriteLineAsync("### Stack Trace");

                    await sw.WriteLineAsync(result.Exception.ToString());
                }
Beispiel #48
0
        public void PollGame(ICommandContext ctx, ChessOptions opts, string challengeUrl, string speed)
        {
            var _ = Task.Run(async() =>
            {
                var gameId     = challengeUrl.Substring(challengeUrl.LastIndexOf('/'));
                using var http = _http.CreateClient();
                http.DefaultRequestHeaders.Add("Accept", "application/json");
                var retry = 0;
                string response;
                do
                {
                    try
                    {
                        response = await http.GetStringAsync($"https://lichess.org/game/export{gameId}").ConfigureAwait(false);
                        goto found;
                    }
                    catch (HttpRequestException)
                    {
                        // retry
                    }

                    await Task.Delay(20000);
                    retry++;
                } while (retry < 15);

                return;

                found:

                await ctx.Channel.EmbedAsync(new EmbedBuilder().WithDynamicColor(ctx)
                                             .WithTitle($"Chess Challenge {ctx.User} vs {opts.ChallengeTo}")
                                             .WithAuthor(speed)
                                             .WithDescription($"[Click here for to spectate game]({challengeUrl})"))
                .ConfigureAwait(false);

                using var json = JsonDocument.Parse(response);

                var counter    = 0;
                var maxCounter = opts.Time * 60 * 3;
                while (!json.RootElement.TryGetProperty("winner", out var _))
                {
                    await Task.Delay(2000);
                    counter += 2;
                    if (counter >= maxCounter)
                    {
                        return;
                    }
                }

                json.RootElement.TryGetProperty("winner", out var winner);

                var description = new StringBuilder();
                description.AppendLine($"{ctx.User.Mention} vs {opts.ChallengeTo.Mention}");

                Enum.TryParse(json.RootElement.GetProperty("status").GetString().ToTitleCase(), out ChessResult result);

                if (result == ChessResult.Draw)
                {
                    description.AppendLine("Game resulted in a Draw");
                }
                else if (result == ChessResult.Mate)
                {
                    description.AppendLine(winner.GetString().Equals("white", StringComparison.Ordinal) ? "White won with Checkmate" : "Black won with Checkmate");
                }
                else if (result == ChessResult.Resign)
                {
                    description.AppendLine(winner.GetString().Equals("white", StringComparison.Ordinal) ? "White won with Black's Resignation" : "Black won with White's Resignation");
                }
                else if (result == ChessResult.Outoftime)
                {
                    description.AppendLine(winner.GetString().Equals("white", StringComparison.Ordinal) ? "White won by Timeout" : "Black won by Timeout");
                }
                else
                {
                    description.AppendLine("Game was Aborted");
                }

                await ctx.Channel.EmbedAsync(new EmbedBuilder().WithDynamicColor(ctx)
                                             .WithTitle("Chess Challenge")
                                             .WithAuthor($"{opts.Time}+{opts.Increment}")
                                             .WithDescription(description.ToString()))
                .ConfigureAwait(false);
            });
        }
Beispiel #49
0
 public abstract bool CanExecute(ICommandContext ctx);
Beispiel #50
0
 protected override Task <ResultStatus> DoCommandOverride(ICommandContext commandContext)
 {
     return(Task.Run(() => ResultStatus.Successful));
 }
Beispiel #51
0
 public virtual void PrintUsage(ICommandContext ctx)
 {
     ctx.SendOutput(String.Format("\tUsage:\t\t{0}", this.GetUsage()));
 }
Beispiel #52
0
 public abstract int Execute(ICommandContext ctx, string[] args, char[] flags);
Beispiel #53
0
 public static ISocketClientRegistry <IRpcClient> AddClientSocketRegistry(ICommandContext commandContext,
                                                                          IScheduler testScheduler)
 {
     commandContext.SocketClientRegistry.Returns(new SocketClientRegistry <IRpcClient>(testScheduler));
     return(commandContext.SocketClientRegistry);
 }
Beispiel #54
0
        public static void GenerateResponse <T>(ICommandContext commandContext, T response) where T : IMessage <T>
        {
            var nodeRpcClient = GenerateRpcResponseOnSubscription(response);

            commandContext.SocketClientRegistry.AddClientToRegistry(1111111111, nodeRpcClient);
        }
Beispiel #55
0
        // results //////////////////////////////////////////

        public override long ExecuteCount(ICommandContext commandContext)
        {
            CheckQueryOk();
            return(commandContext.JobEntityManager.FindJobCountByQueryCriteria(this));
        }
Beispiel #56
0
 public static void MockActiveConnection(ICommandContext commandContext, IRpcClient rpcClient)
 {
     commandContext.IsSocketChannelActive(Arg.Any <IRpcClient>()).Returns(true);
     commandContext.GetConnectedNode(Arg.Any <string>()).Returns(rpcClient);
 }
Beispiel #57
0
 public override IList <IJob> ExecuteList(ICommandContext commandContext, Page page)
 {
     CheckQueryOk();
     return(commandContext.JobEntityManager.FindJobsByQueryCriteria(this, page));
 }
Beispiel #58
0
 public JobQueryImpl(ICommandContext commandContext) : base(commandContext)
 {
 }
Beispiel #59
0
 protected abstract Skeleton LoadSkeleton(ICommandContext commandContext, ContentManager contentManager);
Beispiel #60
0
 public NotEnoughPermissionException(ICommandContext context, string permission) : this(context.ServiceProvider.GetRequiredService <IOpenModStringLocalizer>(), GetPermission(context, permission))
 {
 }