Пример #1
0
        public void FinaliseTeamsTest()
        {
            const string T1_STRING = "cafef00d";
            const string T2_STRING = "deadb33f";
            const string T4_STRING = "b335";

            Team t1 = new Team("Shared Name", new Source("t1", DateTime.Now.AddDays(1)));

            t1.AddBattlefyId(T1_STRING, Builtins.ManualSource);

            Team t2 = new Team("TeamB", new Source("t2", DateTime.Now.AddDays(2)));

            t2.AddBattlefyId(T2_STRING, Builtins.ManualSource);

            // t3 -> t2
            Team t3 = new Team("AnotherTeam", new Source("t3", DateTime.Now.AddDays(3)));

            t3.AddBattlefyId(T2_STRING, Builtins.ManualSource);

            // t4 -> t1
            Team t4 = new Team("Shared Name", new Source("t4", DateTime.Now.AddDays(4)));

            t4.AddBattlefyId(T4_STRING, Builtins.ManualSource);

            // t5 should NOT merge into t1 because it has no players in common despite sharing a name.
            Team t5 = new Team("Shared Name", new Source("t5", DateTime.UtcNow.AddDays(-1)));

            t5.AddDivision(new Division(5, DivType.LUTI, "FirstSeason"), t5.Sources[0]);

            // t6 -> t5
            Team t6 = new Team("Shared Name", new Source("t6", DateTime.UtcNow));

            t6.AddDivision(new Division(4, DivType.LUTI, "LaterSeason"), t6.Sources[0]);

            Player p1 = new Player("username", new[] { t1.Id, t2.Id, t4.Id }, new Source("p1"));

            p1.AddBattlefyInformation("user", "user", "p1id", Builtins.ManualSource);
            Assert.IsNotNull(t1.BattlefyPersistentTeamId);
            p1.AddBattlefyInformation("slug", "user", "p1id2", Builtins.ManualSource);

            Player p2 = new Player("player", new[] { t1.Id, t2.Id, t4.Id }, new Source("p2"));
            Player p3 = new Player("another player", new[] { t1.Id, t2.Id, t4.Id }, new Source("p3"));
            Player p4 = new Player("player 4", new[] { t6.Id, t5.Id }, new Source("p4"));
            Player p5 = new Player("player 5", new[] { t6.Id, t5.Id }, new Source("p5"));

            // Perform the merge
            var players = new List <Player>()
            {
                p1, p2, p3, p4, p5
            };
            var teams = new List <Team>()
            {
                t1, t2, t3, t4, t5, t6
            };

            DumpSourceable("Players before merge:", players);
            DumpSourceable("Teams before merge:", teams);
            var result = Merger.FinaliseTeams(players, teams);

            DumpSourceable("Players after merge:", players);
            DumpSourceable("Teams after merge:", teams);

            Assert.IsTrue(result.ContainsKey(t6.Id), "Expected t6 to be merged");
            Assert.AreEqual(t5.Id, result[t6.Id], "Expected t6 to be merged --> t5");
            Assert.AreEqual(4, t5.CurrentDiv.Value, "Expected t6 to be merged --> t5 (Div should now be 4, not 5)");

            Assert.IsTrue(result.ContainsKey(t4.Id), "Expected t4 to be merged");
            Assert.AreEqual(t1.Id, result[t4.Id], "Expected t4 to be merged --> t1");
            Assert.IsTrue(t1.BattlefyPersistentTeamIdInformation.Contains(T4_STRING), "Expected t4 to be merged --> t1 (Battlefy Id should have merged)");

            Assert.IsTrue(result.ContainsKey(t3.Id), "Expected t3 to be merged");
            Assert.AreEqual(t2.Id, result[t3.Id], "Expected t3 to be merged --> t2");
            Assert.AreEqual <string>("AnotherTeam", t2.Name.Value, "Expected t3 to be merged --> t2 (Names should have merged)");

            Assert.AreEqual(3, result.Count, "3 teams should have been merged.");
            Assert.AreEqual(3, teams.Count, "3 teams should be left.");

            // Fix the players.
            Merger.CorrectTeamIdsForPlayers(players, result);
            Assert.AreEqual(p1.CurrentTeam, t1.Id, "Expected p1's current team to still be team 1");
            Assert.AreEqual(2, p1.TeamInformation.Count, $"Expected p1's number of teams to now be 2, actually: {IdsToString(p1.TeamInformation.GetAllTeamsUnordered())}");
            Assert.AreEqual(1, p4.TeamInformation.Count, $"Expected p4's number of teams to now be 1, actually: {IdsToString(p4.TeamInformation.GetAllTeamsUnordered())}");
            Assert.AreEqual(p4.CurrentTeam, t5.Id, "Expected p4's current team to now be t5");
            Assert.AreEqual(1, p5.TeamInformation.Count, $"Expected p5's number of teams to now be 1, actually: {IdsToString(p5.TeamInformation.GetAllTeamsUnordered())}");
            Assert.AreEqual(p5.CurrentTeam, t5.Id, "Expected p5's current team to now be t5");
        }
