예제 #1
0
        public ActionResult View(int ID)
        {
            Coinflip coinflip = coinflipRepository.GetByID(ID);

            Session["Coinflip"] = coinflip;
            return(View(coinflip));
        }
예제 #2
0
        public int RewardWinner(Coinflip coinflip)
        {
            string query = $"Coinflip_RewardWinner";

            try
            {
                if (OpenConnection())
                {
                    using (SqlCommand command = new SqlCommand(query, sql_connection))
                    {
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        try
                        {
                            command.Parameters.AddWithValue("@CoinflipID", coinflip.ID);

                            return(Convert.ToInt32(command.ExecuteScalar()));
                        }
                        catch (InvalidOperationException exception)
                        {
                        }
                    }
                }
            }
            catch (SqlException exception)
            {
            }
            finally
            {
                CloseConnection();
            }

            return(-1);
        }
예제 #3
0
        public ActionResult JoinCoinflip()
        {
            Coinflip coinflip = (Coinflip)Session["Coinflip"];
            bool     joined   = coinflipRepository.JoinCoinflip(coinflip, (User)Session["User"], (List <Skin>)Session["CoinflipJoinList"]);

            Session.Remove("CoinflipJoinList");
            Session.Remove("CVM");
            return(RedirectToAction("View", new { ID = coinflip.ID }));
        }
예제 #4
0
        public IHttpActionResult FlipCoinBattle([FromBody] Coinflip coinflip)
        {
            var challenger = _database.Users.Where(x => x.DiscordId == coinflip.Challenger.DiscordId).FirstOrDefault();

            challenger.Username = coinflip.Challenger.Username;
            var enemy = _database.Users.Where(x => x.DiscordId == coinflip.Enemy.DiscordId).FirstOrDefault();

            enemy.Username = coinflip.Enemy.Username;


            if (challenger == null || enemy == null)
            {
                return(BadRequest());
            }

            coinflip.ChallengerId = challenger.Id;
            coinflip.EnemyId      = enemy.Id;
            coinflip.Challenger   = challenger;
            coinflip.Enemy        = enemy;

            var existingCoinFlip = _database.Coinflips.Where(x => x.ChallengerId == challenger.Id && x.EnemyId == enemy.Id).FirstOrDefault();

            if (existingCoinFlip != null)
            {
                existingCoinFlip.Result     = CoinflipVsResults.ChallengeAlreadyExists;
                existingCoinFlip.Challenger = challenger;
                existingCoinFlip.Enemy      = enemy;
                return(Ok(existingCoinFlip));
            }

            if (challenger.Points < coinflip.Points)
            {
                coinflip.Result = CoinflipVsResults.ChallengerNoPoints;
                return(Ok(coinflip));
            }

            if (enemy.Points < coinflip.Points)
            {
                // Error
                coinflip.Result = CoinflipVsResults.EnemyNoPoints;
                return(Ok(coinflip));
            }

            coinflip.Result = CoinflipVsResults.ChallengeRequestSet;
            _database.Coinflips.Add(coinflip);
            _database.Context.SaveChanges();

            return(Ok(coinflip));
        }
예제 #5
0
        public bool JoinCoinflip(Coinflip coinflip, User User, List <Skin> Skins)
        {
            string query = $"Coinflip_Join";

            try
            {
                if (OpenConnection())
                {
                    using (SqlCommand command = new SqlCommand(query, sql_connection))
                    {
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        try
                        {
                            DataTable dataTable = new DataTable();
                            dataTable.Columns.Add("ID", typeof(int));
                            foreach (Skin skin in Skins)
                            {
                                dataTable.Rows.Add(skin.ID);
                            }

                            command.Parameters.AddWithValue("@CoinflipID", coinflip.ID);
                            command.Parameters.AddWithValue("@Joiner_ID", User.ID);
                            command.Parameters.AddWithValue("@Skins", dataTable);

                            return(Convert.ToBoolean(command.ExecuteScalar()));
                        }
                        catch (InvalidOperationException exception)
                        {
                        }
                    }
                }
            }
            catch (SqlException exception)
            {
            }
            finally
            {
                CloseConnection();
            }

            return(false);
        }
