using fizmig.Models;
using fizmig.Models.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace fizmig.Controllers
{
public class HeroesController : Controller
{
private HeroesContext _heroesContext;
private IHostingEnvironment _appEnvironment;
public HeroesController(HeroesContext heroesContext, IHostingEnvironment appEnvironment)
{
_heroesContext = heroesContext;
_appEnvironment = appEnvironment;
}
public IActionResult Index(string type)
{
if(type == null)
{
var allHeroes = from h in _heroesContext.Heroes orderby h.Castle orderby h.Name select h;
return View(allHeroes.ToList());
}
var heroes = from h in _heroesContext.Heroes where h.Castle == type orderby h.Name select h;
return View(heroes.ToList());
}
[HttpGet]
public IActionResult AddHero()
{
return View();
}
[HttpPost]
public IActionResult AddHero(Hero hero, IFormFile imageFile)
{
if (imageFile != null && hero != null)
{
// путь к папке Files
string path = "/Files/" + imageFile.FileName;
// сохраняем файл в папку Files в каталоге wwwroot
using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
{
imageFile.CopyToAsync(fileStream);
}
hero.ImageFile = path;
_heroesContext.Heroes.Add(hero);
_heroesContext.SaveChanges();
}
return RedirectToAction("Index");
}
// public IActionResult DeleteHero(string id)
// {
// var hero = (from cr in _heroesContext.Heroes where cr.Id == id select cr).FirstOrDefault();
// _heroesContext.Heroes.Remove(hero);
// _heroesContext.SaveChanges();
// return RedirectToAction("Index");
// }
public IActionResult SetIds()
{
var specs = from s in _heroesContext.Specs select s;
foreach(var item in _heroesContext.Heroes)
{
foreach(var spec in specs)
{
if(item.Speciality == spec.Name ||
item.Speciality == spec.CreatureAffected ||
item.Speciality == spec.Creature2Affected)
{
item.SpecialityId = spec.Id;
_heroesContext.Heroes.Update(item);
}
}
}
_heroesContext.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Details(string id)
{
try{
var hero = (from c in _heroesContext.Heroes where c.Id == id select c).FirstOrDefault();
hero.OriginCastle = (from c in _heroesContext.Castles where c.Name == hero.Castle select c).FirstOrDefault();
ViewBag.Image = hero.ImageFile;
var properties = hero.GetType().GetProperties();
List<string> values = new List<string>();
if(!hero.HasBook)
{
ViewBag.Book = "Нет";
}else ViewBag.Book = "Есть";
var specs = from s in _heroesContext.Specs select s;
foreach(var item in _heroesContext.Heroes)
{
foreach(var spec in specs)
{
if(item.Speciality == spec.Name)
{
item.SpecialityId = spec.Id;
}
}
}
foreach(var item in properties)
{
values.Add(item.GetValue(hero).ToString());
}
return View(values);
}
catch{
return NotFound();
}
}
public IActionResult ResourceSpecialityPartial()
{
return PartialView();
}
// <option>Ресурсы</option>
// <option>Заклинания</option>
// <option>Существа</option>
// <option>Боевые машины</option>
// <option>Вторичные навыки</option>
// <option>Сэр Мюллих :)</option>
public IActionResult SpecialityTable(string type)
{
var specs = from s in _heroesContext.Specs select s;
return View(specs);
}
public IActionResult _GetSpec(string type)
{
var redirecter = "";
switch(type)
{
case "Ресурсы":
redirecter = "ResourceSpecialityPartial";
break;
case "Заклинания":
redirecter = "SpellSpecialityPartial";
break;
case "Существа":
redirecter = "CreatureSpecialityPartial";
break;
case "Боевые машины":
redirecter = "MachineSpecialityPartial";
break;
case "Вторичные навыки":
redirecter = "SecondSkillSpecialityPartial";
break;
case "Сэр Мюллих :)":
redirecter = "MullichPartial";
break;
case "Все специализации":
redirecter = "SpecsPartial";
break;
default:
redirecter = "SpecsPartial";
break;
}
return RedirectToAction(redirecter);
}
public IActionResult SpecsPartial()
{
var specs = from s in _heroesContext.Specs select s;
return PartialView(specs.ToList().OrderBy(s => s.Name));
}
public IActionResult SpellSpecialityPartial()
{
var spells = from s in _heroesContext.Spells select s;
var heroes = from h in _heroesContext.Heroes select h;
foreach(var item in heroes)
{
item.OriginCastle = (from c in _heroesContext.Castles where c.Name == item.Castle select c).FirstOrDefault();
}
int[] Sortindex = new int[10];
foreach(var item in _heroesContext.Castles)
{
Sortindex.Append(item.Sortindex);
}
SpecViewModel svm = new SpecViewModel
{
Heroes = heroes.ToList().OrderBy(h => h.OriginCastle.Sortindex),
Specialities = (from s in _heroesContext.Specs where s.Type == "Заклинание" select s).ToList()
};
/*Замок - Оплот - Башня - Инферно - Некрополис - Темница - Цитадель - Крепость - Сопряжение - Причал.*/
return PartialView(svm);
}
public IActionResult CreatureSpecialityPartial()
{
var heroes = from h in _heroesContext.Heroes select h;
var specs = from s in _heroesContext.Specs where s.Type == "Существо" select s;
var creatures = from c in _heroesContext.Creatures select c;
CreatureSpecViewModel csvm = new CreatureSpecViewModel(_heroesContext)
{
Heroes = heroes.ToList(),
Specialities = new List<Speciality>(),
Creatures = new List<Creature>(),
UpgradedCreatures = new List<Creature>()
};
foreach(var item in heroes)
{
foreach(var spec in specs)
{
if(item.SpecialityId == spec.Id)
{
var creature = (from c in creatures where c.Name == spec.CreatureAffected select c).FirstOrDefault();
var upgradedCreature = (from c in creatures where c.Name == spec.Creature2Affected select c).FirstOrDefault();
csvm.Specialities.Add(spec);
csvm.Creatures.Add(creature);
csvm.UpgradedCreatures.Add(upgradedCreature);
}
}
}
foreach(var cr in csvm.Creatures)
{
cr.Upgrades = cr.GetAttack();
}
foreach(var cr in csvm.UpgradedCreatures)
{
cr.Upgrades = cr.GetAttack();
}
return PartialView(csvm);
}
[HttpGet]
public IActionResult SortByCastle(string castle)
{
var sortedHeroes = from h in _heroesContext.Heroes where h.Castle == castle select h;
if(sortedHeroes.Count() <= 0)
{
return RedirectToAction("Index", "Heroes");
}
return View(sortedHeroes.ToList());
}
public IActionResult HeroesTable(string castle)
{
HeroesTableViewModel htvm = new HeroesTableViewModel();
var heroes = from h in _heroesContext.Heroes select h;
var classes = from c in _heroesContext.Classes select c;
foreach(var item in heroes)
{
item.OriginCastle = (from c in _heroesContext.Castles where c.Name == item.Castle select c).FirstOrDefault();
}
foreach(var item in classes)
{
item.OriginCastle = (from c in _heroesContext.Castles where c.Name == item.Castle select c).FirstOrDefault();
}
if(castle == "Все города")
{
htvm.Classes = (from c in _heroesContext.Classes select c).ToList().OrderBy(h => h.OriginCastle.Sortindex);
htvm.Heroes = heroes.ToList().OrderBy(h => h.OriginCastle.Sortindex);
ViewBag.CastleName = castle;
return View(htvm);
}
else
{
var sortedHeroes = from h in _heroesContext.Heroes select h;
var sortedClasses = from c in _heroesContext.Classes select c;
foreach(var item in heroes)
{
item.OriginCastle = (from c in _heroesContext.Castles where c.Name == item.Castle select c).FirstOrDefault();
}
foreach(var item in sortedClasses)
{
item.OriginCastle = (from c in _heroesContext.Castles where c.Name == item.Castle select c).FirstOrDefault();
}
htvm.Classes = (from c in _heroesContext.Classes where c.Castle == castle select c).ToList().OrderBy(h => h.OriginCastle.Sortindex);
htvm.Heroes = sortedHeroes.ToList().OrderBy(h => h.OriginCastle.Sortindex);
ViewBag.CastleName = castle;
return View(htvm);
}
}
[HttpGet]
public IActionResult AddSpec()
{
List<string> allEntities = new List<string>();
allEntities.AddRange((from i in _heroesContext.Skills select i.Name).ToList());
allEntities.AddRange((from s in _heroesContext.Spells select s.Name).ToList());
allEntities.AddRange((from c in _heroesContext.Creatures select c.Name).ToList());
ViewBag.Names = (from h in _heroesContext.Heroes select h.Name).ToList();
ViewBag.Creatures = (from c in _heroesContext.Creatures select c.Name).ToList();
ViewBag.Specs = allEntities.ToList().OrderBy(x => x);
return View();
}
[HttpPost]
public IActionResult AddSpec(Speciality spec, IFormFile imageFile, string hero)
{
if (imageFile != null && spec != null)
{
// путь к папке Files
string path = "/Files/" + imageFile.FileName;
// сохраняем файл в папку Files в каталоге wwwroot
using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
{
imageFile.CopyToAsync(fileStream);
}
var specHero = (from h in _heroesContext.Heroes where h.Name == hero select h).FirstOrDefault();
spec.ImageFile = path;
_heroesContext.Specs.Add(spec);
specHero.SpecialityId = spec.Id;
_heroesContext.Heroes.Update(specHero);
_heroesContext.SaveChanges();
}
return RedirectToAction("SpecialityTable", "Heroes", new { type = "Все специализации"});
}
public IActionResult MachineSpecialityPartial(int heroLevel)
{
if(heroLevel == 0)
{
heroLevel = 1;
}
var creatures = from c in _heroesContext.Creatures select c;
var heroes = from h in _heroesContext.Heroes select h;
var specs = from s in _heroesContext.Specs where s.Type == "Боевая машина" select s;
var upgCreatures = from c in _heroesContext.Creatures where c.IsUpgraded select c;
CreatureSpecViewModel csvm = new CreatureSpecViewModel(_heroesContext)
{
Heroes = heroes.OrderBy(h=>h.OriginCastle.Sortindex).ToList(),
Specialities = new List<Speciality>(),
Creatures = new List<Creature>(),
UpgradedCreatures = new List<Creature>()
};
foreach(var item in heroes)
{
foreach(var spec in specs)
{
if(item.Speciality.Contains(spec.Name.Remove(spec.Name.Length - 1)))
{
var creature = (from c in creatures where c.Name.Contains(spec.Name.Remove(spec.Name.Length - 1)) select c).FirstOrDefault();
var upgradedCreature = (from c in upgCreatures where c.Level == creature.Level && c.Castle == creature.Castle select c).FirstOrDefault();
csvm.Specialities.Add(spec);
csvm.Creatures.Add(creature);
csvm.UpgradedCreatures.Add(upgradedCreature);
}
}
}
foreach(var cr in csvm.Creatures)
{
cr.Upgrades = cr.GetAttack();
}
foreach(var cr in csvm.UpgradedCreatures)
{
cr.Upgrades = cr.GetAttack();
}
return PartialView(csvm);
}
public IActionResult SecondSkillSpecialityPartial(int heroLevel)
{
if(heroLevel == 0)
{
heroLevel = 1;
}
var creatures = from c in _heroesContext.Creatures select c;
var heroes = from h in _heroesContext.Heroes select h;
var specs = from s in _heroesContext.Specs where s.Type == "Боевая машина" select s;
var upgCreatures = from c in _heroesContext.Creatures where c.IsUpgraded select c;
CreatureSpecViewModel csvm = new CreatureSpecViewModel(_heroesContext)
{
Heroes = heroes.OrderBy(h=>h.OriginCastle.Sortindex).ToList(),
Specialities = new List<Speciality>(),
Creatures = new List<Creature>(),
UpgradedCreatures = new List<Creature>()
};
foreach(var item in heroes)
{
foreach(var spec in specs)
{
if(item.Speciality.Contains(spec.Name.Remove(spec.Name.Length - 1)))
{
var creature = (from c in creatures where c.Name.Contains(spec.Name.Remove(spec.Name.Length - 1)) select c).FirstOrDefault();
var upgradedCreature = (from c in upgCreatures where c.Level == creature.Level && c.Castle == creature.Castle select c).FirstOrDefault();
csvm.Specialities.Add(spec);
csvm.Creatures.Add(creature);
csvm.UpgradedCreatures.Add(upgradedCreature);
}
}
}
foreach(var cr in csvm.Creatures)
{
cr.Upgrades = cr.GetAttack();
}
foreach(var cr in csvm.UpgradedCreatures)
{
cr.Upgrades = cr.GetAttack();
}
return PartialView(csvm);
}
public IActionResult MullichPartial()
{
return PartialView();
}
public IActionResult Luck()
{
var luckCreatures = (from c in _heroesContext.Creatures
where c.Name == "Дьявол" || c.Name == "Архидьявол" || c.Name == "Хоббит" select c).ToList();
var luckObjects = (from o in _heroesContext.Objects
where o.Type.Contains("Удача") select o).ToList();
foreach(var item in luckCreatures)
{
if(item.Ability.Contains("Удача -1"))
item.Ability = "-1 к удаче всех вражеских существ";
if(item.Ability.Contains("Удача -2"))
item.Ability = "-2 к удаче всех вражеских существ";
}
ArticleViewModel avm = new ArticleViewModel
{
Heroes = (from h in _heroesContext.Heroes select h).ToList(),
Spells = (from s in _heroesContext.Spells where s.Name == "Удача" ||
s.Name == "Неудача" select s).ToList(),
Skills = (from s in _heroesContext.Skills where s.Name == "Удача" select s).ToList(),
Artifacts = (from a in _heroesContext.Artifacts select a).ToList(),
Creatures = luckCreatures,
Objects = luckObjects
};
avm.Article = new Article[2];
avm.Article[0] = (from a in _heroesContext.Articles
where a.Name == "Удача и боевой дух" select a).FirstOrDefault();
avm.Article[1] = (from a in _heroesContext.Articles
where a.Name == "Боевой дух" select a).FirstOrDefault();
return View(avm);
}
}
}
/* if(heroLevel == 0)
{
heroLevel = 1;
}
var creatures = from c in _heroesContext.Creatures select c;
var heroes = from h in _heroesContext.Heroes select h;
var specs = from s in _heroesContext.Specs where s.Type == "Существо" select s;
var upgCreatures = from c in _heroesContext.Creatures where c.IsUpgraded select c;
CreatureSpecViewModel csvm = new CreatureSpecViewModel(_heroesContext)
{
Heroes = heroes.OrderBy(h=>h.OriginCastle.Sortindex).ToList(),
Specialities = new List<Speciality>(),
Creatures = new List<Creature>(),
UpgradedCreatures = new List<Creature>()
};
foreach(var item in heroes)
{
foreach(var spec in specs)
{
if(item.Speciality.Contains(spec.Name.Remove(spec.Name.Length - 1)))
{
var creature = (from c in creatures where c.Name.Contains(spec.Name.Remove(spec.Name.Length - 1))
select c).FirstOrDefault();
var upgradedCreature = (from c in upgCreatures where c.Level == creature.Level
&& c.Castle == creature.Castle select c).FirstOrDefault();
csvm.Specialities.Add(spec);
csvm.Creatures.Add(creature);
csvm.UpgradedCreatures.Add(upgradedCreature);
}
}
}
foreach(var cr in csvm.Creatures)
{
cr.Upgrades = cr.GetAttack();
}
foreach(var cr in csvm.UpgradedCreatures)
{
cr.Upgrades = cr.GetAttack();
}
return View(csvm);*/