Пример #2
0
        public void SerializeTeam()
        {
            var sources      = new Dictionary <string, Source>();
            var oldestSource = new Source("old_source", DateTime.Now.AddDays(-6));
            var newestSource = new Source("new_source", DateTime.Now);

            sources.Add(newestSource.Id, newestSource);
            sources.Add(oldestSource.Id, oldestSource);

            Team team = new Team();

            team.AddBattlefyId("2teamid2", oldestSource);
            team.AddBattlefyId("1teamid1", newestSource);

            team.AddClanTag("old", oldestSource);
            team.AddClanTag("new", newestSource);

            team.AddDivision(new Division(10, DivType.DSB, "S9"), oldestSource);
            team.AddDivision(new Division(8, DivType.LUTI, "SX"), newestSource);

            team.AddName("team2", oldestSource);
            team.AddName("team1", newestSource);

            string json = Serialize(team);

            Console.WriteLine(nameof(SerializeTeam) + ": ");
            Console.WriteLine(json);
            Team deserialized = Deserialize <Team>(json, sources);

            var battlefy = deserialized.BattlefyPersistentTeamIdInformation.GetItemsOrdered();

            Assert.AreEqual(2, battlefy.Count, "Unexpected number of team battlefy slugs");
            Assert.AreEqual("1teamid1", battlefy[0].Value, "Slug [0] unexpected handle");
            Assert.AreEqual("2teamid2", battlefy[1].Value, "Slug [1] unexpected handle");
            Assert.AreEqual("new_source", battlefy[0].Sources[0].Name, "Slug [0] unexpected source");
            Assert.AreEqual("old_source", battlefy[1].Sources[0].Name, "Slug [1] unexpected source");

            var clanTags = deserialized.ClanTagInformation.GetItemsOrdered();

            Assert.AreEqual(2, clanTags.Count, "Unexpected number of clanTags");
            Assert.AreEqual("new", clanTags[0].Value, "clanTags[0] Unexpected Value");
            Assert.AreEqual("old", clanTags[1].Value, "clanTags[1] Unexpected Value");
            Assert.AreEqual("new_source", clanTags[0].Sources[0].Name, "clanTags[0] unexpected source");
            Assert.AreEqual("old_source", clanTags[1].Sources[0].Name, "clanTags[1] unexpected source");

            var divisions = deserialized.DivisionInformation.GetDivisionsOrdered();

            Assert.AreEqual(2, divisions.Count, "Unexpected number of divisions");
            // First - most recent
            Assert.AreEqual(8, divisions[0].Value, "Unexpected Value");
            Assert.AreEqual(DivType.LUTI, divisions[0].DivType, "Unexpected DivType");
            Assert.AreEqual("SX", divisions[0].Season, "Unexpected Season");
            // Next
            Assert.AreEqual(10, divisions[1].Value, "Unexpected Value");
            Assert.AreEqual(DivType.DSB, divisions[1].DivType, "Unexpected DivType");
            Assert.AreEqual("S9", divisions[1].Season, "Unexpected Season");

            var names = deserialized.NamesInformation.GetItemsOrdered();

            Assert.AreEqual(2, names.Count, "Unexpected number of team names");
            Assert.AreEqual("team1", names[0].Value, "Names [0] unexpected handle");
            Assert.AreEqual("team2", names[1].Value, "Names [1] unexpected handle");
            Assert.AreEqual("new_source", names[0].Sources[0].Name, "Names [0] unexpected source");
            Assert.AreEqual("old_source", names[1].Sources[0].Name, "Names [1] unexpected source");
        }