<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Snowflake.Ajax;
using Snowflake.Game;
using Snowflake.Platform;
using Snowflake.Extensions;
using Snowflake.Utility;
using Snowflake.Emulator;
using Snowflake.Emulator.Configuration;
using Newtonsoft.Json;

namespace Snowflake.StandardAjax
{
    public partial class StandardAjax
    {
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "filename", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGameResults(IJSRequest request)
        {
            string filename = request.GetParameter("filename");
            string platform = request.GetParameter("platform");
            return new JSResponse(request, this.CoreInstance.LoadedPlatforms[platform].GetScrapeEngine().GetGameResults(filename));
        }
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "resultid", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "filename", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGameInfo(IJSRequest request)
        {
            string resultid = request.GetParameter("resultid");
            string platform = request.GetParameter("platform");
            string filename = request.GetParameter("filename");
            return new JSResponse(request, this.CoreInstance.LoadedPlatforms[platform].GetScrapeEngine().GetGameInfo(resultid, filename));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "enc_gameinfo", ParameterType = AjaxMethodParameterType.ObjectParameter)]
        public IJSResponse AddGameInfo(IJSRequest request)
        {
            string gameinfo_pre = request.GetParameter("enc_gameinfo");
            IGameInfo game = JsonConvert.DeserializeObject<GameInfo>(StringEncode.btoa(gameinfo_pre));
            this.CoreInstance.GameDatabase.AddGame(game);
            return new JSResponse(request, "added " + game.FileName, true);
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGame(IJSRequest request)
        {
            string id = request.GetParameter("id");
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetGameByUUID(id));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGamesByPlatform(IJSRequest request)
        {
            string platform = request.GetParameter("platform");
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetGamesByPlatform(platform));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        public IJSResponse GetAllGames(IJSRequest request)
        {
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetAllGames());
        }

