Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-29-2008, 10:43 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Scratch

Long time no post.

Well for my quest system I needed some sort of alternative for clientr. variables so I developed this System I call Scratch. Feel free to give it your own name if you decide to implement it on your server.

Basicly think of it as a Scratchpad for your variables, it keeps them in memory and allows you to manipulate them and when your player logs out the variables are stored back in their respective variable files and ready to be reloaded on login.

I'm sure many have a system similiar to this in their own scripts that allow variables to be saved to files but here's how I decided to do mine

Feel free to modify, and implement into your own servers/scripts. I haven't really tested it that in depth but it should meet a majority of your alternative clientr. needs.

Giving credit to me in the script is appreciated but not required, hope someone can find use for this

Possible Implementations:
- MUDLib, see example.
- Quest System

MUDLib Theory Example:
PHP Code:
function onActionPlayerOnline() {
  
// Load Scratch Data
  
Scratch.onLoadScratch("levels/mudlib/players/fowlplay4.txt""playerstats"true);
  
// Set Player Health to 10
  
Scratch.onSetScratch("playerstats""health"10);
}

function 
onPlayerLogout(acc) {
  
// Remove from Memory and Save
  
Scratch.onUnloadScratch(acc);
}

//#CLIENTSIDE

function onCreated() {
  
// As long as the variables were in sync before this ran
  // 10 would echo in your F2 Log
  
echo(Scratch.onGetScratch("playerstats""health"));

The Database NPC

PHP Code:
/*
   Main Idea: Alternative to clientr. values

   - Loads a Variable File into Memory
   - Allows Option to Sync Data with Client
   - Unloads and Saves on PlayerLogout
   
*/

function onCreated() {
  
// For Calling Scratch instead of using findnpc()
  
const Scratch this;
  
// The Clientside Weapon used with Scratch
  
this.db "+Scratch";
}

/*
   Loads Scratch Data into Database
   @param file Absolute Path to File
   @param identity The ID you're going to give your file
   @param clientsync Sync Data with Client? True or False
*/

public function onLoadScratch(fileidentityclientsync) {
  
// Add Files to Memory
  
this.identifiers.(@player.account).add(identity);
  
this.files.(@player.account).add(file); 
  
// Load Scratch Memory
  
this.scratch.(@player.account).(@extractfilebase(file)).loadvars(file);
  
// Sync with Client?
  
if (clientsync) {
    
temp.lines.loadlines(file);
    
player.triggerclient(this.db"load"identitytemp.lines);  
    
this.clientsync.(@player.account).add(identity);
  }
}

/*
   Unloads Scratch data associated with account
   @param acc Player's Account Name
   @param identity Optional Param: Use if you only want to unload one specific Scratch
*/

public function onUnloadScratch(accidentity) {
  
// Remove Specific Scratch
  
if (identity) {
    
// Determine File
    
temp.file onGetFile(identity);
    
// Save File
    
this.scratch.(@acc).(@extractfilebase(file)).savevars(file0);
    
// Remove from Memory
    
this.scratch.(@acc).(@extractfilebase(file)).clearvars();
    
// Remove from Player Variables
    
this.identifiers.(@acc).remove(identity);
    
this.files.(@acc).remove(file);
    if (
identity in this.clientsync.(@acc)) 
      
this.clientsync.(@acc).remove(identity);
    return;
  }
  
// Save Files and Remove from Memory
  
for (temp.filethis.files.(@acc)) {
    
this.scratch.(@acc).(@extractfilebase(file)).savevars(file0);
    
this.scratch.(@acc).(@extractfilebase(file)).clearvars();
  }
  
// Remove Player Variables from Memory
  
this.identifiers.(@acc) = "";
  
this.files.(@acc) = "";
  
this.clientsync.(@acc) = "";
}

/*
   Sets variable to value in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's New Value
*/

public function onSetScratch(identityvariablevalue) {
  
// Locate File
  
temp.file onGetFile(identity);
  
// Adjust Scratch
  
this.scratch.(@player.account).(@extractfilebase(file)).(@variable) = value;
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariablevalue);
  }
}

