-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDatabase.cs
135 lines (125 loc) · 5.67 KB
/
Database.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using MySql.Data.MySqlClient;
using Rocket.Core.Logging;
using System;
namespace fr34kyn01535.Uconomy
{
public class DatabaseManager
{
internal DatabaseManager()
{
new I18N.West.CP1250(); //Workaround for database encoding issues with mono
CheckSchema();
}
private MySqlConnection createConnection()
{
MySqlConnection connection = null;
try
{
if (Uconomy.Instance.Configuration.Instance.DatabasePort == 0) Uconomy.Instance.Configuration.Instance.DatabasePort = 3306;
connection = new MySqlConnection(String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", Uconomy.Instance.Configuration.Instance.DatabaseAddress, Uconomy.Instance.Configuration.Instance.DatabaseName, Uconomy.Instance.Configuration.Instance.DatabaseUsername, Uconomy.Instance.Configuration.Instance.DatabasePassword, Uconomy.Instance.Configuration.Instance.DatabasePort));
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return connection;
}
/// <summary>
/// returns the current balance of an account
/// </summary>
/// <param name="steamId"></param>
/// <returns></returns>
public decimal GetBalance(string id)
{
decimal output = 0;
try
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "select `balance` from `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` where `steamId` = '" + id.ToString() + "';";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) Decimal.TryParse(result.ToString(), out output);
connection.Close();
Uconomy.Instance.OnBalanceChecked(id, output);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return output;
}
/// <summary>
/// Increasing balance to increaseBy (can be negative)
/// </summary>
/// <param name="steamId">steamid of the accountowner</param>
/// <param name="increaseBy">amount to change</param>
/// <returns>the new balance</returns>
public decimal IncreaseBalance(string id, decimal increaseBy)
{
decimal output = 0;
try
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "update `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` set `balance` = balance + (" + increaseBy + ") where `steamId` = '" + id.ToString() + "'; select `balance` from `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` where `steamId` = '" + id.ToString() + "'";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) Decimal.TryParse(result.ToString(), out output);
connection.Close();
Uconomy.Instance.BalanceUpdated(id, increaseBy);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return output;
}
public void CheckSetupAccount(Steamworks.CSteamID id)
{
try
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
int exists = 0;
command.CommandText = "SELECT EXISTS(SELECT 1 FROM `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` WHERE `steamId` ='" + id + "' LIMIT 1);";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) Int32.TryParse(result.ToString(), out exists);
connection.Close();
if (exists == 0)
{
command.CommandText = "insert ignore into `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` (balance,steamId,lastUpdated) values(" + Uconomy.Instance.Configuration.Instance.InitialBalance + ",'" + id.ToString() + "',now())";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
internal void CheckSchema()
{
try
{
MySqlConnection connection = createConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "show tables like '" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "'";
connection.Open();
object test = command.ExecuteScalar();
if (test == null)
{
command.CommandText = "CREATE TABLE `" + Uconomy.Instance.Configuration.Instance.DatabaseTableName + "` (`steamId` varchar(32) NOT NULL,`balance` decimal(15,2) NOT NULL DEFAULT '25.00',`lastUpdated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`steamId`)) ";
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
}
}