#5 - Users can be given CP over the MUD

Currently not usable in the UI, but will be used to power shops.
This commit is contained in:
Michael 2017-01-14 10:28:57 -05:00
parent 9aab77b2f1
commit ca7062c3e6
4 changed files with 84 additions and 0 deletions

View file

@ -65,6 +65,7 @@
<Compile Include="Save.cs" />
<Compile Include="ShiftFS.cs" />
<Compile Include="ShiftOSMenuRenderer.cs" />
<Compile Include="Shop.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

43
ShiftOS.Objects/Shop.cs Normal file
View file

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Objects
{
public class Shop
{
public string Name { get; set; }
public string Description { get; set; }
public List<ShopItem> Items { get; set; }
}
public abstract class ShopItem
{
public string Name { get; set; }
public string Description { get; set; }
public int Cost { get; set; }
public string ShopOwner { get; set; }
protected abstract void OnBuy();
protected abstract void GiveCPToShopOwner(int cp);
public bool Buy(ref Save buyer)
{
if(buyer.Codepoints >= Cost)
{
buyer.Codepoints -= Cost;
OnBuy();
GiveCPToShopOwner(Cost);
return true;
}
else
{
return false;
}
}
}
}

View file

@ -281,6 +281,36 @@ Contents:
switch (msg.Name)
{
case "usr_givecp":
if (args["username"] != null && args["amount"] != null)
{
string userName = args["username"] as string;
int amount = (int)args["amount"];
if (Directory.Exists("saves"))
{
foreach(var saveFile in Directory.GetFiles("saves"))
{
var saveFileContents = JsonConvert.DeserializeObject<Save>(File.ReadAllText(saveFile));
if(saveFileContents.Username == userName)
{
saveFileContents.Codepoints += amount;
File.WriteAllText(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
server.DispatchAll(new NetObject("pikachu_use_thunderbolt_oh_yeah_and_if_you_happen_to_be_doing_backend_and_see_this_post_a_picture_of_ash_ketchum_from_the_unova_series_in_the_discord_dev_room_holy_crap_this_is_a_long_snake_case_thing_about_ash_ketchum_and_pikachu", new ServerMessage
{
Name = "update_your_cp",
GUID = "server",
Contents = $@"{{
username: ""{userName}"",
amount: {amount}
}}"
}));
return;
}
}
}
}
break;
case "mud_login":
if (args["username"] != null && args["password"] != null)
{

View file

@ -126,6 +126,16 @@ namespace ShiftOS.Engine
thisGuid = new Guid(msg.Contents);
GUIDReceived?.Invoke(msg.Contents);
}
else if(msg.Name == "update_your_cp")
{
var args = JsonConvert.DeserializeObject<Dictionary<string, object>>(msg.Contents);
if(args["username"] as string == SaveSystem.CurrentSave.Username)
{
SaveSystem.CurrentSave.Codepoints += (int)args["amount"];
Infobox.Show($"MUD Control Centre", $"Someone bought an item in your shop, and they have paid {args["amount"]}, and as such, you have been granted these Codepoints.");
SaveSystem.SaveGame();
}
}
else if(msg.Name =="broadcast")
{
Console.WriteLine(msg.Contents);