Files
vilya/src/as/com/threerings/ezgame/data/PropertySetEvent.as
T
Robert Zubeck bdfcd156ab For EZ property sets, added a setter that performs an "atomic" test-and-set:
it's atomic in the sense that a single server event will test the variable, 
and only set it if the previous value was null (i.e. didn't exist). 
This allows for a level of elementary synchronization between the clients.

The new function on EZGameControl is:
  _gameCtrl.testAndSet (propertyName, newValue[, index])

I hoped to generalize this to test against arbitrary values, but that's 
significantly harder, since properties accept numerous types as values,
and those can have different representations on the client and the server.
So it remains a check against null until we need to generalize it. :)





git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@193 c613c5cb-e716-0410-b11b-feb51c14d237
2007-02-15 21:55:49 +00:00

111 lines
2.6 KiB
ActionScript

//
// $Id$
package com.threerings.ezgame.data {
import flash.utils.ByteArray;
import flash.utils.IExternalizable;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
import com.threerings.util.StringBuilder;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.dobj.NamedEvent;
import com.threerings.ezgame.util.EZObjectMarshaller;
/**
* Represents a property change on the actionscript object we
* use in FlashGameObject.
*/
public class PropertySetEvent extends NamedEvent
{
/**
* Create a PropertySetEvent.
*/
public function PropertySetEvent () // unserialize-only
{
super(0, null);
}
// from abstract DEvent
override public function applyToObject (target :DObject) :Boolean
{
_oldValue =
EZGameObject(target).applyPropertySet(_name, _data, _index, _testAndSet);
return true;
}
/**
* Get the value that was set for the property.
*/
public function getValue () :Object
{
return _data;
}
/**
* Get the index, or -1 if not applicable.
*/
public function getIndex () :int
{
return _index;
}
/**
* Get the old value.
*/
public function getOldValue () :Object
{
return _oldValue;
}
// from interface Streamable
override public function readObject (ins :ObjectInputStream) :void
{
super.readObject(ins);
_index = ins.readInt();
_data = EZObjectMarshaller.decode(ins.readObject());
_testAndSet = ins.readBoolean();
}
// from interface Streamable
override public function writeObject (out :ObjectOutputStream) :void
{
super.writeObject(out);
out.writeInt(_index);
out.writeObject(_data);
out.writeBoolean(_testAndSet);
}
override protected function notifyListener (listener :Object) :void
{
if (listener is PropertySetListener) {
(listener as PropertySetListener).propertyWasSet(this);
}
}
override protected function toStringBuf (buf :StringBuilder) :void
{
buf.append("PropertySetEvent ");
super.toStringBuf(buf);
buf.append(", index=").append(_index);
}
/** The index of the property, if applicable. */
protected var _index :int;
/** The client-side data that is assigned to this property. */
protected var _data :Object;
/** When true, the property will only be set if its previous value was null. */
protected var _testAndSet :Boolean;
/** The old value. */
protected var _oldValue :Object;
}
}