예제 #1
0
        public async Task RebuildIndex()
        {
            await Context.Channel.SendMessageAsync("**[Admin]** OK, building index");

            // Create a dummy ContainerIndex using latest data
            ContainerIndex containerIndex = new ContainerIndex();

            containerIndex.Event     = StrippedContainer.ConvertToStrippedContainer(ContainerCache.GetEvents().First());
            containerIndex.LineNews  = StrippedContainer.ConvertToStrippedContainer(ContainerCache.GetLineNews().First());
            containerIndex.PopUpNews = StrippedContainer.ConvertToStrippedContainer(ContainerCache.GetPopUpNews().First());
            containerIndex.Present   = StrippedContainer.ConvertToStrippedContainer(ContainerCache.GetPresents().First());

            // Acquire the WebFileHandler lock
            lock (WebFileHandler.Lock)
            {
                // Connect
                WebFileHandler.Connect(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig);

                // Write the file
                WebFileHandler.WriteAllText(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath, WebFileHandler.ToJson(containerIndex));

                // Disconnect
                WebFileHandler.Disconnect();
            }

            await Context.Channel.SendMessageAsync("**[Admin]** Done");
        }
예제 #2
0
        public async Task RebuildList(string type)
        {
            await Context.Channel.SendMessageAsync("**[Admin]** OK, building list");

            // Create a new StrippedContainer list
            List <StrippedContainer> containerList = new List <StrippedContainer>();

            // Declare a variable to hold the path
            string indexPath = ((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerListPath;

            switch (type)
            {
            case "event":
                ContainerCache.GetEvents().ForEach(x => containerList.Add(StrippedContainer.ConvertToStrippedContainer(x)));
                indexPath = string.Format(indexPath, "event");

                break;

            case "linenews":
                ContainerCache.GetLineNews().ForEach(x => containerList.Add(StrippedContainer.ConvertToStrippedContainer(x)));
                indexPath = string.Format(indexPath, "line_news");

                break;

            case "popupnews":
                ContainerCache.GetPopUpNews().ForEach(x => containerList.Add(StrippedContainer.ConvertToStrippedContainer(x)));
                indexPath = string.Format(indexPath, "popup_news");

                break;

            case "present":
                ContainerCache.GetPresents().ForEach(x => containerList.Add(StrippedContainer.ConvertToStrippedContainer(x)));
                indexPath = string.Format(indexPath, "present");

                break;

            default:
                throw new Exception("Invalid type (must be event, linenews, popupnews, or present)");
            }

            // Acquire the WebFileHandler lock
            lock (WebFileHandler.Lock)
            {
                // Connect
                WebFileHandler.Connect(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig);

                // Upload the file
                WebFileHandler.WriteAllText(indexPath, WebFileHandler.ToJson(containerList));

                // Disconnect
                WebFileHandler.Disconnect();
            }

            await Context.Channel.SendMessageAsync("**[Admin]** Done");
        }
예제 #3
0
        public static void HandleContainer(Container container)
        {
            // Get the FileType
            FileType fileType = FileTypeExtensions.GetTypeFromContainer(container);

            // Format the destination S3 path
            string s3Path = $"/smash/{FileTypeExtensions.GetNamePrefixFromType(fileType)}/{container.Id}";

            // Convert the Container into a JSON string
            byte[] json = Encoding.UTF8.GetBytes(WebFileHandler.ToJson(container));

            // Write the data to S3
            S3Api.TransferFile(json, s3Path, "data.json", "application/json");

            // Check if this has an image
            if (fileType == FileType.Event || fileType == FileType.PopUpNews || fileType == FileType.Present)
            {
                // Get the image
                byte[] image = (byte[])container.GetType().GetProperty("Image").GetValue(container);

                // Write the image to S3
                S3Api.TransferFile(image, s3Path, "image.jpg", "image/jpeg");

                // Create a new MagickImage
                using (MagickImage magickImage = new MagickImage(image))
                {
                    // Set the output format to WebP
                    magickImage.Format = MagickFormat.WebP;

                    // Create the raw WebP
                    byte[] webpImage = magickImage.ToByteArray();

                    // Upload to S3
                    S3Api.TransferFile(webpImage, s3Path, "image.webp", "image/webp");
                }
            }

            lock (WebFileHandler.Lock)
            {
                // Connect to the remote server if needed
                WebFileHandler.Connect(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig);

                // Convert the Container to a StrippedContainer
                StrippedContainer strippedContainer = StrippedContainer.ConvertToStrippedContainer(container);

                // Declare a variable to hold the container list
                List <StrippedContainer> containerList;

                // Format the container list path
                string indexPath = string.Format(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerListPath, FileTypeExtensions.GetNamePrefixFromType(fileType));

                // Check if the file exists
                if (WebFileHandler.Exists(indexPath))
                {
                    // Deserialize the List
                    containerList = WebFileHandler.ReadAllText <List <StrippedContainer> >(indexPath);
                }
                else
                {
                    // Create a new List
                    containerList = new List <StrippedContainer>();
                }

                // Check if the Container already exists in the list
                int index = containerList.FindIndex(x => x.Id == container.Id);

                // Check the index
                if (index == -1)
                {
                    // Add the StrippedContainer to the List
                    containerList.Insert(0, strippedContainer);
                }
                else
                {
                    // Replace the item at the index
                    containerList[index] = strippedContainer;
                }

                // Serialize and write the container list
                WebFileHandler.WriteAllText(indexPath, WebFileHandler.ToJson(containerList));

                // Declare a variable to hold the ContainerIndex
                ContainerIndex containerIndex;

                // Check if the ContainerIndex exists
                if (!WebFileHandler.Exists(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath))
                {
                    // Create a dummy StrippedContainer
                    StrippedContainer dummyStrippedContainer = new StrippedContainer();
                    dummyStrippedContainer.Id   = "-1";
                    dummyStrippedContainer.Text = new Dictionary <Nintendo.Bcat.Language, string>();

                    // Create a dummy ContainerIndex
                    containerIndex           = new ContainerIndex();
                    containerIndex.Event     = dummyStrippedContainer;
                    containerIndex.LineNews  = dummyStrippedContainer;
                    containerIndex.PopUpNews = dummyStrippedContainer;
                    containerIndex.Present   = dummyStrippedContainer;
                }
                else
                {
                    // Read the file
                    containerIndex = WebFileHandler.ReadAllText <ContainerIndex>(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath);
                }

                // Get the correct property
                PropertyInfo propertyInfo = containerIndex.GetType().GetProperty(container.GetType().Name);

                // Set the value
                propertyInfo.SetValue(containerIndex, strippedContainer);

                // Write out the ContainerIndex
                WebFileHandler.WriteAllText(((SsbuBotConfiguration)Configuration.LoadedConfiguration).WebConfig.ContainerIndexPath, WebFileHandler.ToJson(containerIndex));

                // Disconnect from the remote server
                WebFileHandler.Disconnect();
            }
        }