View Single Post
  #1  
Old 08-07-2009, 01:48 AM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Staff Announcements/Messages

This is a tool to send staff members either global or private messages, that will be auto-PM'ed within 30 seconds when they logon RC, or straight away if they're already on RC.

Feedback appreciated!


Configuration:

You need to add the following code to your Control-NPC:

PHP Code:
function onRCChat(command)
{
  switch (
command) {
    case 
"staffmessage":
      
// --- Strings/Variables ---------------------------
      
temp.reciever = params[1];  // Reciever(s) of message
                                  // account, or 'all'
      
temp.message  = params[2];  // Message
      
temp.title    = params[3];  // Title of message
      // -------------------------------------------------
      
      // Did we recieve the correct parameters?
      
if (params.size() != 4) {
        echo(
"[" @ name @ "]: Incorrect amount of parameters submitted for \"staffmessage\"");
        break;
      }
      
      
RCTracker.addMessage(reciever == "all" ? "_all_" : reciever, message, title);
    break;
  }
} 
And make a new database NPC containing this code:

PHP Code:
function onInitialized()
  
onCreated();

function 
onCreated()
{
  
// --- Strings/Variables ---------------------------
  
enum {
    
// SQLite table
    
TABLE = "rc_tracker",
    
// PM header
    
HEADER = "=== Maloria Staff Announcement ===\n" @
             
"This is an automated message relay system.\n" @
             
"You have new message(s)!\n" @
             
"=====================\n",
    
// PM footer
    
FOOTER = "------------------------"
  
};
  
temp.online_rcs = "";  // Currently online RC's
  // -------------------------------------------------
  
  
join("functions_sql");
  
  
// Is the SQLite database up-to-date?
  
initializeDatabase();
  
onTimeOut();
}

function 
onTimeOut()
{
  
// --- Strings/Variables ---------------------------
  
temp.online_rcs = "";  // Currently online RC's
  // -------------------------------------------------
  
  
online_rcs = findRCs();
  
// Is there any new messages for anyone?
  
checkForUpdates(online_rcs);
  
setTimer(30);
}

function 
initializeDatabase()
{ 
  
// --- Strings/Variables ---------------------------
  
temp.query = "";  // SQLite query
  // -------------------------------------------------
  
  // Does the table exist?
  
query = getSQL("SELECT * FROM" SPC TABLE);
  if (
query.rows.size() == 0) {
    
// Creation of table
    
echo("[" @ name @ "] Creating SQLite table \"" @ TABLE @ "\"...");
    
getSQL("CREATE TABLE" SPC TABLE SPC "(rowid INTEGER PRIMARY KEY AUTOINCREMENT, account VARCHAR NOT NULL, message VARCHAR, title VARCHAR, sender VARCHAR, time_creation INTEGER, time_read INTEGER DEFAULT 0, readCount INTEGER DEFAULT 0, receivers VARCHAR)");
  }
}

public function 
findRCs(account)
{
  
// --- Params --------------------------------------
  // account (optional) - returns RC object if online,
  //                      false if not
  // -------------------------------------------------
  // --- Strings/Variables ---------------------------
  
temp.pl = "";       // Current player object
  
temp.rcs = new[0];  // List of online RC's
  // -------------------------------------------------
  
  
for (pl: allplayers) {
    if (
pl.level.name = "") {
      
// Checking for submitted 'account'...
      
if (account != "" && pl.account == account)
        return 
pl;
      
// No 'account' submitted, let's return all RC's...
      
else
        
rcs.add(pl.account);
    }
  }
  
  
// 'account' not online!
  
if (account != "")
    return 
false;
  
// list of RC's
  
else
    return 
rcs;
}

