public ActionResult TerritoryPhasePlatoonClosures(Guid id)
        {
            TerritoryBattlePhase tbp = db.TerritoryBattlePhases.Find(id);

            var model = Mapper.Map <TerritoryBattlePhase, PlatoonClosureModel>(tbp);

            return(View(model));
        }
        public ActionResult ChangeAlignment(Guid id, Alignment alignment)
        {
            TerritoryBattlePhase tbp = db.TerritoryBattlePhases.Find(id);

            tbp.Alignment = alignment;
            db.SaveChanges();
            return(RedirectToAction("Details", tbp));
        }
        public ActionResult DeleteConfirmed(Guid id)
        {
            TerritoryBattlePhase territoryBattlePhase = db.TerritoryBattlePhases.Find(id);

            db.TerritoryBattlePhases.Remove(territoryBattlePhase);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult ChangePhase(Guid id, int phase)
        {
            TerritoryBattlePhase tbp = db.TerritoryBattlePhases.Find(id);

            tbp.Phase         = phase;
            tbp.RequiredStars = phase + 1;
            db.SaveChanges();
            return(RedirectToAction("Details", tbp));
        }
        public ActionResult Edit(TerritoryBattlePhase territoryBattlePhase)
        {
            if (ModelState.IsValid)
            {
                db.Entry(territoryBattlePhase).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(territoryBattlePhase));
        }
        public ActionResult Create(TerritoryBattlePhase territoryBattlePhase)
        {
            if (ModelState.IsValid)
            {
                territoryBattlePhase.Id = Guid.NewGuid();
                db.TerritoryBattlePhases.Add(territoryBattlePhase);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(territoryBattlePhase));
        }
        public ActionResult PlatoonAssignmentsByCharacter(Guid id)
        {
            TerritoryBattlePhase phase = db.TerritoryBattlePhases.Find(id);

            DataSet ds = new DataSet("Assignments");
            List <PlatoonAssignmentsByCharacter> model = (List <PlatoonAssignmentsByCharacter>)HttpContext.Cache.Get("PlatoonAssignmentsByCharacter" + id.ToString());

            if (model == null)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SwgohDb"].ConnectionString))
                {
                    SqlCommand sqlComm = new SqlCommand("PlattonAssignmentsByCharacter", conn);
                    sqlComm.CommandTimeout = 60;
                    sqlComm.Parameters.AddWithValue("@phaseGuid", phase.Id);

                    sqlComm.CommandType = System.Data.CommandType.StoredProcedure;

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = sqlComm;

                    da.Fill(ds);

                    model = ds.Tables[0].AsEnumerable().Select(
                        dataRow => new PlatoonAssignmentsByCharacter
                    {
                        CharacterName    = dataRow.Field <string>("DisplayName"),
                        NeededCount      = dataRow.Field <int>("NeedCount"),
                        HaveCount        = dataRow.Field <int>("HaveCount"),
                        AssignedMembers  = dataRow.Field <string>("AssignedMembers"),
                        AssignedPlatoons = dataRow.Field <string>("Platoons")
                    }).ToList();
                    foreach (var assign in model)
                    {
                        if (assign.AssignedMembers != null && assign.AssignedMembers != "")
                        {
                            assign.AssignedMembers = assign.AssignedMembers.Replace(",", "<br/>");
                        }
                        if (assign.AssignedPlatoons != null && assign.AssignedPlatoons != "")
                        {
                            assign.AssignedPlatoons = assign.AssignedPlatoons.Replace(",", "<br/>");
                        }
                    }
                }
                HttpContext.Cache.Insert("PlatoonAssignmentsByCharacter" + id.ToString(), model, null, Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));
            }

            ViewBag.PhaseNumber = phase.Phase;
            ViewBag.Id          = phase.Id;

            return(View(model));
        }
        public ActionResult Delete(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TerritoryBattlePhase territoryBattlePhase = db.TerritoryBattlePhases.Find(id);

            if (territoryBattlePhase == null)
            {
                return(HttpNotFound());
            }
            return(View(territoryBattlePhase));
        }
        public ActionResult Details(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TerritoryBattlePhase territoryBattlePhase = db.TerritoryBattlePhases.Include(x => x.PhaseTerritories).SingleOrDefault(x => x.Id == id);

            if (territoryBattlePhase == null)
            {
                return(HttpNotFound());
            }
            TerritoryBattlePhaseModel model = Mapper.Map <TerritoryBattlePhase, TerritoryBattlePhaseModel>(territoryBattlePhase);

            return(View(model));
        }
        public ActionResult PlatoonAssignmentsByMember(Guid id)
        {
            TerritoryBattlePhase phase = db.TerritoryBattlePhases.Find(id);

            DataSet ds = new DataSet("MemberAssignments");
            List <PlatoonAssignmentsByMember> model = (List <PlatoonAssignmentsByMember>)HttpContext.Cache.Get("PlatoonAssignmentsByMember" + id.ToString());

            if (model == null)
            {
                List <PlatoonAssignmentsByCharacter> paByChar = (List <PlatoonAssignmentsByCharacter>)HttpContext.Cache.Get("PlatoonAssignmentsByCharacter" + id.ToString());
                model = new List <ViewModels.PlatoonAssignmentsByMember>();

                foreach (var charAssignment in paByChar)
                {
                    if (charAssignment.HaveCount != 0)
                    {
                        string[] members  = charAssignment.AssignedMembers.Split(new string[] { "<br/>" }, StringSplitOptions.None);
                        string[] platoons = charAssignment.AssignedPlatoons.Split(new string[] { "<br/>" }, StringSplitOptions.None);

                        for (int i = 0; i < members.Count(); i++)
                        {
                            if (model.Where(x => x.MemberName.Contains(members[i])).Count() == 0 && members[i] != "")
                            {
                                PlatoonAssignmentsByMember memAssignment = new ViewModels.PlatoonAssignmentsByMember()
                                {
                                    MemberName         = members[i],
                                    AssignedCharacters = charAssignment.CharacterName + " - " + platoons[i]
                                };
                                model.Add(memAssignment);
                            }
                            else if (members[i] != "")
                            {
                                var obj = model.FirstOrDefault(x => x.MemberName == members[i]);
                                if (obj != null)
                                {
                                    obj.AssignedCharacters = obj.AssignedCharacters + "<br/>" + charAssignment.CharacterName + " - " + platoons[i];
                                }
                            }
                        }
                    }
                }

                HttpContext.Cache.Insert("PlatoonAssignmentsByMember" + id.ToString(), model, null, Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));
            }

            return(PartialView(model));
        }
        public ActionResult NewPlatoonAssignments(Guid id, Guid?memberId, Guid?guildId = null)
        {
            TerritoryBattlePhase tbp = db.TerritoryBattlePhases.Find(id);

            ViewBag.PhaseNumber = tbp.Phase;

            var members = db.Members.Where(x => x.Guild_Id == tbp.TerritoryBattle.Guild_Id).Select(s => new
            {
                Text  = s.DisplayName,
                Value = s.Id
            }).OrderBy(x => x.Text).AsEnumerable();

            SelectList selectList = new SelectList(members.ToList(), "Value", "Text");

            TerritoryPhaseReportModel model = new TerritoryPhaseReportModel()
            {
                Members  = selectList,
                MemberId = memberId,
                Id       = id,
            };

            ViewBag.GuildId = guildId;

            if (tbp.RefreshReport)
            {
                foreach (DictionaryEntry key in HttpContext.Cache)
                {
                    HttpContext.Cache.Remove(key.Key.ToString());
                }

                db.BulkDelete(db.PhaseReports.Where(x => x.TerritoryBattlePhase_Id == id).ToList());

                db = new SwgohDb();
                var tbpUpdate = db.TerritoryBattlePhases.Find(id);
                tbpUpdate.RefreshReport = false;

                db.Entry(tbpUpdate).State = EntityState.Modified;
                db.SaveChanges();
                db = new SwgohDb();
            }

            return(View(model));
        }
        public ActionResult ShipPlatoonAssignmentsData(DataSourceRequest command, Guid id, Guid?memberId)
        {
            var gridModel = new DataSourceResult();

            TerritoryBattlePhase      tbp       = db.TerritoryBattlePhases.Find(id);
            DataTable                 ds        = new DataTable();
            IEnumerable <PhaseReport> newReport = (IEnumerable <PhaseReport>)HttpContext.Cache.Get("ShipPlatoonAssignments" + id);

            if (newReport == null)
            {
                newReport = db.PhaseReports.Where(x => x.TerritoryBattlePhase_Id == id && x.MemberCharacter_Id == null);
                if (!newReport.Any())
                {
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SwgohDb"].ConnectionString))
                    {
                        SqlCommand sqlComm = new SqlCommand("sp_RequiredPlatoonShips", conn);
                        sqlComm.Parameters.AddWithValue("@phaseGuid", id);

                        sqlComm.CommandType = System.Data.CommandType.StoredProcedure;

                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = sqlComm;

                        da.Fill(ds);

                        Guid?currentShipId            = Guid.Empty;
                        List <MemberShip> memberShips = new List <MemberShip>();
                        int assignedShips             = 0;

                        foreach (DataRow row in ds.Rows)
                        {
                            if (currentShipId != row.Field <Guid>("Ship_Id"))
                            {
                                currentShipId = row.Field <Guid>("Ship_Id");
                                memberShips   = db.MemberShips.Where(x => x.Ship_Id == currentShipId &&
                                                                     x.Member.Guild_Id == tbp.TerritoryBattle.Guild_Id &&
                                                                     x.Stars >= tbp.RequiredStars)
                                                .OrderBy(x => x.ShipPower)
                                                .ThenBy(x => x.Stars)
                                                .ThenBy(x => x.Level).ToList();
                                assignedShips = 0;
                            }

                            try
                            {
                                row[1] = memberShips[assignedShips].Id;
                            }
                            catch
                            {
                                row[1] = DBNull.Value;
                            }
                            assignedShips++;
                        }
                        newReport = ds.AsEnumerable().Select(
                            dataRow => new PhaseReport
                        {
                            Id                      = Guid.NewGuid(),
                            PlatoonShip_Id          = dataRow.Field <Guid?>("PlatoonShip_Id"),
                            PlatoonShip             = db.PlatoonShips.Find(dataRow.Field <Guid?>("PlatoonShip_Id")),
                            TerritoryBattlePhase_Id = dataRow.Field <Guid>("TerritoryBattlePhase_Id"),
                            TerritoryBattlePhase    = db.TerritoryBattlePhases.Find(dataRow.Field <Guid>("TerritoryBattlePhase_Id")),
                            MemberShip_Id           = dataRow.Field <Guid?>("Ship_Id"),
                            MemberShip              = db.MemberShips.Find(dataRow.Field <Guid?>("Ship_Id"))
                        }).ToList();

                        db.BulkInsert(newReport);
                    }
                }
                HttpContext.Cache.Insert("ShipPlatoonAssignments" + id, newReport, null, Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));
            }
            if (memberId.HasValue)
            {
                //gridModel = newReport.Where(x => x.MemberShip.Member_Id == memberId).ToList().ToDataSourceResult(command, Mapper.Map<PhaseReport, PlatoonAssignmentsModel>);
                gridModel = db.PhaseReports.Where(x => x.TerritoryBattlePhase_Id == id && x.MemberCharacter_Id == null & x.MemberShip.Member_Id == memberId)
                            .ToList().ToDataSourceResult(command, Mapper.Map <PhaseReport, PlatoonAssignmentsModel>);
            }
            else
            {
                gridModel = (DataSourceResult)HttpContext.Cache.Get("ShipPlatoonAssignmentsGrid" + id.ToString());
                if (gridModel == null)
                {
                    gridModel = newReport.ToDataSourceResult(command, Mapper.Map <PhaseReport, PlatoonAssignmentsModel>);
                    HttpContext.Cache.Insert("ShipPlatoonAssignmentsGrid" + id, gridModel, null, Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0));
                }
            }
            return(Json(gridModel));
        }