public async Task <IActionResult> Edit(int id, [Bind("Id,PlayerId,CharacterId")] CharacterChoose characterChoose) { if (id != characterChoose.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(characterChoose); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CharacterChooseExists(characterChoose.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CharacterId"] = new SelectList(_context.Characters, "Id", "Name", characterChoose.CharacterId); ViewData["PlayerId"] = new SelectList(_context.Players, "Id", "Login", characterChoose.PlayerId); return(View(characterChoose)); }
public async Task <IActionResult> Create([Bind("Id,PlayerId,CharacterId")] CharacterChoose characterChoose) { if (ModelState.IsValid) { _context.Add(characterChoose); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CharacterId"] = new SelectList(_context.Characters, "Id", "Name", characterChoose.CharacterId); ViewData["PlayerId"] = new SelectList(_context.Players, "Id", "Login", characterChoose.PlayerId); return(View(characterChoose)); }
public async Task <IActionResult> ImportDOC(IFormFile fileWord) { using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(fileWord.FileName, false)) { try { var text = wordDocument.MainDocumentPart.Document.InnerText; string [] sess = text.Split("---"); foreach (var cur in sess) { string[] sesInfo = cur.Split("*"); string[] map = sesInfo[0].Split(" "); string[] session = sesInfo[1].Split(" "); Map newmap; Session newses; var c = (from cat in _context.Sessions where cat.Server.Contains(session[0]) select cat).ToList(); if (c.Count > 0) { newses = c[0]; } else { newses = new Session(); newses.Server = session[0]; newses.Duration = Convert.ToInt32(session[1]); var ma = (from m in _context.Maps where m.Name.Contains(map[0]) select m).ToList(); if (ma.Count > 0) { newmap = ma[0]; } else { newmap = new Map(); newmap.Name = map[0]; newmap.Size = Convert.ToInt32(map[1]); _context.Maps.Add(newmap); } newses.Map = newmap; _context.Add(newses); } for (int i = 2; i < sesInfo.Length; i++) { string[] playerAndCharacters = sesInfo[i].Split(": "); string[] play = playerAndCharacters[0].Split(" "); string[] characters = playerAndCharacters[1].Split(" | "); Player player = new Player(); player.Login = play[0]; player.Password = play[1]; player.Nickname = play[2]; player.Session = newses; _context.Players.Add(player); foreach (var cure in characters) { string[] cha = cure.Split(" "); Character character; var a = (from aut in _context.Characters where aut.Name.Contains(cha[1]) select aut).ToList(); if (a.Count > 0) { character = a[0]; } else { character = new Character(); character.Playable = Convert.ToInt32(cha[0]); character.Name = cha[1]; character.Health = Convert.ToInt32(cha[2]); character.Stamina = Convert.ToInt32(cha[3]); character.Backpack = Convert.ToInt32(cha[4]); _context.Characters.Add(character); } CharacterChoose ab = new CharacterChoose(); ab.Player = player; ab.Character = character; _context.CharacterChooses.Add(ab); } } } } catch (Exception e) { Log(e.ToString()); } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { using (var stream = new FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { foreach (IXLWorksheet worksheet in workBook.Worksheets) { try { Map newmap; Session newses; var c = (from cat in _context.Sessions where cat.Server.Contains(worksheet.Name) select cat).ToList(); if (c.Count > 0) { newses = c[0]; } else { newses = new Session(); newses.Server = worksheet.Name; newses.Duration = Convert.ToInt32(worksheet.Cell(1, 3).Value); var m = (from map in _context.Maps where map.Name.Contains(worksheet.Cell(1, 1).Value.ToString()) select map).ToList(); if (m.Count > 0) { newmap = m[0]; } else { newmap = new Map(); newmap.Name = worksheet.Cell(1, 1).Value.ToString(); newmap.Size = Convert.ToInt32(worksheet.Cell(1, 2).Value); _context.Maps.Add(newmap); } newses.Map = newmap; _context.Add(newses); } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { Player player = new Player(); player.Login = row.Cell(1).Value.ToString(); player.Password = row.Cell(2).Value.ToString(); player.Nickname = row.Cell(3).Value.ToString(); player.Session = newses; _context.Players.Add(player); int maxInfo = 14, oneChar = 5; for (int i = 4; i <= maxInfo; i += oneChar) { if (row.Cell(i).Value.ToString().Length > 0) { Character character; var a = (from aut in _context.Characters where aut.Name.Contains(row.Cell(i).Value.ToString()) select aut).ToList(); if (a.Count > 0) { character = a[0]; } else { character = new Character(); character.Playable = Convert.ToInt32(row.Cell(i).Value); character.Name = row.Cell(i + 1).Value.ToString(); character.Health = Convert.ToInt32(row.Cell(i + 2).Value); character.Stamina = Convert.ToInt32(row.Cell(i + 3).Value); character.Backpack = Convert.ToInt32(row.Cell(i + 4).Value); _context.Characters.Add(character); } CharacterChoose ab = new CharacterChoose(); ab.Player = player; ab.Character = character; _context.CharacterChooses.Add(ab); } } } } catch (Exception e) { Log(e.ToString()); } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }