public ActionResult NewTreaty(int? acceptingFactionID) {
     // first display of the form, target faction not yet set 
     var db = new ZkDataContext();
     var treaty = new FactionTreaty();
     treaty.AccountByProposingAccountID = Global.Account;
     treaty.FactionByProposingFactionID = Global.Account.Faction;
     treaty.FactionByAcceptingFactionID = db.Factions.SingleOrDefault(x => x.FactionID == acceptingFactionID);
     return View("FactionTreatyDefinition", treaty);
 }
        /// <summary>
        /// Create or modify a PlanetWars <see cref="FactionTreaty"/>
        /// </summary>
        /// <param name="factionTreatyID">Existing <see cref="FactionTreaty"/> ID, if modifying one</param>
        /// <param name="turns">How long the treaty lasts</param>
        /// <param name="acceptingFactionID"></param>
        /// <param name="effectTypeID"><see cref="TreatyEffect"/> to add or remove, if applicable</param>
        /// <param name="effectValue"></param>
        /// <param name="planetID">Specifies the <see cref="Planet"/> for planet-based effects</param>
        /// <param name="isReverse"></param>
        /// <param name="note">Diplomatic note readable by both parties, to better communicate their intentions</param>
        /// <param name="add">If not null or empty, add the specified <see cref="TreatyEffect"/></param>
        /// <param name="delete">Delete the specified <see cref="TreatyEffect"/>?</param>
        /// <param name="propose">If not null or empty, this is a newly proposed treaty</param>
        /// <returns></returns>
        public ActionResult ModifyTreaty(int factionTreatyID,
                                         int? turns,
                                         int? acceptingFactionID,
                                         int? effectTypeID,
                                         double? effectValue,
                                         int? planetID,
                                         bool? isReverse,
                                         string note,
                                         string add,int? delete,string propose) {
            if (!Global.Account.HasFactionRight(x => x.RightDiplomacy)) return Content("Not a diplomat!");

            FactionTreaty treaty;

            // submit, store treaty in db 
            var db = new ZkDataContext();

            if (factionTreatyID > 0) {
                treaty = db.FactionTreaties.Single(x => x.FactionTreatyID == factionTreatyID);
                if (treaty.TreatyState != TreatyState.Invalid) return Content("Treaty already in progress!");
            }
            else {
                treaty = new FactionTreaty();
                db.FactionTreaties.InsertOnSubmit(treaty);
                treaty.FactionByAcceptingFactionID = db.Factions.Single(x => x.FactionID == acceptingFactionID);
            }
            treaty.AccountByProposingAccountID = db.Accounts.Find(Global.AccountID);
            treaty.FactionByProposingFactionID = db.Factions.Single(x => x.FactionID == Global.FactionID);
            treaty.TurnsRemaining = turns;
            treaty.TurnsTotal = turns;
            treaty.TreatyNote = note;
            

            if (!string.IsNullOrEmpty(add)) {
                TreatyEffectType effectType = db.TreatyEffectTypes.Single(x => x.EffectTypeID == effectTypeID);
                var effect = new TreatyEffect
                             {
                                 FactionByGivingFactionID = isReverse == true ? treaty.FactionByAcceptingFactionID : treaty.FactionByProposingFactionID,
                                 FactionByReceivingFactionID = 
                                     isReverse == true ? treaty.FactionByProposingFactionID : treaty.FactionByAcceptingFactionID,
                                 TreatyEffectType = effectType,
                                 FactionTreaty = treaty
                             };
                if (effectType.HasValue) {
                    if (effectType.MinValue.HasValue && effectValue < effectType.MinValue.Value) effectValue = effectType.MinValue;
                    if (effectType.MaxValue.HasValue && effectValue > effectType.MaxValue.Value) effectValue = effectType.MaxValue;
                }
                if (effectType.HasValue) effect.Value = effectValue;
                if (effectType.IsPlanetBased) effect.PlanetID = planetID.Value;
                db.TreatyEffects.InsertOnSubmit(effect);
            }
            if (delete != null) {
                db.TreatyEffects.DeleteOnSubmit(db.TreatyEffects.Single(x=>x.TreatyEffectID == delete));
            }
            db.SubmitAndMergeChanges();

            if (!string.IsNullOrEmpty(propose)) {
                treaty.TreatyState = TreatyState.Proposed;
                
                db.Events.InsertOnSubmit(Global.CreateEvent("{0} proposes a new treaty between {1} and {2} - {3}",treaty.AccountByProposingAccountID, treaty.FactionByProposingFactionID, treaty.FactionByAcceptingFactionID, treaty));

                db.SubmitAndMergeChanges();
                return RedirectToAction("Detail", new { id = treaty.ProposingFactionID});
            }

            return View("FactionTreatyDefinition", treaty);
        }
 public static MvcHtmlString PrintFactionTreaty(this HtmlHelper helper, FactionTreaty treaty) {
     var url = Global.UrlHelper();
     if (treaty != null) {
         return
             new MvcHtmlString(string.Format("<a href='{1}' nicetitle='$treaty${0}'>TR{0}</span></a>",
                                             treaty.FactionTreatyID,
                                             url.Action("TreatyDetail", "Factions", new { id = treaty.FactionTreatyID })));
     }
     else return new MvcHtmlString("");
 }