function 
checkForUpdates(accounts)
{
  
// --- Params --------------------------------------
  // accounts   - account(s) to check for new messages
  //              for
  // -------------------------------------------------
  // --- Strings/Variables ---------------------------
  
temp.account         = "";  // Current processed account
  
temp.query           = "";  // Current processed SQLite query
  
temp.row             = "";  // Current processed query row
  
temp.unread_messages = "";  // Number of unread messages
  
temp.headerSent      = "";  // Checking if header has been sent or not
  
temp.receivers       = "";  // Temporary modified 'receivers'
  // -------------------------------------------------
  
  // Let's transform 'accounts' to an array if it's only one
  // account.
  
if (accounts.size() == 0)
    
accounts = { accounts };  
  
  for (
account: accounts) {     
    
query = getSQL("SELECT * FROM" SPC TABLE SPC "WHERE account='_all_'");
    
    
// Do we need to fetch a 'global' message?
    
for (row: query.rows) {
      
// Have 'account' fetched this one?
      
if (account in row.receivers)
        continue;
      
      
// Send the message to 'account'
      
getSQL("INSERT INTO" SPC TABLE SPC "(account, message, title, sender, time_creation) VALUES ('" @ account @ "', '" @ row.message.escape() @ "', '" @ row.title.escape() @ "', '" @ row.sender @ "', " @ int(timevar2) @ ")");
      
receivers = row.receivers;
      
receivers.add(account);
      
// Update receivers of this message
      
getSQL("UPDATE" SPC TABLE SPC "SET receivers='" @ receivers @ "' WHERE rowid=" @ row.rowid);
    }
    
    
// How many unread messages does 'account' have?
    
query = getSQL("SELECT * FROM" SPC TABLE SPC "WHERE account='" @ account @ "' AND time_read=0");
    
unread_messages = query.rows.size();
    
// Let's query messages for 'account'!
    
query = getSQL("SELECT * FROM" SPC TABLE SPC "WHERE account='" @ account @ "'");
    
//echo("Number of messages for" SPC account @ ":" SPC query.rows.size() SPC "(" @ unread_messages SPC "new)");
    
    // Change layout depending on the above result
    
for (row: query.rows) {
      
// Is this message already read?
      
if (row.readCount >= 1)
        continue;
      
      
// Do we need to send one, or several messages?
      
if (unread_messages > 1) {
        
// Send the header once...
        
if (!headerSent) {
          
findRCs(account).sendPM(HEADER);
          
headerSent = true;
        }
        
// And then the messages!
        
sendMessage(row.rowid, false, true);
      }
      else
        
sendMessage(row.rowid, true);
    }
  }
}

public function 
addMessage(account, txt, title)
{
  
// --- Params --------------------------------------
  // account    - account that will retrieve the message
  // txt        - message
  // title      - title of message
  // -------------------------------------------------
  // --- Strings/Variables ---------------------------
  
temp.query      = "";  // SQLite query
  
temp.row        = "";  // current query row
  
temp.sender     = "";  // Account of person who sent message
  
temp.new_index  = "";  // Next column index
  
temp.online_rcs = "";  // Currently online RC's
  // -------------------------------------------------
  
  // SQLite query
  
query = getSQL("SELECT * FROM" SPC TABLE);
  
// Next column index would be...
  
new_index = query.rows[query.rows.size()-1].rowid + 1;
  
  
  
// Who sent this message?
  
if (player.account == "")
    
sender = "(npcserver)";
  else
    
sender = player.account;
  
  
// Insert message into SQLite table
  
getSQL("INSERT INTO" SPC TABLE SPC "(account, message, title, sender, time_creation) VALUES ('" @ account @ "', '" @ txt.escape() @ "', '" @ title.escape() @ "', '" @ sender @ "', " @ int(timevar2) @ ")");
  
  
// Is the recipient online?
  
online_rcs = findRCs();
  if (
account in online_rcs)
    
sendMessage(new_index, true);
  else if (
account == "_all_")
    
checkForUpdates(online_rcs);
}

function 
sendMessage(msg_index, header, footer)
{
  
// --- Params --------------------------------------
  // msg_index  - what rowid will we query?
  // header     - send header? true/false
  // footer     - send footer? true/false
  // -------------------------------------------------
  // --- Strings/Variables ---------------------------
  
temp.query   = "";  // SQLite query
  
temp.account = "";  // RC object
  // -------------------------------------------------
  
  
  
query = getSQL("SELECT * FROM" SPC TABLE SPC "WHERE rowid=" @ msg_index);
  
temp.account = findRCs(query.account);
  
  
// Abort if recipient is offline
  
if (account == false)
    return echo(
"[" @ name @ "]: PM aborted," SPC query.account SPC "not online!");
  
  
// Header
  
if (header)
    
account.sendPM(HEADER);
  
// Message
  
account.sendPM("Sender:" SPC query.sender @ "\nTitle:" SPC query.title @ "\nMessage:\n" @ query.message);
  
// Footer
  
if (footer)
    
account.sendPM(FOOTER);
  
  
// Mark message as read
  
getSQL("UPDATE" SPC TABLE SPC "SET time_read=" @ int(timevar2) @ ", readCount=" @ query.readCount + 1 SPC "WHERE rowid=" @ query.rowid);
} 
And finally, you need to create a class called functions_sql and add this function to it:

PHP Code:
function getSQL(query)
{
  
temp.req = requestSQL(query, true);
  if (!
temp.req.completed)//&& !waitfor(temp.req, "onReceiveData", 1))
    
return NULL;
  
  if (
temp.req.error != "")
    echo(
temp.req.error);
  
  return 
temp.req;
} 
Usage:

PHP Code:
/npc sendmessage account message title 
PHP Code:
By RC chat: /npc sendmessage xXziroXx "You need to start working on this, and later on that.\n\nAnd dont you dare slacking!" "Work Assignment"

By script: addMessage("xXziroXx", "You need to start working on this, and later on that.\n\nAnd dont you dare slacking!" "Work Assignment"); 
Instead of providing an account, you can simply type "all" and it will be sent to all the staff members.
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote