Stage 1 of refactoring economy.

This commit is contained in:
UnknownShadow200 2017-05-12 22:19:13 +10:00
parent e1aa99a090
commit 4dcc4b7cdc
12 changed files with 183 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright 2015 MCGalaxy
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may

View File

@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.Fun {
public override void Use(Player p, string message) {
if (Player.IsSuper(p)) { MessageInGameOnly(p); return; }
if (!p.EnoughMoney(10)) {
if (p.money < 10) {
Player.Message(p, "You need &f10 " + Server.moneys + " %Sto enter the lottery."); return;
}

View File

@ -16,9 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using MCGalaxy.DB;
using MCGalaxy.Games;
namespace MCGalaxy.Commands.Info {
public sealed class CmdWhois : Command {

View File

@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using MCGalaxy.Eco;
using MCGalaxy.Events;
using MCGalaxy.Tasks;
@ -36,12 +37,15 @@ namespace MCGalaxy.Core {
Priority.Critical, this);
OnPlayerConnectingEvent.Register(ConnectingHandler.HandleConnecting,
Priority.Critical, this);
OnJoinedLevelEvent.Register(MiscHandlers.HandleOnJoinedLevel,
Priority.Critical, this);
OnPlayerMoveEvent.Register(MiscHandlers.HandlePlayerMove,
Priority.Critical, this);
OnPlayerClickEvent.Register(MiscHandlers.HandlePlayerClick,
Priority.Critical, this);
OnPlayerClickEvent.Register(MiscHandlers.HandlePlayerClick,
Priority.Critical, this);
OnEcoTransactionEvent.Register(EcoHandlers.HandleEcoTransaction,
Priority.Critical, this);
clearTask = Server.Background.QueueRepeat(IPThrottler.CleanupTask, null,
TimeSpan.FromMinutes(10));
@ -51,8 +55,12 @@ namespace MCGalaxy.Core {
OnPlayerConnectEvent.UnRegister(this);
OnPlayerCommandEvent.UnRegister(this);
OnPlayerConnectingEvent.UnRegister(this);
OnJoinedLevelEvent.UnRegister(this);
OnPlayerMoveEvent.UnRegister(this);
OnPlayerClickEvent.UnRegister(this);
OnEcoTransactionEvent.UnRegister(this);
Server.Background.Cancel(clearTask);
}
}

View File

@ -0,0 +1,48 @@
/*
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;
using MCGalaxy.Blocks.Extended;
using MCGalaxy.Events;
using MCGalaxy.Maths;
using MCGalaxy.Network;
using MCGalaxy.Eco;
using System.Globalization;
namespace MCGalaxy.Core {
internal static class EcoHandlers {
internal static void HandleEcoTransaction(EcoTransaction transaction) {
switch (transaction.Type) {
case EcoTransactionType.Purchase:
HandlePurchase(transaction); break;
}
}
static void HandlePurchase(EcoTransaction transaction) {
Economy.EcoStats stats = Economy.RetrieveStats(transaction.TargetName);
stats.TotalSpent += transaction.Amount;
stats.Purchase = transaction.ItemName + "%3 for %f" + transaction.Amount + " %3" + Server.moneys
+ " on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Player p = PlayerInfo.FindExact(transaction.TargetName);
if (p != null) Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys);
Economy.UpdateStats(stats);
}
}
}

View File

@ -19,8 +19,8 @@ using System;
using System.IO;
using MCGalaxy.Blocks.Extended;
using MCGalaxy.Events;
using MCGalaxy.Network;
using MCGalaxy.Maths;
using MCGalaxy.Network;
namespace MCGalaxy.Core {
internal static class MiscHandlers {

View File

@ -0,0 +1,56 @@
/*
Copyright 2014 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;
namespace MCGalaxy.Eco {
/// <summary> Describes an economic transaction. </summary>
public sealed class EcoTransaction {
/// <summary> Name of player who caused the transaction. Can be null. (e.g. person who used /give /pay /take) </summary>
public string SourceName;
/// <summary> Formatted name of source player. Can be null. </summary>
public string SourceFormatted;
/// <summary> Name of player who was target of the transaction. (e.g. person who received money from /pay) </summary>
public string TargetName;
/// <summary> Formatted name of target player. </summary>
public string TargetFormatted;
/// <summary> Name of the item involved in the transaction. Can be null. </summary>
public string ItemName;
/// <summary> Reason provided for the transaction. Can be null. </summary>
public string Reason;
/// <summary> Amount involved in the transaction. Zero or positive. </summary>
public int Amount;
/// <summary> Type of this transaction. </summary>
public EcoTransactionType Type;
}
public enum EcoTransactionType {
Give, Payment, Take, Purchase,
}
}

View File

@ -19,11 +19,17 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using MCGalaxy.Events;
namespace MCGalaxy.Eco {
public static partial class Economy {
public static bool Enabled;
public delegate void OnMoneyChanged(Player p);
public delegate void OnEcoTransaction(EcoTransaction transaction);
public static void Load() {
if (!File.Exists(Paths.EconomyPropsFile)) {
@ -105,13 +111,13 @@ namespace MCGalaxy.Eco {
public static void MakePurchase(Player p, int cost, string item) {
p.SetMoney(p.money - cost);
Player.Message(p, "Your balance is now &f{0} &3{1}", p.money, Server.moneys);
Economy.EcoStats stats = RetrieveStats(p.name);
stats.TotalSpent += cost;
stats.Purchase = item + "%3 for %f" + cost + " %3" + Server.moneys +
" on %f" + DateTime.Now.ToString(CultureInfo.InvariantCulture);
Economy.UpdateStats(stats);
EcoTransaction transaction = new EcoTransaction();
transaction.TargetName = p.name;
transaction.TargetFormatted = p.ColoredName;
transaction.Amount = cost;
transaction.Type = EcoTransactionType.Purchase;
transaction.ItemName = item;
OnEcoTransactionEvent.Call(transaction);
}
}
}

View File

@ -395,6 +395,7 @@
<Compile Include="CorePlugin\ConnectingHandler.cs" />
<Compile Include="CorePlugin\CorePlugin.cs" />
<Compile Include="CorePlugin\ConnectHandler.cs" />
<Compile Include="CorePlugin\EcoHandlers.cs" />
<Compile Include="CorePlugin\IPThrottler.cs" />
<Compile Include="CorePlugin\MiscHandlers.cs" />
<Compile Include="Database\Backends\SQLite.cs" />
@ -462,6 +463,7 @@
<Compile Include="Drawing\Transform\Transform.cs" />
<Compile Include="Economy\Awards.cs" />
<Compile Include="Economy\Economy.DB.cs" />
<Compile Include="Economy\EcoTransaction.cs" />
<Compile Include="Economy\ReviveItem.cs" />
<Compile Include="Economy\ZombieItems.cs" />
<Compile Include="Economy\Economy.cs" />
@ -586,6 +588,7 @@
<Compile Include="Database\Undo\UndoFormat.cs" />
<Compile Include="Database\Undo\UndoFormatBin.cs" />
<Compile Include="Database\Undo\UndoFormatText.cs" />
<Compile Include="Plugins\Events\EconomyEvents.cs" />
<Compile Include="Plugins\Events\Enums.cs" />
<Compile Include="Plugins\Events\GroupEvents.cs" />
<Compile Include="Plugins\Events\IPluginEvent.cs" />

View File

@ -21,6 +21,7 @@ using MCGalaxy.Drawing.Transforms;
using MCGalaxy.Games;
using MCGalaxy.Undo;
using MCGalaxy.Maths;
using MCGalaxy.Events;
namespace MCGalaxy {
@ -262,10 +263,5 @@ namespace MCGalaxy {
/// <summary> Returns whether the given player is console or IRC. </summary>
public static bool IsSuper(Player p) { return p == null || p.ircChannel != null || p.ircNick != null; }
public void SetMoney(int amount) {
money = amount;
OnMoneyChanged();
}
}
}

View File

@ -479,14 +479,13 @@ namespace MCGalaxy {
catch { }
return false;
}
public bool EnoughMoney(int amount) {
return money >= amount;
}
public void OnMoneyChanged() {
public void SetMoney(int amount) {
money = amount;
if (Server.zombie.Running) Server.zombie.PlayerMoneyChanged(this);
if (Server.lava.active) Server.lava.PlayerMoneyChanged(this);
OnMoneyChangedEvent.Call(this);
}
public void TntAtATime() {

View File

@ -0,0 +1,44 @@
/*
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 MCGalaxy.Eco;
namespace MCGalaxy.Events {
/// <summary> Raised whenever a player's online money changes. </summary>
public sealed class OnMoneyChangedEvent : IPluginEvent<Economy.OnMoneyChanged> {
internal OnMoneyChangedEvent(Economy.OnMoneyChanged method, Priority priority, Plugin plugin)
: base(method, priority, plugin) { }
public static void Call(Player p) {
if (handlers.Count == 0) return;
CallImpl(pl => pl(p));
}
}
/// <summary> Raised whenever an economic transaction occurs. </summary>
public sealed class OnEcoTransactionEvent : IPluginEvent<Economy.OnEcoTransaction> {
internal OnEcoTransactionEvent(Economy.OnEcoTransaction method, Priority priority, Plugin plugin)
: base(method, priority, plugin) { }
public static void Call(EcoTransaction transaction) {
if (handlers.Count == 0) return;
CallImpl(pl => pl(transaction));
}
}
}