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.pltemp.func) {
  
processPlayer(temp.pltemp.functrue);
}

function 
processPlayer(temp.pltemp.functemp.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.pltemp.op));
    return;
  }
  
temp.pobj temp.onhd ? new TServerPlayer(@temp.pl) : temp.pobj;
  
with (temp.pobj) {
    if (
player.onlinetime 0) { 
      
temp.save = (@temp.func)(playertemp.op);
    } else {
      if (
temp.op) {
        echo(
format("Unable to find player %s, %s"temp.pltemp.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.pltemp.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.optemp.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.= 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.pltemp.op) {
  echo(
player.account);
  echo(
temp.pl.onlinetime);




All times are GMT +2. The time now is 07:28 PM.

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