private async Task <XmlDocument> getConcludeAttackDocAsync(Raid raid, int Attack, User user, string sessionId) { XmlDocument doc = new XmlDocument(); //(1) the xml declaration is recommended, but not mandatory XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement root = doc.DocumentElement; doc.InsertBefore(xmlDeclaration, root); //(2) string.Empty makes cleaner code XmlElement document = doc.CreateElement(string.Empty, "document", string.Empty); doc.AppendChild(document); XmlElement work = doc.CreateElement(string.Empty, "work", string.Empty); document.AppendChild(work); work.AppendChild(doc.CreateElement(string.Empty, "answer", string.Empty)); var UserRaid = await _context.UserRaids.Where(ur => ur.UserId == user.Id && ur.RaidId == raid.Id).FirstOrDefaultAsync(); if (UserRaid == null) { UserRaid = new UserRaid(); UserRaid.Id = Guid.NewGuid(); UserRaid.UserId = user.Id; UserRaid.RaidId = raid.Id; UserRaid.CallId = sessionId; _context.UserRaids.Add(UserRaid); } var rand = new Random(); var critAttack = rand.Next(1, 3); if (Attack == critAttack) { UserRaid.damage = user.ExpPoints * 2; work.AppendChild(addSpeak(doc, $"Your attack was a critical hit! You contributed {UserRaid.damage} damage to the monster!")); } else { UserRaid.damage = user.ExpPoints; work.AppendChild(addSpeak(doc, $"You hit the monster! You contributed {UserRaid.damage} damage to the monster!")); } work.AppendChild(addSpeak(doc, $"Please wait for the rest of the party to finish attacking the monster")); work.AppendChild(addWait(doc, 10)); work.AppendChild(addDialURL(doc, $"https://telequestbackend.azurewebsites.net/api/APIDazeRaid/GetRaid?DoneAttacking=true")); _context.SaveChanges(); return(doc); }
public async Task <ActionResult> GetRaid( [FromQuery(Name = "caller_id_number")] string PhoneNumber, [FromQuery(Name = "session_id")] string SessionId, [FromQuery(Name = "Attacking")] Boolean Attacking = false, [FromQuery(Name = "Attack")] int Attack = 0, [FromQuery(Name = "DoneAttacking")] Boolean DoneAttack = false ) { PhoneNumber = PhoneNumber.Replace("+", ""); var log = new CallLog() { Id = Guid.NewGuid(), PhoneNumber = PhoneNumber, UUID = SessionId, Time = DateTime.UtcNow, }; _context.CallLog.Add(log); _context.SaveChanges(); var user = _context.Users.Where(u => u.PhoneNumber == PhoneNumber).Take(1).FirstOrDefault(); // Handle no known user if (user == null) { return(File(Encoding.UTF8.GetBytes(getUnknownDoc().OuterXml), "text/xml")); } var onRaid = _context.UserRaids.Where(ur => ur.CallId == SessionId && ur.UserId == user.Id).Take(1).FirstOrDefault(); var nextRaid = _context.Raid.Where(r => r.Date >= DateTime.UtcNow).OrderBy(r => r.Date).Take(1).FirstOrDefault(); // Check if inside an actual raid if (onRaid != null) { var activeRaid = _context.Raid.Find(onRaid.RaidId); if (activeRaid == null) { return(File(Encoding.UTF8.GetBytes(getNoRaid(user).OuterXml), "text/xml")); } if (DoneAttack == true) { var raiders = await _context.UserRaids.Where(ur => ur.RaidId == activeRaid.Id).ToListAsync(); return(File(Encoding.UTF8.GetBytes((await getConclueRaidDoc(activeRaid, raiders, user)).OuterXml), "text/xml")); } if (Attack > 0) { return(File(Encoding.UTF8.GetBytes((await getConcludeAttackDocAsync(activeRaid, Attack, user, SessionId)).OuterXml), "text/xml")); } if (Attacking == true) { return(File(Encoding.UTF8.GetBytes(getStartAttackDoc(activeRaid).OuterXml), "text/xml")); } var activeRaiders = _context.UserRaids.Where(UR => UR.RaidId == activeRaid.Id).Include(r => r.User).ToList(); return(File(Encoding.UTF8.GetBytes(getStartRaidDoc(activeRaid, activeRaiders).OuterXml), "text/xml")); } if (nextRaid != null && onRaid == null) { var RaidUser = new UserRaid(); RaidUser.RaidId = nextRaid.Id; RaidUser.UserId = user.Id; RaidUser.CallId = SessionId; _context.UserRaids.Add(RaidUser); _context.SaveChanges(); var userRaids = _context.UserRaids.Where(ur => ur.RaidId == nextRaid.Id).ToList(); return(File(Encoding.UTF8.GetBytes(getWelcomeDoc(user, nextRaid, userRaids).OuterXml), "text/xml")); } return(File(Encoding.UTF8.GetBytes(getUnknownDoc().OuterXml), "text/xml")); }