public async Task <ActionResult <Combo> > CreateCombo([FromForm] ComboDto combo)
        {
            try
            {
                var cmbo = new Combo
                {
                    Name        = combo.Name,
                    Description = combo.Description,
                    Total       = combo.Total
                };
                await _unitOfWork.ComboRepository.InsertTask(cmbo);

                await _unitOfWork.Commit();


                if (combo.Image != null)
                {
                    var imageToRemove = await _unitOfWork.ComboImageRepository.GetByComboId(combo.Id);

                    if (imageToRemove != null)
                    {
                        await _unitOfWork.ComboImageRepository.DeleteTask(imageToRemove.Id);
                    }

                    var comboImage = new ComboImage
                    {
                        ComboId = cmbo.Id
                    };

                    using (var ms = new MemoryStream())
                    {
                        combo.Image.CopyTo(ms);
                        comboImage.Image = ms.ToArray();
                    }

                    await _unitOfWork.ComboImageRepository.InsertTask(comboImage);

                    await _unitOfWork.Commit();
                }


                if (!string.IsNullOrEmpty(combo.ComboDetails))
                {
                    var comboDetailsJson = JsonConvert.DeserializeObject <List <ComboDetail> >(combo.ComboDetails);;

                    var comboDetails = comboDetailsJson.Select(x => new ComboDetail
                    {
                        ComboId = cmbo.Id,
                        DishId  = x.DishId,
                        Price   = x.Price,
                        Qty     = x.Qty
                    }).ToList();

                    await _unitOfWork.ComboDetailRepository.InsertRangeTask(comboDetails);

                    await _unitOfWork.Commit();
                }

                return(cmbo);
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
示例#2
0
        internal void InsertSkills(string folder)
        {
            _folder = folder;
            var skills     = new List <SkillDto>();
            var skill      = new SkillDto();
            var combo      = new List <ComboDto>();
            var skillCards = new List <BCardDto>();
            var counter    = 0;

            using (var skillIdStream = new StreamReader(_folder + _fileSkillId, Encoding.Default))
            {
                string line;
                while ((line = skillIdStream.ReadLine()) != null)
                {
                    var currentLine = line.Split('\t');

                    if (currentLine.Length > 2 && currentLine[1] == "VNUM")
                    {
                        skill = new SkillDto
                        {
                            SkillVNum = short.Parse(currentLine[2])
                        };
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "NAME")
                    {
                        skill.Name = currentLine[2];
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "TYPE")
                    {
                        skill.SkillType = byte.Parse(currentLine[2]);
                        skill.CastId    = short.Parse(currentLine[3]);
                        skill.Class     = byte.Parse(currentLine[4]);
                        skill.Type      = byte.Parse(currentLine[5]);
                        skill.Element   = byte.Parse(currentLine[7]);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "FCOMBO")
                    {
                        for (var i = 3; i < currentLine.Length - 4; i += 3)
                        {
                            var comb = new ComboDto
                            {
                                SkillVNum = skill.SkillVNum,
                                Hit       = short.Parse(currentLine[i]),
                                Animation = short.Parse(currentLine[i + 1]),
                                Effect    = short.Parse(currentLine[i + 2])
                            };

                            if (comb.Hit == 0 && comb.Animation == 0 && comb.Effect == 0)
                            {
                                continue;
                            }

                            if (DaoFactory.ComboDao.FirstOrDefault(s =>
                                                                   s.SkillVNum.Equals(comb.SkillVNum) && s.Hit.Equals(comb.Hit) &&
                                                                   s.Effect.Equals(comb.Effect)) == null)
                            {
                                combo.Add(comb);
                            }
                        }
                    }
                    else if (currentLine.Length > 3 && currentLine[1] == "COST")
                    {
                        skill.CpCost = currentLine[2] == "-1" ? (byte)0 : byte.Parse(currentLine[2]);
                        skill.Price  = int.Parse(currentLine[3]);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "LEVEL")
                    {
                        skill.LevelMinimum = currentLine[2] != "-1" ? byte.Parse(currentLine[2]) : (byte)0;
                        if (skill.Class > 31)
                        {
                            var firstskill = skills.Find(s => s.Class == skill.Class);
                            if (firstskill == null || skill.SkillVNum <= firstskill.SkillVNum + 10)
                            {
                                switch (skill.Class)
                                {
                                case 8:
                                    switch (skills.Count(s => s.Class == skill.Class))
                                    {
                                    case 3:
                                        skill.LevelMinimum = 20;
                                        break;

                                    case 2:
                                        skill.LevelMinimum = 10;
                                        break;

                                    default:
                                        skill.LevelMinimum = 0;
                                        break;
                                    }

                                    break;

                                case 9:
                                    switch (skills.Count(s => s.Class == skill.Class))
                                    {
                                    case 9:
                                        skill.LevelMinimum = 20;
                                        break;

                                    case 8:
                                        skill.LevelMinimum = 16;
                                        break;

                                    case 7:
                                        skill.LevelMinimum = 12;
                                        break;

                                    case 6:
                                        skill.LevelMinimum = 8;
                                        break;

                                    case 5:
                                        skill.LevelMinimum = 4;
                                        break;

                                    default:
                                        skill.LevelMinimum = 0;
                                        break;
                                    }

                                    break;

                                case 16:
                                    switch (skills.Count(s => s.Class == skill.Class))
                                    {
                                    case 6:
                                        skill.LevelMinimum = 20;
                                        break;

                                    case 5:
                                        skill.LevelMinimum = 15;
                                        break;

                                    case 4:
                                        skill.LevelMinimum = 10;
                                        break;

                                    case 3:
                                        skill.LevelMinimum = 5;
                                        break;

                                    case 2:
                                        skill.LevelMinimum = 3;
                                        break;

                                    default:
                                        skill.LevelMinimum = 0;
                                        break;
                                    }

                                    break;

                                default:
                                    switch (skills.Count(s => s.Class == skill.Class))
                                    {
                                    case 10:
                                        skill.LevelMinimum = 20;
                                        break;

                                    case 9:
                                        skill.LevelMinimum = 16;
                                        break;

                                    case 8:
                                        skill.LevelMinimum = 12;
                                        break;

                                    case 7:
                                        skill.LevelMinimum = 8;
                                        break;

                                    case 6:
                                        skill.LevelMinimum = 4;
                                        break;

                                    default:
                                        skill.LevelMinimum = 0;
                                        break;
                                    }

                                    break;
                                }
                            }
                        }

                        skill.MinimumAdventurerLevel = currentLine[3] != "-1" ? byte.Parse(currentLine[3]) : (byte)0;
                        skill.MinimumSwordmanLevel   = currentLine[4] != "-1" ? byte.Parse(currentLine[4]) : (byte)0;
                        skill.MinimumArcherLevel     = currentLine[5] != "-1" ? byte.Parse(currentLine[5]) : (byte)0;
                        skill.MinimumMagicianLevel   = currentLine[6] != "-1" ? byte.Parse(currentLine[6]) : (byte)0;
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "EFFECT")
                    {
                        skill.CastEffect      = short.Parse(currentLine[3]);
                        skill.CastAnimation   = short.Parse(currentLine[4]);
                        skill.Effect          = short.Parse(currentLine[5]);
                        skill.AttackAnimation = short.Parse(currentLine[6]);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "TARGET")
                    {
                        skill.TargetType  = byte.Parse(currentLine[2]);
                        skill.HitType     = byte.Parse(currentLine[3]);
                        skill.Range       = byte.Parse(currentLine[4]);
                        skill.TargetRange = byte.Parse(currentLine[5]);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "DATA")
                    {
                        skill.UpgradeSkill = short.Parse(currentLine[2]);
                        skill.UpgradeType  = short.Parse(currentLine[3]);
                        skill.CastTime     = short.Parse(currentLine[6]);
                        skill.Cooldown     = short.Parse(currentLine[7]);
                        skill.MpCost       = short.Parse(currentLine[10]);
                        skill.ItemVNum     = short.Parse(currentLine[12]);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "BASIC")
                    {
                        var type = (byte)int.Parse(currentLine[3]);
                        if (type == 0 || type == 255)
                        {
                            continue;
                        }

                        var first    = int.Parse(currentLine[5]);
                        var itemCard = new BCardDto
                        {
                            SkillVNum      = skill.SkillVNum,
                            Type           = type,
                            SubType        = (byte)(((int.Parse(currentLine[4]) + 1) * 10) + 1 + (first < 0 ? 1 : 0)),
                            IsLevelScaled  = Convert.ToBoolean((uint)(first < 0 ? 0 : first) % 4),
                            IsLevelDivided = (uint)(first < 0 ? 0 : first) % 4 == 2,
                            FirstData      = (short)(first > 0 ? first : -first / 4),
                            SecondData     = (short)(int.Parse(currentLine[6]) / 4),
                            ThirdData      = (short)(int.Parse(currentLine[7]) / 4)
                        };
                        skillCards.Add(itemCard);
                    }
                    else if (currentLine.Length > 2 && currentLine[1] == "CELL")
                    {
                        // investigate
                    }
                    else if (currentLine.Length > 1 && currentLine[1] == "Z_DESC")
                    {
                        // investigate
                        var skill1 = skill;
                        if (DaoFactory.SkillDao.FirstOrDefault(s => s.SkillVNum.Equals(skill1.SkillVNum)) != null)
                        {
                            continue;
                        }

                        skills.Add(skill);
                        counter++;
                    }
                }

                IEnumerable <SkillDto> skillDtos = skills;
                IEnumerable <ComboDto> comboDtos = combo;
                IEnumerable <BCardDto> bCardDtos = skillCards;

                DaoFactory.SkillDao.InsertOrUpdate(skillDtos);
                DaoFactory.ComboDao.InsertOrUpdate(comboDtos);
                DaoFactory.BCardDao.InsertOrUpdate(bCardDtos);

                _logger.Information(LogLanguage.Instance.GetMessageFromKey(LanguageKey.SKILLS_PARSED),
                                    counter);
            }
        }
        public async Task <IActionResult> UpdateCombo([FromForm] ComboDto combo)
        {
            try
            {
                var cmbo = new Combo
                {
                    Id          = combo.Id,
                    Name        = combo.Name,
                    Description = combo.Description,
                    Total       = combo.Total
                };

                _unitOfWork.ComboRepository.Update(cmbo);
                await _unitOfWork.Commit();

                if (combo.Image != null)
                {
                    var imageToRemove = await _unitOfWork.ComboImageRepository.GetByComboId(combo.Id);

                    if (imageToRemove != null)
                    {
                        await _unitOfWork.ComboImageRepository.DeleteTask(imageToRemove.Id);
                    }

                    var comboImage = new ComboImage
                    {
                        ComboId = cmbo.Id
                    };

                    using (var ms = new MemoryStream())
                    {
                        combo.Image.CopyTo(ms);
                        comboImage.Image = ms.ToArray();
                    }

                    await _unitOfWork.ComboImageRepository.InsertTask(comboImage);

                    await _unitOfWork.Commit();
                }

                if (!string.IsNullOrEmpty(combo.ComboDetails))
                {
                    var comboDetailsJson = JsonConvert.DeserializeObject <List <ComboDetail> >(combo.ComboDetails);;

                    var comboDetails = comboDetailsJson.Select(x => new ComboDetail
                    {
                        ComboId = cmbo.Id,
                        DishId  = x.DishId,
                        Price   = x.Price,
                        Qty     = x.Qty
                    }).ToList();

                    await _unitOfWork.ComboDetailRepository.InsertRangeTask(comboDetails);

                    await _unitOfWork.Commit();

                    var comboDetailsDb = await _unitOfWork.ComboDetailRepository.GetDetailsByComboNoTracking(combo.Id);

                    var ids = comboDetails.Select(x => x.Id);

                    if (comboDetailsDb.Count >= comboDetails.Count)
                    {
                        var removeDetail = comboDetailsDb.Where(p => !comboDetails.Any(p2 => p2.Id == p.Id)).ToList();
                        _unitOfWork.ComboDetailRepository.DeleteRange(removeDetail);
                        await _unitOfWork.Commit();
                    }

                    _unitOfWork.ComboDetailRepository.UpdateRange(comboDetails);
                }



                await _unitOfWork.Commit();
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
            return(Ok(combo));
        }