Example #1
0
 public bool TradingDealExists(string dealId, out TradingDeal deal)
 {
     try
     {
         Guid guid = Guid.Parse(dealId);
         return((deal = _marketService.GetOpenTradingDeal(guid)) != null);
     }
     catch (Exception e)
     {
         deal = null;
         return(false);
     }
 }
Example #2
0
        public ResponseContext Post(Dictionary <string, object> param)
        {
            RequestContext request = (RequestContext)param["request"];
            MtcgClient     client  = (MtcgClient)param["client"];

            //trade with existing deal
            if (!string.IsNullOrWhiteSpace(request.RequestedResource))
            {
                TradingDeal pendingDeal = null;
                if (!_marketController.TradingDealExists(request.RequestedResource, out pendingDeal))
                {
                    return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, $"There is no open deal with the given id {request.RequestedResource}")));
                }

                if (pendingDeal.PublisherId == client.User.UserId)
                {
                    return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, "You can't trade with yourself")));
                }

                Card cardToTrade = null;
                Guid cardGuid;
                if (string.IsNullOrWhiteSpace(request.Payload) || !Guid.TryParse(request.Payload, out cardGuid) ||
                    (cardToTrade = client.User.Stack.GetAllCards().FirstOrDefault(c => c.Guid.Equals(cardGuid))) == null)
                {
                    return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, "You must provide a card id that you possess and want to trade")));
                }

                return(new ResponseContext(request, _marketController.ProcessTrade(pendingDeal, cardToTrade, client)));
            }

            //create new deal
            TradingDeal deal = JsonSerializer.Deserialize <TradingDeal>(request.Payload);

            if (deal == null || string.IsNullOrWhiteSpace(deal.RequestedElement) || string.IsNullOrWhiteSpace(deal.RequestedType))
            {
                return(new ResponseContext(request,
                                           new KeyValuePair <StatusCode, object>(StatusCode.BadRequest,
                                                                                 "All properties must be set: CardId, RequestedElement, MinimumDamage, RequestedType")));
            }
            if (!client.User.Stack.GetAllCards().Any(c => c.Guid == deal.CardId))
            {
                return(new ResponseContext(request,
                                           new KeyValuePair <StatusCode, object>(StatusCode.BadRequest,
                                                                                 "Nice try, but you don't have this card ;)")));
            }
            deal.PublisherId = client.User.UserId;
            return(new ResponseContext(request, _marketController.AddTradingDeal(deal)));
        }
Example #3
0
 public KeyValuePair <StatusCode, object> AddTradingDeal(TradingDeal dealToAdd)
 {
     try
     {
         var deal = _marketService.AddTradingDeal(dealToAdd);
         if (deal == null)
         {
             return(new KeyValuePair <StatusCode, object>(StatusCode.InternalServerError, "Deal could not be saved"));
         }
         return(new KeyValuePair <StatusCode, object>(StatusCode.Created, deal));
     }
     catch (Exception e)
     {
         return(HandleException(e));
     }
 }
Example #4
0
        public KeyValuePair <StatusCode, object> DeleteTradingDeal(TradingDeal deal)
        {
            try
            {
                if (_marketService.DeleteTradingDeal(deal))
                {
                    return(new KeyValuePair <StatusCode, object>(StatusCode.OK, $"Deleted {deal.Guid} successfully"));
                }

                return(new KeyValuePair <StatusCode, object>(StatusCode.InternalServerError, "Something went wrong"));
            }
            catch (Exception e)
            {
                return(HandleException(e));
            }
        }
        public bool DeleteTradingDeal(TradingDeal deal)
        {
            string statement = "DELETE FROM mtcg.\"TradingDeal\" " +
                               "WHERE \"Guid\"=@guid";

            using (NpgsqlCommand cmd = new NpgsqlCommand(statement, PostgreSQLSingleton.GetInstance.Connection))
            {
                cmd.Parameters.Add("guid", NpgsqlDbType.Uuid).Value = deal.Guid;
                cmd.Prepare();
                if (cmd.ExecuteNonQuery() != 1)
                {
                    return(false);
                }

                return(true);
            }
        }
        public bool Trade(TradingDeal deal, int fullfillerId)
        {
            string statement = "UPDATE mtcg.\"TradingDeal\" " +
                               "SET \"IsFullfilled\"=TRUE, \"FullfillerId\"=@fullfillerId " +
                               "WHERE \"Guid\"=@guid";

            using (NpgsqlCommand cmd = new NpgsqlCommand(statement, PostgreSQLSingleton.GetInstance.Connection))
            {
                cmd.Parameters.Add("guid", NpgsqlDbType.Uuid).Value            = deal.Guid;
                cmd.Parameters.Add("fullfillerId", NpgsqlDbType.Integer).Value = fullfillerId;
                cmd.Prepare();
                if (cmd.ExecuteNonQuery() != 1)
                {
                    return(false);
                }
            }
            return(true);
        }
        public TradingDeal AddTradingDeal(TradingDeal deal)
        {
            Guid guid = Guid.NewGuid();

            deal.Guid = guid;
            string statement =
                "INSERT INTO mtcg.\"TradingDeal\"(\"Guid\", \"PublisherId\", \"CardId\", \"RequestedType\", \"MinimumDamage\", \"RequestedElement\", \"IsFullfilled\") " +
                "VALUES (@guid, @publisherId, @cardId, @requestedType, @minimumDamage, @requestedElement, @isFullfilled);";

            using (NpgsqlCommand cmd = new NpgsqlCommand(statement, PostgreSQLSingleton.GetInstance.Connection))
            {
                cmd.Parameters.Add("guid", NpgsqlDbType.Uuid).Value                = deal.Guid;
                cmd.Parameters.Add("publisherId", NpgsqlDbType.Integer).Value      = deal.PublisherId;
                cmd.Parameters.Add("cardId", NpgsqlDbType.Uuid).Value              = deal.CardId;
                cmd.Parameters.Add("requestedType", NpgsqlDbType.Varchar).Value    = deal.RequestedType;
                cmd.Parameters.Add("minimumDamage", NpgsqlDbType.Double).Value     = deal.MinimumDamage;
                cmd.Parameters.Add("requestedElement", NpgsqlDbType.Varchar).Value = deal.RequestedElement;
                cmd.Parameters.Add("isFullfilled", NpgsqlDbType.Boolean).Value     = false;
                cmd.Prepare();
                //ExecuteNonQuery returns affected rows
                return(cmd.ExecuteNonQuery() == 1 ? deal : null);
            }
        }
