Skip to content

Commit

Permalink
Midspaces Milestone Version
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcsupplies committed Sep 12, 2015
1 parent 8c96add commit 15cc91a
Show file tree
Hide file tree
Showing 22 changed files with 1,422 additions and 402 deletions.
12 changes: 0 additions & 12 deletions Economy/Data/Scripts/Economy.scripts/BankConfig.cs

This file was deleted.

16 changes: 7 additions & 9 deletions Economy/Data/Scripts/Economy.scripts/BankManagement.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Sandbox.Common.ObjectBuilders;
using Sandbox.ModAPI;

namespace Economy
namespace Economy.scripts
{
using System.Collections.Generic;
using System.IO;
using Economy.scripts.Config;
using Sandbox.ModAPI;

public class BankManagement
{
public static string GetContentFilename()
Expand Down Expand Up @@ -38,6 +35,7 @@ public static BankConfig LoadContent()
catch
{
// content failed to deserialize.
EconomyScript.Instance.ServerLogger.Write("Failed to deserialize BankConfig. Creating new BankConfig.");
config = InitContent();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Economy
namespace Economy.scripts.Config
{
using System;

public class BankAccountStruct
{
public ulong SteamId { get; set; }
Expand Down
92 changes: 92 additions & 0 deletions Economy/Data/Scripts/Economy.scripts/Config/BankConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace Economy.scripts.Config
{
using System;
using System.Collections.Generic;
using System.Linq;
using Sandbox.ModAPI;

public class BankConfig
{
public List<BankAccountStruct> Accounts;

#region Generic helpers

/// <summary>
/// Load the relevant bank balance data - check player even has an account yet; make one if not
/// </summary>
/// <param name="steamId"></param>
/// <param name="nickName"></param>
/// <returns></returns>
public BankAccountStruct FindOrCreateAccount(ulong steamId, string nickName)
{
var account = Accounts.FirstOrDefault(a => a.SteamId == steamId);
if (account == null)
{
account = CreateNewDefaultAccount(steamId, nickName);
Accounts.Add(account);
}
return account;
}

/// <summary>
/// Find the account by nickName. Does not create a new account.
/// </summary>
/// <param name="nickName"></param>
/// <returns></returns>
public BankAccountStruct FindAccount(string nickName)
{
// Try for an exact match first.
var accounts = Accounts.Where(a => a.NickName.Equals(nickName, StringComparison.InvariantCultureIgnoreCase)).ToList();

// only return a result if there was 1 exact match.
if (accounts.Count == 1)
return accounts.FirstOrDefault();

// Try for a partial match if we didn't get any matches before.
if (accounts.Count == 0)
{
accounts = Accounts.Where(a => a.NickName.IndexOf(nickName, StringComparison.InvariantCultureIgnoreCase) > 0).ToList();

if (accounts.Count == 1)
return accounts.FirstOrDefault();
}

// Either too many players with the same name, or the name doesn't exist.
return null;
}

public BankAccountStruct CreateNewDefaultAccount(ulong steamId, string nickName)
{
return new BankAccountStruct() { BankBalance = 100, Date = DateTime.Now, NickName = nickName, SteamId = steamId };
}

public void ResetAccount(BankAccountStruct account)
{
account.BankBalance = 100;
account.Date = DateTime.Now;
}

public void UpdateLastSeen(ulong steamId)
{
var player = MyAPIGateway.Players.FindPlayerBySteamId(steamId);
if (player != null)
{
UpdateLastSeen(steamId, player.DisplayName);
}
}

public void UpdateLastSeen(ulong steamId, string nickName)
{
var account = EconomyScript.Instance.BankConfigData.Accounts.FirstOrDefault(
a => a.SteamId == steamId);

if (account != null)
{
account.NickName = nickName;
account.Date = DateTime.Now;
}
}

#endregion
}
}
Loading

0 comments on commit 15cc91a

Please sign in to comment.