// Local delete
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            MarketplaceEntry me = (MarketplaceEntry)((Button)sender).DataContext;

            if (MessageBox.Show("Are you sure you want to delete this script? This action can't be reversed!", "Confirm script deletion", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if (me.HasNoMetadata) // Is outdated local script before the marketplace introduction
                {
                    File.Delete(me.Link);
                }
                else
                {
                    File.Delete(me.LocalPath);
                    File.Delete(me.LocalScriptPath);
                    FileInfo fi = new FileInfo(me.LocalScriptPath);
                    if (fi.Directory.GetFiles().Length == 0)
                    {
                        fi.Directory.Delete();
                    }
                }

                MarketplaceManager.Instance.LocalDB.Remove(me);
                MarketplaceEntry otherEntry = MarketplaceManager.Instance.RemoteDB.FirstOrDefault(re => re.ProjectID.Equals(me.ProjectID, StringComparison.OrdinalIgnoreCase));
                if (otherEntry != null)
                {
                    otherEntry.DownloadButtonEnabled = true;
                }
            }
        }
        // Local or remote download
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MarketplaceEntry me   = (MarketplaceEntry)((Button)sender).DataContext;
            Action           post = null;

            if (me.IsLocal) // Update is pressed, just delete local and treat as remote DL
            {
                MarketplaceEntry capture = me;
                post = new Action(() =>
                {
                    File.Delete(capture.LocalPath);
                    File.Delete(capture.LocalScriptPath);
                    FileInfo fi = new FileInfo(capture.LocalScriptPath);
                    if (fi.Directory.GetFiles().Length == 0)
                    {
                        fi.Directory.Delete();
                    }

                    MarketplaceManager.Instance.LocalDB.Remove(capture);
                });

                me = MarketplaceManager.Instance.RemoteDB.FirstOrDefault(re => re.ProjectID.Equals(me.ProjectID, StringComparison.OrdinalIgnoreCase));
            }

            try
            {
                WebClient wc = new WebClient();
                string    s  = wc.DownloadString(me.Link);
                wc.Dispose();
                using (MD5 md5 = MD5.Create())
                {
                    if (!string.Equals(AppState.GetMd5Hash(md5, s), me.MD5, StringComparison.OrdinalIgnoreCase))
                    {
                        MarketplaceManager.Instance.Alerts.Add(MarketplaceAlert.DownloadFailed);
                    }
                }

                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts", me.ProjectID);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                post?.Invoke();
                string meta = JsonConvert.SerializeObject(me);
                me.LocalPath       = Path.Combine(path, me.ProjectID + ".json");
                me.LocalScriptPath = Path.Combine(path, me.ProjectID + ".lua");
                File.WriteAllText(me.LocalPath, meta);
                File.WriteAllText(me.LocalScriptPath, s);
                MarketplaceEntry local = JsonConvert.DeserializeObject <MarketplaceEntry>(meta);
                local.LocalPath       = me.LocalPath;
                local.LocalScriptPath = me.LocalScriptPath;
                local.IsLocal         = true;
                local.MD5             = me.MD5;
                MarketplaceManager.Instance.Alerts.Add(MarketplaceAlert.RestartNeeded);
                MarketplaceManager.Instance.LocalDB.Add(local);
                MarketplaceManager.Instance.CheckMarketplaceEntryAvailability(local);
            }
            catch
            {
                MarketplaceManager.Instance.Alerts.Add(MarketplaceAlert.DownloadFailed);
            }
        }
Beispiel #3
0
 public MarketplaceModel(MarketplaceEntry meme)
 {
     Meme = meme;
 }
Beispiel #4
0
        public static void TradeMeme(int quantity, int marketEntryID, string username)
        {
            using (UsersDB db = new UsersDB())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        MarketplaceEntry marketEntry = db.Marketplace.Include(u => u.MemeAsset.MemeEntry)
                                                       .Include(u => u.User)
                                                       .SingleOrDefault(m => m.ID == marketEntryID);

                        User buyer = db.Users.SingleOrDefault(u => u.Username == username);

                        User owner = marketEntry.User;

                        decimal memePrice = marketEntry.Price;

                        decimal totalPrice = memePrice * quantity;

                        if (buyer.Currency >= totalPrice && marketEntry.Quantity >= quantity && quantity > 0)
                        {
                            buyer.Currency -= totalPrice;
                            owner.Currency += totalPrice;

                            MemeAsset asset = new MemeAsset {
                                UserID    = buyer.ID,
                                MemeID    = marketEntry.MemeAsset.MemeEntry.ID,
                                Amount    = quantity,
                                AssetName = marketEntry.MemeAsset.AssetName
                            };

                            MemeAsset existingAsset = db.MemeOwners
                                                      .SingleOrDefault(a => a.UserID == buyer.ID &&
                                                                       a.MemeID == marketEntry.MemeAsset.MemeID);

                            if (existingAsset == null)
                            {
                                db.MemeOwners.Add(asset);
                            }
                            else
                            {
                                existingAsset.Amount += quantity;
                            }

                            if (marketEntry.Quantity > quantity)
                            {
                                marketEntry.Quantity -= quantity;
                            }
                            else
                            {
                                db.Marketplace.Remove(marketEntry);
                            }

                            db.SaveChanges();
                            trans.Commit();
                        }
                    }
                    catch
                    {
                        trans.Rollback();
                    }
                }
            }
        }