Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions TBot.Ogame.Infrastructure/Models/Ships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,9 @@ public Ships Add(Buildables buildable, long quantity) {
public Ships Remove(Buildables buildable, int quantity) {
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
if (prop.Name == buildable.ToString()) {
long val = (long) prop.GetValue(this);
if (val >= quantity)
prop.SetValue(this, val);
else
prop.SetValue(this, 0);
long val = (long)prop.GetValue(this);
long newVal = val - quantity;
prop.SetValue(this, newVal >= 0 ? newVal : 0);
}
}
return this;
Expand All @@ -177,9 +175,8 @@ public void SetAmount(Buildables buildable, long number) {

public bool HasAtLeast(Ships ships, long times = 1) {
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
if ((long) prop.GetValue(this) * times < (long) prop.GetValue(ships)) {
if ((long) prop.GetValue(this) < (long) prop.GetValue(ships) *times)
return false;
}
}
return true;
}
Expand All @@ -193,6 +190,45 @@ public override string ToString() {
}
return output;
}
}

}
public Ships Merge(Ships other) {
Ships result = this;
foreach (PropertyInfo prop in other.GetType().GetProperties()) {
result.Add((Buildables)Enum.Parse(typeof(Buildables), prop.Name), (long)prop.GetValue(other));
}
return result;
}

public Ships unMerge(Ships other) {
Ships result = this;
foreach (PropertyInfo prop in other.GetType().GetProperties()) {
result.Remove((Buildables) Enum.Parse(typeof(Buildables), prop.Name), (int)(long) prop.GetValue(other));
}
return result;
}

public Ships Multiply(int quantity) {
Ships result = this;
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
prop.SetValue(result, (long) Math.Ceiling((double)(long) prop.GetValue(result) *quantity));
}
return result;
}

public Ships Divide(int quantity) {
Ships result = this;
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
prop.SetValue(result, (long) Math.Ceiling((double)(long) prop.GetValue(result) /quantity));
}
return result;
}

public Ships Clone() {
Ships clone = new Ships();
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
prop.SetValue(clone, prop.GetValue(this));
}
return clone;
}
}
}
Loading
Loading