public static void AutoClosePolls()
        {
            var db = new ZkDataContext();
            foreach (var p in db.Polls.Where(x => x.IsHeadline && x.ExpireBy != null && x.ExpireBy < DateTime.UtcNow && x.RoleTypeID != null).ToList())
            {
                var yes = p.PollVotes.Count(x => x.PollOption.OptionText == "Yes");
                var no = p.PollVotes.Count(x => x.PollOption.OptionText == "No");
                var acc = p.AccountByRoleTargetAccountID;
                if (yes > no)
                {
                    if (p.RoleIsRemoval)
                    {
                        var toDelete = db.AccountRoles.Where(x => x.AccountID == acc.AccountID && x.RoleTypeID == p.RoleTypeID);
                        db.AccountRoles.DeleteAllOnSubmit(toDelete);
                        db.Events.InsertOnSubmit(Global.CreateEvent("{0} was removed from the {1} role of {2} by a vote - {3} for, {4} against", acc, (object)p.Clan ?? p.Faction, p.RoleType, yes, no));

                        db.SubmitAndMergeChanges();

                        AuthServiceClient.SendLobbyMessage(acc, string.Format("You were recalled from the function of {0} by a vote", p.RoleType.Name));
                    }
                    else
                    {
                        if (!acc.AccountRolesByAccountID.Any(x => x.RoleTypeID == p.RoleTypeID))
                        {
                            Account previous = null;
                            if (p.RoleType.IsOnePersonOnly)
                            {
                                var entries = db.AccountRoles.Where(x => x.RoleTypeID == p.RoleTypeID && (p.RoleType.IsClanOnly ? x.ClanID == p.RestrictClanID : x.FactionID == p.RestrictFactionID)).ToList();

                                if (entries.Any())
                                {
                                    previous = entries.First().Account;
                                    db.AccountRoles.DeleteAllOnSubmit(entries);
                                    db.SubmitAndMergeChanges();
                                }
                            }

                            var entry = new AccountRole()
                                        {
                                            Account = acc,
                                            Inauguration = DateTime.UtcNow,
                                            Clan = p.Clan,
                                            Faction = p.Faction,
                                            RoleType = p.RoleType
                                        };
                            acc.AccountRolesByAccountID.Add(entry);
                            if (previous == null)
                                db.Events.InsertOnSubmit(Global.CreateEvent("{0} was elected for the {1} role of {2} by a vote - {3} for, {4} against",
                                                   acc,
                                                   (object)p.Clan ?? p.Faction,
                                                   p.RoleType,
                                                   yes,
                                                   no));

                            else db.Events.InsertOnSubmit(Global.CreateEvent("{0} was elected for the {1} role of {2} by a vote, replacing {3} - {4} for, {5} against",
                                                   acc,
                                                   (object)p.Clan ?? p.Faction,
                                                   p.RoleType,
                                                   previous,
                                                   yes,
                                                   no));

                            AuthServiceClient.SendLobbyMessage(acc, string.Format("Congratulations!! You were elected into a function of {0} by a vote", p.RoleType.Name));
                        }
                    }
                }

                p.IsHeadline = false;
                db.Polls.DeleteOnSubmit(p);
            }
            db.SubmitAndMergeChanges();
        }
 public ActionResult AppointRole(int accountID, int roletypeID)
 {
     var db = new ZkDataContext();
     Account targetAccount = db.Accounts.Single(x => x.AccountID == accountID);
     Account myAccount = db.Accounts.Single(x => x.AccountID == Global.AccountID);
     RoleType role = db.RoleTypes.Single(x => x.RoleTypeID == roletypeID);
     if (myAccount.CanAppoint(targetAccount, role))
     {
         Account previous = null;
         if (role.IsOnePersonOnly)
         {
             List<AccountRole> entries =
                 db.AccountRoles.Where(
                 x => x.RoleTypeID == role.RoleTypeID && (role.IsClanOnly ? x.ClanID == myAccount.ClanID : x.FactionID == myAccount.FactionID)).ToList();
             if (entries.Any())
             {
                 previous = entries.First().Account;
                 db.AccountRoles.DeleteAllOnSubmit(entries);
             }
         }
         var entry = new AccountRole
                     {
                         AccountID = accountID,
                         Inauguration = DateTime.UtcNow,
                         Clan = role.IsClanOnly ? myAccount.Clan : null,
                         Faction = !role.IsClanOnly ? myAccount.Faction : null,
                         RoleTypeID = roletypeID,
                     };
         db.AccountRoles.InsertOnSubmit(entry);
         if (previous != null)
         {
             db.Events.InsertOnSubmit(Global.CreateEvent("{0} was appointed to the {1} role of {2} by {3} - replacing {4}",
                                                         targetAccount,
                                                         role.IsClanOnly ? (object)myAccount.Clan : myAccount.Faction,
                                                         role,
                                                         myAccount,
                                                         previous));
         }
         else
         {
             db.Events.InsertOnSubmit(Global.CreateEvent("{0} was appointed to the {1} role of {2} by {3}",
                                                         targetAccount,
                                                         role.IsClanOnly ? (object)myAccount.Clan : myAccount.Faction,
                                                         role,
                                                         myAccount));
         }
         Global.Server.GhostPm(targetAccount.Name, string.Format("You were appointed for the function of {0} by {1}", role.Name, myAccount.Name));
         db.SaveChanges();
         return RedirectToAction("Detail", "Users", new { id = accountID });
     }
     else return Content("Cannot recall");
 }