Exemple #1
0
    List <Vector2> SearchForPath(Vector2 startPos, Vector2 endPos, int[,] mapWeights)
    {
        int[,] newWeights = (int[, ])mapWeights.Clone();
        foreach (var loc in eventedLocations)
        {
            newWeights[(int)loc.x, (int)loc.y] = eventedLocationWeight;
        }

        foreach (var loc in occupiedLocations)
        {
            newWeights[(int)loc.x, (int)loc.y] = occupiedWeight;
        }

        SearchPoint        start = new SearchPoint((int)startPos.x, (int)startPos.y);
        SearchPoint        end   = new SearchPoint((int)endPos.x, (int)endPos.y);
        List <SearchPoint> path  = Pathfinding.CreateConnectingPath(start, end, newWeights, true, 10000);

        List <Vector2> retVal = new List <Vector2>();

        foreach (SearchPoint sp in path)
        {
            retVal.Add(sp.position);
        }

        return(retVal);
    }
Exemple #2
0
    static int CompareSearchPointsByPathShortness(SearchPoint a, SearchPoint b)
    {
        float aTotal = a.heuristic + a.depth;
        float bTotal = b.heuristic + b.depth;

        return((int)((aTotal * 100) - (bTotal * 100)));
    }
Exemple #3
0
    static void CreatePath(SearchPoint pathEnd, ref Dictionary <Vector2, SearchPoint> fastestToPoints, ref List <SearchPoint> pathOut)
    {
        if (pathEnd.previousPosition != pathEnd.position)
        {
            CreatePath(fastestToPoints[pathEnd.previousPosition], ref fastestToPoints, ref pathOut);
        }

        pathOut.Add(pathEnd);
    }
Exemple #4
0
        public SearchPoint HPScanAd(string url)
        {
            SearchPoint rec = new SearchPoint();

            rec.Id            = Guid.NewGuid();
            rec.WebSite       = 1;
            rec.CheckDate     = DateTime.Now;
            rec.UrlParameters = url.Trim();

            try
            {
                if (rec.UrlParameters.Substring(0, 1) != "/")
                {
                    rec.UrlParameters = "/" + rec.UrlParameters;
                }

                HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(WebSiteContext.GetURL(rec.WebSite) + rec.UrlParameters);

                var price = doc.DocumentNode.Descendants("span").Where(d => d.Attributes.Count > 0 &&
                                                                       d.Attributes["id"] != null && d.Attributes["id"].Value.Contains("offering-price")).Select(a => a.Attributes["content"]).FirstOrDefault();

                if (price != null)
                {
                    rec.Price = Convert.ToDouble(price.Value);
                }

                string priceDiscounted = "";
                try
                {
                    priceDiscounted = doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Count > 0 &&
                                                                                d.Attributes["class"] != null && d.Attributes["class"].Value.Contains("extra-discount-price")).Select(a => a.ChildNodes.Descendants("span").FirstOrDefault().InnerText).FirstOrDefault();

                    rec.Price = Convert.ToDouble(priceDiscounted);
                }
                catch (Exception)
                {
                    if (price == null)
                    {
                        return(null);
                    }
                }

                var name = doc.DocumentNode.Descendants("h1").Where(d => d.Attributes.Count > 0 &&
                                                                    d.Attributes["id"] != null && d.Attributes["id"].Value.Contains("product-name")).FirstOrDefault().InnerText;

                rec.Name = name.Trim().Replace("\r", "").Replace("\n", "");
            }
            catch (Exception e)
            {
                return(null);
            }

            return(rec);
        }
