generated from TShockResources/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,351 additions
and
134 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.