Skip to content

Commit

Permalink
first push
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderBr committed Nov 13, 2022
1 parent 38bb568 commit 89641ad
Show file tree
Hide file tree
Showing 12 changed files with 3,351 additions and 134 deletions.
Binary file added .vs/SimpleEcon/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file added .vs/SimpleEcon/v17/.futdcache.v1
Binary file not shown.
66 changes: 0 additions & 66 deletions PluginTemplate/PluginTemplate.cs

This file was deleted.

67 changes: 0 additions & 67 deletions PluginTemplate/PluginTemplate.csproj

This file was deleted.

2 changes: 1 addition & 1 deletion PluginTemplate.sln → SimpleEcon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluginTemplate", "PluginTemplate\PluginTemplate.csproj", "{DCA3A85E-22A6-48E2-998D-343C2BE8402F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleEcon", "SimpleEcon\SimpleEcon.csproj", "{DCA3A85E-22A6-48E2-998D-343C2BE8402F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
48 changes: 48 additions & 0 deletions SimpleEcon/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TShockAPI;
using Newtonsoft.Json;

namespace SimpleEcon
{
public class Config
{
public string currencyNameSingular { get; set; } = "dollar";
public string currencyNamePlural { get; set; } = "dollars";


public bool giveRewardsForPlaytime { get; set; } = false;
public int rewardtimer { get; set; } = 5;

public void Write()
{
string path = Path.Combine(TShock.SavePath, "SimpleEcon.json");
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
}
public static Config Read()
{
string filepath = Path.Combine(TShock.SavePath, "SimpleEcon.json");
try
{
Config config = new Config();

if (!File.Exists(filepath))
{
File.WriteAllText(filepath, JsonConvert.SerializeObject(config, Formatting.Indented));
}
config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(filepath));


return config;
}
catch (Exception ex)
{
TShock.Log.ConsoleError(ex.ToString());
return new Config();
}
}

}
}
86 changes: 86 additions & 0 deletions SimpleEcon/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Data;
using System.Linq;
using MySql.Data.MySqlClient;
using TShockAPI.DB;
using SimpleEcon;
using System.Collections.Generic;

namespace SimpleEcon
{
public class Database
{
private readonly IDbConnection _db;

public Database(IDbConnection db)
{
_db = db;

var sqlCreator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());

var table = new SqlTable("SimpleEcon",
new SqlColumn("ID", MySqlDbType.Int32) { Primary = true, AutoIncrement = true },
new SqlColumn("Name", MySqlDbType.VarChar, 50) { Unique = true },
new SqlColumn("Balance", MySqlDbType.Float)
);
sqlCreator.EnsureTableStructure(table);
}

public bool InsertPlayer(EconPlayer player)
{
return _db.Query("INSERT INTO SimpleEcon (Name, Balance)" + "VALUES (@0, @1)", player.name, 0) != 0;
}

public bool DeletePlayer(string playerName)
{
return _db.Query("DELETE FROM SimpleEcon WHERE Name = @0", playerName) != 0;
}

public bool SavePlayer(EconPlayer player)
{
return _db.Query("UPDATE SimpleEcon SET Balance = @0 WHERE Name = @1",
player.balance, player.name) != 0;
}

public void SaveAllPlayers()
{
foreach (var player in SimpleEcon.econPlayers){
SavePlayer(player);

}
}

public bool RetrieveBalance(EconPlayer player)
{
using (var reader = _db.QueryReader("SELECT * FROM SimpleEcon WHERE Name = @0", player.name))
{
while (reader.Read())
{
var name = reader.Get<string>("Name");
var bal = reader.Get<float>("Balance");

PlayerManager.GetPlayer(name).balance = bal;
return true;
}
return false;
}
}
public List<Tuple<string, float>> RetrieveBalTop()
{
SaveAllPlayers();
List<Tuple<string, float>> p = new List<Tuple<string, float>>();

using (var reader = _db.QueryReader("SELECT * FROM SimpleEcon ORDER BY balance DESC"))
{
while (reader.Read() && p.Count != 10)
{
var name = reader.Get<string>("Name");
var bal = reader.Get<float>("Balance");

p.Add(new Tuple<string, float>(name, bal));
}
return p;
}
}
}
}
50 changes: 50 additions & 0 deletions SimpleEcon/EconPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;

namespace SimpleEcon
{
public class EconPlayer
{
public string name { get; set; }

public TSPlayer player { get; set; }

public float balance { get; set; }

public EconPlayer(string playerName, TSPlayer player)
{
this.name = playerName;
this.player = player;

}

}

public static class PlayerManager
{
public static EconPlayer GetPlayer(int playerId)
{
var name = TShock.Players[playerId].Name;

return SimpleEcon.econPlayers.Find(p => p.name == name);
}
public static EconPlayer GetPlayer(string name)
{
return SimpleEcon.econPlayers.Find(p => p.name == name);
}

public static void UpdatePlayerBalance(string name, float am)
{
var p = SimpleEcon.econPlayers.Find(p => p.name == name);
p.balance = am;
SimpleEcon.dbManager.SavePlayer(p);
return;
}


}
}
File renamed without changes.
Loading

0 comments on commit 89641ad

Please sign in to comment.