forked from Zaicon/Z-DieMob
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDatabase.cs
128 lines (116 loc) · 3.98 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
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using TShockAPI;
using TShockAPI.DB;
namespace DieMob
{
public static class Database
{
public static IDbConnection db;
public static List<DieMobRegion> DieMobRegions = new List<DieMobRegion>();
public static void Connect()
{
switch (TShock.Config.StorageType.ToLower())
{
case "mysql":
string[] dbHost = TShock.Config.MySqlHost.Split(':');
db = new MySqlConnection()
{
ConnectionString = string.Format("Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};",
dbHost[0],
dbHost.Length == 1 ? "3306" : dbHost[1],
TShock.Config.MySqlDbName,
TShock.Config.MySqlUsername,
TShock.Config.MySqlPassword)
};
break;
case "sqlite":
string sql = Path.Combine(TShock.SavePath, "DieMob.sqlite");
db = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
break;
}
SqlTableCreator sqlcreator = new SqlTableCreator(Database.db, Database.db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
sqlcreator.EnsureTableStructure(new SqlTable("DieMobRegions",
new SqlColumn("ID", MySqlDbType.Int32) { Primary = true, Unique = true, AutoIncrement = true, Length = 6 },
new SqlColumn("Region", MySqlDbType.VarChar) { Length = 30 },
new SqlColumn("WorldID", MySqlDbType.Int32),
new SqlColumn("AffectFriendlyNPCs", MySqlDbType.Int32),
new SqlColumn("AffectStatueSpawns", MySqlDbType.Int32),
new SqlColumn("ReplaceMobs", MySqlDbType.Text),
new SqlColumn("Type", MySqlDbType.Int32)));
}
public static void DieMob_Read()
{
QueryResult reader;
reader = db.QueryReader("SELECT * FROM DieMobRegions WHERE WorldID=@0", Main.worldID);
List<string> obsoleteRegions = new List<string>();
while (reader.Read())
{
var regionName = reader.Get<string>("Region");
var region = TShock.Regions.GetRegionByName(regionName);
if (region != null && region.Name != "")
{
DieMobRegions.Add(new DieMobRegion(region)
{
AffectFriendlyNPCs = reader.Get<bool>("AffectFriendlyNPCs"),
AffectStatueSpawns = reader.Get<bool>("AffectStatueSpawns"),
ReplaceMobs = JsonConvert.DeserializeObject<Dictionary<int, int>>(reader.Get<string>("ReplaceMobs")),
Type = (RegionType)reader.Get<int>("Type")
});
}
else
{
obsoleteRegions.Add(regionName);
}
}
reader.Dispose();
foreach (string region in obsoleteRegions)
{
Console.WriteLine("Deleting region from DB: " + region);
db.Query("DELETE FROM DieMobRegions WHERE Region=@0 AND WorldID=@1", region, Main.worldID);
}
}
public static bool DieMob_Add(string name)
{
db.Query("INSERT INTO DieMobRegions (Region, WorldID, AffectFriendlyNPCs, AffectStatueSpawns, Type, ReplaceMobs) VALUES (@0, @1, 0, 0, 0, @2)",
name, Main.worldID, JsonConvert.SerializeObject(new Dictionary<int, int>()));
return true;
}
public static void DieMob_Delete(String name)
{
db.Query("DELETE FROM DieMobRegions WHERE Region=@0 AND WorldID=@1", name, Main.worldID);
for (int i = 0; i < DieMobRegions.Count; i++)
{
if (DieMobRegions[i].TSRegion.Name == name)
{
DieMobRegions.RemoveAt(i);
break;
}
}
}
public static void Diemob_Update(DieMobRegion region)
{
db.Query("UPDATE DieMobRegions SET AffectFriendlyNPCs=@2, AffectStatueSpawns=@3, Type=@4, ReplaceMobs=@5 WHERE Region=@0 AND WorldID=@1",
region.TSRegion.Name, Main.worldID, region.AffectFriendlyNPCs, region.AffectStatueSpawns,
(int)region.Type, JsonConvert.SerializeObject(region.ReplaceMobs));
}
public static DieMobRegion GetRegionByName(string name)
{
foreach (DieMobRegion reg in Database.DieMobRegions)
{
if (reg.TSRegion.Name == name)
return reg;
}
return null;
}
}
}