예제 #1
0
        public void LoadDatabaseTest()
        {
            UnitTestDatabase   database   = new UnitTestDatabase();
            SplatTagController controller = new SplatTagController(database);

            controller.Initialise();
            Assert.IsTrue(database.loadCalled);

            database.loadCalled = false;
            Team exampleTeam = new Team("Example Team", Builtins.ManualSource);

            exampleTeam.AddClanTag("e.g", Builtins.ManualSource, TagOption.Front);
            exampleTeam.AddDivision(new Division(1, DivType.DSB), Builtins.ManualSource);
            var TEAM_ID = exampleTeam.Id;

            database.expectedTeams = new List <Team> {
                exampleTeam
            };
            var PLAYER_NAME = "Example Name";

            database.expectedPlayers = new List <Player>
            {
                new Player(PLAYER_NAME, new Guid[] { TEAM_ID }, Builtins.ManualSource)
            };
            var PLAYER_ID = database.expectedPlayers[0].Id;

            controller.LoadDatabase();
            Assert.IsTrue(database.loadCalled);

            // Also check the player and team is now in the controller
            var players = controller.MatchPlayer(PLAYER_ID.ToString(), new MatchOptions {
                FilterOptions = FilterOptions.SlappId
            });

            Assert.IsNotNull(players.FirstOrDefault());
            var player = players[0];

            Assert.IsTrue(player.Name.Value == PLAYER_NAME);

            var teams = controller.MatchTeam(TEAM_ID.ToString(), new MatchOptions {
                FilterOptions = FilterOptions.SlappId
            });

            Assert.IsNotNull(teams.FirstOrDefault());
            var team = teams[0];

            Assert.IsTrue(team.CurrentDiv.Value == 1);
            Assert.IsTrue(team.CurrentDiv.DivType == DivType.DSB);

            // Verify getting the players for that team returns our player
            var playersForExampleTeam = controller.GetPlayersForTeam(exampleTeam);

            Assert.IsNotNull(playersForExampleTeam);
            Assert.IsTrue(playersForExampleTeam.Count == 1);
            Assert.IsTrue(playersForExampleTeam[0].player.Equals(player));
        }
