Exemplo n.º 1
0
        public async Task LearnAsync([Remainder][Summary("Separate the species and moves with a comma.")] string speciesAndMoves)
        {
            var args    = speciesAndMoves.Split(Splitters, StringSplitOptions.RemoveEmptyEntries);
            var species = args[0];

            // Sanity check
            var str = GameInfo.Strings;

            if (StringUtil.FindIndexIgnoreCase(str.specieslist, species) < 0)
            {
                await ReplyAsync($"Bad species argument ({species})!").ConfigureAwait(false);

                return;
            }
            foreach (var move in args.Skip(1))
            {
                if (StringUtil.FindIndexIgnoreCase(str.movelist, move) >= 0)
                {
                    continue;
                }
                await ReplyAsync($"Bad move argument ({move})!").ConfigureAwait(false);

                return;
            }

            var summary = EncounterLearn.CanLearn(species, args.Skip(1));
            var msg     = summary
                ? $"Yep! {species} can learn {string.Join(", ", args.Skip(1))}."
                : $"Nope, {species} can't learn {string.Join(", ", args.Skip(1))}";

            await ReplyAsync(msg).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        public async Task LearnAsync([Remainder] string context)
        {
            var args    = context.Split(", ");
            var species = args[0];
            var summary = EncounterLearn.CanLearn(species, args.Skip(1));
            var msg     = summary
                ? $"Yep! {species} can learn {string.Join(", ", args.Skip(1))}."
                : $"Nope, {species} can't' learn {string.Join(", ", args.Skip(1))}";

            await ReplyAsync(msg).ConfigureAwait(false);
        }
Exemplo n.º 3
0
        public List <LearnableMove> Post([FromForm][Required] string query, [FromForm] string generation)
        {
            //var sav = SaveUtil.GetBlankSAV(Utils.GetGameVersion(generation), "Scatman");
            //PKM asdf = new PKM()
            var data = Utils.SplitQueryString(query);

            if (data.Length < 2)
            {
                Response.StatusCode = 400;
                return(null);
            }

            // Check if the Pokemon actually exists
            if (!Utils.PokemonExists(data[0]))
            {
                Response.StatusCode = 400;
                return(null);
            }

            var moves = new List <LearnableMove>(4);

            foreach (var move in data.Skip(1))
            {
                var           workaround = move.Split(',');
                LearnableMove lm         = new LearnableMove
                {
                    Name      = move,
                    Learnable = bool.Parse(EncounterLearn.CanLearn(data[0], workaround).ToString())
                };
                moves.Add(lm);
                if (moves.Count == 4)
                {
                    break;
                }
            }
            return(moves);
        }
Exemplo n.º 4
0
        public async Task EncounterAsync([Remainder] string context)
        {
            var args    = context.Split(", ");
            var species = args[0];
            var builder = new EmbedBuilder
            {
                Color       = new Color(114, 137, 218),
                Description = "Encounters:"
            };

            var  summary = EncounterLearn.GetLearnSummary(species, args.Skip(1));
            var  sb      = new StringBuilder();
            var  key     = string.Empty;
            bool any     = false;
            bool capped  = false;

            foreach (var line in summary)
            {
                if (line.StartsWith("="))
                {
                    any = true;
                    if (sb.Length > 0)
                    {
                        var key1 = key;
                        var msg  = sb.ToString();
                        builder.AddField(x =>
                        {
                            x.Name     = key1;
                            x.Value    = msg;
                            x.IsInline = false;
                        });
                    }
                    key    = line.Replace("=", "");
                    capped = false;
                    sb.Clear();
                    continue;
                }

                if (sb.Length > 850 && !capped)
                {
                    capped = true;
                    sb.AppendLine("...and more! Too long to show all.");
                }
                else if (!capped)
                {
                    sb.AppendLine(line);
                }
            }

            if (sb.Length > 0)
            {
                var key1 = key;
                var msg  = sb.ToString();
                builder.AddField(x =>
                {
                    x.Name     = key1;
                    x.Value    = msg;
                    x.IsInline = false;
                });
            }

            if (!any)
            {
                await ReplyAsync("None").ConfigureAwait(false);

                return;
            }

            await ReplyAsync($"Here's where you can find {species}:", false, builder.Build()).ConfigureAwait(false);
        }
Exemplo n.º 5
0
        public async Task EncounterAsync([Remainder][Summary("Separate the species and moves with a comma.")] string speciesAndMoves)
        {
            var args    = speciesAndMoves.Split(Splitters, StringSplitOptions.RemoveEmptyEntries);
            var species = args[0];

            // Sanity check
            var str = GameInfo.Strings;

            if (StringUtil.FindIndexIgnoreCase(str.specieslist, species) < 0)
            {
                await ReplyAsync($"Bad species argument ({species})!").ConfigureAwait(false);

                return;
            }
            foreach (var move in args.Skip(1))
            {
                if (StringUtil.FindIndexIgnoreCase(str.movelist, move) >= 0)
                {
                    continue;
                }
                await ReplyAsync($"Bad move argument ({move})!").ConfigureAwait(false);

                return;
            }

            var summary = EncounterLearn.GetLearnSummary(species, args.Skip(1));
            var builder = new EmbedBuilder
            {
                Color       = new Color(114, 137, 218),
                Description = "Encounters:"
            };

            var  sb     = new StringBuilder();
            var  key    = string.Empty;
            bool any    = false;
            bool capped = false;

            foreach (var line in summary)
            {
                if (line.StartsWith("="))
                {
                    any = true;
                    if (sb.Length > 0)
                    {
                        var key1 = key;
                        var msg  = sb.ToString();
                        builder.AddField(x =>
                        {
                            x.Name     = key1;
                            x.Value    = msg;
                            x.IsInline = false;
                        });
                    }
                    key    = line.Replace("=", "");
                    capped = false;
                    sb.Clear();
                    continue;
                }

                if (sb.Length > 850 && !capped)
                {
                    capped = true;
                    sb.AppendLine("...and more! Too long to show all.");
                }
                else if (!capped)
                {
                    sb.AppendLine(line);
                }
            }

            if (!any)
            {
                var fail = $"Sorry, can't find {species}";
                if (args.Length > 1)
                {
                    fail += $" with {string.Join(", ", args.Skip(1))}.";
                }
                await ReplyAsync(fail).ConfigureAwait(false);

                return;
            }

            if (sb.Length > 0)
            {
                var key1 = key;
                var msg  = sb.ToString();
                builder.AddField(x =>
                {
                    x.Name     = key1;
                    x.Value    = msg;
                    x.IsInline = false;
                });
            }

            var response = $"Here's where you can find {species}";

            if (args.Length > 1)
            {
                response += $" with {string.Join(", ", args.Skip(1))}";
            }

            await ReplyAsync(response + ":", false, builder.Build()).ConfigureAwait(false);
        }
Exemplo n.º 6
0
        public static void VerifyCanLearnParse(string species, params string[] moves)
        {
            var encs = EncounterLearn.GetLearn(species, moves);

            encs.Any().Should().BeTrue($"{species} should be able to learn all moves: {string.Join(", ", moves)}");
        }
Exemplo n.º 7
0
    public void Handle_connection(IAsyncResult result)        //the parameter is a delegate, used to communicate between threads
    {
        Accept_connection();                                  //once again, checking for any other incoming connections
        TcpClient client = server.EndAcceptTcpClient(result); //creates the TcpClient

        NetworkStream ns = client.GetStream();

        while (client.Connected)  //while the client is connected, we look for incoming messages
        {
            try
            {
                if (type != "bot_info")
                {
                    string version = "";
                    if (type == "legal_fix")
                    {
                        byte[] versionBytes = Read_NS_Data(ns, 8);
                        version = Encoding.UTF8.GetString(versionBytes, 0, versionBytes.Length);
                        Console.WriteLine("Version sent over is: " + version);
                    }
                    var pk = GetPokemon(ns);
                    if (pk == null)
                    {
                        throw new System.ArgumentException("There was an issue reading the Pokemon data, is it not a Pokemon? (or maybe timeout?)");
                    }
                    if (type == "legality_check")
                    {
                        var lc = new LegalityAnalysis(pk);

                        byte[] report = Encoding.Default.GetBytes(lc.Report());
                        ns.Write(report, 0, report.Length);
                    }
                    else if (type == "info_get")
                    {
                        var summary = new GPSSSummary(pk, GameInfo.Strings);
                        var pkmn    = summary.CreatePKMN();
                        if (pkmn == null)
                        {
                            throw new System.ArgumentException("There was an issue reading the Pokemon data, is it not a Pokemon? (or maybe timeout?)");
                        }
                        var ser = new DataContractJsonSerializer(typeof(Pokemon));
                        ser.WriteObject(ns, pkmn);
                    }
                    else if (type == "legal_fix")
                    {
                        var alm = new AutoLegality(pk, version);
                        ns.Write(alm.GetLegalPKM().DecryptedBoxData, 0, alm.GetLegalPKM().DecryptedBoxData.Length);
                    }
                }
                else
                {
                    byte[] request_string_size    = Read_NS_Data(ns, 10);
                    string request_string_sizeStr = Encoding.UTF8.GetString(request_string_size, 0, request_string_size.Length);

                    int.TryParse(request_string_sizeStr, out int dataSize);
                    byte[] request_type    = Read_NS_Data(ns, dataSize);
                    string request_typeStr = Encoding.UTF8.GetString(request_type, 0, request_type.Length);
                    switch (request_typeStr)
                    {
                    case "enc_base64_get":
                    {
                        var qr = GenQR(GetPokemon(ns));
                        if (qr == null || qr.Length == 0)
                        {
                            ns.Write(Encoding.UTF8.GetBytes("."), 0, Encoding.UTF8.GetBytes(".").Length);
                            //throw new System.ArgumentException("Tried to upload something that wasn't a pokemon or something else went wrong during qr generation!");
                        }
                        else
                        {
                            ns.Write(qr, 0, qr.Length);
                        }
                        break;
                    }

                    case "encounter":
                    {
                        var queryStr = GetQueryString(ns);

                        var queries = queryStr.Split('|');

                        if (!Enum.GetNames(typeof(Species)).Any(s => s.ToLower() == queries[0]))
                        {
                            throw new System.ArgumentException("Invalid pokemon name provided!");
                        }
                        var data = EncounterLearn.GetLearnSummary(queries[0], queries.Skip(1));
                        var e    = new Encounters
                        {
                            Gen1 = new List <GenLoc>(),
                            Gen2 = new List <GenLoc>(),
                            Gen3 = new List <GenLoc>(),
                            Gen4 = new List <GenLoc>(),
                            Gen5 = new List <GenLoc>(),
                            Gen6 = new List <GenLoc>(),
                            Gen7 = new List <GenLoc>(),
                            Gen8 = new List <GenLoc>(),
                        };
                        bool first = true;
                        var  enc   = "";
                        foreach (var line in data)
                        {
                            if (line.StartsWith("="))
                            {
                                if (!first)
                                {
                                }
                                enc   = line.Replace("=", "");
                                first = false;
                                continue;
                            }
                            var gen   = GetStringFromRegex(@"Gen[0-9]", line);
                            var loc   = GetStringFromRegex(@"(?<=.{8}).+?(?=:)", line);
                            var games = GetStringFromRegex(@"([\t ][A-Z |,]{1,100}$|Any)", line);
                            games = games.Replace(" ", "");
                            games = games.Trim(':');
                            games = games.Trim('\t');
                            string[] gamesArray = games.Split(',');
                            GenLoc   entry      = new GenLoc();
                            switch (gen)
                            {
                            case "Gen1":
                            {
                                entry = e.Gen1.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen2":
                            {
                                entry = e.Gen2.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen3":
                            {
                                entry = e.Gen3.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen4":
                            {
                                entry = e.Gen4.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen5":
                            {
                                entry = e.Gen5.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen6":
                            {
                                entry = e.Gen6.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen7":
                            {
                                entry = e.Gen7.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }

                            case "Gen8":
                            {
                                entry = e.Gen8.FirstOrDefault(l => l.EncounterType == enc);
                                break;
                            }
                            }
                            if (entry != null)
                            {
                                if (entry.Locations == null)
                                {
                                    entry.Locations = new List <Locs>();
                                }
                                var tmpGamesList = new List <string>();
                                foreach (var game in gamesArray)
                                {
                                    tmpGamesList.Add(game);
                                }
                                entry.Locations.Add(new Locs
                                    {
                                        Location = loc,
                                        Games    = tmpGamesList,
                                    });
                            }
                            else
                            {
                                var tmpGamesList = new List <string>();
                                foreach (var game in gamesArray)
                                {
                                    tmpGamesList.Add(game);
                                }
                                var tmpLocations = new List <Locs>
                                {
                                    new Locs
                                    {
                                        Location = loc,
                                        Games    = tmpGamesList,
                                    }
                                };
                                var tmpGenLoc = new GenLoc
                                {
                                    EncounterType = enc,
                                    Locations     = tmpLocations,
                                };
                                switch (gen)
                                {
                                case "Gen1":
                                {
                                    e.Gen1.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen2":
                                {
                                    e.Gen2.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen3":
                                {
                                    e.Gen3.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen4":
                                {
                                    e.Gen4.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen5":
                                {
                                    e.Gen5.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen6":
                                {
                                    e.Gen6.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen7":
                                {
                                    e.Gen7.Add(tmpGenLoc);
                                    break;
                                }

                                case "Gen8":
                                {
                                    e.Gen8.Add(tmpGenLoc);
                                    break;
                                }
                                }
                            }
                        }
                        var json = JsonConvert.SerializeObject(e);
                        ns.Write(Encoding.UTF8.GetBytes(json), 0, Encoding.UTF8.GetBytes(json).Length);
                        break;
                    }

                    case "move_learn":
                    {
                        var queryStr = GetQueryString(ns);
                        var queries  = queryStr.Split('|');
                        if (!Enum.GetNames(typeof(Species)).Any(s => s.ToLower() == queries[0]))
                        {
                            throw new System.ArgumentException("Invalid pokemon name provided!");
                        }
                        var i     = 1;
                        var moves = new List <LearnableMove>();
                        foreach (var move in queries.Skip(1))
                        {
                            if (i > 4)
                            {
                                break;
                            }
                            var workaround = move.Split(',');
                            var tmpMove    = new LearnableMove
                            {
                                MoveName = move,
                            };
                            if (!ConsoleIndex.moveNames.Any(m => m.ToLower().Contains(move.ToLower())))
                            {
                                tmpMove.Learnable = false;
                            }
                            else
                            {
                                tmpMove.Learnable = bool.Parse(EncounterLearn.CanLearn(queries[0], workaround).ToString());
                            }
                            moves.Add(tmpMove);
                            i++;
                        }
                        var json = JsonConvert.SerializeObject(moves);
                        ns.Write(Encoding.UTF8.GetBytes(json), 0, Encoding.UTF8.GetBytes(json).Length);
                        break;
                    }

                    case "poke_info":
                    {
                        var summary = new GPSSSummary(GetPokemon(ns), GameInfo.Strings);
                        var pkmn    = summary.CreatePKMN();
                        if (pkmn == null)
                        {
                            throw new System.ArgumentException("There was an issue reading the Pokemon data, is it not a Pokemon? (or maybe timeout?)");
                        }
                        var ser = new DataContractJsonSerializer(typeof(Pokemon));
                        ser.WriteObject(ns, pkmn);
                        break;
                    }

                    case "auto_legality":
                    {
                        byte[] size        = Read_NS_Data(ns, 8);
                        string dataSizeStr = Encoding.UTF8.GetString(size, 0, size.Length);
                        int.TryParse(dataSizeStr, out int dSize);
                        byte[]      version    = Read_NS_Data(ns, dSize);
                        string      versionStr = Encoding.UTF8.GetString(version, 0, version.Length);
                        GameVersion versionInt = 0;

                        var pkmn = GetPokemon(ns);
                        if (pkmn == null)
                        {
                            throw new System.ArgumentException("There was an issue reading the Pokemon data, is it not a Pokemon? (or maybe timeout?)");
                        }
                        if (versionStr == "?")
                        {
                            versionInt = GetPlaceholderVersion(pkmn);
                        }
                        else
                        {
                            int.TryParse(versionStr, out int vInt);
                            versionInt = (GameVersion)vInt;
                        }

                        var lc     = new LegalityAnalysis(pkmn);
                        var report = lc.Report().Split('\n');
                        var data   = new LegalityReturn
                        {
                            ran     = false,
                            success = true
                        };
                        if (!lc.Valid)
                        {
                            var alm = new AutoLegality(pkmn, versionInt.ToString());
                            Console.WriteLine(pkmn.Version);
                            var legalPK = alm.Legalize(pkmn, versionInt);
                            var qr      = System.Convert.ToBase64String(GenQR(legalPK));
                            if (qr == null || qr.Length == 0)
                            {
                                throw new ArgumentException("bad pokemon!");
                            }
                            data.qr      = System.Convert.ToBase64String(GenQR(legalPK));
                            data.pokemon = System.Convert.ToBase64String(legalPK.DecryptedBoxData);
                            data.ran     = true;
                            data.species = new GPSSSummary(legalPK, GameInfo.Strings).Species;
                            data.success = new LegalityAnalysis(legalPK).Valid;
                        }
                        if (data.ran && data.success)
                        {
                            data.report = report;
                        }
                        var json = JsonConvert.SerializeObject(data);
                        ns.Write(Encoding.UTF8.GetBytes(json), 0, Encoding.UTF8.GetBytes(json).Length);
                        break;
                    }

                    /*           case "index_lookup":
                     *             {
                     *                 var queryStr = GetQueryString(ns);
                     *                 break;
                     *             }*/
                    default: {
                        ns.Write(Encoding.UTF8.GetBytes("I don't know how to handle this query type yet!"), 0, Encoding.UTF8.GetBytes("I don't know how to handle this query type yet!").Length);
                        break;
                    }
                    }
                }
            } catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                byte[] err = Encoding.Default.GetBytes("Not a Pokemon!");
                ns.Write(err, 0, err.Length);
            } finally
            {
                ns.Flush();
                ns.Dispose();
                client.Dispose();
            }
        }
    }