        [AjaxMethod(MethodPrefix = "Game")]
        public IJSResponse GetAllGamesSorted(IJSRequest request)
        {
            IList<IGameInfo> games = this.CoreInstance.GameDatabase.GetAllGames();
            IDictionary<string, List<IGameInfo>> sortedGames = this.CoreInstance.LoadedPlatforms.ToDictionary(platform => platform.Key, platform => new List<IGameInfo>());
            foreach(IGameInfo game in games)
            {
                if (sortedGames.ContainsKey(game.PlatformId))
                {
                    sortedGames[game.PlatformId].Add(game);
                }
            }
            return new JSResponse(request, sortedGames);
        }
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetFlags(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            return new JSResponse(request, bridge.ConfigurationFlags);
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "key", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetFlagValue(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            string id = request.GetParameter("id");
            string key = request.GetParameter("key");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            IGameInfo game = this.CoreInstance.GameDatabase.GetGameByUUID(id);
            IConfigurationFlag flag = bridge.ConfigurationFlags[key];
            return new JSResponse(request, bridge.ConfigurationFlagStore.GetValue(game, flag.Key, flag.Type));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "key", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "value", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse SetFlagValue(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            string id = request.GetParameter("id");
            string key = request.GetParameter("key");
            string value = request.GetParameter("value");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            IGameInfo game = this.CoreInstance.GameDatabase.GetGameByUUID(id);
            IConfigurationFlag flag = bridge.ConfigurationFlags[key];
            dynamic castedValue = bridge.ConfigurationFlagStore.GetDefaultValue(flag.Key, flag.Type);
            switch (flag.Type)
            {<commit_msg>StandardAjax: Implement a method to get all flag values instead of just one<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Snowflake.Ajax;
using Snowflake.Game;
using Snowflake.Platform;
using Snowflake.Extensions;
using Snowflake.Utility;
using Snowflake.Emulator;
using Snowflake.Emulator.Configuration;
using Newtonsoft.Json;

namespace Snowflake.StandardAjax
{
    public partial class StandardAjax
    {
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "filename", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGameResults(IJSRequest request)
        {
            string filename = request.GetParameter("filename");
            string platform = request.GetParameter("platform");
            return new JSResponse(request, this.CoreInstance.LoadedPlatforms[platform].GetScrapeEngine().GetGameResults(filename));
        }
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "resultid", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "filename", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGameInfo(IJSRequest request)
        {
            string resultid = request.GetParameter("resultid");
            string platform = request.GetParameter("platform");
            string filename = request.GetParameter("filename");
            return new JSResponse(request, this.CoreInstance.LoadedPlatforms[platform].GetScrapeEngine().GetGameInfo(resultid, filename));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "enc_gameinfo", ParameterType = AjaxMethodParameterType.ObjectParameter)]
        public IJSResponse AddGameInfo(IJSRequest request)
        {
            string gameinfo_pre = request.GetParameter("enc_gameinfo");
            IGameInfo game = JsonConvert.DeserializeObject<GameInfo>(StringEncode.btoa(gameinfo_pre));
            this.CoreInstance.GameDatabase.AddGame(game);
            return new JSResponse(request, "added " + game.FileName, true);
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGame(IJSRequest request)
        {
            string id = request.GetParameter("id");
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetGameByUUID(id));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "platform", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetGamesByPlatform(IJSRequest request)
        {
            string platform = request.GetParameter("platform");
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetGamesByPlatform(platform));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        public IJSResponse GetAllGames(IJSRequest request)
        {
            return new JSResponse(request, this.CoreInstance.GameDatabase.GetAllGames());
        }

        [AjaxMethod(MethodPrefix = "Game")]
        public IJSResponse GetAllGamesSorted(IJSRequest request)
        {
            IList<IGameInfo> games = this.CoreInstance.GameDatabase.GetAllGames();
            IDictionary<string, List<IGameInfo>> sortedGames = this.CoreInstance.LoadedPlatforms.ToDictionary(platform => platform.Key, platform => new List<IGameInfo>());
            foreach(IGameInfo game in games)
            {
                if (sortedGames.ContainsKey(game.PlatformId))
                {
                    sortedGames[game.PlatformId].Add(game);
                }
            }
            return new JSResponse(request, sortedGames);
        }
        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetFlags(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            return new JSResponse(request, bridge.ConfigurationFlags);
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "key", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetFlagValue(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            string id = request.GetParameter("id");
            string key = request.GetParameter("key");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            IGameInfo game = this.CoreInstance.GameDatabase.GetGameByUUID(id);
            IConfigurationFlag flag = bridge.ConfigurationFlags[key];
            return new JSResponse(request, bridge.ConfigurationFlagStore.GetValue(game, flag.Key, flag.Type));
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse GetFlagValues(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            string id = request.GetParameter("id");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            IGameInfo game = this.CoreInstance.GameDatabase.GetGameByUUID(id);
            IList<dynamic> flags = bridge.ConfigurationFlags.Select(flag => bridge.ConfigurationFlagStore.GetValue(game, flag.Value.Key, flag.Value.Type)).ToList();
            return new JSResponse(request, flags);
        }

        [AjaxMethod(MethodPrefix = "Game")]
        [AjaxMethodParameter(ParameterName = "emulator", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "id", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "key", ParameterType = AjaxMethodParameterType.StringParameter)]
        [AjaxMethodParameter(ParameterName = "value", ParameterType = AjaxMethodParameterType.StringParameter)]
        public IJSResponse SetFlagValue(IJSRequest request)
        {
            string emulator = request.GetParameter("emulator");
            string id = request.GetParameter("id");
            string key = request.GetParameter("key");
            string value = request.GetParameter("value");
            IEmulatorBridge bridge = this.CoreInstance.PluginManager.LoadedEmulators[emulator];
            IGameInfo game = this.CoreInstance.GameDatabase.GetGameByUUID(id);
            IConfigurationFlag flag = bridge.ConfigurationFlags[key];
            dynamic castedValue = bridge.ConfigurationFlagStore.GetDefaultValue(flag.Key, flag.Type);
            switch (flag.Type)
            {
{'fraction_non_alphanumeric': 0.09336866327313298, 'fraction_numerical': 0.0, 'mean_word_length': 3.399386503067485, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4728693', 'n_tokens_mistral': 3582, 'n_tokens_neox': 3522, 'n_words': 636}