Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
comdar16 authored Oct 4, 2021
1 parent 89753ea commit 1ad2b5f
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 0 deletions.
39 changes: 39 additions & 0 deletions OnlineAnnounceV2-master/OnlineAnnounceV2/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

namespace OnlineAnnounceV2
{
public class Config
{
public List<string> badwords = new List<string>()
{
"admin",
"mod",
"staff",
"owner"
};

public int defaultR = 127;
public int defaultG = 255;
public int defaultB = 212;

public void Write(string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
}

public static void Read(string path)
{
OAMain.config = !File.Exists(path)
? new Config()
: JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
}

public Color ToColor()
{
return new Color(defaultR, defaultG, defaultB);
}
}
}
99 changes: 99 additions & 0 deletions OnlineAnnounceV2-master/OnlineAnnounceV2/DB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;
using TShockAPI.DB;

namespace OnlineAnnounceV2
{
public static class DB
{
private static IDbConnection db;

public static void Connect()
{
switch (TShock.Config.Settings.StorageType.ToLower())
{
case "mysql":
string[] dbHost = TShock.Config.Settings.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.Settings.MySqlDbName,
TShock.Config.Settings.MySqlUsername,
TShock.Config.Settings.MySqlPassword)

};
break;

case "sqlite":
string sql = Path.Combine(TShock.SavePath, "OnlineAnnounce.sqlite");
db = new SqliteConnection(string.Format("uri=file://{0},Version=3", sql));
break;

}

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

sqlcreator.EnsureTableStructure(new SqlTable("onlineannounce",
new SqlColumn("userid", MySqlDbType.Int32) { Primary = true, Unique = true, Length = 6 },
new SqlColumn("greet", MySqlDbType.Text) { Length = 100 },
new SqlColumn("leaving", MySqlDbType.Text) { Length = 100 }));
}

public static void AddAnnouncement(OAInfo info)
{
int result = db.Query("INSERT INTO `onlineannounce` (`userid`, `greet`, `leaving`) VALUES (@0, @1, @2);", info.userid, info.greet, info.leave);
if (result != 1)
{
TShock.Log.ConsoleError("Error adding entry to database for user: " + info.userid);
}
}

public static void UpdateAnnouncement(OAInfo info)
{
int result = db.Query("UPDATE `onlineannounce` SET `greet` = @0, `leaving` = @1 WHERE `userid` = @2; ", info.greet, info.leave, info.userid);

if (result != 1)
{
TShock.Log.ConsoleError("Error updating entry in database for user: " + info.userid);
}
}

public static void DeleteAnnouncement(int userid)
{
int result = db.Query("DELETE FROM `onlineannounce` WHERE `userid` = @0;", userid);
if (result != 1)
{
TShock.Log.ConsoleError("Error deleting entry in database for user: " + userid);
}
}

public static string SetInfo(TSPlayer plr)
{
//Using null to signify that it was not in database
OAInfo newInfo = new OAInfo(plr.Account.ID, false, null, null);

using (var reader = db.QueryReader("SELECT * FROM `onlineannounce` WHERE `userid` = @0;", plr.Account.ID))
{
if (reader.Read())
{
newInfo.wasInDatabase = true;
newInfo.greet = reader.Get<string>("greet");
newInfo.leave = reader.Get<string>("leaving");
}
}

plr.SetData(OAMain.OAString, newInfo);
return newInfo.greet;
}
}
}
30 changes: 30 additions & 0 deletions OnlineAnnounceV2-master/OnlineAnnounceV2/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.RegularExpressions;
using TShockAPI;

namespace OnlineAnnounceV2
{
public static class Extensions
{
private static string ColorRegex = @"\[c\/\w{3,6}:\w+\]";

public static void StripColors(this string str)
{
str = Regex.Replace(str, ColorRegex, ReplaceString, RegexOptions.IgnoreCase);
}

public static string Specfier(this bool isSilent)
{
return isSilent ? TShock.Config.Settings.CommandSilentSpecifier : TShock.Config.Settings.CommandSpecifier;
}

public static string OAType(this bool isGreet)
{
return isGreet ? "greeting" : "leaving";
}

private static string ReplaceString(Match match)
{
return match.Value.Substring(match.Value.IndexOf(":") + 1).TrimEnd(']');
}
}
}
18 changes: 18 additions & 0 deletions OnlineAnnounceV2-master/OnlineAnnounceV2/OAInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace OnlineAnnounceV2
{
public class OAInfo
{
public int userid;
public string greet;
public string leave;
public bool wasInDatabase;

public OAInfo(int _userid, bool _inDatabase, string _greet = "", string _leave = "")
{
userid = _userid;
wasInDatabase = _inDatabase;
greet = _greet;
leave = _leave;
}
}
}
Loading

0 comments on commit 1ad2b5f

Please sign in to comment.