comparison API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs @ 438:410f3d85c9c5

Re #350: Add requirement to allow N of unit for specific other units * Add code to get failure message for "allowed to add" * Refactor out common for checking if numbers exceed limits
author IBBoard <dev@ibboard.co.uk>
date Wed, 30 Nov 2011 20:44:03 +0000
parents 0922851f6125
children 5252dfb9cdfb
comparison
equal deleted inserted replaced
437:0922851f6125 438:410f3d85c9c5
1 // This file(RequiresNUnitsForMUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard 1 // This file(RequiresNUnitsForMUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
2 // 2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or(at your option) any later version. Please see COPYING for more information and the full license. 3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or(at your option) any later version. Please see COPYING for more information and the full license.
4 using System; 4 using System;
5 using System.Collections.Generic; 5 using System.Collections.Generic;
6 using System.Text;
6 7
7 namespace IBBoard.WarFoundry.API.Objects.Requirement 8 namespace IBBoard.WarFoundry.API.Objects.Requirement
8 { 9 {
9 public class RequiresNUnitsForMUnitsRequirement : AbstractRequirement 10 public class RequiresNUnitsForMUnitsRequirement : AbstractRequirement
10 { 11 {
56 return ""; 57 return "";
57 } 58 }
58 59
59 protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy) 60 protected override string GetAllowsAddingFailedMessage(UnitType toAdd, Army toArmy)
60 { 61 {
61 return ""; 62 StringBuilder sb = new StringBuilder();
63 sb.Append(FailureStringPrefix);
64 sb.Append(String.Join("; ", GetFailedAddingRequirements(toAdd, toArmy).ToArray()));
65 sb.Append(".");
66 return sb.ToString();
67 }
68
69 private List<string> GetFailedAddingRequirements(UnitType unitType, Army toArmy)
70 {
71 List<string> failures = new List<string>();
72 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, unitType);
73
74 foreach (UnitCountRequirementData limit in requiredTypes)
75 {
76 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType);
77
78 if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount))
79 {
80 failures.Add(String.Format("{0} {1} for every {2} {3} (have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount));
81 }
82 }
83
84 return failures;
85 }
86
87 private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount)
88 {
89 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
90 return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount);
91 }
92
93 private bool IsValidByRequirement (UnitCountRequirementData limit, int allowedTypeCount, int limitedTypeCount)
94 {
95 double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
96 double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
97 return allowedTypeMultiplier <= limitedTypeMultiplier;
62 } 98 }
63 99
64 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) 100 public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
65 { 101 {
66 Validation canAdd = Validation.NotApplicable; 102 Validation canAdd = Validation.NotApplicable;
69 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, wfObject); 105 int allowedTypeCount = GetUnitTypeCount(toArmy, allowedType, wfObject);
70 106
71 foreach (UnitCountRequirementData limit in requiredTypes) 107 foreach (UnitCountRequirementData limit in requiredTypes)
72 { 108 {
73 typeFound |= (addedUnitType == limit.UnitType); 109 typeFound |= (addedUnitType == limit.UnitType);
74 int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
75 double limitedTypeMultiplier = limitedTypeCount / (limit.Count * 1.0);
76 double allowedTypeMultiplier = allowedTypeCount / (limit.AllowsCount * 1.0);
77 110
78 if (allowedTypeMultiplier > limitedTypeMultiplier) 111 if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount))
79 { 112 {
80 canAdd = Validation.Failed; 113 canAdd = Validation.Failed;
81 break; 114 break;
82 } 115 }
83 } 116 }