예제 #2
0
        private static void HandleCommandLineQuery(
            string?b64,
            string?query,
            string?slappId,
            bool exactCase,
            bool exactCharacterRecognition,
            string?rebuild,
            string?patch,
            bool keepOpen,
            int limit,
            bool verbose,
            bool queryIsRegex,
            bool queryIsClanTag,
            bool queryIsTeam,
            bool queryIsPlayer)
        {
            try
            {
                var options = new MatchOptions
                {
                    IgnoreCase = !exactCase,
                    NearCharacterRecognition = !exactCharacterRecognition,
                    QueryIsRegex             = queryIsRegex,
                    Limit = limit
                };

                if (queryIsPlayer)
                {
                    options.FilterOptions = FilterOptions.Player;
                }
                else if (queryIsTeam)
                {
                    options.FilterOptions = FilterOptions.Team;
                }
                else if (queryIsClanTag)
                {
                    options.FilterOptions = FilterOptions.ClanTag;
                }
                else
                {
                    options.FilterOptions = FilterOptions.Default;
                }

                CommandLineResult result = new CommandLineResult
                {
                    Message = "OK",
                    Options = options
                };

                string?[] inputs = new string?[] { b64, query, slappId };
                SplatTagController.Verbose = verbose;

                if (rebuild != null)
                {
                    if (rebuild.Equals(string.Empty))
                    {
                        SplatTagControllerFactory.GenerateNewDatabase();
                        result.Message = "Database rebuilt from default sources!";
                    }
                    else if (File.Exists(rebuild))
                    {
                        string?saveFolder  = Directory.GetParent(rebuild)?.FullName;
                        string sourcesFile = Path.GetFileName(rebuild);
                        SplatTagControllerFactory.GenerateNewDatabase(saveFolder: saveFolder, sourcesFile: sourcesFile);
                        result.Message = $"Database rebuilt from {rebuild}!";
                    }
                    else
                    {
                        result.Message = $"Rebuild specified but the sources file specified `{rebuild}` from `{Directory.GetCurrentDirectory()}` does not exist. Aborting.";
                    }
                }
                else if (patch != null)
                {
                    if (patch.Equals(string.Empty))
                    {
                        result.Message = "Patch specified but no patch sources file specified. Aborting.";
                    }
                    else if (File.Exists(patch))
                    {
                        SplatTagControllerFactory.GenerateDatabasePatch(patchFile: patch);
                        result.Message = $"Database patched from {patch}!";
                    }
                    else
                    {
                        result.Message = $"Patch specified but the sources file specified `{patch}` from `{Directory.GetCurrentDirectory()}` does not exist. Aborting.";
                    }
                }
                else if (inputs.All(s => string.IsNullOrEmpty(s)))
                {
                    if (keepOpen)
                    {
                        // First run to open
                        result.Message = $"Connection established. {splatTagController.MatchPlayer(null).Length} players and {splatTagController.MatchTeam(null).Length} teams loaded.";
                    }
                    else
                    {
                        result.Message = "Nothing to search!";
                    }
                }
                else
                {
                    try
                    {
                        if (new[] { b64, query, slappId }.Count(s => !string.IsNullOrEmpty(s)) > 1)
                        {
                            result.Message = "Warning: Multiple inputs defined. YMMV.";
                        }

                        if (!string.IsNullOrEmpty(b64))
                        {
                            result.Query = query = Encoding.UTF8.GetString(Convert.FromBase64String(b64));
                        }

                        if (!string.IsNullOrEmpty(slappId))
                        {
                            result.Query          = slappId;
                            options.FilterOptions = FilterOptions.SlappId;
                            result.Players        = splatTagController.MatchPlayer(slappId, options);
                            result.Teams          = splatTagController.MatchTeam(slappId, options);
                        }
                        else
                        {
                            Console.WriteLine($"Building result for query={query}");
                            result.Query   = query ?? string.Empty;
                            result.Players = splatTagController.MatchPlayer(query, options);
                            result.Teams   = splatTagController.MatchTeam(query, options);
                        }

                        if (result.Players.Length > 0 || result.Teams.Length > 0)
                        {
                            result.AdditionalTeams =
                                result.Players
                                .SelectMany(p => p.TeamInformation.GetAllTeamsUnordered().Select(id => splatTagController.GetTeamById(id)))
                                .Distinct()
                                .ToDictionary(t => t.Id, t => t);
                            result.AdditionalTeams[Team.NoTeam.Id]       = Team.NoTeam;
                            result.AdditionalTeams[Team.UnlinkedTeam.Id] = Team.UnlinkedTeam;

                            result.PlayersForTeams =
                                result.Teams
                                .ToDictionary(t => t.Id, t => splatTagController.GetPlayersForTeam(t).ToArray());

                            foreach (var pair in result.PlayersForTeams)
                            {
                                foreach ((Player, bool)tuple in pair.Value)
                                {
                                    foreach (Guid t in tuple.Item1.TeamInformation.GetAllTeamsUnordered())
                                    {
                                        result.AdditionalTeams.TryAdd(t, splatTagController.GetTeamById(t));
                                    }
                                }
                            }

                            var sources = new HashSet <string>();
                            foreach (var s in result.Players.SelectMany(p => p.Sources))
                            {
                                sources.Add(s.Name);
                            }
                            foreach (var s in result.Teams.SelectMany(t => t.Sources))
                            {
                                sources.Add(s.Name);
                            }
                            foreach (var s in result.AdditionalTeams.Values.SelectMany(t => t.Sources))
                            {
                                sources.Add(s.Name);
                            }
                            foreach (var s in result.PlayersForTeams.Values.SelectMany(tupleArray => tupleArray.SelectMany(p => p.Item1.Sources)))
                            {
                                sources.Add(s.Name);
                            }
                            sources.Add(Builtins.BuiltinSource.Name);
                            sources.Add(Builtins.ManualSource.Name);
                            result.Sources = sources.ToArray();

                            try
                            {
                                result.PlacementsForPlayers = new Dictionary <Guid, Dictionary <string, Bracket[]> >();
                                foreach (var player in result.Players)
                                {
                                    result.PlacementsForPlayers[player.Id] = new Dictionary <string, Bracket[]>();
                                    foreach (var source in player.Sources)
                                    {
                                        result.PlacementsForPlayers[player.Id][source.Name] = source.Brackets;
                                    }
                                }
                            }
                            catch (OutOfMemoryException oom)
                            {
                                const string message = "ERROR: OutOfMemoryException on PlacementsForPlayers. Will continue anyway.";
                                Console.WriteLine(message);
                                Console.WriteLine(oom.ToString());
                                result.PlacementsForPlayers = new Dictionary <Guid, Dictionary <string, Bracket[]> >();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        string message = $"ERROR: {ex.GetType().Name} while compiling data for serialization...";
                        Console.WriteLine(message);
                        Console.WriteLine(ex.ToString());

                        string q = result.Query;
                        result = new CommandLineResult
                        {
                            Query   = q,
                            Options = options,
                            Message = message,
                        };
                    }
                }

                string messageToSend;

                try
                {
                    StringWriter sw = new StringWriter();
                    serializer.Serialize(sw, result);
                    messageToSend = sw.ToString();
                }
                catch (Exception ex)
                {
                    string message = $"ERROR: {ex.GetType().Name} while serializing result...";
                    Console.WriteLine(message);
                    Console.WriteLine(ex.ToString());

                    string q = result.Query;
                    result = new CommandLineResult
                    {
                        Query   = q,
                        Options = options,
                        Message = message
                    };

                    // Attempt to send the message as a different serialized error
                    StringWriter sw = new StringWriter();
                    serializer.Serialize(sw, result);
                    messageToSend = sw.ToString();
                }

                // Send back as a b64 string
                Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(messageToSend)));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: Outer Exception handler, caught a {ex.GetType().Name}...");
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                errorMessagesReported.Clear();
            }
        }