Example #8
0
        public ResponseContext Delete(Dictionary <string, object> param)
        {
            RequestContext request = (RequestContext)param["request"];
            MtcgClient     client  = (MtcgClient)param["client"];

            //trade with existing deal
            if (!string.IsNullOrWhiteSpace(request.RequestedResource))
            {
                TradingDeal pendingDeal = null;
                if (!_marketController.TradingDealExists(request.RequestedResource, out pendingDeal))
                {
                    return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, $"There is no open deal with the given id {request.RequestedResource}")));
                }

                if (pendingDeal.PublisherId != client.User.UserId)
                {
                    return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, "You can't delete a trade that you did not create")));
                }

                return(new ResponseContext(request, _marketController.DeleteTradingDeal(pendingDeal)));
            }

            return(new ResponseContext(request, new KeyValuePair <StatusCode, object>(StatusCode.BadRequest, "You must provide an id of an open trading deal")));
        }
Example #9
0
        public KeyValuePair <StatusCode, object> ProcessTrade(TradingDeal deal, Card cardToTrade, MtcgClient client)
        {
            try
            {
                if (deal.MinimumDamage > cardToTrade.Damage || cardToTrade.GetType().Name != deal.RequestedType ||
                    Enum.GetName(typeof(ElementType), cardToTrade.Element) != deal.RequestedElement)
                {
                    return(new KeyValuePair <StatusCode, object>(StatusCode.BadRequest,
                                                                 $"The given card does not match the requirements Min Damage:{deal.MinimumDamage} Element: {deal.RequestedElement} Type: {deal.RequestedType}"));
                }

                _userService.AddToStack(deal.PublisherId, cardToTrade.Guid);
                _userService.AddToStack(client.User.UserId, deal.CardId);
                _userService.DeleteFromStack(deal.PublisherId, deal.Guid);
                _userService.DeleteFromStack(client.User.UserId, cardToTrade.Guid);

                _marketService.Trade(deal, client.User.UserId);
                return(new KeyValuePair <StatusCode, object>(StatusCode.OK, "Trade completed successfully"));
            }
            catch (Exception e)
            {
                return(HandleException(e));
            }
        }
        //Xem bang này có bảng lịch sử clone từ bảng này ra hay không?
        public static string GetEntityNameHist(string entityName)
        {
            if (entityName == ApprovalAccount.EntityName())
            {
                return(ApprovalAccountHist.EntityName());
            }
            if (entityName == ApprovalDealing.EntityName())
            {
                return(ApprovalDealingHist.EntityName());
            }
            if (entityName == ApprovalMember.EntityName())
            {
                return(ApprovalMemberHist.EntityName());
            }
            if (entityName == ApprovalOrder.EntityName())
            {
                return(ApprovalOrderHist.EntityName());
            }
            if (entityName == ApprovalPreRisk.EntityName())
            {
                return(ApprovalPreRiskHist.EntityName());
            }
            if (entityName == ApprovalSystem.EntityName())
            {
                return(ApprovalSystemHist.EntityName());
            }

            if (entityName == AccountTransaction.EntityName())
            {
                return(AccountTransactionHist.EntityName());
            }
            if (entityName == BrokerOrder.EntityName())
            {
                return(BrokerOrderHist.EntityName());
            }
            if (entityName == ExecutionReport.EntityName())
            {
                return(ExecutionReportHist.EntityName());
            }

            if (entityName == OpenPositionDetail.EntityName())
            {
                return(OpenPositionDetailHist.EntityName());
            }
            if (entityName == OpenPosition.EntityName())
            {
                return(OpenPositionHist.EntityName());
            }
            if (entityName == OrderTransaction.EntityName())
            {
                return(OrderTransactionHist.EntityName());
            }
            if (entityName == SpecAccounting.EntityName())
            {
                return(SpecAccountingHist.EntityName());
            }
            if (entityName == SymbolSettlementPrice.EntityName())
            {
                return(SymbolSettlementPriceDaily.EntityName());
            }
            if (entityName == TradingDeal.EntityName())
            {
                return(TradingDealHist.EntityName());
            }

            return(null);
        }