Make items purchasing code more consistent

This commit is contained in:
UnknownShadow200 2020-06-23 21:35:28 +10:00
parent 52253a7b26
commit 02663729ae
7 changed files with 60 additions and 81 deletions

View File

@ -47,6 +47,27 @@ namespace MCGalaxy.Eco {
/// <summary> Writes the properties of this item to the economy.properties file. </summary>
public abstract void Serialise(StreamWriter writer);
/// <summary> Called when the player does /buy [item name] &lt;value&gt; </summary>
protected internal abstract void OnBuyCommand(Player p, string message, string[] args);
/// <summary> Called when the player does /eco [item name] [option] &lt;value&gt; </summary>
protected internal abstract void OnSetupCommand(Player p, string[] args);
/// <summary> Called when the player does /eco help [item name] </summary>
protected internal virtual void OnSetupCommandHelp(Player p) {
p.Message("%T/Eco {0} enable/disable", Name.ToLower());
p.Message("%HEnables/disables purchasing this item.");
p.Message("%T/Eco {0} purchaserank [rank]", Name.ToLower());
p.Message("%HSets the lowest rank which can purchase this item.");
}
/// <summary> Called when the player does /store </summary>
protected internal abstract void OnStoreOverview(Player p);
/// <summary> Called when the player does /store [item name] </summary>
protected internal abstract void OnStoreCommand(Player p);
internal void Setup(Player p, string[] args) {
string cmd = args[1];
@ -68,32 +89,20 @@ namespace MCGalaxy.Eco {
}
}
protected void UseCommand(Player p, string cmd, string args) {
protected static void UseCommand(Player p, string cmd, string args) {
CommandData data = default(CommandData);
data.Rank = LevelPermission.Nobody;
data.Context = CommandContext.Purchase;
Command.Find(cmd).Use(p, args, data);
}
/// <summary> Called when the player does /buy [item name] &lt;value&gt; </summary>
protected internal abstract void OnBuyCommand(Player p, string message, string[] args);
/// <summary> Called when the player does /eco [item name] [option] &lt;value&gt; </summary>
protected internal abstract void OnSetupCommand(Player p, string[] args);
/// <summary> Called when the player does /eco help [item name] </summary>
protected internal virtual void OnSetupCommandHelp(Player p) {
p.Message("%T/Eco {0} enable/disable", Name.ToLower());
p.Message("%HEnables/disables purchasing this item.");
p.Message("%T/Eco {0} purchaserank [rank]", Name.ToLower());
p.Message("%HSets the lowest rank which can purchase this item.");
protected static bool CheckPrice(Player p, int price, string item) {
if (p.money < price) {
p.Message("%WYou don't have enough &3{1} %Wto buy {0}.", item, Server.Config.Currency);
return false;
}
return true;
}
/// <summary> Called when the player does /store </summary>
protected internal abstract void OnStoreOverview(Player p);
/// <summary> Called when the player does /store [item name] </summary>
protected internal abstract void OnStoreCommand(Player p);
}
/// <summary> Simple item, in that it only has one cost value. </summary>
@ -102,9 +111,6 @@ namespace MCGalaxy.Eco {
/// <summary> How much this item costs to purchase. </summary>
public int Price = 100;
/// <summary> Whether providing no arguments is allowed. </summary>
protected bool AllowsNoArgs;
public override void Parse(string line, string[] args) {
if (args[1].CaselessEq("price"))
Price = int.Parse(args[2]);
@ -114,26 +120,7 @@ namespace MCGalaxy.Eco {
writer.WriteLine(Name + ":price:" + Price);
}
protected bool CheckPrice(Player p) {
if (p.money < Price) {
p.Message("%WYou don't have enough &3{1} %Wto buy a {0}.", Name, Server.Config.Currency);
return false;
}
return true;
}
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (AllowsNoArgs && args.Length == 1) {
DoPurchase(p, message, args); return;
}
// Must always provide an argument.
if (args.Length < 2) { OnStoreCommand(p); return; }
// TODO: Move this into item stuff
if (!CheckPrice(p)) return;
DoPurchase(p, message, args);
}
protected abstract void DoPurchase(Player p, string message, string[] args);
protected bool CheckPrice(Player p) { return CheckPrice(p, Price, "a " + Name); }
protected internal override void OnSetupCommand(Player p, string[] args) {
if (args[1].CaselessEq("price")) {

View File

@ -75,11 +75,9 @@ namespace MCGalaxy.Eco {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length < 2) { OnStoreCommand(p); return; }
LevelPreset preset = FindPreset(args[1]);
if (preset == null) { p.Message("%WThat isn't a level preset"); return; }
if (p.money < preset.price) {
p.Message("%WYou don't have enough &3" + Server.Config.Currency + "%W to buy that map"); return;
}
if (preset == null) { p.Message("%WThat isn't a level preset"); return; }
if (!CheckPrice(p, preset.price, "that map")) return;
string name = null;
if (args.Length >= 3) {

View File

@ -24,18 +24,18 @@ namespace MCGalaxy.Eco {
public LoginMessageItem() {
Aliases = new string[] { "login", "loginmsg", "loginmessage" };
AllowsNoArgs = true;
}
public override string Name { get { return "LoginMessage"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length == 1) {
PlayerDB.SetLoginMessage(p.name, "");
p.Message("&aYour login message was removed for free.");
return;
}
if (!CheckPrice(p)) return;
string msg = message.SplitSpaces(2)[1]; // keep spaces this way
if (msg == PlayerDB.GetLoginMessage(p)) {
p.Message("%WYou already have that login message."); return;
@ -53,18 +53,18 @@ namespace MCGalaxy.Eco {
public LogoutMessageItem() {
Aliases = new string[] { "logout", "logoutmsg", "logoutmessage" };
AllowsNoArgs = true;
}
public override string Name { get { return "LogoutMessage"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length == 1) {
PlayerDB.SetLogoutMessage(p.name, "");
p.Message("&aYour logout message was removed for free.");
return;
}
}
if (!CheckPrice(p)) return;
string msg = message.SplitSpaces(2)[1]; // keep spaces this way
if (msg == PlayerDB.GetLogoutMessage(p)) {
p.Message("%WYou already have that logout message."); return;

View File

@ -23,17 +23,17 @@ namespace MCGalaxy.Eco {
public TitleItem() {
Aliases = new string[] { "titles", "title" };
AllowsNoArgs = true;
}
public override string Name { get { return "Title"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length == 1) {
UseCommand(p, "Title", "-own");
p.Message("&aYour title was removed for free."); return;
}
if (!CheckPrice(p)) return;
string title = message.SplitSpaces(2)[1]; // keep spaces this way
if (title == p.title) {
p.Message("%WYou already have that title."); return;
@ -51,17 +51,17 @@ namespace MCGalaxy.Eco {
public NickItem() {
Aliases = new string[] { "nickname", "nick", "name" };
AllowsNoArgs = true;
}
public override string Name { get { return "Nickname"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length == 1) {
UseCommand(p, "Nick", "-own");
p.Message("&aYour nickname was removed for free."); return;
}
if (!CheckPrice(p)) return;
string nick = message.SplitSpaces(2)[1]; // keep spaces this way
if (nick == p.DisplayName) {
p.Message("%WYou already have that nickname."); return;
@ -83,11 +83,13 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "TitleColor"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length < 2) { OnStoreCommand(p); return; }
string color = Matcher.FindColor(p, args[1]);
if (color == null) return;
string colName = Colors.Name(color);
if (!CheckPrice(p)) return;
if (color == p.titlecolor) {
p.Message("%WYour title color is already " + color + colName); return;
}
@ -105,11 +107,13 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "Color"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length < 2) { OnStoreCommand(p); return; }
string color = Matcher.FindColor(p, args[1]);
if (color == null) return;
string colName = Colors.Name(color);
if (!CheckPrice(p)) return;
if (color == p.color) {
p.Message("%WYour color is already " + color + colName); return;
}

View File

@ -25,13 +25,12 @@ namespace MCGalaxy.Eco {
public SnackItem() {
Aliases = new string[] { "snack" };
AllowsNoArgs = true;
Price = 0;
}
public override string Name { get { return "Snack"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (DateTime.UtcNow < p.NextEat) {
p.Message("You're still full - you need to wait at least " +
"10 seconds between snacks."); return;

View File

@ -87,10 +87,8 @@ namespace MCGalaxy.Eco {
RankEntry nextRank = NextRank(p);
if (nextRank == null) {
p.Message("%WYou are already at or past the max buyable rank"); return;
}
if (p.money < nextRank.Price) {
p.Message("%WYou don't have enough &3" + Server.Config.Currency + " %Wto buy the next rank"); return;
}
if (!CheckPrice(p, nextRank.Price, "the next rank")) return;
Group rank = Group.Find(nextRank.Perm); // TODO: What if null reference happens here
Command.Find("SetRank").Use(Player.Console, p.name + " " + rank.Name);

View File

@ -28,20 +28,16 @@ namespace MCGalaxy.Eco {
public BlocksItem() {
Aliases = new string[] { "blocks", "bl", "b" };
Price = 1;
AllowsNoArgs = true;
}
public override string Name { get { return "10Blocks"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
int count = 1;
const string group = "Number of groups of 10 blocks";
if (args.Length >= 2 && !CommandParser.GetInt(p, args[1], group, ref count, 0, 10)) return;
if (p.money < Price * count) {
p.Message("%WYou don't have enough &3{2} %Wto buy {1} {0}.",
Name, count * 10, Server.Config.Currency); return;
}
if (!CheckPrice(p, count * Price, (count * 10) + " blocks")) return;
ZSData data = ZSGame.Get(p);
data.BlocksLeft += 10 * count;
@ -64,13 +60,16 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "QueueLevel"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (ZSGame.Instance.Picker.QueuedMap != null) {
p.Message("Someone else has already queued a level."); return;
}
if (args.Length < 2) { OnStoreCommand(p); return; }
string map = Matcher.FindMaps(p, args[1]);
if (map == null) return;
if (!CheckPrice(p)) return;
UseCommand(p, "Queue", "level " + map);
Economy.MakePurchase(p, Price, "%3QueueLevel: " + map);
}
@ -92,7 +91,8 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "InfectMessage"; } }
protected override void DoPurchase(Player p, string message, string[] args) {
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (args.Length < 2) { OnStoreCommand(p); return; }
string text = message.SplitSpaces(2)[1]; // keep spaces this way
bool hasAToken = false;
for (int i = 0; i < text.Length; i++) {
@ -105,6 +105,7 @@ namespace MCGalaxy.Eco {
"and/or a \"{1}\" (placeholder for human player) in the infect message."); return;
}
if (!CheckPrice(p)) return;
ZSData data = ZSGame.Get(p);
if (data.InfectMessages == null) data.InfectMessages = new List<string>();
data.InfectMessages.Add(text);
@ -134,9 +135,7 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "Invisibility"; } }
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (p.money < Price) {
p.Message("%WYou don't have enough &3{1} %Wto buy an {0}.", Name, Server.Config.Currency); return;
}
if (!CheckPrice(p, Price, "an invisibility potion")) return;
if (!ZSGame.Instance.Running || !ZSGame.Instance.RoundInProgress) {
p.Message("You can only buy an invisiblity potion " +
"when a round of zombie survival is in progress."); return;
@ -165,8 +164,6 @@ namespace MCGalaxy.Eco {
Economy.MakePurchase(p, Price, "%3Invisibility: " + duration);
}
protected override void DoPurchase(Player p, string message, string[] args) { }
protected internal override void OnStoreCommand(Player p) {
p.Message("%T/Buy " + Name);
OutputItemInfo(p);
@ -188,11 +185,9 @@ namespace MCGalaxy.Eco {
public override string Name { get { return "Revive"; } }
protected internal override void OnBuyCommand(Player p, string message, string[] args) {
if (p.money < Price) {
p.Message("%WYou don't have enough &3" + Server.Config.Currency + "%W to buy a " + Name + "."); return;
}
if (!CheckPrice(p, Price, "a revive potion")) return;
if (!ZSGame.Instance.Running || !ZSGame.Instance.RoundInProgress) {
p.Message("You can only buy an revive potion " +
p.Message("You can only buy a revive potion " +
"when a round of zombie survival is in progress."); return;
}
@ -222,8 +217,6 @@ namespace MCGalaxy.Eco {
Economy.MakePurchase(p, Price, "%3Revive:");
}
protected override void DoPurchase(Player p, string message, string[] args) { }
protected internal override void OnStoreCommand(Player p) {
int time = ZSGame.Config.ReviveNoTime, expiry = ZSGame.Config.ReviveTooSlow;
int potions = ZSGame.Config.ReviveTimes;