mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-25 22:30:52 -04:00
Implement ColorItem, TitleColorItem, and TitleItem for the new economy system.
This commit is contained in:
parent
8695d90945
commit
c2f4132168
149
Economy/Item.cs
149
Economy/Item.cs
@ -1,43 +1,106 @@
|
||||
/*
|
||||
Copyright 2015 MCGalaxy
|
||||
|
||||
Dual-licensed under the Educational Community License, Version 2.0 and
|
||||
the GNU General Public License, Version 3 (the "Licenses"); you may
|
||||
not use this file except in compliance with the Licenses. You may
|
||||
obtain a copy of the Licenses at
|
||||
|
||||
http://www.opensource.org/licenses/ecl2.php
|
||||
http://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the Licenses are distributed on an "AS IS"
|
||||
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
or implied. See the Licenses for the specific language governing
|
||||
permissions and limitations under the Licenses.
|
||||
*/
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace MCGalaxy {
|
||||
|
||||
/// <summary> An abstract object that can be bought in the economy. (e.g. a rank, title, levels, etc) </summary>
|
||||
public abstract class Item {
|
||||
|
||||
/// <summary> Simple name for this item. </summary>
|
||||
public abstract string Name { get; }
|
||||
|
||||
/// <summary> Other common names for this item. </summary>
|
||||
public abstract string[] Aliases { get; }
|
||||
|
||||
/// <summary> Whether this item can currently be bought in the economy. </summary>
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary> Reads the properties of this item from the economy.properties file. </summary>
|
||||
public abstract void Parse(StreamReader reader);
|
||||
|
||||
/// <summary> Writes the properties of this item to the economy.properties file. </summary>
|
||||
public virtual void Serialise(StreamWriter writer) {
|
||||
writer.WriteLine(Name + ":enabled:" + Enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
Copyright 2015 MCGalaxy
|
||||
|
||||
Dual-licensed under the Educational Community License, Version 2.0 and
|
||||
the GNU General Public License, Version 3 (the "Licenses"); you may
|
||||
not use this file except in compliance with the Licenses. You may
|
||||
obtain a copy of the Licenses at
|
||||
|
||||
http://www.opensource.org/licenses/ecl2.php
|
||||
http://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the Licenses are distributed on an "AS IS"
|
||||
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
or implied. See the Licenses for the specific language governing
|
||||
permissions and limitations under the Licenses.
|
||||
*/
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
||||
namespace MCGalaxy.Eco {
|
||||
|
||||
/// <summary> An abstract object that can be bought in the economy. (e.g. a rank, title, levels, etc) </summary>
|
||||
public abstract class Item {
|
||||
|
||||
/// <summary> Simple name for this item. </summary>
|
||||
public abstract string Name { get; }
|
||||
|
||||
/// <summary> Other common names for this item. </summary>
|
||||
public string[] Aliases;
|
||||
|
||||
/// <summary> Whether this item can currently be bought in the economy. </summary>
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary> Reads the given property of this item from the economy.properties file. </summary>
|
||||
/// <remarks> split is line split by the : character. </remarks>
|
||||
public abstract void Parse(string line, string[] split);
|
||||
|
||||
/// <summary> Writes the properties of this item to the economy.properties file. </summary>
|
||||
public abstract void Serialise(StreamWriter writer);
|
||||
|
||||
protected internal abstract void OnBuyCommand(Command cmd, Player p, string[] args);
|
||||
|
||||
protected internal abstract void OnSetupCommand(Player p, string[] args);
|
||||
|
||||
protected internal abstract void OnStoreCommand(Player p, string[] args);
|
||||
|
||||
protected static void MakePurchase(Player p, int cost, string item) {
|
||||
Economy.EcoStats ecos = Economy.RetrieveEcoStats(p.name);
|
||||
p.money -= cost;
|
||||
ecos.money = p.money;
|
||||
ecos.totalSpent += cost;
|
||||
ecos.purchase = item + "%3 - Price: %f" + cost + " %3" + Server.moneys +
|
||||
" - Date: %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
|
||||
Economy.UpdateEcoStats(ecos);
|
||||
Player.SendMessage(p, "%aYour balance is now %f" + p.money + " %3" + Server.moneys);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Simple item, in that it only has one cost value. </summary>
|
||||
public abstract class SimpleItem : Item {
|
||||
|
||||
/// <summary> How much this item costs to purchase. </summary>
|
||||
public int Price { get; set; }
|
||||
|
||||
public override void Parse(string line, string[] split) {
|
||||
if (split.Length < 3) return;
|
||||
|
||||
if (split[1].CaselessEquals("enabled"))
|
||||
Enabled = split[2].CaselessEquals("true");
|
||||
if (split[1].CaselessEquals("price"))
|
||||
Price = int.Parse(split[2]);
|
||||
}
|
||||
|
||||
public override void Serialise(StreamWriter writer) {
|
||||
writer.WriteLine(Name + ":enabled:" + Enabled);
|
||||
writer.WriteLine(Name + ":price:" + Price);
|
||||
}
|
||||
|
||||
protected internal override void OnSetupCommand(Player p, string[] args) {
|
||||
switch (args[2]) {
|
||||
case "enable":
|
||||
Player.SendMessage(p, "%a" + Name + "s are now enabled for the economy system.");
|
||||
Enabled = true; break;
|
||||
case "disable":
|
||||
Player.SendMessage(p, "%a" + Name + "s are now disabled for the economy system.");
|
||||
Enabled = false; break;
|
||||
case "price":
|
||||
int cost;
|
||||
if (!int.TryParse(args[3], out cost)) {
|
||||
Player.SendMessage(p, "\"" + args[3] + "\" is not a valid integer."); return;
|
||||
}
|
||||
Player.SendMessage(p, "%aSuccessfully changed the " + Name + " price to %f" + cost + " %3" + Server.moneys);
|
||||
Price = cost; break;
|
||||
default:
|
||||
Player.SendMessage(p, "Supported actions: enable, disable, price [pcost]"); break;
|
||||
}
|
||||
}
|
||||
|
||||
protected internal override void OnStoreCommand(Player p, string[] args) {
|
||||
if (!Enabled) { Player.SendMessage(p, "%c" + Name + "s are not enabled for the economy system."); return; }
|
||||
Player.SendMessage(p, Name + "s cost %f" + Price + " %3" + Server.moneys + " %Seach");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
95
Economy/NameItems.cs
Normal file
95
Economy/NameItems.cs
Normal file
@ -0,0 +1,95 @@
|
||||
|
||||
using System;
|
||||
|
||||
namespace MCGalaxy.Eco {
|
||||
|
||||
public sealed class TitleItem : SimpleItem {
|
||||
|
||||
public TitleItem() {
|
||||
Aliases = new[] { "titles", "title" };
|
||||
}
|
||||
|
||||
public override string Name { get { return "Title"; } }
|
||||
|
||||
protected internal override void OnBuyCommand(Command cmd, Player p, string[] args) {
|
||||
if (args.Length < 2) { cmd.Help(p); return; }
|
||||
if (!p.EnoughMoney(Economy.Settings.TitlePrice)) {
|
||||
Player.SendMessage(p, "%cYou don't have enough %3" + Server.moneys + "%c to buy a title"); return;
|
||||
}
|
||||
if (args[1] == p.title) {
|
||||
Player.SendMessage(p, "%cYou already have that title"); return;
|
||||
}
|
||||
if (args[1].Length > 17) {
|
||||
Player.SendMessage(p, "%cTitles cannot be longer than 17 characters"); return;
|
||||
}
|
||||
var regex = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9-_\\.]*$");
|
||||
if (!regex.IsMatch(args[1])) {
|
||||
Player.SendMessage(p, "%cInvalid title! Titles may only contain alphanumeric characters and .-_");
|
||||
return;
|
||||
}
|
||||
|
||||
bool free = args[1] == "";
|
||||
Command.all.Find("title").Use(null, p.name + " " + args[1]);
|
||||
if (!free) {
|
||||
Player.SendMessage(p, "%aYour title has been successfully changed to [" + p.titlecolor + args[1] + "%a]");
|
||||
MakePurchase(p, Price, "%3Title: %f" + args[1]);
|
||||
} else {
|
||||
Player.SendMessage(p, "%aYour title has been successfully removed for free");
|
||||
Player.SendMessage(p, "%aYour balance is now %f" + p.money + " %3" + Server.moneys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TitleColorItem : SimpleItem {
|
||||
|
||||
public TitleColorItem() {
|
||||
Aliases = new[] { "tcolor", "tcolors", "titlecolor", "titlecolors", "tc" };
|
||||
}
|
||||
|
||||
public override string Name { get { return "TitleColor"; } }
|
||||
|
||||
protected internal override void OnBuyCommand(Command cmd, Player p, string[] args) {
|
||||
if (args.Length < 2) { cmd.Help(p); return; }
|
||||
if (!p.EnoughMoney(Economy.Settings.TColorPrice)) {
|
||||
Player.SendMessage(p, "%cYou don't have enough %3" + Server.moneys + "%c to buy a titlecolor"); return;
|
||||
}
|
||||
if (!args[1].StartsWith("&") || !args[1].StartsWith("%")) {
|
||||
args[1] = Colors.Parse(args[1]);
|
||||
if (args[1] == "") { Player.SendMessage(p, "%cThat wasn't a color"); return; }
|
||||
}
|
||||
if (args[1] == p.titlecolor) {
|
||||
Player.SendMessage(p, "%cYou already have a " + args[1] + Colors.Name(args[1]) + "%c titlecolor"); return;
|
||||
}
|
||||
|
||||
Command.all.Find("tcolor").Use(null, p.name + " " + Colors.Name(args[1]));
|
||||
Player.SendMessage(p, "%aYour titlecolor has been successfully changed to " + args[1] + Colors.Name(args[1]));
|
||||
MakePurchase(p, Price, "%3Titlecolor: " + args[1] + Colors.Name(args[1]));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ColorItem : SimpleItem {
|
||||
|
||||
public ColorItem() {
|
||||
Aliases = new[] { "colors", "color", "colours", "colour" };
|
||||
}
|
||||
|
||||
public override string Name { get { return "Color"; } }
|
||||
|
||||
protected internal override void OnBuyCommand(Command cmd, Player p, string[] args) {
|
||||
if (args.Length < 2) { cmd.Help(p); return; }
|
||||
if (!p.EnoughMoney(Economy.Settings.ColorPrice)) {
|
||||
Player.SendMessage(p, "%cYou don't have enough %3" + Server.moneys + "%c to buy a color"); return;
|
||||
}
|
||||
if (!args[1].StartsWith("&") || !args[1].StartsWith("%")) {
|
||||
args[1] = Colors.Parse(args[1]);
|
||||
if (args[1] == "") { Player.SendMessage(p, "%cThat wasn't a color"); return; }
|
||||
}
|
||||
if (args[1] == p.color) {
|
||||
Player.SendMessage(p, "%cYou already have a " + args[1] + Colors.Name(args[1]) + "%c color"); return;
|
||||
}
|
||||
|
||||
Command.all.Find("color").Use(null, p.name + " " + Colors.Name(args[1]));
|
||||
MakePurchase(p, Price, "%3Color: " + args[1] + Colors.Name(args[1]));
|
||||
}
|
||||
}
|
||||
}
|
@ -414,6 +414,7 @@
|
||||
<Compile Include="Economy\Awards.cs" />
|
||||
<Compile Include="Economy\Economy.cs" />
|
||||
<Compile Include="Economy\Item.cs" />
|
||||
<Compile Include="Economy\NameItems.cs" />
|
||||
<Compile Include="Games\Countdown\CountdownGame.cs" />
|
||||
<Compile Include="Games\Countdown\CountdownGame.Game.cs" />
|
||||
<Compile Include="Games\Countdown\CountdownMapGen.cs" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user