using fizmig Models using fizmig Models ViewModels using Microsoft Asp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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);*/