Pserver auto broadcast news from database
So I made this little thing to read the news from the database and them auto-broadcast them
Tested on trappeds c# server
Then at wServer / Program.cs
At the top add
Then add this method where it should go.
Then find line
And add
Then open db / Database.cs and add
And its done
Tested on trappeds c# server
Then at wServer / Program.cs
At the top add
Code:
using db;
Code:
private static void autoBroadcast()
{
List<string> broadcast = new List<string>();
Random select = new Random();
using (var db = new Database())
{
var cmd = db.readAutoNews();
while (true)
{
foreach (var title in cmd)
{
broadcast.Add(title.Title);
}
int r = select.Next(broadcast.Count);
foreach (var i in RealmManager.Clients.Values.ToArray())
{
i.SendPacket(new TextPacket()
{
Name = "@Info",
Stars = -1,
BubbleTime = 0,
Recipient = "",
Text = (string)broadcast[r],
CleanText = (string)broadcast[r]
});
}
//TextHandler.WriteInfo("Sending broadcast : " + (string)broadcast[r]);
Thread.Sleep(42000);
}
}
}
Code:
HostPolicyServer();
Code:
new Thread(autoBroadcast).Start();
Code:
public List<NewsItem> readAutoNews()
{
using (Database dbx = new Database())
{
var cmd = dbx.CreateQuery();
cmd.CommandText = "SELECT icon, title, text, link, date FROM news ORDER BY date LIMIT 10;";
List<NewsItem> b = new List<NewsItem>();
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
b.Add(new NewsItem()
{
Icon = rdr.GetString("icon"),
Title = rdr.GetString("title"),
TagLine = rdr.GetString("text"),
Link = rdr.GetString("link"),
Date = DateTimeToUnixTimestamp(rdr.GetDateTime("date"))
});
}
return b;
}
}
}