예제 #6
0
        public IHttpActionResult DeclineCoinflip([FromBody] Coinflip coinflip)
        {
            var challenger = _database.Users.Where(x => x.DiscordId == coinflip.Challenger.DiscordId).FirstOrDefault();

            challenger.Username = coinflip.Challenger.Username;
            var enemy = _database.Users.Where(x => x.DiscordId == coinflip.Enemy.DiscordId).FirstOrDefault();

            enemy.Username = coinflip.Enemy.Username;

            if (challenger == null || enemy == null)
            {
                return(BadRequest());
            }

            var existingCoinFlip = _database.Coinflips.Where(x => x.ChallengerId == challenger.Id && x.EnemyId == enemy.Id).FirstOrDefault();

            if (existingCoinFlip == null)
            {
                // Error
                existingCoinFlip = new Coinflip();
                _database.Context.Entry(existingCoinFlip).State = System.Data.Entity.EntityState.Detached;
                existingCoinFlip.Result     = CoinflipVsResults.ChallengeDoesntExist;
                existingCoinFlip.Challenger = challenger;
                existingCoinFlip.Enemy      = enemy;
                return(Ok(existingCoinFlip));
            }

            existingCoinFlip.Result     = CoinflipVsResults.ChallengeDeclined;
            existingCoinFlip.Challenger = challenger;
            existingCoinFlip.Enemy      = enemy;

            _database.Coinflips.Remove(existingCoinFlip);
            _database.Context.SaveChanges();

            return(Ok(existingCoinFlip));
        }
예제 #7
0
 public int RewardWinner(Coinflip coinflip)
 {
     return(context.RewardWinner(coinflip));
 }
예제 #8
0
 public bool JoinCoinflip(Coinflip coinflip, User User, List <Skin> Skins)
 {
     return(context.JoinCoinflip(coinflip, User, Skins));
 }
예제 #9
0
 public bool PotentialJoin(Coinflip coinflip)
 {
     return(context.PotentialJoin(coinflip));
 }
예제 #10
0
 public ActionResult GetResult(Coinflip coinflip)
 {
     coinflipRepository.RewardWinner(coinflip);
     return(RedirectToAction("View", new { ID = coinflip.ID }));
 }
예제 #11
0
        public ActionResult Join(CoinflipViewModel coinflipViewModel)
        {
            CoinflipViewModel sessioncoinflipViewModel = (CoinflipViewModel)Session["CVM"];

            sessioncoinflipViewModel.SelectedSkins = coinflipViewModel.SelectedSkins;
            sessioncoinflipViewModel.ErrorMessage  = null;

            foreach (Skin skin in sessioncoinflipViewModel.OrderedSkins[sessioncoinflipViewModel.Index])
            {
                if (sessioncoinflipViewModel.SelectedSkins.Contains(skin.ID))
                {
                    if (sessioncoinflipViewModel.FinalSkinList.Contains(skin))
                    {
                    }
                    else
                    {
                        sessioncoinflipViewModel.FinalSkinList.Add(skin);
                    }
                }
                else
                {
                    if (sessioncoinflipViewModel.FinalSkinList.Contains(skin))
                    {
                        sessioncoinflipViewModel.FinalSkinList.Remove(skin);
                    }
                }
            }

            if (Request.Form["Next"] != null)
            {
                sessioncoinflipViewModel.Index++;
            }
            else if (Request.Form["Previous"] != null)
            {
                sessioncoinflipViewModel.Index--;
            }
            else if (Request.Form["Submit"] != null)
            {
                Coinflip coinflip = (Coinflip)Session["Coinflip"];
                if (sessioncoinflipViewModel.GetFinalSkinListPrice() >= coinflip.getBetPrice(0) * 0.95M)
                {
                    if (sessioncoinflipViewModel.GetFinalSkinListPrice() <= coinflip.getBetPrice(0) * 1.05M)
                    {
                        Session["CoinflipJoinList"] = sessioncoinflipViewModel.FinalSkinList;
                        return(RedirectToAction("ConfirmJoin"));
                    }
                    else
                    {
                        sessioncoinflipViewModel.ErrorMessage = "The total price of your selected skins ($" + sessioncoinflipViewModel.GetFinalSkinListPrice() +
                                                                ") was too high to meet the required maximal price of $" + (coinflip.getBetPrice(0) * 1.05M);
                    }
                }
                else
                {
                    sessioncoinflipViewModel.ErrorMessage = "The total price of your selected skins ($" + sessioncoinflipViewModel.GetFinalSkinListPrice() +
                                                            ") was too low to meet the required minimal price of $" + (coinflip.getBetPrice(0) * 0.95M);
                }
            }
            else
            {
                return(View("Index", "Home"));
            }

            sessioncoinflipViewModel.SelectedSkins = new List <int>();

            int x = 0;

            foreach (Skin skin in sessioncoinflipViewModel.OrderedSkins[sessioncoinflipViewModel.Index])
            {
                if (sessioncoinflipViewModel.FinalSkinList.Any(m => m.ID == skin.ID))
                {
                    sessioncoinflipViewModel.SelectedSkins.Add(skin.ID);
                }
                else
                {
                    sessioncoinflipViewModel.SelectedSkins.Add(0);
                }
                x++;
            }


            Session["CVM"] = sessioncoinflipViewModel;
            return(View(sessioncoinflipViewModel));
        }