/*
   Adds value to variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onAddScratch(identityvariablevalue) {
  
// Locate File
  
temp.file onGetFile(identity);
  
// Adjust Scratch
  
this.scratch.(@player.account).(@extractfilebase(file)).(@variable).add(value);
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariableonGetScratch(identityvariable));
  }
}

/*
   Removes value from variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onRemScratch(identityvariablevalue) {
  
// Locate File
  
temp.file onGetFile(identity);
  
// Adjust Scratch
  
this.scratch.(@player.account).(@extractfilebase(file)).(@variable).remove(value);
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariableonGetScratch(identityvariable));
  }
}

/*
   Accesses identity's Scratch and returns Variable's Value
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @return Value of Variable
*/

public function onGetScratch(identityvariable) {
  
// Locate File
  
temp.file onGetFile(identity);
  
// Return Variable
  
return this.scratch.(@player.account).(@extractfilebase(file)).(@variable);
}

/*
   Gets the Scratch's associated file
   @param identity Scratch's Identity Value
   @return Files Absolute Location
*/

public function onGetFile(identity) {
  return 
this.files.(@player.account)[this.identifiers.(@player.account).index(identity)];

The Weapon NPC

PHP Code:

function onActionServerside() {
  if (
params[0] != "scratch") return;
  switch (
params[1]) {
    case 
"set":
      
Scratch.onSetScratch(params[2], params[3], params[4]);
      break;
    case 
"add":
      
Scratch.onAddScratch(params[2], params[3], params[4]);
      break;
    case 
"rem":
      
Scratch.onRemScratch(params[2], params[3], params[4]);
      break;
  }
}

//#CLIENTSIDE

function onCreated() {
  
// Allows other Scripts to access it as Scratch 
  // instead of using findweapon()
  
const Scratch this;
}

function 
onActionClientside(cmd) {
  
temp.identity params[1];
  switch (
cmd) {
    case 
"load":
      
// Clear Sync'd Vars if they Exist
      
if (identity in this.synced
        
this.scratch.(@identity).clearvars();
      else 
        
this.synced.add(identity);
      
// Load Variables
      
this.scratch.(@identity).loadvarsfromarray(params[2]);
      break;
    case 
"sync":
      
// Sync Variable
      
if (identity in this.synced
        
this.scratch.(@identity).(@params[2]) = params[3];
      break;
  } 
}

/*
   Accesses identity's Scratch and returns Variable's Value
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @return Value of Variable
*/

public function onGetScratch(identityvariable) {
  
// Return Variable
  
return this.scratch.(@identity).(@variable);
}

/*
   Tells server to set variable to value in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's New Value
*/

public function onSetScratch(identityvariablevalue) {
  if (!(
identity in this.synced)) return;
  
triggerserver("gui"name"scratch""set"identityvariablevalue);
}

/*
   Tells server to add value to variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onAddScratch(identityvariablevalue) {
  if (!(
identity in this.synced)) return;
  
triggerserver("gui"name"scratch""add"identityvariablevalue);
}

/*
   Tells server to remove value from variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onRemScratch(identityvariablevalue) {
  if (!(
identity in this.synced)) return;
  
triggerserver("gui"name"scratch""rem"identityvariablevalue);

__________________
Quote:

Last edited by fowlplay4; 12-30-2008 at 12:45 AM..
Reply With Quote
  #2  
Old 12-29-2008, 10:50 PM
Chompy Chompy is offline
»\(║_o)/»
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
I really like this, as I've done since you showed it to me on Zodiac RC.

It's simple, effecient and has a lot of functionality. I hope to see more releases from you Jerret As they're always interesting judging from your prior releases

Also rep++
__________________
Reply With Quote
  #3  
Old 01-01-2009, 01:36 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Since I can't exactly edit my top post anymore for whatever reason.. just a slight update to the DB NPC. Instead of using extractfilebase I decided to use the identity variable which is a slight efficieny boost so.. woot.

Basicly overlapping filenames shouldn't be a problem if someone decides to put this to extensive use, also I added a little check to prevent overlapping identities, it causes the file using the identity to save and unload, and then the new file can load which is a scenario that would be caused by a server crash.

If someone could update my main post with this, that would be appreciated.

The Database NPC

PHP Code:
/*
   Main Idea: Alternative to clientr. values

   - Loads a Variable File into Memory
   - Allows Option to Sync Data with Client
   - Unloads and Saves on PlayerLogout
   
*/

function onCreated() {
  
// For Calling Scratch instead of using findnpc()
  
const Scratch this;
  
// The Clientside Weapon used with Scratch
  
this.db "+Scratch";
}

/*
   Loads Scratch Data into Database
   @param file Absolute Path to File
   @param identity The ID you're going to give your file
   @param clientsync Sync Data with Client? True or False
*/

public function onLoadScratch(fileidentityclientsync) {
  
// Content Overlap Prevention
  // Example: Server Crashes, this will save the File and Reload It.
  
if (identity in this.identifiers.(@player.account)) onUnloadScratch(player.accountidentity);
  
// Add Files to Memory
  
this.identifiers.(@player.account).add(identity);
  
this.files.(@player.account).add(file); 
  
// Load Scratch Memory
  
this.scratch.(@player.account).(@identity).loadvars(file);
  
// Sync with Client?
  
if (clientsync) {
    
temp.lines.loadlines(file);
    
player.triggerclient(this.db"load"identitytemp.lines);  
    
this.clientsync.(@player.account).add(identity);
  }
}

/*
   Unloads all Scratch data associated with account
   @param acc Player's Account Name
*/

public function onUnloadScratch(accidentity) {
  
// Remove Specific Scratch 
  
if (identity) { 
    
// Determine File 
    
temp.file onGetFile(identity); 
    
// Save File 
    
this.scratch.(@acc).(@identity).savevars(file0); 
    
// Remove from Memory 
    
this.scratch.(@acc).(@identity).clearvars(); 
    
// Remove from Player Variables 
    
this.identifiers.(@acc).remove(identity); 
    
this.files.(@acc).remove(file); 
    if (
identity in this.clientsync.(@acc))  
      
this.clientsync.(@acc).remove(identity); 
    return; 
  }
  
// Save Files and Remove from Memory
  
for (temp.filethis.files.(@acc)) {
    
temp.identity this.identifiers.(@acc)[0+temp.i];
    
this.scratch.(@acc).(@identity).savevars(file0);
    
this.scratch.(@acc).(@identity).clearvars();
    
temp.i++;
  }
  
// Remove Player Variables from Memory
  
this.identifiers.(@acc) = "";
  
this.files.(@acc) = "";
  
this.clientsync.(@acc) = "";
}

/*
   Sets variable to value in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's New Value
*/

public function onSetScratch(identityvariablevalue) {
  
// Adjust Scratch
  
this.scratch.(@player.account).(@identity).(@variable) = value;
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariablevalue);
  }
  
// Forces DB to Save
  
this.trigger("update""");
}

/*
   Adds value to variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onAddScratch(identityvariablevalue) {
  
// Adjust Scratch
  
this.scratch.(@player.account).(@identity).(@variable).add(value);
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariableonGetScratch(identityvariable));
  }
  
// Forces DB to Save
  
this.trigger("update""");
}

/*
   Removes value from variable's array in identity's scratch data
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @param value Variable's Value to Add
*/

public function onRemScratch(identityvariablevalue) {
  
// Adjust Scratch
  
this.scratch.(@player.account).(@identity).(@variable).remove(value);
  
// Sync with Client?
  
if (identity in this.clientsync.(@player.account)) {
    
player.triggerclient(this.db"sync"identityvariableonGetScratch(identityvariable));
  }
  
// Forces DB to Save
  
this.trigger("update""");
}

/*
   Accesses identity's Scratch and returns Variable's Value
   @param identity Scratch's Identity Value
   @param variable Variable Name
   @return Value of Variable
*/

public function onGetScratch(identityvariable) {
  return 
this.scratch.(@player.account).(@identity).(@variable);
}

/*
   getstringkeys for your Scratch data
   @param identity Scratch's Identity Value
   @param prefix Variable Name
   @return String Keys from identity's Scratch
*/

public function onGetScratchKeys(identityprefix) {
  return 
getstringkeys("this.scratch." player.account "." identity "." prefix);
}

/*
   Gets the Scratch's associated file
   @param identity Scratch's Identity Value
   @return Files Absolute Location
*/

public function onGetFile(identity) {
  return 
this.files.(@player.account)[this.identifiers.(@player.account).index(identity)];

__________________
Quote:

Last edited by fowlplay4; 01-01-2009 at 03:43 AM..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:24 AM.


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