View Single Post
  #25  
Old 03-11-2012, 09:10 PM
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
I am still very opposed to storing actual item data on clients, one way or the other. Fills up attributes with useless garbage that only have to be sent to clients once per session and stored in a weapon NPC.

I don't know if reading this script will help you at all, since it's not commented, but this is exactly how temporary data like descriptions etc. is handled on Maloria.

PHP Code:
function onCreated()
{
  
join("functions_sql");
  
requestSQL("PRAGMA synchronous=OFF", true);
}

function 
onActionServerSide(action)
{
  if (
action == "query") {
    
temp.cache = params[1];
    
temp.identifier = params[2];
    
temp.querier = params[3];
    
    
queryToClient(temp.cache, temp.identifier, temp.querier);
  }
}

public function 
queryToClient(temp.cache, temp.identifier, temp.querier) {
  
temp.object = query(temp.cache, temp.identifier);
  
temp.object = temp.object.saveVarsToArray(false);
  
  
triggerClient("gui", name, "queryReturn", temp.cache, temp.identifier, temp.object, temp.querier);
}

public function 
query(category, identifier)
{
  if (
category in { "items", "skills", "spells", "buffs", "quests", "baddies", "nations", "enchants" }) {
    
temp.tableName = category;
    
    
temp.queryString = "SELECT * FROM" SPC temp.tableName SPC "WHERE identifier='" @ identifier @ "'";
    
temp.query = getSQL(temp.queryString);
    if (
temp.query.rows[0].size() == 0) {
      return;
      echo(
"Query for '" @ identifier @ "' failed (" @ category SPC "@" SPC player.account @ "), testing callstack:");
      
temp.callstack = getCallStack();
    
      for (
temp.i = temp.callstack.size() - 1; temp.i >= 0; temp.i --)
        echo(
"call stack entry " @ temp.i @ ": " @ temp.callstack[temp.i].scriptcallobject.name @ "." @ temp.callstack[temp.i].name);
      
//echo("[" @ name @ "]: Query on '" @ identifier @ "' failed (" @ category SPC "@" SPC player.account @ ")");
      
return false;
    }
    
    
temp.object.copyFrom(temp.query.rows[0]);
    if (
category == "nations") {
      
temp.object.ranks.loadVarsFromArray(temp.object.ranks);
      
temp.object.ranks = "";
    }
    return 
temp.object;
  }
  
  else {
    if (
category == null)
      echo(
name @ ".getObject - parameter category is null");
    else
      echo(
name @ ".getObject - " @ category @ " is not a valid category.");
  }
}

public function 
getTimestamp(table, identifier)
{
  
temp.query = getSQL("SELECT * FROM" SPC table SPC "WHERE identifier='" @ identifier @ "'");
  
  if (
temp.query.rows.size() == 0)
    return 
false; //return echo("[" @ name @ "]: Query on '" @ identifier @ "' failed");
  
else
    return 
temp.query.timestamp;
}

//#CLIENTSIDE
function onCreated()
{
  
this.queryList = new TStaticVar();
  const 
QUERYTIMELIMIT = 5;
  
  
setTimer(0.05);
}

function 
onActionClientside(action)
{
  if (
action == "queryReturn") {
    
temp.cache = params[1];
    
temp.identifier = params[2];
    
temp.serializedObject = params[3];
    
temp.querier = params[4];
    
    
temp.object = new TStaticVar();
    
temp.object.loadVarsFromArray(temp.serializedObject);
    
updateCacheEntry(temp.cache, temp.identifier, temp.object, temp.querier);
    
    
updatePlayerVariables();
    
    
this.(@"query_" @ querier @ "_list").remove(temp.identifier);
    
temp.querier = this.(@"query_" @ temp.querier);
    
    
//echo("Debug Query: (" @ temp.identifier @ ") (" @ temp.querier @ ")");
    
if (isObject(temp.querier)) {
      
//echo("yes");
      
temp.querier.onReceivedCacheObject(temp.identifier, temp.cache);
    }
  }
}

public function 
query(cache, identifier, querier)
{
  if (
cacheEntryExist(cache, identifier))
    return 
getCacheEntry(cache, identifier);
  
  
//echo("Query(" @ cache @ "," SPC identifier @ ")...");
  
if (identifier in this.(@"query_" @ querier @ "_list"))
    return;
  
  
this.(@"query_" @ querier) = querier;
  
this.(@"query_" @ querier @ "_list").add(identifier);
  
  
triggerServer("gui", name, "query", cache, identifier, querier);
}

public function 
updatePlayerVariables()
{
  
this.playerItemNames = player.getItemNames();
  
this.playerItemIdentifiers = player.getItemIdentifiers();
}

public function 
getCacheSize(cache)
  return 
cacheExist(cache) ? this.(@"cache_" @ cache).getDynamicVarNames().size() : -1;

public function 
getCache(cache)
  return 
cacheExist(cache) ? this.(@"cache_" @ cache).getDynamicVarNames() : -1;

public function 
cacheExist(cache)
  return (
this.(@"cache_" @ cache).getDynamicVarNames().size() > 0);
  
  
//return this.(@"cache_" @ cache).getDynamicVarNames().size() > 0 ? true : false;

public function getCacheEntrySize(cache, identifier)
  return 
cacheEntryExist(cache, identifier) ? this.(@"cache_" @ cache).(@identifier).getDynamicVarNames().size() : -1;

public function 
getCacheEntry(cache, identifier)
  return 
cacheEntryExist(cache, identifier) ? this.(@"cache_" @ cache).(@identifier) : -1;

public function 
cacheEntryExist(cache, identifier)
  return (
this.(@"cache_" @ cache).(@identifier).objecttype() == "TStaticVar");

  
//return this.(@"cache_" @ cache).(@identifier).objecttype() == "TStaticVar" ? true : false;

function updateCacheEntry(cache, identifier, data)
{
  
this.(@"cache_" @ cache).(@identifier) = new TStaticVar();
  
this.(@"cache_" @ cache).(@identifier).copyFrom(data);
  
this.trigger("onCacheUpdated_" @ cache @ "_" @ identifier, "");
} 
__________________
Follow my work on social media post-Graal:Updated august 2025.
Reply With Quote