public IActionResult GenerateHackingPuzzle([FromBody] HackingPuzzle hackingPuzzle) { try { _context.Database.ExecuteSqlCommand("TRUNCATE TABLE tblHackingPuzzleRow;"); _context.Database.ExecuteSqlCommand("DELETE FROM tblHackingPuzzle;"); var validator = new HackingPuzzleValidator(); var result = validator.Validate(hackingPuzzle); if (!result.IsValid) { var errors = result.Errors.Select(e => e.ErrorMessage).ToList(); var errorMessages = string.Join(" ", errors); throw new Exception(errorMessages); } hackingPuzzle.AttemptsRemaining = 0; _context.tblHackingPuzzle.Add(hackingPuzzle); _context.tblHackingPuzzleRow.AddRange(hackingPuzzle.Rows); _context.SaveChanges(); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public Event UpdateOrCreateEvent([FromBody] Event lfEvent) { Event eventToUpdate = null; var lfEventStartTime = lfEvent.StartDate.TimeOfDay; lfEvent.StartDate = lfEvent.StartDate.Subtract(lfEventStartTime); var lfEventEndTime = lfEvent.EndDate.TimeOfDay; lfEvent.EndDate = lfEvent.EndDate.Subtract(lfEventEndTime); if (lfEvent.Id != 0) { eventToUpdate = _context.tblEvent.FirstOrDefault(e => e.Id == lfEvent.Id); } if (eventToUpdate != null) { _context.Entry(eventToUpdate).CurrentValues.SetValues(lfEvent); } else { _context.Add(lfEvent); } _context.SaveChanges(); return(lfEvent); }
public Character UpdateOrCreateCharacter(Character character) { Character characterToUpdate = null; if (character.Id != 0) { characterToUpdate = _context.tblCharacter.Include(s => s.Skills).FirstOrDefault(c => c.Id == character.Id); } if (characterToUpdate != null) { foreach (var skill in characterToUpdate.Skills) { _context.tblCharacterSkills.Remove(skill); } _context.Entry(characterToUpdate).CurrentValues.SetValues(character); } else { _context.Add(character); } _context.tblCharacterSkills.AddRange(character.Skills); _context.SaveChanges(); return(character); }
public IActionResult UpdateCharacterEventDetails([FromBody] CharacterEvent characterEvent) { var characterEventToUpdate = _context.tblCharacterEvents.FirstOrDefault(ce => ce.Id == characterEvent.Id); _context.Entry(characterEventToUpdate).CurrentValues.SetValues(characterEvent); _context.SaveChanges(); return(Ok()); }
public void AddCharacterToEvent(int characterId, int eventId) { var newCharacterEvent = new CharacterEvent { CharacterId = characterId, EventId = eventId }; _context.Add(newCharacterEvent); _context.SaveChanges(); }
public IActionResult AddPlayerToNpcShift([FromBody] PlayerNpcShifts playerNpcShift) { try { var playerNpcShiftToAdd = new PlayerNpcShifts { PlayerId = playerNpcShift.PlayerId, NpcShiftId = playerNpcShift.NpcShiftId }; _context.tblPlayerNpcShifts.Add(playerNpcShiftToAdd); _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); throw; } return(Ok()); }
public IActionResult DeleteCharacterById(int id) { var characterToDelete = _context.tblCharacter.Include(c => c.CartItems).FirstOrDefault(c => c.Id == id); if (characterToDelete == null) { return(BadRequest("Unable to find character with ID: " + id)); } _context.tblCharacter.Attach(characterToDelete); _context.tblCharacter.Remove(characterToDelete); _context.SaveChanges(); return(Ok()); }
public IActionResult DeleteCharacterFromEvent(int id) { try { var characterEvent = _context.tblCharacterEvents.FirstOrDefault(c => c.Id == id); if (characterEvent == null) { return(BadRequest("Could not find character event with id '" + id + "'!")); } _context.tblCharacterEvents.Remove(characterEvent); _context.SaveChanges(); return(Ok()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task DeletePlayer(Player player) { _appDbContext.tblPlayer.Remove(player); var charactersToDelete = _lfContext.tblCharacter.Where(c => c.PlayerId == player.Id); _lfContext.tblCharacter.RemoveRange(charactersToDelete); var playerNpcShiftsToDelete = _lfContext.tblPlayerNpcShifts.Where(s => s.PlayerId == player.Id); _lfContext.tblPlayerNpcShifts.RemoveRange(playerNpcShiftsToDelete); _lfContext.SaveChanges(); var deletePlayerResult = await _userManager.DeleteAsync(player.Identity); if (!deletePlayerResult.Succeeded) { throw new Exception("Make this exception better :("); } }
public IActionResult SubmitAnswer([FromBody] JObject submission) { try { var symbols = new List <char> { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', '\\', '|', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', '~', '`' }; var code = submission["code"].ToString(); var wordSelected = code; foreach (var n in code) { if (symbols.Contains(n)) { wordSelected = wordSelected.Replace($"{n}", ""); } } var hackingPuzzle = _context.tblHackingPuzzle.Include(hp => hp.Rows).FirstOrDefault(); if (hackingPuzzle == null) { throw new Exception("HackingPuzzle cannot be null!"); } if (hackingPuzzle.AttemptsRemaining <= 0) { var outOfAttempts = new JObject { { "OutOfAttempts", true } }; } var answerRow = hackingPuzzle.Rows.FirstOrDefault(r => r.IsAnswer); if (answerRow == null) { throw new Exception("There is no answer! Well this puzzle sucks..."); } var answer = answerRow.Word; if (answer.Equals(wordSelected)) { var solution = new JObject { { "Flag", hackingPuzzle.Flag } }; hackingPuzzle.AttemptsRemaining = hackingPuzzle.Attempts; _context.tblHackingPuzzle.Update(hackingPuzzle); return(Ok(solution)); } if (hackingPuzzle.AttemptsRemaining == 1) { var outOfAttempts = new JObject { { "OutOfAttempts", true } }; hackingPuzzle.AttemptsRemaining--; _context.tblHackingPuzzle.Update(hackingPuzzle); _context.SaveChanges(); return(Ok(outOfAttempts)); } var numberCorrect = 0; for (var i = 0; i < answer.Length; i++) { if (answer[i] == wordSelected[i]) { numberCorrect++; } } var result = new JObject { { "NumberCorrect", numberCorrect }, { "WordLength", answer.Length } }; hackingPuzzle.AttemptsRemaining--; _context.tblHackingPuzzle.Update(hackingPuzzle); _context.SaveChanges(); return(Ok(result)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <IActionResult> AddToCart([FromBody] Cart cart) { try { if (cart == null) { return(BadRequest("Unable to create cart data")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userName = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var user = await _userManager.FindByEmailAsync(userName); var roles = await _userManager.GetRolesAsync(user); Player player; if (!roles.Contains("Admin")) { player = _appDbContext.tblPlayer.Include(p => p.Identity) .FirstOrDefault(p => p.Identity.Equals(user)); } else { var playerId = cart.PlayerId; player = _appDbContext.tblPlayer.Include(p => p.Identity).FirstOrDefault(p => p.Id == playerId); } if (player == null) { return(BadRequest("Unable to find player")); } var activeEvent = _lfContext.tblEvent.FirstOrDefault(e => e.IsActiveEvent); if (activeEvent == null) { return(BadRequest("Unable to retrieve the active event")); } var paidCarts = _lfContext.tblCart.Include(c => c.CartItems).Where(c => c.PlayerId == player.Id && c.EventId == activeEvent.Id && c.Paid).ToList(); if (paidCarts.Any()) { var charactersAlreadyPaid = string.Empty; foreach (var paidCart in paidCarts) { foreach (var cartItem in cart.CartItems) { var paidCharacterCartItem = paidCart.CartItems.FirstOrDefault(pci => pci.CharacterId == cartItem.CharacterId); if (paidCharacterCartItem == null) { continue; } var paidCharacter = _lfContext.tblCharacter.FirstOrDefault(c => c.Id == paidCharacterCartItem.CharacterId); if (paidCharacter != null) { charactersAlreadyPaid = charactersAlreadyPaid + " " + paidCharacter.Name; } } } if (charactersAlreadyPaid != string.Empty) { return(BadRequest("You have already registered characters: " + charactersAlreadyPaid + ". They cannot be registered again.")); } } cart.PlayerId = player.Id; cart.EventId = activeEvent.Id; cart.CreatedDate = DateTime.UtcNow; cart.Key = Guid.NewGuid(); cart.Paid = false; _lfContext.tblCart.Add(cart); _lfContext.tblCartItem.AddRange(cart.CartItems); _lfContext.SaveChanges(); return(Ok(cart.Key)); } catch (Exception e) { return(BadRequest("An unknown error occurred.")); } }
public IActionResult GetCurrentEventNpcShifts() { var npcShifts = _context.tblNpcShift.Include(n => n.Event).Where(n => n.Event.IsActiveEvent).ToList(); if (!npcShifts.Any()) { var activeEvent = _eventService.GetActiveEvent(); var npcShiftBegins = activeEvent.StartDate.AddDays(1).AddHours(8); var npcShiftsEnds = activeEvent.EndDate.AddHours(2).AddMinutes(30); npcShifts = NpcShiftGenerator.GenerateShifts(activeEvent, npcShiftBegins, npcShiftsEnds, 150, 60); var fridayNightShiftStart = activeEvent.StartDate.AddHours(21); var fridayNightShiftEnd = activeEvent.StartDate.AddHours(23).AddMinutes(30); var fridayNightShift = new NpcShift { Event = activeEvent, EventId = activeEvent.Id, EndDateTime = fridayNightShiftEnd, StartDateTime = fridayNightShiftStart }; npcShifts.Add(fridayNightShift); var saturdayMorningShiftStart = activeEvent.StartDate.AddDays(1).AddHours(7).AddMinutes(30); var saturdayMorningShiftEnd = activeEvent.StartDate.AddDays(1).AddHours(10); var saturdayNightShift = new NpcShift { Event = activeEvent, EventId = activeEvent.Id, EndDateTime = saturdayMorningShiftEnd, StartDateTime = saturdayMorningShiftStart }; npcShifts.Add(saturdayNightShift); _context.tblNpcShift.AddRange(npcShifts); _context.SaveChanges(); return(Ok(npcShifts)); } List <NpcShiftWithCountAndPlayers> npcShiftsWithCountsAndPlayers; try { var npcShiftsWithCounts = _context.Sp_GetNpcShiftsWithPlayerCount.FromSql("GetNpcShiftsWithPlayerCount").ToList(); npcShiftsWithCountsAndPlayers = new List <NpcShiftWithCountAndPlayers>(); foreach (var npcShift in npcShiftsWithCounts) { var players = _playerService.GetPlayersFromNpcShift(npcShift.Id); var npcShiftWithCountAndPlayers = new NpcShiftWithCountAndPlayers { EventId = npcShift.EventId, Id = npcShift.Id, StartDateTime = npcShift.StartDateTime, EndDateTime = npcShift.EndDateTime, NpcCount = npcShift.NpcCount, Players = players }; npcShiftsWithCountsAndPlayers.Add(npcShiftWithCountAndPlayers); } } catch (Exception e) { Console.WriteLine(e); throw; } return(Ok(npcShiftsWithCountsAndPlayers)); }
public IActionResult CheckIn([FromBody] Cart cart) { try { cart = cart.ThrowIfNull("Cart"); var player = _appDbContext.tblPlayer.FirstOrDefault(p => p.Id == cart.PlayerId).ThrowIfNull("Player"); var currentEvent = _lfContext.tblEvent.FirstOrDefault(e => e.IsActiveEvent).ThrowIfNull("CurrentEvent"); var cartItems = cart.CartItems.ThrowIfNull("CartItems"); foreach (var cartItem in cart.CartItems) { var characterTotalXp = 0; characterTotalXp += cartItem.PurchaseXp; characterTotalXp += cartItem.VpToXp; var character = _lfContext.tblCharacter.FirstOrDefault(c => c.Id == cartItem.CharacterId) .ThrowIfNull($"Character with ID '{cartItem.CharacterId}'"); if (_lfContext.tblCharacterEvents.FirstOrDefault(ce => ce.CharacterId == character.Id && ce.EventId == currentEvent.Id) != null) { return(BadRequest( $"Character '{character.Name}' has already been registered for this event! Please remove.")); } if (character.AccumulatedXP == null) { character.AccumulatedXP = 0; } if (character.AvailableXP == null) { character.AvailableXP = 0; } var baseXp = 40; characterTotalXp += baseXp; character.AccumulatedXP += characterTotalXp; character.AvailableXP += characterTotalXp; var newCharacterEvent = new CharacterEvent { VpToXp = cartItem.VpToXp, XpBought = cartItem.PurchaseXp, EventId = currentEvent.Id, CharacterId = cartItem.CharacterId }; _lfContext.tblCharacter.Update(character); _lfContext.tblCharacterEvents.Add(newCharacterEvent); } _lfContext.SaveChanges(); var totalVp = cartItems.Sum(ci => ci.VpToXp); player.VolunteerPoints = player.VolunteerPoints - totalVp; _appDbContext.tblPlayer.Update(player); _appDbContext.SaveChanges(); return(Ok(player.Id)); } catch (Exception e) { return(BadRequest(e)); } }
public IActionResult CheckOut([FromBody] JObject token) { try { StripeConfiguration.SetApiKey(StripeKey); var amount = 0; var totalVp = 0; var totalXp = 0; var key = Guid.Parse(token["cartKey"].ToString()); var cart = _lfContext.tblCart.Include(c => c.CartItems).FirstOrDefault(c => c.Key == key); if (cart == null) { return(BadRequest("Unable to retrieve cart data.")); } var player = _appDbContext.tblPlayer.FirstOrDefault(p => p.Id == cart.PlayerId).ThrowIfNull("Player"); var currentEvent = _lfContext.tblEvent.FirstOrDefault(e => e.IsActiveEvent).ThrowIfNull("Current Event"); foreach (var cartItem in cart.CartItems) { if (cartItem == cart.CartItems.First()) { amount += 40; } else { amount += 10; } amount += cartItem.PurchaseXp; totalVp += cartItem.VpToXp; totalXp = totalXp + cartItem.PurchaseXp + cartItem.VpToXp; } var options = new StripeChargeCreateOptions { Amount = amount * 100, Currency = "usd", Description = $"Pre-registration for event '{currentEvent.Title}'", SourceTokenOrExistingSourceId = token["id"].ToString() }; var service = new StripeChargeService(); var charge = service.Create(options); cart.Paid = true; _lfContext.tblCart.Update(cart); player.VolunteerPoints = player.VolunteerPoints - totalVp; player.VolunteerPoints += 10; _appDbContext.tblPlayer.Update(player); _appDbContext.SaveChanges(); var characterDetailForEmail = new List <PreRegCharacterDetail>(); foreach (var cartItem in cart.CartItems) { var characterTotalXp = 0; characterTotalXp += cartItem.PurchaseXp; characterTotalXp += cartItem.VpToXp; var character = _lfContext.tblCharacter.FirstOrDefault(c => c.Id == cartItem.CharacterId); if (character == null) { return(BadRequest("Unable to get character data for characterid " + cartItem.CharacterId)); } if (character.AccumulatedXP == null) { character.AccumulatedXP = 0; } if (character.AvailableXP == null) { character.AvailableXP = 0; } var baseXp = 40; characterTotalXp += baseXp; character.AccumulatedXP += characterTotalXp; character.AvailableXP += characterTotalXp; var newCharacterEvent = new CharacterEvent { VpToXp = cartItem.VpToXp, XpBought = cartItem.PurchaseXp, EventId = currentEvent.Id, CharacterId = cartItem.CharacterId }; _lfContext.tblCharacter.Update(character); _lfContext.tblCharacterEvents.Add(newCharacterEvent); var characterToAdd = new PreRegCharacterDetail { Character = character, CartItem = cartItem, BaseXp = baseXp, BaseXpCost = characterDetailForEmail.FirstOrDefault() == null ? 40 : 10 }; characterDetailForEmail.Add(characterToAdd); } _lfContext.SaveChanges(); var stripeResonse = JObject.Parse(charge.StripeResponse.ObjectJson); var billingDetails = charge.Source.Card; var email = token["email"].ToString(); var emailDetails = new PreRegEmailDetails { Email = email, PreRegCharacterDetails = characterDetailForEmail, OrderID = cart.Key, BillingDetails = billingDetails, GrandTotal = amount, Event = currentEvent, RemainingVp = (int)player.VolunteerPoints }; Email.SendPreRegConfirmationEmail(emailDetails); return(Ok(charge)); } catch (Exception e) { return(BadRequest("An unknown error has ocurred")); } }