[CONTRIB]Groovy SFSObjects

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Democre
Posts: 77
Joined: 16 Sep 2010, 17:58

[CONTRIB]Groovy SFSObjects

Postby Democre » 05 Apr 2011, 16:44

First, I realize this isn't exactly the right place to post this, but it'll do until a more appropriate place gets set up

prereqs
- SFS2X RC2a
- Groovy 1.7.10

summary
This is a pair of groovy classes which enable a per instance pretending of properties. The init methods check to see if the methods are added to the instance before adding them, to avoid duplicate work. These classes enable the following code and similar to be written in Groovy:

Code: Select all

ISFSObject response = new SFSObject()
//enable it
SFSObjectEnabler.init(response)

//populate response
response.strings = ["hello", "world"]
//still use old put methods if you would like
response.putUtfString("oldstring", "old methods still work")

//following lines are equivalent
//response.putSFSObject("userdata", new SFSObject())
response.userdata = new SFSObject()
//set members of child sfsobjects
response.userdata.userid = 12345
response.userdata.dbid = 54321

//set an array of sfsobjects
//(actually creates an SFSArray with addSFSObject(...) calls)
response.characters = [new SFSObject(), new SFSObject()]
//add one more SFSObject to that array
response.characters << new SFSObject()

//work with the array members
response.characters[0].alias = "Joe"
response.characters[1].alias = "Bob"
response.characters[2].alias = "Ray"

println response.getDump()

This prints:

Code: Select all

   (utf_string) oldstring: old methods still work
   (sfs_array) characters:
       (sfs_object)
         (utf_string) alias: Joe
         
      
       (sfs_object)
         (utf_string) alias: Bob
         
      
       (sfs_object)
         (utf_string) alias: Ray
         
      
   
   (sfs_object) userdata:
      (int) dbid: 54321
      (int) userid: 12345
      
   
   (utf_string_array) strings: [hello, world]

Code for enablers follows in reply
Democre
Posts: 77
Joined: 16 Sep 2010, 17:58

Postby Democre » 05 Apr 2011, 16:46

SFSObjectEnabler:

Code: Select all

package com.pillar4.disco.enablers

import com.smartfoxserver.v2.entities.data.ISFSObject
import com.smartfoxserver.v2.entities.data.ISFSArray
import com.smartfoxserver.v2.entities.data.SFSArray

/**
 * (c) 2011 Pillar 4 Entertainment
 */
class SFSObjectEnabler {
  static def init(ISFSObject self) {
    if (!self.metaClass.getMetaProperty("groovy_sfs_disco_enabled")) {
      def mc = self.metaClass
      mc.groovy_sfs_disco_enabled = true;
//getters
      mc.getProperty = {String name ->
        def metaProperty = mc.getMetaProperty(name)
        def result
        if (metaProperty) result = metaProperty.getProperty(delegate)
        else {
          result = delegate.get(name)?.object
          //convenience to enable child SFS objects and arrays
          switch(result){
            case ISFSObject:
              SFSObjectEnabler.init(result)
              break
            case ISFSArray:
              SFSArrayEnabler.init(result)
              break
          }
        }
        result
      }
//setters
      mc.setProperty = {String name, Object value ->
        def metaProperty = mc.getMetaProperty(name)
        if (metaProperty) result = metaProperty.setProperty(delegate, value)
        else {
          switch (value) {
            case null:
              delegate.putNull(name)
              break
            case Collection:
              if (value.size() > 0) {
                def first = value[0]
                switch (first) {
                  case Boolean:
                    delegate.putBoolArray(name, value)
                    break
                  case Byte:
                    delegate.putByteArray(name, value)
                    break
                  case Double:
                    delegate.putDoubleArray(name, value)
                    break
                  case Float:
                    delegate.putDoubleArray(name, value)
                    break
                  case Integer:
                    delegate.putIntArray(name, value)
                    break
                  case Long:
                    delegate.putLongArray(name, value)
                    break
                  case ISFSObject:
                    ISFSArray sfsArray = new SFSArray()
                    SFSArrayEnabler.init(sfsArray)
                    value.each{
                      sfsArray << it
                    }
                    delegate.putSFSArray(name, sfsArray)
                    break
                  case Short:
                    delegate.putLongArray(name, value)
                    break
                  case String:
                    delegate.putUtfStringArray(name, value)
                    break
                  default:
                    println "unsupported collection type"
                }
              } else {
                delegate.putSFSArray(name, new SFSArray())
              }
              break
            case Boolean:
              delegate.putBool(name, value)
              break
            case Byte:
              delegate.putByte(name, value)
              break
            case Double:
              delegate.putDouble(name, value)
              break
            case Float:
              delegate.putFloat(name, value)
              break
            case Integer:
              delegate.putInt(name, value)
              break
            case Long:
              delegate.putLong(name, value)
              break
            case ISFSArray:
              delegate.putSFSArray(name, value)
              break
            case ISFSObject:
              delegate.putSFSObject(name, value)
              break
            case Short:
              delegate.putShort(name, value)
              break
            case String:
              delegate.putUtfString(name, value)
              break
            default:
              println("unsupported type")
          }
        }
      }
    }
  }
}
Last edited by Democre on 05 Apr 2011, 16:52, edited 2 times in total.
Democre
Posts: 77
Joined: 16 Sep 2010, 17:58

