Ez game stuff, javafied.
git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@63 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -29,7 +29,8 @@ public class EZGameConfig extends GameConfig
|
||||
// from abstract GameConfig
|
||||
public GameConfigurator createConfigurator ()
|
||||
{
|
||||
return null; // nothing here on the java side
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,8 @@ import com.threerings.io.Streamable;
|
||||
import com.threerings.parlor.game.data.GameObject;
|
||||
import com.threerings.parlor.turn.data.TurnGameObject;
|
||||
|
||||
import com.threerings.ezgame.util.EZObjectMarshaller;
|
||||
|
||||
/**
|
||||
* Contains the data for an ez game.
|
||||
*/
|
||||
@@ -43,6 +45,14 @@ public class EZGameObject extends GameObject
|
||||
/** The service interface for requesting special things from the server. */
|
||||
public EZGameMarshaller ezGameService;
|
||||
|
||||
/**
|
||||
* Access the underlying user properties
|
||||
*/
|
||||
public HashMap<String, Object> getUserProps ()
|
||||
{
|
||||
return _props;
|
||||
}
|
||||
|
||||
// from TurnGameObject
|
||||
public String getTurnHolderFieldName ()
|
||||
{
|
||||
@@ -64,23 +74,40 @@ public class EZGameObject extends GameObject
|
||||
/**
|
||||
* Called by PropertySetEvent to effect the property update.
|
||||
*/
|
||||
protected void applyPropertySet (String propName, Object data, int index)
|
||||
public Object applyPropertySet (String propName, Object data, int index)
|
||||
{
|
||||
Object oldValue = _props.get(propName);
|
||||
if (index >= 0) {
|
||||
Object something = _props.get(propName);
|
||||
byte[][] arr = (something instanceof byte[][])
|
||||
? (byte[][]) something : null;
|
||||
if (arr == null || arr.length <= index) {
|
||||
// TODO: in case a user sets element 0 and element 90000,
|
||||
// we might want to store elements in a hash
|
||||
byte[][] newArr = new byte[index + 1][];
|
||||
if (arr != null) {
|
||||
System.arraycopy(arr, 0, newArr, 0, arr.length);
|
||||
if (isOnServer()) {
|
||||
byte[][] arr = (oldValue instanceof byte[][])
|
||||
? (byte[][]) oldValue : null;
|
||||
if (arr == null || arr.length <= index) {
|
||||
// TODO: in case a user sets element 0 and element 90000,
|
||||
// we might want to store elements in a hash
|
||||
byte[][] newArr = new byte[index + 1][];
|
||||
if (arr != null) {
|
||||
System.arraycopy(arr, 0, newArr, 0, arr.length);
|
||||
}
|
||||
_props.put(propName, newArr);
|
||||
arr = newArr;
|
||||
}
|
||||
_props.put(propName, newArr);
|
||||
arr = newArr;
|
||||
oldValue = arr[index];
|
||||
arr[index] = (byte[]) data;
|
||||
|
||||
} else {
|
||||
Object[] arr = (oldValue instanceof Object[])
|
||||
? (Object[]) oldValue : null;
|
||||
if (arr == null || arr.length <= index) {
|
||||
Object[] newArr = new Object[index + 1];
|
||||
if (arr != null) {
|
||||
System.arraycopy(arr, 0, newArr, 0, arr.length);
|
||||
}
|
||||
_props.put(propName, newArr);
|
||||
arr = newArr;
|
||||
}
|
||||
oldValue = arr[index];
|
||||
arr[index] = data;
|
||||
}
|
||||
arr[index] = (byte[]) data;
|
||||
|
||||
} else if (data != null) {
|
||||
_props.put(propName, data);
|
||||
@@ -88,6 +115,8 @@ public class EZGameObject extends GameObject
|
||||
} else {
|
||||
_props.remove(propName);
|
||||
}
|
||||
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
// AUTO-GENERATED: METHODS START
|
||||
@@ -132,11 +161,15 @@ public class EZGameObject extends GameObject
|
||||
{
|
||||
out.defaultWriteObject();
|
||||
|
||||
// write the number of properties, followed by each one
|
||||
out.writeInt(_props.size());
|
||||
for (Map.Entry<String, Object> entry : _props.entrySet()) {
|
||||
out.writeUTF(entry.getKey());
|
||||
out.writeObject(entry.getValue());
|
||||
if (isOnServer()) {
|
||||
// write the number of properties, followed by each one
|
||||
out.writeInt(_props.size());
|
||||
for (Map.Entry<String, Object> entry : _props.entrySet()) {
|
||||
out.writeUTF(entry.getKey());
|
||||
out.writeObject(entry.getValue());
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,13 +183,31 @@ public class EZGameObject extends GameObject
|
||||
|
||||
_props.clear();
|
||||
int count = ins.readInt();
|
||||
boolean onClient = !isOnServer();
|
||||
while (count-- > 0) {
|
||||
String key = ins.readUTF();
|
||||
_props.put(key, ins.readObject());
|
||||
Object o = ins.readObject();
|
||||
if (onClient) {
|
||||
o = EZObjectMarshaller.decode(o);
|
||||
}
|
||||
_props.put(key, o);
|
||||
}
|
||||
}
|
||||
|
||||
/** The current state of game data, opaque to us here on the server. */
|
||||
/**
|
||||
* Called internally and by PropertySetEvent to determine if we're
|
||||
* on the server or on the client.
|
||||
*/
|
||||
boolean isOnServer ()
|
||||
{
|
||||
return _omgr.isManager(this);
|
||||
}
|
||||
|
||||
/** The current state of game data.
|
||||
* On the server, this will be a byte[] for normal properties
|
||||
* and a byte[][] for array properties.
|
||||
* On the client, the actual values are kept whole.
|
||||
*/
|
||||
protected transient HashMap<String, Object> _props =
|
||||
new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import com.threerings.io.ObjectInputStream;
|
||||
import com.threerings.io.ObjectOutputStream;
|
||||
import com.threerings.io.Streamable;
|
||||
import com.threerings.io.Streamer;
|
||||
|
||||
|
||||
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 EZGameObject.
|
||||
@@ -35,16 +37,55 @@ public class PropertySetEvent extends NamedEvent
|
||||
_index = index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value that was set for the property.
|
||||
*/
|
||||
public Object getValue ()
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the old value.
|
||||
*/
|
||||
public Object getOldValue ()
|
||||
{
|
||||
return _oldValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the index, or -1 if not applicable.
|
||||
*/
|
||||
public int getIndex ()
|
||||
{
|
||||
return _index;
|
||||
}
|
||||
|
||||
// from abstract DEvent
|
||||
public boolean applyToObject (DObject target)
|
||||
{
|
||||
((EZGameObject) target).applyPropertySet(_name, _data, _index);
|
||||
EZGameObject ezObj = (EZGameObject) target;
|
||||
if (!ezObj.isOnServer()) {
|
||||
_data = EZObjectMarshaller.decode(_data);
|
||||
}
|
||||
_oldValue = ezObj.applyPropertySet(_name, _data, _index);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notifyListener (Object listener)
|
||||
{
|
||||
if (listener instanceof PropertySetListener) {
|
||||
((PropertySetListener) listener).propertyWasSet(this);
|
||||
}
|
||||
}
|
||||
|
||||
/** The index. */
|
||||
protected int _index;
|
||||
|
||||
/** The client-side data that is assigned to this property. */
|
||||
protected Object _data;
|
||||
|
||||
/** The old value. */
|
||||
protected transient Object _oldValue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.threerings.ezgame.data;
|
||||
|
||||
import com.threerings.presents.dobj.ChangeListener;
|
||||
|
||||
/**
|
||||
* A listener that will be notified of PropertySet events.
|
||||
*/
|
||||
public interface PropertySetListener extends ChangeListener
|
||||
{
|
||||
/**
|
||||
* Called when a property was set on a flash game object.
|
||||
* This will be called <em>after</em> the event has been applied
|
||||
* to the object.
|
||||
*/
|
||||
public void propertyWasSet (PropertySetEvent event);
|
||||
}
|
||||
Reference in New Issue
Block a user