Exemple #5
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            if (cbWebSite.SelectedItem == null || txtURL.Text == "" || WebSiteContext.GetModel(((WebSiteModel)cbWebSite.SelectedItem).Id) == null)
            {
                MessageBox.Show("Lutfen gerekli alanlari oldurun!", "Uyari", MessageBoxButtons.OK);
                return;
            }

            if (DataContext.IsSearchPointExist(txtURL.Text))
            {
                MessageBox.Show("Ilan hali hazirlda kayitli!", "Uyari", MessageBoxButtons.OK);
                return;
            }

            WebSiteModel site = (WebSiteModel)cbWebSite.SelectedItem;

            switch (site.Id)
            {
            case 1:
                Rec = SC.HPScanAd(txtURL.Text);
                break;

            case 2:
                Rec = SC.N11ScanAd(txtURL.Text);
                break;

            default:
                break;
            }

            if (Rec == null)
            {
                lblTestStatus.ForeColor = Color.Red;
                lblTestStatus.Text      = "Operation failed!";
                btnAdd.Enabled          = false;
            }
            else
            {
                if (txtName.Text != "")
                {
                    string tempText = txtName.Text;
                    FillForm();
                    txtName.Text = tempText;
                    Rec.Name     = tempText;
                }
                else
                {
                    FillForm();
                }

                lblTestStatus.ForeColor = Color.Green;
                lblTestStatus.Text      = "Operation succecful!";
                btnAdd.Enabled          = true;
            }
        }
Exemple #6
0
        private void btnClearRegistrationFrom_Click(object sender, EventArgs e)
        {
            lblId.Text             = "-";
            lblCheckDate.Text      = "-";
            cbWebSite.SelectedItem = null;
            txtName.Text           = "";
            txtURL.Text            = "";
            txtPrice.Text          = "";
            lblCurrency.Text       = "";
            lblTestStatus.Text     = "";
            btnAdd.Enabled         = false;
            cbWebSite.Enabled      = true;
            btnTest.Enabled        = true;
            txtName.Enabled        = true;
            txtURL.Enabled         = true;

            Rec = null;
        }
Exemple #7
0
        private void tsBtnShow_Click(object sender, EventArgs e)
        {
            var rowIndex = dgAds.CurrentCell.RowIndex;
            var id       = (Guid)dgAds.Rows[rowIndex].Cells["Id"].Value;

            if (id != null && id != Guid.Empty)
            {
                var gridRec = DataContext.searchPoints.Where(a => a.Id == id).FirstOrDefault();

                if (gridRec != null)
                {
                    Rec = gridRec;

                    btnTest.Enabled = false;
                    txtName.Enabled = false;
                    txtURL.Enabled  = false;
                }
            }

            FillForm();
        }
Exemple #8
0
        public SearchPoint HPUpdateAd(SearchPoint rec)
        {
            try
            {
                HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(WebSiteContext.GetURL(rec.WebSite) + rec.UrlParameters);

                var price = doc.DocumentNode.Descendants("span").Where(d => d.Attributes.Count > 0 &&
                                                                       d.Attributes["id"] != null && d.Attributes["id"].Value.Contains("offering-price")).Select(a => a.Attributes["content"]).FirstOrDefault();

                if (price != null)
                {
                    rec.Price = Convert.ToDouble(price.Value);
                }

                string priceDiscounted = "";
                try
                {
                    priceDiscounted = doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Count > 0 &&
                                                                                d.Attributes["class"] != null && d.Attributes["class"].Value.Contains("extra-discount-price")).Select(a => a.ChildNodes.Descendants("span").FirstOrDefault().InnerText).FirstOrDefault();

                    rec.Price = Convert.ToDouble(priceDiscounted);
                }
                catch (Exception)
                {
                    if (price == null)
                    {
                        return(null);
                    }
                }
            }
            catch (Exception e)
            {
                return(null);
            }

            return(rec);
        }
