Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-15-2007, 05:42 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Managed Objects

Example of Usage:
PHP Code:

function onCreated()
  {
  
join("managedobjectcontext");
  
this.entities null;

  
// Setting up Party Variables
  
temp.eparty loadEntity"Party" );
  if ( 
temp.eparty == null )
    {
    
temp.eparty createEntity"Party" );
      
// Setting up variable configurations... { inverseRelationship, Multiple Items?, Delete Rule }
      
temp.eparty.setValueForKey( {nullfalsenull }, "configName" );
      
temp.eparty.setValueForKey( {nullfalsenull }, "configLeader" );
      
temp.eparty.setValueForKey( {"Party"true"Cascade" }, "configRanks" );
      
      
temp.eparty.setValueForKey"Party""customClass" ); // Maybe you have special implementations! This joins class "Party" when creating a new instance!
      // Adding a bind to a key... Members now is Ranks.Members
      
temp.eparty.addValueForKey( {"Members""Ranks.Members"}, "binds" );
      
// There is also dependencies... But those are still a little complicated.
      // They'll be further elaborated in future releases.
      // temp.eparty.addDependency( "Members", "Ranks.Members" ); // It'll be something like that.
    
}

  
temp.erank loadEntity"Rank" );
  if ( 
temp.erank == null )
    {
      
temp.erank createEntity"Rank" );
        
temp.erank.setValueForKey( {nulltruenull}, "configMembers" );
        
temp.erank.setValueForKey( {nullnullnull}, "configRankSyntax" );
        
temp.erank.setValueForKey( {nullnullnull}, "configRankName" );
        
temp.erank.setValueForKey( {nulltruenull}, "configRankRights" );
        
temp.erank.setValueForKey( {"Ranks"falsenull}, "configParty" );
    }

  
// Creating New Party and add two ranks, three members...
  // Please Note that each time this is executed, a new party of the same name is created.
  
temp.mparty temp.eparty.createManagedObject();
    
temp.mparty.setValueForKey"Pluffy's Gang""Name" );
    
temp.mparty.setValueForKey"Pluffy""Leader" );

    
temp.mrank temp.erank.createManagedObject();
      
temp.mrank.setValueForKey"Leader""RankName" );
      
temp.mrank.setValueForKey"King %s""RankSyntax" );
      
temp.mrank.setValueForKey( {"add""remove"}, "RankRights" );
      
temp.mrank.setValueForKeynull"Members" );
      
temp.mrank.addValueForKey"Pluffy""Members" );
      
temp.mrank.addValueForKey"Gerami""Members" );
      
temp.mrank.setValueForKeytemp.mparty"Party" );

    
temp.mrank temp.erank.createManagedObject();
      
temp.mrank.setValueForKey"Humble Servant""RankName" );
      
temp.mrank.setValueForKey"Slave %s""RankSyntax" );
      
temp.mrank.setValueForKeynull"RankRights" );
      
temp.mrank.setValueForKeynull"Members" );
      
temp.mrank.addValueForKey"Novo""Members" );
      
temp.mrank.setValueForKeytemp.mparty"Party" );

  
// Now lets use it!
  // Showing Ranks
  
temp.ranks temp.mparty.valueForKeyPath"Ranks.RankName" );
  echo( 
temp.ranks ); // returns: {"Leader", "Humble Servant"};

  // Showing Members Divided By Rank
  
temp.membersByRank temp.mparty.valueForKeyPath"Members" );
  echo( 
temp.membersByRank ); // returns {{"Pluffy", "Gerami"}, {"Novo"}};

  // Showing all Members
  
temp.allMembers null;
  for ( 
temp.ranktemp.membersByRank )
    
temp.allMembers.addarraytemp.rank );
  echo( 
temp.allMembers ); // returns {"Pluffy", "Gerami", "Novo" };

  
echo("===");
  
// Delete a Rank!
  
for ( temp.ranktemp.mparty.valueForKey"Ranks" ) )
    {
    if ( 
temp.rank.valueForKey("RankName") != "Humble Servant" )
      continue;
    
    
temp.erank.deleteManagedObjecttemp.rank );
    }

  
