示例#1
0
        private void LocalHandler(int gameId, PackedResult result)
        {
            var worldId = pool.GetContext(gameId).World;

            if (result.Type == PackedResultType.MarketRequest)
            {
                var itemid = ((MarketRequestResult)result.Result).ItemId;
                Task.Run(() =>
                {
                    RETRY:
                    int ret                = 0;
                    var listing            = new List <ListingData>();
                    var history            = new List <HistoryData>();
                    PackedResultBundle res = null;

                    try
                    {
                        var local = new ExchangeProcedure();

                        try
                        {
                            ret = local.Step2(itemid, out res);
                        }
                        catch (HttpRequestException exception)
                        {
                            ret = 02_0000 + (int)exception.Data["StatusCode"];
                        }

                        if (!CheckVendor.NotValidResponseCode(ret))
                        {
                            listing.AddRange(res.Listings.Select(x =>
                                                                 ListingData.FromResultItem(x.RawItem, x.ReportTime, 0, x.WorldId)));
                            history.AddRange(res.Histories.Select(x =>
                                                                  HistoryData.FromResultItem(x.RawItem, x.ReportTime, 0, x.WorldId)));
                        }

                        if (ret % 10000 == 0511 && ApiVendor.ValidateAndRenewToken())
                        {
                            goto RETRY;
                        }

                        var lst = (List <ListingData>)DataHolder.GetCache(worldId, itemid, 0);
                        var hst = (List <HistoryData>)DataHolder.GetCache(worldId, itemid, 1);

                        if (lst != null)
                        {
                            listing.RemoveAll(x => x.World == worldId);
                            br.OverlayListingData = listing.Concat(lst)
                                                    .OrderBy(x => x.UnitPrice).ToList();
                        }
                        else
                        {
                            lst = br.OverlayListingData.Where(x => x.World == worldId && x.ItemId == itemid).ToList();

                            if (lst.Any())
                            {
                                listing.RemoveAll(x => x.World == worldId);
                                br.OverlayListingData = listing.Concat(lst)
                                                        .OrderBy(x => x.UnitPrice).ToList();
                            }

                            br.OverlayListingData = listing.OrderBy(x => x.UnitPrice).ToList();
                        }

                        if (hst != null)
                        {
                            history.RemoveAll(x => x.World == worldId);
                            br.OverlayHistoryData = history.Concat(hst)
                                                    .OrderByDescending(x => x.PurchaseTime).ToList();
                        }
                        else
                        {
                            hst = br.OverlayHistoryData.Where(x => x.World == worldId && x.ItemId == itemid).ToList();

                            if (hst.Any())
                            {
                                history.RemoveAll(x => x.World == worldId);
                                br.OverlayHistoryData = history.Concat(hst)
                                                        .OrderByDescending(x => x.PurchaseTime).ToList();
                            }

                            br.OverlayHistoryData = history.OrderByDescending(x => x.PurchaseTime).ToList();
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                        ret = 02_0000 + (int)(ex.Data["StatusCode"]);
                        if (ret == 02_0511 && ApiVendor.ValidateAndRenewToken())
                        {
                            goto RETRY;
                        }
                    }
                    catch (Exception)
                    {
                        ret = 02_0000;
                    }

                    if (CheckVendor.NotValidResponseCode(ret))
                    {
                        LoggingManagementService.WriteLine(
                            $"Api Error: {MessageVendor.FormatError(ret)} on requesting {DictionaryManagementService.Item[itemid]}",
                            "ApiSys");
                    }
                });
            }

            if (result.Type == PackedResultType.MarketHistory)
            {
                var r = (MarketHistoryResult)result.Result;
                br.OverlayItemId = r.ItemId;
                var t = new List <HistoryData>();
                foreach (var i in r.HistoryItems)
                {
                    t.Add(new HistoryData
                    {
                        BuyerName    = i.BuyerName,
                        IsHq         = i.IsHq == 1,
                        ItemId       = i.ItemId,
                        OnMannequin  = i.OnMannequin == 1,
                        PurchaseTime = Helper.UnixTimeStampToDateTime(i.PurchaseTime),
                        Quantity     = i.Quantity,
                        UnitPrice    = i.UnitPrice,
                        UpdateTime   = DateTime.Now,
                        World        = worldId,
                        Zone         = 0
                    });
                }

                DataHolder.AddCache(worldId, r.ItemId, 1, t, 120);
            }

            if (result.Type == PackedResultType.MarketListing)
            {
                var r = (MarketListingResult)result.Result;
                var t = new List <ListingData>();
                foreach (var i in r.ListingItems)
                {
                    t.Add(new ListingData
                    {
                        ItemId           = i.ItemId,
                        IsHq             = i.IsHq == 1,
                        Materia          = new int[] { i.Materia1, i.Materia2, i.Materia3, i.Materia4, i.Materia5 },
                        UnitPrice        = i.UnitPrice,
                        Quantity         = i.Quantity,
                        Tax              = i.TotalTax,
                        Retainer         = i.RetainerName,
                        Artisan          = i.ArtisanId.ToString("X"),
                        OnMannequin      = i.OnMannequin == 1,
                        PlayerName       = i.PlayerName,
                        RetainerLocation = i.RetainerLocation,
                        DyeId            = i.DyeId,
                        ListingTime      = Helper.UnixTimeStampToDateTime(i.UpdateTime),
                        UpdateTime       = DateTime.Now,
                        World            = worldId,
                        Zone             = 0,
                    });
                }

                var sample = t.FirstOrDefault();
                if (sample != null)
                {
                    DataHolder.AddCache(worldId, sample.ItemId, 0, t, 120);
                }
            }

            LoggingManagementService.WriteLine($"Data Received: {result.Type.ToString()} @ {DictionaryManagementService.World[worldId]}", $"Game {gameId}");
        }