Pserver auto broadcast news from database

Posts 1–2 of 2 · Page 1 of 1
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

Code:
using db;
Then add this method where it should go.

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);
                }
            }
        }
Then find line

Code:
HostPolicyServer();
And add

Code:
new Thread(autoBroadcast).Start();
Then open db / Database.cs and add

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;
                }
            }
        }
And its done
Quote Originally Posted by alvaritos View Post
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);
}
}
}
I get this error:
The type or namespace name 'TextPacket' could not be found (are you missing a using directive or an assembly reference?)

What's wrong with it?
Posts 1–2 of 2 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us