예제 #12
0
        public IHttpActionResult AcceptCoinflip([FromBody] Coinflip coinflip)
        {
            var challenger = _database.Users.Where(x => x.DiscordId == coinflip.Challenger.DiscordId).FirstOrDefault();

            challenger.Username = coinflip.Challenger.Username;
            var enemy = _database.Users.Where(x => x.DiscordId == coinflip.Enemy.DiscordId).FirstOrDefault();

            enemy.Username = coinflip.Enemy.Username;

            if (challenger == null || enemy == null)
            {
                return(BadRequest());
            }

            var existingCoinFlip = _database.Coinflips.Where(x => x.ChallengerId == challenger.Id && x.EnemyId == enemy.Id).FirstOrDefault();

            existingCoinFlip.ChallengerId = challenger.Id;
            existingCoinFlip.EnemyId      = enemy.Id;
            existingCoinFlip.Challenger   = challenger;
            existingCoinFlip.Enemy        = enemy;

            if (existingCoinFlip == null)
            {
                // Error
                _database.Context.Entry(existingCoinFlip).State = System.Data.Entity.EntityState.Detached;
                existingCoinFlip            = new Coinflip();
                existingCoinFlip.Result     = CoinflipVsResults.ChallengeDoesntExist;
                existingCoinFlip.Challenger = challenger;
                existingCoinFlip.Enemy      = enemy;
                return(Ok(existingCoinFlip));
            }

            if (challenger.Points < existingCoinFlip.Points)
            {
                // Error
                existingCoinFlip.Result = CoinflipVsResults.ChallengerNoPoints;
                return(Ok(existingCoinFlip));
            }

            if (enemy.Points < existingCoinFlip.Points)
            {
                // Error
                existingCoinFlip.Result = CoinflipVsResults.EnemyNoPoints;
                return(Ok(existingCoinFlip));
            }

            Random rnd = new Random();

            if (rnd.Next(0, 2) == existingCoinFlip.Side)
            {
                existingCoinFlip.Result = CoinflipVsResults.ChallengerWon;
                challenger.Points      += existingCoinFlip.Points;
                enemy.Points           -= existingCoinFlip.Points;
                challenger.BetsExecuted++;
                enemy.BetsExecuted++;
                challenger.BetsWon++;
                enemy.BetsLost++;

                _database.Coinflips.Remove(existingCoinFlip);
                _database.Context.SaveChanges();

                return(Ok(existingCoinFlip));
            }
            else
            {
                existingCoinFlip.Result = CoinflipVsResults.EnemyWon;
                challenger.Points      -= existingCoinFlip.Points;
                enemy.Points           += existingCoinFlip.Points;
                challenger.BetsExecuted++;
                enemy.BetsExecuted++;
                challenger.BetsLost++;
                enemy.BetsWon++;

                _database.Coinflips.Remove(existingCoinFlip);
                _database.Context.SaveChanges();

                return(Ok(existingCoinFlip));
            }
        }
예제 #13
0
        public List <Coinflip> GetAll()
        {
            string query = $"Coinflip_GetAll";

            try
            {
                if (OpenConnection())
                {
                    using (SqlCommand command = new SqlCommand(query, sql_connection))
                    {
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        try
                        {
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                List <Coinflip> flipList = new List <Coinflip>();
                                while (reader.Read())
                                {
                                    //Putting all the reader values into objects to simplify the values in the reader
                                    //Skin is added later in IF statement to check if skin is not null
                                    Coinflip coinflip = new Coinflip(reader.GetInt32(0), new List <Bet>(), reader.GetInt32(1));
                                    Bet      bet      = new Bet(reader.GetInt32(2), new User(), reader.GetDateTime(3), new List <Skin>());
                                    User     user;
                                    //Converting price to decimal
                                    decimal balance = reader.GetInt32(8);
                                    balance = balance / 100;
                                    if (!reader.IsDBNull(5))
                                    {
                                        user = new User(reader.GetInt32(4), reader.GetInt64(5), reader.GetString(6), reader.GetBoolean(7), balance);
                                    }
                                    else
                                    {
                                        user = new User(reader.GetInt32(4), reader.GetString(6), reader.GetBoolean(7), balance);
                                    }

                                    bet.User = user;

                                    //putting the data in the actual list

                                    //Check if the row is still about the same coinflip
                                    if (flipList.Count != 0)
                                    {
                                        if (coinflip.ID != flipList.Last().ID)
                                        {
                                            flipList.Add(coinflip);
                                        }
                                    }
                                    else
                                    {
                                        flipList.Add(coinflip);
                                    }

                                    //Checks if the coinflip has any bets and adds one if it doesn't
                                    if (flipList.Last().Bets.Count != 0)
                                    {
                                        //Checks if the latest bet is the same as the one in the list, adds a new one if it isn't
                                        if (flipList.Last().Bets.Last().ID != bet.ID)
                                        {
                                            flipList.Last().Bets.Add(bet);
                                        }
                                    }
                                    else
                                    {
                                        flipList.Last().Bets.Add(bet);
                                    }

                                    //Checks if the skin in not null and adds it
                                    if (!reader.IsDBNull(9))
                                    {
                                        decimal price = reader.GetInt32(15);
                                        price = price / 100;
                                        Skin skin = new Skin(reader.GetInt32(9), reader.GetString(10), reader.GetString(11), reader.GetString(12),
                                                             reader.GetInt32(13), reader.GetInt32(14), price, reader.GetDateTime(16), new Rarity(1));

                                        Rarity rarity;

                                        if (!reader.IsDBNull(22))
                                        {
                                            rarity = new Rarity(reader.GetInt32(17), reader.GetString(18), reader.GetInt32(19),
                                                                reader.GetInt32(20), reader.GetInt32(21), reader.GetInt32(22));
                                        }
                                        else
                                        {
                                            rarity = new Rarity(reader.GetInt32(17), reader.GetString(18), reader.GetInt32(19),
                                                                reader.GetInt32(20), reader.GetInt32(21));
                                        }

                                        skin.Rarity = rarity;

                                        flipList.Last().Bets.Last().Skins.Add(skin);
                                    }
                                }
                                return(flipList);
                            }
                        }
                        catch (InvalidOperationException exception)
                        {
                        }
                    }
                }
            }
            catch (SqlException exception)
            {
            }
            finally
            {
                CloseConnection();
            }

            return(new List <Coinflip>());
        }