Exemple #9
0
    public static List <SearchPoint> CreateConnectingPath(SearchPoint start, SearchPoint end, int[,] weights, bool canDiagonal = true, int maxSearchDepth = 1000)
    {
        //Use A* to look for a path real quick
        List <SearchPoint> openList = new List <SearchPoint>();

        Dictionary <Vector2, SearchPoint> closedList = new Dictionary <Vector2, SearchPoint>();

        SearchPoint curTile = new SearchPoint((int)start.position.x, (int)start.position.y);

        curTile.heuristic        = (float)RealDistanceHeuristic(start, end);
        curTile.depth            = 0;
        curTile.previousPosition = start.position;
        curTile.weight           = weights[(int)start.position.x, (int)start.position.y];
        openList.Add(curTile);

        if (debug)
        {
            Debug.Log("START " + start.position + ", TO " + end.position);
        }

        int depthSearched = -1;

        //We will return out of this loop when we hit the right place
        while (openList.Count != 0)
        {
            //Get the next node from the open list for the next search
            curTile = openList[0];
            openList.RemoveAt(0);
            closedList.Add(curTile.position, curTile);

            if (curTile.position == end.position)
            {
                //Success!
                List <SearchPoint> output = new List <SearchPoint>();
                CreatePath(curTile, ref closedList, ref output);
                return(output);
            }

            depthSearched++;
            if (maxSearchDepth > 0 && depthSearched > maxSearchDepth)
            {
                return(new List <SearchPoint>());
            }

            //Add all adjacent squares to the open list
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    //ignore the space we're searching from
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    //if we're looking at a diagonal skip it if we're ignoring them...
                    if (!canDiagonal && i != 0 && j != 0)
                    {
                        continue;
                    }

                    SearchPoint newPoint = new SearchPoint((int)curTile.position.x + i, (int)curTile.position.y + j);
                    newPoint.previousPosition = curTile.position;

                    if (newPoint.position.x >= 0 && newPoint.position.x < weights.GetLength(0) &&
                        newPoint.position.y >= 0 && newPoint.position.y < weights.GetLength(1) &&
                        curTile.depth < maxSearchDepth)
                    {
                        //Create this search node
                        newPoint.heuristic = (float)RealDistanceHeuristic(newPoint, end);
                        newPoint.weight    = weights[(int)newPoint.position.x, (int)newPoint.position.y];
                        if (newPoint.weight == SearchPoint.kImpassableWeight)
                        {
                            continue;
                        }

                        newPoint.depth = curTile.depth + RealDistanceHeuristic(curTile, newPoint);

                        //if we're looking at a diagonal make it cost a bit more to move there.
                        if (i != 0 && j != 0)
                        {
                            newPoint.heuristic += 0.2f;
                        }

                        //Check if this tile is in the closed list
                        if (closedList.ContainsKey(newPoint.position))
                        {
                            //Check to see if this is a faster route to the closed list point.
                            SearchPoint prevClosedListPoint = closedList[newPoint.position];
                            if (prevClosedListPoint.depth > newPoint.depth)
                            {
                                closedList[newPoint.position] = newPoint;
                            }

                            continue;
                        }

                        //Add it to the open list
                        //Check if it is in the open list first
                        bool addToList = true;
                        foreach (SearchPoint sp in openList)
                        {
                            if (newPoint.position.x == sp.position.x && newPoint.position.y == sp.position.y)
                            {
                                //It's in the list... but if this still has a better heuristic, we need to remove the old one and add this
                                if (newPoint.depth < sp.depth)
                                {
                                    openList.Remove(sp);
                                }
                                else
                                {
                                    addToList = false;
                                }
                                break;
                            }
                        }
                        if (addToList)
                        {
                            openList.Add(newPoint);
                        }
                    }
                }
            }

            openList.Sort(CompareSearchPointsByPathShortness);
            if (debug)
            {
                Debug.Log("Step");
                for (int i = 0; i < Mathf.Min(5, openList.Count); i++)
                {
                    Debug.Log(openList[i].position + ", depth " + openList[i].depth + ", h " + openList[i].heuristic);
                }
            }
        }

        return(new List <SearchPoint>());
    }
Exemple #10
0
 static int CompareSearchPointsByHeuristic(SearchPoint a, SearchPoint b)
 {
     return((int)(a.heuristic - b.heuristic));
 }
Exemple #11
0
 static float ManhattanDistanceHeuristic(SearchPoint start, SearchPoint end)
 {
     return(Mathf.Abs(start.position.x - end.position.x) + Mathf.Abs(start.position.y - end.position.y));
 }
Exemple #12
0
 static float RealDistanceHeuristic(SearchPoint start, SearchPoint end)
 {
     return(Vector2.Distance(start.position, end.position));
 }