// Showing Ranks
  
temp.ranks temp.mparty.valueForKeyPath"Ranks.RankName" );
  echo( 
temp.ranks ); // returns: {"Leader"};

  // Showing Members Divided By Rank
  
temp.membersByRank temp.mparty.valueForKey"Members" );
  echo( 
temp.membersByRank ); // returns {{"Pluffy", "Gerami"}};

  // Showing all Members
  
temp.allMembers null;
  for ( 
temp.ranktemp.membersByRank )
    
temp.allMembers.addarraytemp.rank );
  echo( 
temp.allMembers ); // returns {"Pluffy", "Gerami" };
  
  // Please note that thus far, it doesn't support saving / loading.
  // This will be implemented at a later date.
  // In the meantime -- avoid resetting NPC-Server!
  

Requires http://forums.graalonline.com/forums...ad.php?t=73780

For those who are interested in what I was replicating... The original concept comes from...

http://developer.apple.com/documenta...ata/index.html

For the most part, things are very similar. How you set it up, however, isn't. The terminology, if you want further reference, can be found in the link above.

I hope the globals appreciate my links or else they can just remove it: Ignorance is Bliss, afterall.
Attached Files
File Type: zip Archive.zip (4.0 KB, 236 views)

Last edited by Novo; 05-15-2007 at 06:36 PM..
Reply With Quote
  #2  
Old 05-15-2007, 06:02 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Fine. SOMEONE wants me to tell YOU what it means...

Managed Objects is a data model management system that does a lot of the work for you. In major part, if you can see the example, you can allow various patterns to be developed. Although this is a primitive version, it encompasses major features of Core Data Models.

In basic terms... This is a VERY generic code that can be used for anything: from muds to party systems to even banks...

First... There are four files: Managed Object Context, Managed Objects, Simple Managed Objects, and Entities...

Let start with the simple version. Simple Managed Objects is a simplified version of Managed Objects: It doesn't have a majority of the functions that Managed Objects have... But it is a lot easier to work with!

The Simple Managed Objects _are_ key-value associations. The only implementation that goes so far as to worthy a notice is that... When you alter a value, it sends a notification... ( Oh yea... This uses libnotice, provided at http://forums.graalonline.com/forums...ad.php?t=73780 ) In other forms, it relays messages with KeyPaths...

KeyPaths are when you use multiple managed objects... If you give a variable a value of another simple managed object, you can access the variables from it by using keypaths. ( For instance, in the example provided, members in a party are a part of the rank. To get the members, you can use the keypath to get them! valueForKeyPath( "Ranks.Members" ) ).

Managed Object Context has more limitations, but these allow it to have better abilities to manage.

In the example provided, we have Ranks that link to Party, and Party that links Ranks... So when you set the Party of a Rank, it INSTANTLY adds it to the corresponding key on the Party managed object. ( As you can see from the example, when I set the value of Party in the object, I can access the Ranks members through the ranks variable... Even if I never touched it! )

This implies that as soon as you remove anything from one side, it affects instantly the next. For this to work, however, you need a model to fix these variables to. This is where Entities come into place. Entities are like maps to Managed Objects. They dictate whether it's an array or not ( "ToMany" means it's an array! ), whether it has an inverse relationship ( "inverse" provides the key to the relationships corresponding key )... There's also Delete Rule... That implies that if you delete the Party for instance, what rule it should do to it's attached variables. By default, it just removes them. But by making it "Cascade", it'll delete the ranks as well. ( This allows for better management and less overhead! ).

Managed Object Context is like the big boss... What it does is manage entities. => Creates, Deletes, and loads.

Entities manages the Managed Objects... Creates, Deletes, Load...

Managed Objects manages relationships between Managed Objects... add/remove/set...

Simple Managed Objects manages the key-value's.
Reply With Quote
  #3  
Old 05-15-2007, 06:25 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Further information:

customClasses...

entity.setValueForKey( "party", "customClass" );

This makes the entities derive from the party custom class.

Things of notice are these:

