網路遊戲設計Web Game Design
授課教師 江素貞
Chapter 6 User Variable and Room VariableUser Variables
User Variable TypesIn multiplayer games, chats, and other types of interactive
applications, it is often useful to attach information to a user that stays with that user at all times.
ElectroServer supports that concept through User Variables, User Plugin Variables, User Extension Variables, and User Server Variables.
User VariablesA User Variable is a name/value pair stored on the server
and scoped to a user.A user can have many User Variables. User Variables of a
user are seen by other users in the same room.Users in that room can receive User Variable Update
events when User Variables are changed. (MessageType.UserListUpdateEvent)
A user can create, update, or delete his own User Variables.
Server extensions can create, update, or delete User Variables on any user.
EsObjectThe value of a User Variable is an EsObject. EsObject is an object that supports a large number of data
types. It allows the server and client, or clients and other clients, to exchange simple or complex object structures that retain data types.
In addition, EsObjects are used in many places when dealing with extensions and values for room variables, user variables, and user server variables.
Creating an EsObjectTo use an EsObject, you must first create a new instance
and then set values on it using the provided methods.
import com.electrotank.electroserver4.esobject.EsObject;
var esob:EsObject = new EsObject();
esob.setString("FirstName", "Jobe");
esob.setInteger("Age",32);
esob.setBoolean("isCool", false);
esob.setFloatArray("FavoriteFloats", [3.141, 2.718]);
Update User VariableSimple Example:
var userData:EsObject = new EsObject();
userData.setString("realName", loginScreen.name_txt.text);
userData.setInteger("score", 0);
var uv:UpdateUserVariableRequest = new UpdateUserVariableRequest();
uv.setValue( userData );
uv.setName( "data" );
es.send(uv);
Obtain User Variablevar user:User=es.getUserManager().getUserByName(“xxx”)
//user 必須視狀況取得var d:EsObject = user.getUserVariable("data").getValue();
FlashConnect.trace(d.getString("realName"));
Chapter 6 User Variable and Room VariableRoom Variables
Room VariablesRoom Variables are name/value pairs stored on the server
and scoped to a room. These variables are seen by users in that room and are
accessible by server extensions. Room variables can be created during the room creation
process, by users in the room, or by server extensions.The value of a room variable is an EsObject.
Why Are Room Variables Useful?Room Variables provide a convenient way for developers
to accomplish simple things easily. For instance, Room Variables can be set in a room to specify the background music to play, the chat skin to use, etc.
CreateRoomVariableRequestThis class allows you to create a room variable. A room
variable is a variable scoped to a room that you are in and stored on the server. All users in your room receive these variables on entering the room, and receive create, update, and delete events on variables after they are already in the room.
Creating a Room VariableWhen a client creates a room variable the following are specified:Room Id (integer) – The id of the room in which to create the Room
Variable.Zone Id (integer) – The id of the zone that owns the room.Name (string) – The name of the Room Variable.Value (EsObject) – The EsObject value of the Room Variable.Locked (Boolean) – If true then the variable cannot be modified until
it is unlocked. If false then the variable value can be modified.Persistent (Boolean) – If true then when the user that creates the
Room Variable leaves the room the variable remains. If false then when the user that created the Room Variable leaves the room the variable is removed
Creating a Room Variablevar eob:EsObject = new EsObject();
eob.setString("LoopName", "Happy.mp3");
eob.setInteger("Volume", 80); var crr:CreateRoomVariableRequest = new CreateRoomVariableRequest();
crr.setName("MusicInfo");
crr.setValue(eob);
crr.setRoomId(myRoom.getRoomId());
crr.setZoneId(myRoom.getZone().getZoneId());
//crr.setLocked(false);
//crr.setPersistent(false);
es.send(crr);
Event Listenes.addEventListener(MessageType.RoomVariableUpdateEvent,
"onRoomVariableUpdateEvent", this);
function onRoomVariableUpdateEvent(e:RoomVariableUpdateEvent):void {
var varName:String = e.getName(); var room:Room = es.getZoneManager().getZoneById(e.getZoneId()).getRoomById(e.getRoomId());
switch (e.getUpdateAction()) {
case RoomVariableUpdateEvent.VariableCreated:
case RoomVariableUpdateEvent.VariableUpdated:
case RoomVariableUpdateEvent.VariableDeleted:
default:
}
}
UpdateRoomVariableRequestThis class allows you to update an existing room variable.
You identify a room variable by room id, zone id, and name. You can update a variables value and locked property. The value of the variable is an EsObject.
Updating a Room VariableA Room Variable can be updated by changing its locked
status or by changing its value.
Update Room Variablevar eob:EsObject = new EsObject();
eob.setString("LoopName", "Sad.mp3");
eob.setInteger("Volume", 50); var urvr:UpdateRoomVariableRequest = new UpdateRoomVariableRequest ();
urvr.setName("MusicInfo");
urvr.setValue(eob);
urvr.setRoomId(myRoom.getRoomId());
urvr.setZoneId(myRoom.getZone().getZoneId());
//urvr.setLocked(true);
es.send(urvr);