internal static Rectangle DrawPortrait(System.Drawing.Graphics g, Models.Player.PortraitData p, int x, int y, double xScale, double yScale)
        {
            // Creating a full scale image of the portrait in the frame is necessary to avoid a scaling clusterfuck.
            Image    newPortrait = new Bitmap(105, 101);
            Graphics newGraphics = Graphics.FromImage(newPortrait);

            if (p == null || !System.IO.File.Exists(p.Filename))
            {
                newGraphics.DrawImage(Properties.Resources.defaultportrait, 7, 5);
            }
            else
            {
                try
                {
                    Image image = Bitmap.FromFile(p.Filename);
                    newGraphics.DrawImage(image, new Rectangle(7, 5, 90, 90), p.Position.X, p.Position.Y, p.Position.Width, p.Position.Height, GraphicsUnit.Pixel);
                    image.Dispose();
                }
                catch
                {
                    newGraphics.DrawImage(Properties.Resources.defaultportrait, 7, 5);
                }
            }

            newGraphics.DrawImage(Properties.Resources.portrait_summary, 0, 0);

            Rectangle destination = new Rectangle(x, y, (int)((newPortrait.Width * 0.6) * xScale), (int)((newPortrait.Height * 0.6) * yScale));

            g.DrawImage(newPortrait, destination, 0, 0, newPortrait.Width, newPortrait.Height, GraphicsUnit.Pixel);

            newGraphics.Dispose();
            newPortrait.Dispose();

            return(destination);
        }
Example #2
0
        internal static Models.Player GetPlayer(string name, string id)
        {
            string url = GetProfileFromID(name, id);

            if (string.IsNullOrEmpty(url))
            {
                return(null);
            }


            try
            {
                System.Net.WebClient client = new System.Net.WebClient();
                string data = client.DownloadString(url);

                int initPos  = data.IndexOf(@"<h3 class=""title-globe"">");
                int boundary = -1;

                int pos  = initPos;
                int pos2 = -1;


                if (pos == -1)
                {
                    return(null);
                }

                string race = "";
                pos = data.IndexOf(@"<div class=""module-body snapshot-", initPos);
                if (pos == -1)
                {
                    race = "random";
                }
                else
                {
                    pos += 33;
                    pos2 = data.IndexOf(@"""", pos);

                    race = data.Substring(pos, pos2 - pos);
                }


                Models.Player.PortraitData portrait = null;

                pos = data.IndexOf(@"<span class=""icon-frame """);


                if (pos > -1)
                {
                    pos = data.IndexOf(@"style=""", pos);
                    if (pos > -1)
                    {
                        pos += 7;
                        pos2 = data.IndexOf('"', pos);
                        string style = data.Substring(pos, pos2 - pos);

                        Match match = Regex.Match(style, @".*background: url\('(.*)'\) \-?(\d+)px \-?(\d+)px.*");

                        if (match.Success)
                        {
                            Uri uri = new Uri(url);
                            uri = new Uri(string.Format("http://{0}{1}", uri.Host, match.Groups[1].Value));

                            string localFile = string.Format(@"{0}\PortraitCache\{1}\{2}", Program.DataDirectory, uri.Query.Trim('?'), Path.GetFileName(uri.LocalPath));

                            if (!File.Exists(localFile))
                            {
                                try
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(localFile));
                                    client.DownloadFile(uri, localFile);
                                }
                                catch { localFile = null; }
                            }

                            if (localFile != null)
                            {
                                portrait = new Models.Player.PortraitData(localFile, int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), 90, 90);
                            }
                        }
                    }
                }

                url  = string.Format("{0}ladder/leagues", url);
                data = client.DownloadString(url);

                string league = "none";
                int    rank   = 0;
                int    wins   = -1;
                int    losses = -1;
                int    points = -1;

                if (data.Contains("<title>1v1"))
                {
                    try
                    {
                        pos = data.IndexOf(@"<span class=""badge badge-", pos);
                        if ((pos == -1) || pos > boundary)
                        {
                            league = "none";
                        }
                        else
                        {
                            pos   += 25;
                            pos2   = data.IndexOf(@" badge", pos);
                            league = data.Substring(pos, pos2 - pos);
                        }


                        initPos  = data.IndexOf("id=\"current-rank");
                        boundary = data.IndexOf("<tr", initPos);

                        pos = data.IndexOf("<td", initPos);
                        pos = data.IndexOf("<td", pos + 3);
                        pos = data.IndexOf(">", pos);

                        if ((pos == -1))
                        {
                            rank = 0;
                        }
                        else
                        {
                            pos += 1;
                            pos2 = data.IndexOf('<', pos);

                            if (!int.TryParse(Regex.Replace(data.Substring(pos, pos2 - pos).Trim('\r'), @"\D", "", RegexOptions.IgnoreCase), out rank))
                            {
                                rank = 0;
                            }
                        }



                        if (rank > 0)
                        {
                            pos = data.IndexOf(@"<a href=""", initPos);

                            if (pos > -1)
                            {
                                pos += 9;
                                if (pos < boundary)
                                {
                                    pos2 = data.IndexOf(@""">", pos);

                                    string ladderPath = data.Substring(pos, pos2 - pos);
                                    Uri    uri        = new Uri(url);
                                    uri = new Uri(string.Format("http://{0}{1}", uri.Host, ladderPath));

                                    try
                                    {
                                        string ladderData = data;

                                        pos = ladderData.IndexOf(@"id=""current-rank"">");
                                        if (pos > -1)
                                        {
                                            int ladderBoundary = ladderData.IndexOf("</tr>", pos);
                                            if (ladderBoundary > -1)
                                            {
                                                pos = ladderData.IndexOf(@"<td class=""align-center"">", pos);
                                                if (pos > -1 && pos < ladderBoundary)
                                                {
                                                    pos += 25;
                                                    pos2 = ladderData.IndexOf("</td>", pos);

                                                    if (!int.TryParse(ladderData.Substring(pos, pos2 - pos), out points))
                                                    {
                                                        points = -1;
                                                    }

                                                    pos = ladderData.IndexOf(@"<td class=""align-center"">", pos2);

                                                    if (pos > -1 && pos < ladderBoundary)
                                                    {
                                                        pos += 25;
                                                        pos2 = ladderData.IndexOf("</td>", pos);

                                                        if (!int.TryParse(ladderData.Substring(pos, pos2 - pos), out wins))
                                                        {
                                                            wins = -1;
                                                        }

                                                        pos = ladderData.IndexOf(@"<td class=""align-center"">", pos2);

                                                        if (pos > -1 && pos < ladderBoundary)
                                                        {
                                                            pos += 25;
                                                            pos2 = ladderData.IndexOf("</td>", pos);

                                                            if (!int.TryParse(ladderData.Substring(pos, pos2 - pos), out losses))
                                                            {
                                                                losses = -1;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch { }
                                }
                            }
                        }
                    }
                    catch { }
                }



                return(new Models.Player(name, race, league, rank, points, wins, losses, portrait));
            }
            catch { return(null); }
        }