/// <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.SaveChanges(); if (!string.IsNullOrEmpty(propose)) { treaty.TreatyState = TreatyState.Proposed; db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} proposes a new treaty between {1} and {2} - {3}", treaty.AccountByProposingAccountID, treaty.FactionByProposingFactionID, treaty.FactionByAcceptingFactionID, treaty)); db.SaveChanges(); return(RedirectToAction("Detail", new { id = treaty.ProposingFactionID })); } return(View("FactionTreatyDefinition", db.FactionTreaties.Find(treaty.FactionTreatyID))); }
/// <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, TreatyUnableToTradeMode?treatyUnableToTradeMode, int?proposingFactionGuarantee, int?acceptingFactionGuarantee, string note, string add, int?delete, string propose) { if (Global.Account == null) { return(Content("You must be logged in to manage treaties")); } 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 { if (factionTreatyID == acceptingFactionID) { return(Content("Faction cannot enter into treaty with itself")); } 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; treaty.TreatyUnableToTradeMode = treatyUnableToTradeMode ?? TreatyUnableToTradeMode.Cancel; treaty.ProposingFactionGuarantee = Math.Max(proposingFactionGuarantee ?? 0, 0); treaty.AcceptingFactionGuarantee = Math.Max(acceptingFactionGuarantee ?? 0, 0); 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; effect.Planet = db.Planets.Find(planetID.Value); if (effect.Planet.PlanetStructures.Any(x => x.StructureType.OwnerChangeWinsGame == true) && effectType.TreatyEffects.Any(x => x.TreatyEffectType.EffectGiveInfluence == true)) { return(Content("Cannot trade influence on victory planets")); } } db.TreatyEffects.InsertOnSubmit(effect); } if (delete != null) { db.TreatyEffects.DeleteOnSubmit(db.TreatyEffects.Single(x => x.TreatyEffectID == delete)); } db.SaveChanges(); if (!string.IsNullOrEmpty(propose)) { treaty.TreatyState = TreatyState.Proposed; db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} proposes a new treaty between {1} and {2} - {3}", treaty.AccountByProposingAccountID, treaty.FactionByProposingFactionID, treaty.FactionByAcceptingFactionID, treaty)); db.SaveChanges(); return(RedirectToAction("Detail", new { id = treaty.ProposingFactionID })); } return(View("FactionTreatyDefinition", db.FactionTreaties.Find(treaty.FactionTreatyID))); }