예제 #14
0
        public Coinflip GetByID(int ID)
        {
            string query = $"Coinflip_GetByID";

            try
            {
                if (OpenConnection())
                {
                    using (SqlCommand command = new SqlCommand(query, sql_connection))
                    {
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        try
                        {
                            command.Parameters.AddWithValue("@CoinflipID", ID);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                Coinflip coinflip  = new Coinflip(-1, new List <Bet>(), -1);
                                bool     firstRead = true;
                                while (reader.Read())
                                {
                                    //Putting all the reader values into objects to simplify the values in the reader
                                    if (firstRead)
                                    {
                                        coinflip  = new Coinflip(reader.GetInt32(0), new List <Bet>(), reader.GetInt32(1));
                                        firstRead = false;
                                    }
                                    Bet  bet = new Bet(reader.GetInt32(2), new User(), reader.GetDateTime(3), new List <Skin>());
                                    User user;
                                    //Converting price to decimal
                                    decimal balance = reader.GetInt32(8);
                                    balance = balance / 100;
                                    if (!reader.IsDBNull(5))
                                    {
                                        user = new User(reader.GetInt32(4), reader.GetInt64(5), reader.GetString(6), reader.GetBoolean(7), balance);
                                    }
                                    else
                                    {
                                        user = new User(reader.GetInt32(4), reader.GetString(6), reader.GetBoolean(7), balance);
                                    }

                                    bet.User = user;

                                    //putting the data in the actual list

                                    //Checks if the coinflip has any bets and adds one if it doesn't
                                    if (coinflip.Bets.Count != 0)
                                    {
                                        //Checks if the latest bet is the same as the one in the list, adds a new one if it isn't
                                        if (coinflip.Bets.Last().ID != bet.ID)
                                        {
                                            coinflip.Bets.Add(bet);
                                        }
                                    }
                                    else
                                    {
                                        coinflip.Bets.Add(bet);
                                    }

                                    //Checks if the skin in not null and adds it
                                    if (!reader.IsDBNull(9))
                                    {
                                        decimal price = reader.GetInt32(15);
                                        price = price / 100;
                                        Skin skin = new Skin(reader.GetInt32(9), reader.GetString(10), reader.GetString(11), reader.GetString(12),
                                                             reader.GetInt32(13), reader.GetInt32(14), price, reader.GetDateTime(16), new Rarity(1));

                                        Rarity rarity;

                                        if (!reader.IsDBNull(22))
                                        {
                                            rarity = new Rarity(reader.GetInt32(17), reader.GetString(18), reader.GetInt32(19),
                                                                reader.GetInt32(20), reader.GetInt32(21), reader.GetInt32(22));
                                        }
                                        else
                                        {
                                            rarity = new Rarity(reader.GetInt32(17), reader.GetString(18), reader.GetInt32(19),
                                                                reader.GetInt32(20), reader.GetInt32(21));
                                        }

                                        skin.Rarity = rarity;

                                        coinflip.Bets.Last().Skins.Add(skin);
                                    }
                                }
                                if (!firstRead)
                                {
                                    return(coinflip);
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                        }
                        catch (InvalidOperationException exception)
                        {
                        }
                    }
                }
            }
            catch (SqlException exception)
            {
            }
            finally
            {
                CloseConnection();
            }

            return(null);
        }