Exemplo n.º 1
0
        private void Trade(int mode)
        {
            ShopItemBean shopItem = (ShopItemBean)listBoxShopItems.SelectedItem;

            if (shopItem == null)
            {
                MessageBox.Show("未选择任何条目");
                return;
            }
            int amount;

            try
            {
                amount = int.Parse(textBoxAmount.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("数量输入不正确");
                return;
            }
            if (comboBoxAmount.SelectedItem.Equals("组"))
            {
                amount = amount * shopItem.GroupAmount;
            }
            int price = shopItem.Price * amount;

            if (mode < 0 && price > account.Balance)
            {
                MessageBox.Show("账户余额不足");
                return;
            }
            DialogResult result = MessageBox.Show(string.Format("确认要{0}吗?", mode < 0 ? "买入" : "卖出"), "确认交易", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
            {
                account.Balance += (price * mode);
                system.Balance  -= (price * mode);
                ShopRecordBean shopRecord = new ShopRecordBean
                {
                    PlayerId     = player.Id,
                    WorldId      = world.Id,
                    ItemFirstId  = shopItem.FirstId,
                    ItemSecondId = shopItem.SecondId,
                    ItemName     = shopItem.Name,
                    Amount       = amount,
                    Type         = mode,
                    Time         = DateTime.Now
                };

                BankAccountRecordBean bankRecord = new BankAccountRecordBean
                {
                    Src    = mode > 0 ? 0 : account.Id,
                    Dest   = mode > 0 ? account.Id : 0,
                    Amount = amount,
                    Time   = DateTime.Now,
                    Note   = "系统商店"
                };

                DBUtils.BeginTransaction();
                if (bankAccountDAO.Update(account) > 0 && bankAccountDAO.Update(system) > 0 && shopRecordDAO.Insert(shopRecord) > 0 && bankAccountRecordDAO.Insert(bankRecord) > 0)
                {
                    textBoxPlayerMoney.Text = account.Balance.ToString();
                    DBUtils.Commit();
                    DBUtils.CloseConnection(null);
                }
                else
                {
                    DBUtils.Rollback();
                }
            }
        }