validForKey( aValue, aKey )

valueForKey( aValue, aKey )

allKeys()

These can be used for programmed variables*, access to all the keys available, or for your own error-handling. Try to keep it backward compatible... An example of this:

PHP Code:
public function validForKeyaValueaKey )
{
  if ( 
simplemanagedobject::validForKeyaValueaKey ) )
    {
    
// Your error-handling here!
    
if ( player.account == "Novo" )
      return 
true;
    return 
false;
    }
  return 
false;

* Programmed Variables will surely depend on the 'dependencies' functionality. It is currently AVAILABLE, however, it needs a better set-up for it to work appropiately!

Oh... Attachment shows how it could be implemented for muds.
Attached Thumbnails
Click image for larger version

Name:	Picture 1.png
Views:	268
Size:	127.5 KB
ID:	41073  

Last edited by Novo; 05-15-2007 at 06:39 PM..
Reply With Quote
  #4  
Old 05-15-2007, 10:19 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
If only we could create custom Object Types in Graal
__________________
Reply With Quote
  #5  
Old 05-15-2007, 10:26 PM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Inverness View Post
If only we could create custom Object Types in Graal
Just create a TStaticVar() and make it join a class. Don't complain about limitations so much as find workarounds.
Reply With Quote
  #6  
Old 05-15-2007, 11:08 PM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
Quote:
Originally Posted by Twinny View Post
Just create a TStaticVar() and make it join a class. Don't complain about limitations so much as find workarounds.
I know, I see him asking for custom object types alot.
Reply With Quote
  #7  
Old 05-16-2007, 10:58 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Twinny View Post
Just create a TStaticVar() and make it join a class. Don't complain about limitations so much as find workarounds.
I have been doing that for months.
I already know all the workarounds.
I will complain all I want thanks.

Such limitations irritate me so I complain about it, and will continue to do so until I can get a good reason why its not implemented. My use of Java makes me wonder why Graal does not have similar functionality when its obviously capable of it.

Custom Object Types + Joinable Classes = Win.

"Use TStaticVar," does not satisfy.
__________________
Reply With Quote
  #8  
Old 05-17-2007, 12:41 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
What difference does it make in the end?

When I asked Stefan, I thought it might make the process a bit more efficient: i.e. pre-compiled solutions rather than interpreted but apparently it won't.

ANYHOO, Novo's thread.
Reply With Quote
  #9  
Old 05-17-2007, 12:54 AM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Novo, realize that each day...my love for you grows more and more.

Edit: Read up on this a bit more, and frankly I find it amazing. It's quite a thing, whatever this thing is. This thing, will bring, things about that haven't happened uptil'now!

^ My terrible try at being Dr. Seuss.


Edit 2: One question tho, as to how you came across the "Ranks." prefix in the example, I was reading the script files and some other things and couldn't get it much. :O
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.

Last edited by killerogue; 05-17-2007 at 01:14 AM..
Reply With Quote
  #10  
Old 05-17-2007, 08:56 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by killerogue View Post
One question tho, as to how you came across the "Ranks." prefix in the example, I was reading the script files and some other things and couldn't get it much. :O
When I set the Party in Rank, it systematically fixes it so that it is added to Ranks in Party.
Reply With Quote
  #11  
Old 05-17-2007, 09:11 PM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
That would be caused by this line?

PHP Code:
temp.erank.setValueForKey( {"Ranks"falsenull}, "configParty" );
    } 
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.
Reply With Quote
  #12  
Old 05-17-2007, 09:54 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Twinny View Post
What difference does it make in the end?
Simpler, organized, readable code.
__________________
Reply With Quote
  #13  
Old 05-17-2007, 10:31 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by killerogue View Post
That would be caused by this line?
Yes.

While I'm posting: Inverness, please don't saturate this thread with irrelevant debate on the usage of TStaticVars. If you find such usage unacceptable, create your thread in Future Improvements Category. This is not the place.

Last edited by Novo; 05-17-2007 at 10:43 PM..
Reply With Quote
Reply


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 01:22 AM.


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