diff --git a/PogoLocationFeeder/Helper/IVParser.cs b/PogoLocationFeeder/Helper/IVParser.cs new file mode 100644 index 0000000..638e9b7 --- /dev/null +++ b/PogoLocationFeeder/Helper/IVParser.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace PogoLocationFeeder.Helper +{ + public class IVParser + { + public static double parseIV(string input) + { + double iv; + iv = parseRegexDouble(input, @"(?i)\b(1?\d{1,3}[,.]?\d{0,3})\W*\%?\W*IV"); // 52 IV 52% IV 52IV 52.5 IV + if (iv == default(double)) + iv = parseRegexDouble(input, @"(?i)\bIV\W?(1?\d{1,2}[,.]?\d{0,3})"); + if (iv == default(double)) + iv = parseRegexDouble(input, @"\b(1?\d{1,3}[,.]?\d{0,3})\W*\%?"); // 52% 52 % + + return iv; + } + + private static double parseRegexDouble(string input, string regex) + { + Match match = Regex.Match(input, regex); + if (match.Success) + { + return Convert.ToDouble(match.Groups[1].Value.Replace(',', '.'), CultureInfo.InvariantCulture); + } + else + return default(double); + } + } + +} diff --git a/PogoLocationFeeder/Helper/MessageParser.cs b/PogoLocationFeeder/Helper/MessageParser.cs index 892146c..e9081d8 100644 --- a/PogoLocationFeeder/Helper/MessageParser.cs +++ b/PogoLocationFeeder/Helper/MessageParser.cs @@ -27,7 +27,8 @@ public List parseMessage(string message) sniperInfo.latitude = geoCoordinates.latitude; sniperInfo.longitude = geoCoordinates.longitude; } - parseIV(input); + double iv = IVParser.parseIV(input); + sniperInfo.iv = iv; parseTimestamp(input); PokemonId pokemon = PokemonParser.parsePokemon(input); sniperInfo.id = pokemon; @@ -37,25 +38,8 @@ public List parseMessage(string message) return snipeList; } - private double parseRegexDouble(string input, string regex) - { - Match match = Regex.Match(input, regex); - if (match.Success) - { - return Convert.ToDouble(match.Groups[1].Value.Replace(',', '.'), CultureInfo.InvariantCulture); - } - else - return default(double); - } - private void parseIV(string input) - { - sniperInfo.iv = parseRegexDouble(input, @"(?i)\s(1?\d{1,2}[,.]?\d{0,3})\s?\%?\s?IV"); // 52 IV 52% IV 52IV 52.5 IV - if (sniperInfo.iv == default(double)) - sniperInfo.iv = parseRegexDouble(input, @"(1?\d{1,2}[,.]?\d{0,3})\s?\%"); // 52% 52 % - if (sniperInfo.iv == default(double)) - sniperInfo.iv = parseRegexDouble(input, @"(?i)IV\s?(1?\d{1,2}[,.]?\d{0,3})"); - } + private void parseTimestamp(string input) { diff --git a/PogoLocationFeeder/PogoLocationFeeder.csproj b/PogoLocationFeeder/PogoLocationFeeder.csproj index 090c2b9..c8c344a 100644 --- a/PogoLocationFeeder/PogoLocationFeeder.csproj +++ b/PogoLocationFeeder/PogoLocationFeeder.csproj @@ -100,6 +100,7 @@ + diff --git a/PogoLocationFeeder/PokeSniperReader.cs b/PogoLocationFeeder/PokeSniperReader.cs index 5a78be3..e246eed 100644 --- a/PogoLocationFeeder/PokeSniperReader.cs +++ b/PogoLocationFeeder/PokeSniperReader.cs @@ -54,7 +54,6 @@ public List readAll() private SniperInfo map(Result result) { SniperInfo sniperInfo = new SniperInfo(); - sniperInfo.id = (PokemonId)Enum.Parse(typeof(PokemonId), result.name, true); PokemonId pokemonId = PokemonParser.parsePokemon(result.name); sniperInfo.id = pokemonId; GeoCoordinates geoCoordinates = GeoCoordinatesParser.parseGeoCoordinates(result.coords); diff --git a/PogoLocationFeederTests/Helper/IVParserTests.cs b/PogoLocationFeederTests/Helper/IVParserTests.cs new file mode 100644 index 0000000..e219b93 --- /dev/null +++ b/PogoLocationFeederTests/Helper/IVParserTests.cs @@ -0,0 +1,30 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PogoLocationFeeder.Helper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PogoLocationFeeder.Helper.Tests +{ + [TestClass()] + public class IVParserTests + { + [TestMethod()] + public void parseIVTest() + { + Assert.AreEqual(100, IVParser.parseIV("100 IV")); + Assert.AreEqual(88.01, IVParser.parseIV("88,01 IV")); + Assert.AreEqual(5.2, IVParser.parseIV("5.2 % IV")); + + Assert.AreEqual(15.0, IVParser.parseIV("15 IV")); + Assert.AreEqual(85.11, IVParser.parseIV("IV 85.11 %")); + Assert.AreEqual(85.11, IVParser.parseIV("IV 85,11")); + + Assert.AreEqual(100, IVParser.parseIV("100.00 %")); + + + } + } +} \ No newline at end of file diff --git a/PogoLocationFeederTests/Helper/MessageParserTests.cs b/PogoLocationFeederTests/Helper/MessageParserTests.cs new file mode 100644 index 0000000..2cfd367 --- /dev/null +++ b/PogoLocationFeederTests/Helper/MessageParserTests.cs @@ -0,0 +1,47 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PogoLocationFeeder.Helper; +using POGOProtos.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PogoLocationFeeder.Helper.Tests +{ + [TestClass()] + public class MessageParserTests + { + MessageParser messageParser = new MessageParser(); + + [TestMethod()] + public void parseMessageTest() + { + + verifyParsing("[239 seconds remaining] 52% IV - Jolteon at 42.877637631245,74.620142194759 [ Moveset: ThunderShockFast/Thunderbolt ]", + 42.877637631245, 74.620142194759, PokemonId.Jolteon, 52, DateTime.Now.AddSeconds(239)); + + + } + + private void verifyParsing(String text, double latitude, double longitude, PokemonId pokemonId, double iv, DateTime expiration) + { + List sniperInfo = messageParser.parseMessage(text); + Assert.IsNotNull(sniperInfo); + Assert.AreEqual(pokemonId, sniperInfo[0].id); + Assert.AreEqual(latitude, sniperInfo[0].latitude); + Assert.AreEqual(longitude, sniperInfo[0].longitude); + Assert.AreEqual(iv, sniperInfo[0].iv); + Assert.AreEqual(Truncate(expiration, TimeSpan.FromSeconds(1)), Truncate(sniperInfo[0].timeStamp, TimeSpan.FromSeconds(1))); + + } + + private static DateTime Truncate(DateTime dateTime, TimeSpan timeSpan) + { + if (timeSpan == TimeSpan.Zero) return dateTime; // Or could throw an ArgumentException + return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks)); + } + + } + +} \ No newline at end of file diff --git a/PogoLocationFeederTests/PogoLocationFeederTests.csproj b/PogoLocationFeederTests/PogoLocationFeederTests.csproj index 435f137..279b4cb 100644 --- a/PogoLocationFeederTests/PogoLocationFeederTests.csproj +++ b/PogoLocationFeederTests/PogoLocationFeederTests.csproj @@ -56,7 +56,9 @@ + +