Esempio n. 1
0
        //Backend of Ajax call to buy item
        public async Task <IActionResult> BuyItem(int id, int quantity)
        {
            //Get selected item
            var item = await _context.Items.FindAsync(id + 1);

            //Get current user
            var user_id = _userManager.GetUserId(User);
            var user    = _context.Users.FirstOrDefault(s => s.Id == user_id);

            //If user has enough monney
            if (user.Money < item.Cost * quantity)
            {
                return(BadRequest(new JsonResult(new { success = false })));
            }
            else
            {
                //Subtract required money from total money and add bought items to user's owned items
                user.Money = user.Money - item.Cost * quantity;

                //If user doesn't own item, create new instance of item, otherwise as quantity bought to existing instance
                var curItem = _context.ItemInstances.Where(i => i.ItemId == item.ID && i.ApplicationUserID == user_id).FirstOrDefault();
                if (curItem == null)
                {
                    user.OwnedItems = new List <ItemInstance>
                    {
                        new ItemInstance {
                            ApplicationUser = user, ItemId = item.ID, Quantity = quantity
                        }
                    };
                }
                else
                {
                    curItem.Quantity = curItem.Quantity + quantity;
                }
                _context.SaveChanges();
            }

            return(Json(
                       new
            {
                success = true,
                money = user.Money
            }));
        }
Esempio n. 2
0
        public static void ImportTrivia(string fileName, PrepContext context)
        {
            DataSet ds = GetDatasetFromExcel(fileName);

            foreach (DataTable table in ds.Tables)
            {
                Score currentCatgory = (Score)Enum.Parse(typeof(Score), table.TableName.Trim());
                var   category       = new QuestionCategory
                {
                    CategoryName = table.TableName.Trim(),
                    ScoreWeight  = (int)currentCatgory
                };

                context.QuestionCategories.Add(category);
                context.SaveChanges();

                foreach (DataRow row in table.Rows)
                {
                    //3 columns of quesiton/answer pairs per sheet
                    var question_col1 = row[0].ToString();
                    var question_col2 = row[3].ToString();
                    var question_col3 = row[6].ToString();

                    var answer_col1 = row[1].ToString();
                    var answer_col2 = row[4].ToString();
                    var answer_col3 = row[7].ToString();

                    if (!string.IsNullOrEmpty(question_col1) && !string.IsNullOrEmpty(answer_col1))
                    {
                        var question1 = new Question
                        {
                            QuestionCategoryId = category.QuestionCategoryId,
                            QuestionBody       = question_col1,
                            Answer             = answer_col1.SanitizeAnswer()
                        };

                        context.Questions.Add(question1);
                    }

                    if (!string.IsNullOrEmpty(question_col2) && !string.IsNullOrEmpty(answer_col2))
                    {
                        var question2 = new Question
                        {
                            QuestionCategoryId = category.QuestionCategoryId,
                            QuestionBody       = question_col2,
                            Answer             = answer_col2.SanitizeAnswer()
                        };

                        context.Questions.Add(question2);
                    }

                    if (!string.IsNullOrEmpty(question_col3) && !string.IsNullOrEmpty(answer_col3))
                    {
                        var question3 = new Question
                        {
                            QuestionCategoryId = category.QuestionCategoryId,
                            QuestionBody       = question_col3,
                            Answer             = answer_col3.SanitizeAnswer()
                        };

                        context.Questions.Add(question3);
                    }
                }

                context.SaveChanges();
            }
        }