Adaptors
AppWarp S2 provides adapters which developers can extend to handle events from the AppWarp S2 components.
You need to extend these classes and inside them implement your game logic.
BaseServerAdaptor
It handles the creation and deletion of zones. It contains following methods
/** * Invoked when a new zone is added through the admin dashboard. * This will also be invoked when the server instance is started * and loads zones created in earlier launches (read from HSQLDB) * * @param zone the zone being added */ public void onZoneCreated(IZone zone) /** * Invoked when a zone is deleted from the admin dashboard * * @param zone the zone being deleted */ public void onZoneDeleted(IZone zone)
BaseZoneAdaptor
BaseZoneAdaptor handles all the zone specific requests like creation and deletion of rooms, etc. It also handles the authorization requests. Whenever a client connects to a server, handleAddUserRequest() is invoked. There is a special method named handleTimerTick() which is repeatedly called after a predefined time period (defined in AppConfig.json). This function can be useful in simulating game loops. This class contains following methods
/** * Invoked when a create room request is received from the client. * * By default this will result in a success response sent back to the user, * the room will be added to the list of rooms and a room created notification * will be sent to all the subscribers of the lobby. * * @param user the user who has sent the request. Null if received from admin dashboard * @param room the room being created * @param result use this to override the default behavior */ public void handleCreateRoomRequest(IUser user, IRoom room, HandlingResult result) /** * Invoked when a delete room request is received from the client. * * By default this will result in a success response sent back to the user, * the room will be removed from the list of rooms and a room deleted notification * will be sent to all the subscribers of the lobby. * * @param user the user who has sent the request. Null if received from admin dashboard * @param room the room being created * @param result use this to override the default behavior */ public void handleDeleteRoomRequest(IUser user, IRoom room, HandlingResult result) /** * Invoked when connect request is received from a client. * * By default the user will be added to the zone and if user with the * same name exists that user will be disconnected. * * @param user the user who has sent the connect request. * @param authData the authdata passed in the connect request * @param result use this to override the default behavior */ public void handleAddUserRequest(IUser user, String authData, HandlingResult result) /** * Invoked when a recover request is sent by a client with in the allowance * time set while first connecting. * * By default the user session will be successfully resumed and a response * will be sent back to the user. * * @param user the user who is attempting to recover * @param authData the authData sent by the client * @param result use this to override the default behavior */ public void handleResumeUserRequest(IUser user, String authData, HandlingResult result) /** * Invoked every time the Game loop timer is fired. The frequency of the * tick can be modified through the AppConfig.json file. Do NOT perform * blocking operations in this callback. * * @param time the difference, measured in milliseconds, between * the current time and midnight, January 1, 1970 UTC as returned * by System.currentTimeMillis() */ public void onTimerTick(long time) /** * Invoked when a user is removed from the list of users of the zone. * This happens when the client disconnects from AppWarp. * * @param user the user being removed. */ public void onUserRemoved(IUser user) /** * Invoked when a user is paused. This happens when a client's connection * breaks without receiving a disconnect request and the client has set * a recovery allowance period. * * @param user */ public void onUserPaused(IUser user) /** * Invoked when a private chat request is received from an online user * for another user who is also online. By default the server will send * a success response back to the sender and a private chat notification * to the receiver. * * @param sender * @param toUser * @param result */ public void handlePrivateChatRequest(IUser sender, IUser toUser, HandlingResult result) /** * Invoked when a room is added from the admin dashboard. This is also invoked when the * server is started and the previously created admin rooms are added to the zone. * * @param room the room being added. */ public void onAdminRoomAdded(IRoom room) /** * Invoked when an admin room is deleted from the dashboard. * * @param room the room being removed */ public void onAdminRoomDeleted(IRoom room)
BaseRoomAdaptor
BaseRoomAdaptor handles the room specific methods. It handles all the interactions taking place in between the users in a room. It also contains handleTimerTick() similar to BaseZoneAdapter. BaseRoomAdaptor contains following methods
/** * Invoked when a chat request is received from the client in the room. * * By default this will trigger a success response back to the client and * will broadcast a notification message to all the subscribers of the room. * * * @param sender the user who has sent the request * @param message the message that was sent * @param result use this to override the default behavior */ public void handleChatRequest(IUser sender, String message, HandlingResult result) /** * Invoked when a update peers request is received from the client in the room. * * By default this will trigger a success response back to the client and * will broadcast a notification update to all the subscribers of the room. * * @param sender the user who has sent the request * @param update the byte array sent by the user * @param result use this to override the default behavior */ public void handleUpdatePeersRequest(IUser sender, byte[] update, HandlingResult result) /** * Invoked when a user leaves the room * * @param user the user who is leaving the room */ public void onUserLeaveRequest(IUser user) /** * Invoked when a join request is received by the room and the number of * joined users is less than the maxUsers allowed. * * By default this will result in a success response sent back the user, * the user will be added to the list of joined users of the room and * a user joined room notification will be sent back to all the subscribed * users of the room. * * @param user the user who has sent the request * @param result use this to override the default behavior */ public void handleUserJoinRequest(IUser user, HandlingResult result) /** * Invoked every time the Game loop timer is fired. The frequency of the * tick can be modified through the AppConfig.json file. Do NOT perform * blocking operations in this callback. * * @param time the difference, measured in milliseconds, between * the current time and midnight, January 1, 1970 UTC as returned * by System.currentTimeMillis() */ public void onTimerTick(long time) /** * Invoked when an updateProperties request is received from the client. * The server has already validated the request meets its criteria of * property size and lock validation. If any of the server checks fail, then * this will not be invoked and a error response will be sent back to the * client. * * By default, the server will accept the request and modify the room's * properties as in the request. It will then send a success response back to * the sender and a user change properties notification will be sent to all * the subscribed users of the room. * * @param user sender of the request * @param addOrUpdateMap the <key, value> map of properties to be added or updated * @param removeKeysArray the array of keys for properties to be removed * @param result use this to override the default behavior */ public void handlePropertyChangeRequest(IUser sender, HashMap<String, Object> addOrUpdateMap, ArrayList<String> removeKeysList, HandlingResult result) /** * Invoked when a lockProperties request is received from the client. * The server has already validated the request meets its criteria of * property size and lock validation. If any of the server checks fail, then * this will not be invoked and a error response will be sent back to the * client. * * By default, the server will accept the request and modify the room's * properties as in the request. It will then send a success response back to * the sender and a user change properties notification will be sent to all * the subscribed users of the room. * * @param sender sender of the request * @param locksToUpdate the <key, value> pairs to be updated and locked by the sender * @param result use this to override the default behavior */ public void handleLockPropertiesRequest(IUser sender, HashMap<String, Object> locksToUpdate, HandlingResult result) /** * Invoked when a lockProperties request is received from the client. * The server has already validated the request meets its criteria of * lock validation (i.e. the sender holds the locks its requesting to unlock). * If the server checks fail, then this will not be invoked and a error response * will be sent back to the client. * * By default, the server will accept the request and modify the room's * lock table as in the request. It will then send a success response back to * the sender and a user change properties notification will be sent to all * the subscribed users of the room. * * @param sender sender of the request * @param unlockKeysArray array of keys of the properties to be unlocked */ public void onUnlockPropertiesRequest(IUser sender, ArrayList<String> unlockKeysList) /** * Invoked when a subscribe room request is received by the room. * * By default this will result in a success response sent back the user, * the user will be added to the list of subscribed users of the room. * * @param sender sender of the request * @param result use this to override the default behavior */ public void handleUserSubscribeRequest(IUser sender, HandlingResult result) /** * Invoked when a user sends an unsubscribe request * * @param sender sender of the request */ public void onUserUnsubscribeRequest(IUser sender)
BaseTurnRoomAdaptor
BaseTurnRoomAdaptor is provided to handle the turn based rooms. It contains following methods
/** * Invoked when a move request is received from the client whose turn it is. * * By default, the sender will be sent back a success response, the turn user will be * updated to the next user in order of joining and a move notification will be sent * to all the subscribers of the room. * * @param sender the user who has sent the move * @param moveData the move data sent by the user * @param result use this to override the default behavior */ public void handleMoveRequest(IUser sender, String moveData, HandlingResult result) /** * Invoked when a start game request is received from a client when the room is in * stopped state. * * By default a success response will be sent back to the client, the game state will * be updated and game started notification is sent to all the subscribers of the room. * * @param sender the user who has sent the request. * @param result use this to override the default behavior */ public void handleStartGameRequest(IUser sender, HandlingResult result) /** * Invoked when a stop game request is received from a client when the room is in * started state. * * By default a success response will be sent back to the client, the game state will * be updated and game stopped notification is sent to all the subscribers of the room. * * @param sender the user who has sent the request. * @param result use this to override the default behavior */ public void handleStopGameRequest(IUser sender, HandlingResult result) /** * Invoked when the timer expires for the current turn user. * * By default, the turn user will be updated to the next user in order of joining * and a move notification with empty data will be sent to all the subscribers of * the room. * * @param turn the current turn user whose turn has expired. * @param result use this to override the default behavior */ public void onTurnExpired(IUser turn, HandlingResult result) /** * Invoked when a user leaves the turn based room. * * By default, the turn user will be updated to the next user in order of joining * if the user who is leaving was the current turn user and a move notification with * empty data will be sent to all the subscribers of the room. * * @param user * @param result use this to override the default behavior */ public void handleUserLeavingTurnRoom(IUser user, HandlingResult result)
Handling Result
Most of the methods contains an additional parameter named result of HandlingResult type. Although optional but it can be used to change the default behavior of responses and notifications. It contains following information
// represents the result code sent back in response to the client. // 0 indicates success. code // whether response has to be send to requesting client or not. sendResponse // whether notification has to be send to all other clients or not. sendNotification // a string sent back to the client in response description // applicable only for turn based room requests. Determines whether the server should // perform the default turn logic in updating the turn user or leave that to the application doDefaultTurnLogic