Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Offline Player Manipulation II (https://forums.graalonline.com/forums/showthread.php?t=134268585)

fowlplay4 08-04-2013 02:56 AM

Offline Player Manipulation II
 
Here's a neat function I wrote to help reduce the amount of duplication involved with determining if a player is online and if not load their account, and if they don't exist bail on them. It also properly destroys loaded from HDD accounts.

Basically it's like doing:

PHP Code:

with (findplayer("account")) { // except it will load the player if they're offline
  
if (doesntexist) return;
  
// your code
} 

The main functions (currently tweaked for use in onRCChat()):

PHP Code:

// Shortcut for processing that doesn't need to be done to offline accounts
function processOnlinePlayer(temp.pl, temp.func) {
  
processPlayer(temp.pl, temp.func, true);
}

function 
processPlayer(temp.pl, temp.func, temp.nohd) {
  
temp.op   = player.communityname ? player.communityname : player.account;
  
temp.pobj = findplayer(temp.pl);
  
temp.onhd = temp.pobj == NULL;
  if (
temp.nohd && temp.onhd) {
    echo(
format("Unable to find player %s, %s", temp.pl, temp.op));
    return;
  }
  
temp.pobj = temp.onhd ? new TServerPlayer(@temp.pl) : temp.pobj;
  
with (temp.pobj) {
    if (
player.onlinetime > 0) { 
      
temp.save = (@temp.func)(player, temp.op);
    } else {
      if (
temp.op) {
        echo(
format("Unable to find player %s, %s", temp.pl, temp.op));
      }
    }
  }
  if (
temp.onhd) {
    if (
temp.save) {
      
temp.pobj.saveaccount();
    }
    
temp.pobj.destroy();
  }
} 

Examples:

PHP Code:

function onCreated() {
  
temp.func = function (temp.pl) {
    echo(
player.account);
    echo(
temp.pl.onlinetime);
    return 
false;
  };
  
processPlayer("fowlplay4", temp.func);
}

// Echos
// fowlplay4
// 8727432 

PHP Code:

function onCreated() {
  
// It passes two parameters to the function:
  // The player object
  // The original player's account (I.e. The player.account when called in onRCChat() first instance)
  
temp.func = function (temp.pl, temp.op) {
    
temp.pl.head = "head0.png";
    
temp.pl.attr[1] = "";
    
temp.pl.body = "body1.png";
    if (
temp.op) echo(format("%s has reset %s's look!", temp.op, temp.pl.account));
    return 
true; // Necessary to save offline players.
  
};
  
processPlayer("fowlplay4", temp.func);
} 

You can even nest it in the function call too:

PHP Code:

function onCreated() {
  
processPlayer("fowlplay4", temp.f = function (temp.pl) {
    echo(
player.account);
    echo(
temp.pl.onlinetime);
  });
}

// Echos
// fowlplay4
// 8727432 

Or use defined functions:

PHP Code:

function onCreated() {
  
processPlayer("fowlplay4", this.test);
}

function 
test(temp.pl, temp.op) {
  echo(
player.account);
  echo(
temp.pl.onlinetime);
} 



All times are GMT +2. The time now is 04:41 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.