Esempio n. 1
0
        protected override async Task <string> DataReceived(UserCommandType command)
        {
            string result = "";

            try
            {
                var trigger = BuyTriggerTimer.RemoveUserTrigger(command.username, command.stockSymbol);
                if (trigger != null)
                {
                    command.fundsSpecified = true;
                    command.funds          = trigger.Amount;
                    // Check if trigger exists
                    MySQL db = new MySQL();
                    result = await db.PerformTransaction(CancelBuy, command).ConfigureAwait(false);
                }
                else
                {
                    return(LogErrorEvent(command, $"No trigger set for {command.stockSymbol}"));
                }
            }
            catch (Exception ex)
            {
                LogDebugEvent(command, ex.Message);
                return(LogErrorEvent(command, "Error processing command"));
            }
            return(result);
        }
Esempio n. 2
0
        public static async Task <string> StartOrUpdateTimer(UserCommandType command)
        {
            BuyTriggerTimer timer;
            bool            newTimer;

            lock (_timerLock)
            {
                newTimer = !_timers.TryGetValue(command.stockSymbol, out timer);

                if (newTimer)
                {
                    if (command.fundsSpecified)
                    {
                        return("Set buy amount before creating trigger");
                    }
                    timer = new BuyTriggerTimer(command.stockSymbol);
                    if (!_timers.TryAdd(command.stockSymbol, timer))
                    {
                        LogDebugEvent(command, "Error adding timer");
                    }
                }
            }

            var msg = await timer.AddOrUpdateUser(command).ConfigureAwait(false);

            if (newTimer)
            {
                var _ = timer.Start();
            }
            return(msg);
        }
Esempio n. 3
0
        protected override async Task <string> DataReceived(UserCommandType command)
        {
            try
            {
                command.fundsSpecified = true;
                var msg = await BuyTriggerTimer.StartOrUpdateTimer(command).ConfigureAwait(false);

                if (msg == null)
                {
                    return($"Trigger amount set successfully for stock {command.stockSymbol}");
                }
                return(LogErrorEvent(command, msg));
            }
            catch (Exception ex)
            {
                LogDebugEvent(command, ex.Message);
                return(LogErrorEvent(command, "Error processing command."));
            }
        }
Esempio n. 4
0
        async Task <string> CheckMoney(MySqlCommand cmd, UserCommandType command)
        {
            cmd.CommandText = "get_user_money";

            cmd.Parameters.AddWithValue("@pUserId", command.username);
            cmd.Parameters["@pUserId"].Direction = ParameterDirection.Input;
            cmd.Parameters.Add("@pMoney", DbType.Int32);
            cmd.Parameters["@pMoney"].Direction = ParameterDirection.Output;

            await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);

            if (cmd.Parameters["@pMoney"].Value == DBNull.Value)
            {
                return(LogErrorEvent(command, "User does not exist"));
            }
            if (Convert.ToInt32(cmd.Parameters["@pMoney"].Value) < command.funds)
            {
                return(LogErrorEvent(command, "Insufficient user funds"));
            }

            command.fundsSpecified = false;
            return(await BuyTriggerTimer.StartOrUpdateTimer(command).ConfigureAwait(false));;
        }