// // GET: /My/ public static bool IsUnlockValidForSlot(Unlock unlock, CommanderSlot slot) { if (slot.UnlockType == UnlockTypes.WeaponBoth) { if (unlock.UnlockType != UnlockTypes.Weapon && unlock.UnlockType != UnlockTypes.WeaponManualFire) return false; } else if (unlock.UnlockType != slot.UnlockType) return false; if (unlock.MorphLevel > slot.MorphLevel) return false; return true; }
public ActionResult CommanderProfile(int profileNumber, string name, int?chassis, string deleteCommander) { if (profileNumber < 1 || profileNumber > GlobalConst.CommanderProfileCount) { return(Content("WTF! get lost")); } var db = new ZkDataContext(); using (var scope = new TransactionScope()) { var unlocks = GetUserUnlockCountsListIncludingFree(db); Commander comm = db.Commanders.SingleOrDefault(x => x.ProfileNumber == profileNumber && x.AccountID == Global.AccountID); if (comm != null) { if (!string.IsNullOrEmpty(deleteCommander)) // delete commander { db.Commanders.DeleteOnSubmit(comm); db.SaveChanges(); scope.Complete(); return(GetCommanderProfileView(profileNumber)); } } else { comm = new Commander() { AccountID = Global.AccountID, ProfileNumber = profileNumber }; db.Commanders.InsertOnSubmit(comm); } if (comm.Unlock == null) { var chassisUnlock = unlocks.FirstOrDefault(x => x.Unlock.UnlockID == chassis); if ((chassis == null || chassisUnlock == null)) { return(GetCommanderProfileView(profileNumber)); } else { comm.ChassisUnlockID = chassis.Value; comm.Unlock = chassisUnlock.Unlock; } } if (!string.IsNullOrEmpty(name)) { if (name.Length > GlobalConst.MaxCommanderNameLength) { name = name.Substring(0, GlobalConst.MaxCommanderNameLength); } name = Regex.Replace(name, @"[^\u0020-\u007E]", string.Empty); // remove unicode stuff comm.Name = name; } // process modules foreach (var key in Request.Form.AllKeys.Where(x => !string.IsNullOrEmpty(x))) { var m = Regex.Match(key, "m([0-9]+)"); if (m.Success) { var slotId = int.Parse(m.Groups[1].Value); int unlockId; int.TryParse(Request.Form[key], out unlockId); if (unlockId > 0) { CommanderSlot slot = db.CommanderSlots.SingleOrDefault(x => x.CommanderSlotID == slotId); if (slot == null) { return(Content("WTF get lost!")); } Unlock unlock = db.Unlocks.SingleOrDefault(x => x.UnlockID == unlockId); if (unlock == null) { return(Content("WTF get lost!")); } if (!unlocks.Any(x => x.Unlock.UnlockID == unlock.UnlockID)) { return(Content("WTF get lost!")); } if (slot.MorphLevel < unlock.MorphLevel || !IsUnlockValidForSlot(unlock, slot)) { return(Content(string.Format("WTF cannot use {0} in slot {1}", unlock.Name, slot.CommanderSlotID))); } if (!string.IsNullOrEmpty(unlock.LimitForChassis)) { var validChassis = unlock.LimitForChassis.Split(','); if (!validChassis.Contains(comm.Unlock.Code)) { return(Content(string.Format("{0} cannot be used in commander {1}", unlock.Name, comm.Unlock.Name))); } } if (!IsPrerequisiteUnlockPresent(comm, unlock)) { return(Content(string.Format("{0} missing prerequisite module", unlock.Name))); } var comSlot = comm.CommanderModules.SingleOrDefault(x => x.SlotID == slotId); if (comSlot == null) { comSlot = new CommanderModule() { SlotID = slotId }; comm.CommanderModules.Add(comSlot); } comSlot.ModuleUnlockID = unlockId; } else { var oldModule = comm.CommanderModules.FirstOrDefault(x => x.SlotID == slotId); if (oldModule != null) { comm.CommanderModules.Remove(oldModule); } } } } // process decorations foreach (var key in Request.Form.AllKeys.Where(x => !string.IsNullOrEmpty(x))) { var d = Regex.Match(key, "d([0-9]+)"); if (d.Success) { var slotId = int.Parse(d.Groups[1].Value); int unlockId; int.TryParse(Request.Form[key], out unlockId); if (unlockId > 0) { CommanderDecorationSlot decSlot = db.CommanderDecorationSlots.SingleOrDefault(x => x.CommanderDecorationSlotID == slotId); if (decSlot == null) { return(Content("WTF get lost!")); } Unlock unlock = db.Unlocks.SingleOrDefault(x => x.UnlockID == unlockId && x.UnlockType == UnlockTypes.Decoration); if (unlock == null) { return(Content("WTF get lost!")); } if (!unlocks.Any(x => x.Unlock.UnlockID == unlock.UnlockID)) { return(Content("WTF get lost!")); } if (!string.IsNullOrEmpty(unlock.LimitForChassis)) { var validChassis = unlock.LimitForChassis.Split(','); if (!validChassis.Contains(comm.Unlock.Code)) { return(Content(string.Format("{0} cannot be used in commander {1}", unlock.Name, comm.Unlock.Name))); } } var comSlot = comm.CommanderDecorations.SingleOrDefault(x => x.SlotID == slotId); if (comSlot == null) { comSlot = new CommanderDecoration() { SlotID = slotId }; comm.CommanderDecorations.Add(comSlot); } comSlot.DecorationUnlockID = unlockId; } else { var oldDecoration = comm.CommanderDecorations.FirstOrDefault(x => x.SlotID == slotId); if (oldDecoration != null) { comm.CommanderDecorations.Remove(oldDecoration); } } } } // remove a module/decoration if ordered to foreach (var toDel in Request.Form.AllKeys.Where(x => !string.IsNullOrEmpty(x))) { var m = Regex.Match(toDel, "deleteSlot([0-9]+)"); if (m.Success) { var slotId = int.Parse(m.Groups[1].Value); comm.CommanderModules.Remove(comm.CommanderModules.SingleOrDefault(x => x.SlotID == slotId)); } var d = Regex.Match(toDel, "deleteDecorationSlot([0-9]+)"); if (d.Success) { var decSlotId = int.Parse(d.Groups[1].Value); comm.CommanderDecorations.Remove(comm.CommanderDecorations.SingleOrDefault(x => x.SlotID == decSlotId)); } } // cleanup invalid modules? db.SaveChanges(); foreach (var unlock in comm.CommanderModules.GroupBy(x => x.Unlock)) { if (unlock.Key == null) { continue; } var owned = unlocks.Where(x => x.Unlock.UnlockID == unlock.Key.UnlockID).Sum(x => (int?)x.Count) ?? 0; if (owned < unlock.Count()) { var toRemove = unlock.Count() - owned; foreach (var m in unlock.OrderByDescending(x => x.SlotID)) { db.CommanderModules.DeleteOnSubmit(m); //comm.CommanderModules.Remove(m); toRemove--; if (toRemove <= 0) { break; } } } } db.SaveChanges(); scope.Complete(); } return(GetCommanderProfileView(profileNumber)); }