Postby Democre » 05 Apr 2011, 16:48

SFSArrayEnabler:

Code: Select all

package com.pillar4.disco.enablers

import com.smartfoxserver.v2.entities.data.ISFSArray
import com.smartfoxserver.v2.entities.data.ISFSObject
import com.smartfoxserver.v2.entities.data.SFSArray

/**
 * (c) 2011 Pillar 4 Entertainment
 */
class SFSArrayEnabler {
  static def init(ISFSArray self) {
    if (!self.metaClass.getMetaProperty("groovy_sfs_disco_enabled")) {
      def mc = self.metaClass
      mc.groovy_sfs_disco_enabled = true;
      //getters
      mc.getAt = {int index ->
        def result = delegate.getElementAt(index)
        //convenience to enable child SFS objects and arrays
        switch (result) {
          case ISFSObject:
            SFSObjectEnabler.init(result)
            break
          case ISFSArray:
            SFSArrayEnabler.init(result)
            break
        }
        result
      }
//adder
      mc.leftShift = {Object value ->
        switch (value) {
          case null:
            delegate.addNull()
            break
          case Collection:
            if (value.size() > 0) {
              def first = value[0]
              switch (first) {
                case Boolean:
                  delegate.addBoolArray(value)
                  break
                case Byte:
                  delegate.addByteArray(value)
                  break
                case Double:
                  delegate.addDoubleArray(value)
                  break
                case Float:
                  delegate.addDoubleArray(value)
                  break
                case Integer:
                  delegate.addIntArray(value)
                  break
                case Long:
                  delegate.addLongArray(value)
                  break
                case ISFSObject:
                  ISFSArray sfsArray = new SFSArray()
                  SFSArrayEnabler.init(sfsArray)
                  value.each {
                    sfsArray << it
                  }
                  delegate.addSFSArray(sfsArray)
                  break
                case Short:
                  delegate.addLongArray(value)
                  break
                case String:
                  delegate.addUtfStringArray(value)
                  break
                default:
                  println "unsupported collection type"
              }
            } else {
              delegate.addSFSArray(new SFSArray())
            }
            break
          case Boolean:
            delegate.addBool(value)
            break
          case Byte:
            delegate.addByte(value)
            break
          case Double:
            delegate.addDouble(value)
            break
          case Float:
            delegate.addFloat(value)
            break
          case Integer:
            delegate.addInt(value)
            break
          case Long:
            delegate.addLong(value)
            break
          case ISFSArray:
            delegate.addSFSArray(value)
            break
          case ISFSObject:
            delegate.addSFSObject(value)
            break
          case Short:
            delegate.addShort(value)
            break
          case String:
            delegate.addUtfString(value)
            break
          default:
            println("unsupported type")
        }
        delegate
      }
    }
  }
}
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 06 Apr 2011, 05:59

Since this is for SFS2X, I moved this to a more appropriate place.
Smartfox's forum is my daily newspaper.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: dapedVah and 15 guests