diff --git a/GlobalTerminalService/GTServer4.cs b/GlobalTerminalService/GTServer4.cs index 2ede4a36..d17e13a0 100644 --- a/GlobalTerminalService/GTServer4.cs +++ b/GlobalTerminalService/GTServer4.cs @@ -220,6 +220,7 @@ namespace PkmnFoundations.GlobalTerminalService } // todo: validate or log some of this? + BattleVideoRankings4 ranking = (BattleVideoRankings4)BitConverter.ToUInt32(data, 0x140); ushort species = BitConverter.ToUInt16(data, 0x144); BattleVideoMetagames4 meta = (BattleVideoMetagames4)data[0x146]; byte country = data[0x147]; @@ -235,7 +236,7 @@ namespace PkmnFoundations.GlobalTerminalService logEntry.AppendFormat(", region {0}", region); logEntry.AppendLine("."); - BattleVideoHeader4[] results = DataAbstract.Instance.BattleVideoSearch4(species, meta, country, region, 30); + BattleVideoHeader4[] results = DataAbstract.Instance.BattleVideoSearch4(species, ranking, meta, country, region, 30); response.Write(new byte[] { 0x00, 0x00 }, 0, 2); // result code (0 for OK) response.Write(BitConverter.GetBytes(results.Length), 0, 4); diff --git a/GlobalTerminalService/GTServer5.cs b/GlobalTerminalService/GTServer5.cs index 1dc7cf1a..bd76a392 100644 --- a/GlobalTerminalService/GTServer5.cs +++ b/GlobalTerminalService/GTServer5.cs @@ -247,7 +247,6 @@ namespace PkmnFoundations.GlobalTerminalService default: logEntry.AppendLine("Unrecognized request type."); response.Write(new byte[] { 0x02, 0x00 }, 0, 2); - response.Write(new byte[] { 0x02, 0x00 }, 0, 2); break; } } diff --git a/library/Data/DataAbstract.cs b/library/Data/DataAbstract.cs index 7313795e..667e58ac 100644 --- a/library/Data/DataAbstract.cs +++ b/library/Data/DataAbstract.cs @@ -90,7 +90,7 @@ namespace PkmnFoundations.Data public abstract BoxRecord4[] BoxSearch4(BoxLabels4 label, int count); public abstract long BattleVideoUpload4(BattleVideoRecord4 record); - public abstract BattleVideoHeader4[] BattleVideoSearch4(ushort species, BattleVideoMetagames4 metagame, byte country, byte region, int count); + public abstract BattleVideoHeader4[] BattleVideoSearch4(ushort species, BattleVideoRankings4 ranking, BattleVideoMetagames4 metagame, byte country, byte region, int count); public abstract BattleVideoRecord4 BattleVideoGet4(long serial); #endregion @@ -102,7 +102,7 @@ namespace PkmnFoundations.Data public abstract MusicalRecord5[] MusicalSearch5(ushort species, int count); public abstract long BattleVideoUpload5(BattleVideoRecord5 record); - public abstract BattleVideoHeader5[] BattleVideoSearch5(ushort species, BattleVideoRankings5 type, BattleVideoMetagames5 metagame, byte country, byte region, int count); + public abstract BattleVideoHeader5[] BattleVideoSearch5(ushort species, BattleVideoRankings5 ranking, BattleVideoMetagames5 metagame, byte country, byte region, int count); public abstract BattleVideoRecord5 BattleVideoGet5(long serial); #endregion } diff --git a/library/Data/DataMysql.cs b/library/Data/DataMysql.cs index fa9c5470..22f104d1 100644 --- a/library/Data/DataMysql.cs +++ b/library/Data/DataMysql.cs @@ -955,55 +955,76 @@ namespace PkmnFoundations.Data } } - public override BattleVideoHeader4[] BattleVideoSearch4(ushort species, BattleVideoMetagames4 metagame, byte country, byte region, int count) + public override BattleVideoHeader4[] BattleVideoSearch4(ushort species, BattleVideoRankings4 ranking, BattleVideoMetagames4 metagame, byte country, byte region, int count) { using (MySqlConnection db = CreateConnection()) { List _params = new List(); String where = ""; + String sort = ""; bool hasSearch = false; - if (species != 0xffff) + if (ranking == BattleVideoRankings4.None) { - where += " WHERE EXISTS(SELECT * FROM TerminalBattleVideoPokemon4 " + - "WHERE video_id = TerminalBattleVideos4.id AND Species = @species)"; - _params.Add(new MySqlParameter("@species", species)); - hasSearch = true; - } + if (species != 0xffff) + { + where += " WHERE EXISTS(SELECT * FROM TerminalBattleVideoPokemon4 " + + "WHERE video_id = TerminalBattleVideos4.id AND Species = @species)"; + _params.Add(new MySqlParameter("@species", species)); + hasSearch = true; + } - if (metagame == BattleVideoMetagames4.SearchColosseumSingleNoRestrictions) - metagame = BattleVideoMetagames4.ColosseumSingleNoRestrictions; - if (metagame == BattleVideoMetagames4.SearchColosseumDoubleNoRestrictions) - metagame = BattleVideoMetagames4.ColosseumDoubleNoRestrictions; + if (metagame == BattleVideoMetagames4.SearchColosseumSingleNoRestrictions) + metagame = BattleVideoMetagames4.ColosseumSingleNoRestrictions; + if (metagame == BattleVideoMetagames4.SearchColosseumDoubleNoRestrictions) + metagame = BattleVideoMetagames4.ColosseumDoubleNoRestrictions; - if (metagame == BattleVideoMetagames4.SearchColosseumSingleCupMatch) - { - where += (hasSearch ? " AND " : " WHERE ") + "Metagame BETWEEN 1 AND 6"; - hasSearch = true; - } - else if (metagame == BattleVideoMetagames4.SearchColosseumDoubleCupMatch) - { - where += (hasSearch ? " AND " : " WHERE ") + "Metagame BETWEEN 8 AND 13"; - hasSearch = true; - } - else if (metagame != BattleVideoMetagames4.SearchLatest30) - { - where += (hasSearch ? " AND " : " WHERE ") + "Metagame = @metagame"; - _params.Add(new MySqlParameter("@metagame", (byte)metagame)); - hasSearch = true; - } + if (metagame == BattleVideoMetagames4.SearchColosseumSingleCupMatch) + { + where += (hasSearch ? " AND " : " WHERE ") + "Metagame BETWEEN 1 AND 6"; + hasSearch = true; + } + else if (metagame == BattleVideoMetagames4.SearchColosseumDoubleCupMatch) + { + where += (hasSearch ? " AND " : " WHERE ") + "Metagame BETWEEN 8 AND 13"; + hasSearch = true; + } + else if (metagame != BattleVideoMetagames4.SearchLatest30) + { + where += (hasSearch ? " AND " : " WHERE ") + "Metagame = @metagame"; + _params.Add(new MySqlParameter("@metagame", (byte)metagame)); + hasSearch = true; + } - if (country != 0xff) - { - where += (hasSearch ? " AND " : " WHERE ") + "Country = @country"; - _params.Add(new MySqlParameter("@country", country)); - hasSearch = true; - } + if (country != 0xff) + { + where += (hasSearch ? " AND " : " WHERE ") + "Country = @country"; + _params.Add(new MySqlParameter("@country", country)); + hasSearch = true; + } - if (region != 0xff) + if (region != 0xff) + { + where += (hasSearch ? " AND " : " WHERE ") + "Region = @region"; + _params.Add(new MySqlParameter("@region", region)); + } + + sort = " ORDER BY TimeAdded DESC, id DESC"; + } + else if (ranking == BattleVideoRankings4.Colosseum) { - where += (hasSearch ? " AND " : " WHERE ") + "Region = @region"; - _params.Add(new MySqlParameter("@region", region)); + // todo: sort by .. something. + where = " WHERE Metagame BETWEEN 0 AND 14"; + sort = " ORDER BY Streak DESC, TimeAdded DESC, id DESC"; + } + else if (ranking == BattleVideoRankings4.BattleFrontier) + { + where = " WHERE NOT (Metagame BETWEEN 0 AND 14)"; + sort = " ORDER BY Streak DESC, TimeAdded DESC, id DESC"; + } + else + { + sort = " ORDER BY TimeAdded DESC, id DESC"; } _params.Add(new MySqlParameter("@count", count)); @@ -1013,7 +1034,7 @@ namespace PkmnFoundations.Data List results = new List(count); MySqlDataReader reader = (MySqlDataReader)db.ExecuteReader("SELECT pid, " + "SerialNumber, Header FROM TerminalBattleVideos4" + where + - " ORDER BY TimeAdded DESC, id DESC LIMIT @count", + sort + " LIMIT @count", _params.ToArray()); while (reader.Read()) { @@ -1280,14 +1301,6 @@ namespace PkmnFoundations.Data if (ranking == BattleVideoRankings5.None) { - switch (ranking) - { - case BattleVideoRankings5.LinkBattles: - break; - case BattleVideoRankings5.SubwayBattles: - break; - } - if (species != 0xffff) { where += (hasSearch ? " AND " : " WHERE ") + @@ -1353,7 +1366,7 @@ namespace PkmnFoundations.Data else if (ranking == BattleVideoRankings5.SubwayBattles) { where = " WHERE Metagame BETWEEN 0 AND 4"; - sort = " ORDER BY TimeAdded DESC, id DESC"; + sort = " ORDER BY Streak DESC, TimeAdded DESC, id DESC"; } else { diff --git a/library/Structures/BattleVideoHeader4.cs b/library/Structures/BattleVideoHeader4.cs index 4a19aecb..baf5f13d 100644 --- a/library/Structures/BattleVideoHeader4.cs +++ b/library/Structures/BattleVideoHeader4.cs @@ -309,4 +309,11 @@ namespace PkmnFoundations.Structures SearchColosseumDoubleCupMatch = 0xfd, SearchLatest30 = 0xff, } + + public enum BattleVideoRankings4 : uint + { + None = 0x00000000, + Colosseum = 0x00000001, + BattleFrontier = 0x00000002, + } }