View Single Post
  #1  
Old 06-10-2009, 04:28 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
SQL Item control serious lag!

HTML Code:
  //Perform a function
public function getSQL(query) {
  temp.req = requestSQL(temp.query, true);
  
  if (!temp.req.completed) return null;
  if (temp.req.error != "") echo(temp.req.error);
  
  return temp.req;
}
  
  //EntryID/Account/ItemID
public function onItem(id, quantity, itemdata) {   
    //The players amount
  temp.rquantity = _getitem(temp.id, "quantity");
  
    //If the player has the item just modify the amount
  if (temp.rquantity != null) {
    temp.rquantity = temp.rquantity + temp.quantity;
    _setitem(temp.id, "quantity", temp.rquantity);
      //Update the item clientside
    _senditemtrigger(temp.id, {"quantity", temp.rquantity}, "itemflag");
  } else {
    
      //If not, re-load the variables & add to player
    temp.itemdata = this.onItemData(temp.id);
    
      //Report of possible glitch- item doesn't exist
    if (temp.itemdata == null) {
    
        //Clear the items data...
      _clearitem(temp.id);
      
        //Alert RC  
      echo("Invalid Item:" SPC temp.id);
      echo("Invalid Item: Item doesn't exist- possible hack?");
      echo("Invalid Item:" SPC this SPC this.level.name SPC this.x SPC this.y);
      return false;
    }
    
    temp.data = new TStaticVar();
      //Load the item data
    temp.data.loadvarsfromarray(temp.itemdata);
    for (temp.i: temp.data.getdynamicvarnames()) {
        //Set the item data
      _setitem(temp.id, temp.i[0], temp.i[1]);
    }
    
      //Now add the weapon to the player
    this.addWeapon(_get(temp.id, "useweapon")[0]);
    
      //Now set the quantity of the item
    _setitem(temp.id, "quantity", temp.quantity);
    
      //Now, let's add this item clientside
    temp.data = temp.data.savevarstoarray(false);
    _senditemtrigger(temp.id, temp.data);    
  }
}

  //Sends the item clientside
function _senditemtrigger(id, itemdata, mode) {
    //Let's set the method of the item
  temp.method = "ItemNew";
    //If the mode does exist, change method
  if (temp.mode == "itemflag") {
      //Let's just change an item flag not a full item
    temp.method = "ItemFlag"; 
  }

  this.triggerclient("weapon", "-Player", "UpdateVariable", temp.method, temp.id, temp.itemdata, temp.mode);
}

  //Adding a flag to an item
public function _setitem(id, flag, value) {  
  player.items.(@ temp.id).(@ temp.flag) = temp.value;
}

  //Returning a flag from an item
public function _getitem(id, flag) { 
  temp.value = player.items.(@ temp.id).(@ temp.flag);
  if (temp.flag in {"sprite", "icon"}) {
    temp.value @= ".png";
  }
  
  return temp.value;
}

  //Deleting an item from a player
public function _clearitem(id) {  
    //First clear the item data
  player.items.(@ temp.id) = "";
  player.items.(@ temp.id).clearvars();
  player.items.clearemptyvars();
}

  //Get the item data from the server
public function onItemData(id) {
  return ("Control-Items").getItemData(temp.id);
}

  //Loading the players items from the SQL database
public function onLoadItems(toreturn) {
    //Load the players items from the SQL
  temp.query = this.getSQL("SELECT * FROM items WHERE account = '" @ this.account @ "'");

    //Create an array containing all the data
  temp.charitems = new TStaticVar();
  for (temp.row: temp.query.rows) {
      //Now, let's add this item clientside
    _senditemtrigger(temp.row.item_id, temp.row.item_data, "SQL");
      //and also make a list of the items the player has
    temp.charitems.items.(@ temp.row.item_id).loadvarsfromarray(temp.row.item_data);
  }
  
    //Return the item data   (temp.items.getdynamicvarnames)
  if (temp.toreturn) {
    return temp.charitems.items;
  }

    //Clear the players items variable
    //and then load it.
  player.items = new TStaticVar();
  player.items.copyFrom(temp.charitems.items);  
  this.onLoadWeapons();
}

  //Save the players items to the SQL
public function onSaveItems() {
    //Delete the items that the player has in the SQL
  this.getSQL("DELETE FROM items WHERE account = '" @ this.account @ "'");
  
    //Load the players items
  for (temp.i: this.onGetItemList()) {
      //Reset the item data
    temp.itemdata = new TStaticVar();
      //Load the vars and save to an array
    temp.itemdata = player.items.(@ temp.i).savevarstoarray(false);
    
      //If the flags don't exist, ignore the item
    if (temp.itemdata.size() == 0) continue;

      //And save it to the SQL
    this.getSQL("INSERT INTO items (account, item_id, item_data) VALUES ('" @ this.account @ "', '" @ temp.i @ "','" @ temp.itemdata @ "')");
  }
}

  //Returns the item list
public function onGetItemList() {
  return player.items.getdynamicvarnames();
}

  //Adds all the weapons from the items
public function onLoadWeapons() {
    //Load the items list
  for (temp.i: this.onGetItemList()) {
      //Now add the weapon to the player
    this.addWeapon(_getitem(temp.i, "useweapon").tokenize()[0]);
  }
}
Following the simple table

HTML Code:
Entry ID, Account, Item ID, Item Data
Well, right now it's loading 250< items from my character on login. That's not alot- right? Wrong
Here's the output
HTML Code:
1. 1.458490023 - Control-NPC (functions_projectiles)
2. 0.163530085 - Graal707396 (player_functions_login,player_functions_items,player_functions_health)
It's taking a while to load the items, can anyone think of a better solution?

It's also causing serious lag when sending all the item data clientside. Does anyone else have these kind of problems?

It's only happening when sending quite alot of data to the clientside

Here's a few examples of the item data

HTML Code:
  this.item_Overalls_Aqua = {
    {"id", "Overalls_Aqua"},
    {"category", "coat"},
    {"gender", "male"},
    {"icon", "bi_ico_coat_a_m_004-aqua.png"},
    {"iname", "Overalls (Aqua)"},
    {"iname_m", "Overalls (Aqua)"},
    {"info", "A aqua heavy duty pants that are worn over a tunic. Often adored by fat plumbers."},
    {"info_m", "Some aqua heavy duty pants that are worn over a tunic. Often adored by fat plumbers."},
    {"options", "wd"},
    {"sprite", "bi_coat_a_m_004-aqua.png"},
    {"useweapon", {"ControlDress", "coats", "sprite"}}
  };

  this.item_Overalls_Black = {
    {"id", "Overalls_Black"},
    {"category", "coat"},
    {"gender", "male"},
    {"icon", "bi_ico_coat_a_m_004-black.png"},
    {"iname", "Overalls (Black)"},
    {"iname_m", "Overalls (Black)"},
    {"info", "A black heavy duty pants that are worn over a tunic. Often adored by fat plumbers."},
    {"info_m", "Some black heavy duty pants that are worn over a tunic. Often adored by fat plumbers."},
    {"options", "wd"},
    {"sprite", "bi_coat_a_m_004-black.png"},
    {"useweapon", {"ControlDress", "coats", "sprite"}}
  };

Last edited by [email protected]; 06-10-2009 at 04:59 PM..
Reply With Quote