@Test public void testDeserializeCustom() { SFSSerializer sfsSerializer = new SFSSerializer(); ISFSObject entityObj = new SFSObject(); entityObj.putInt("intField", 10); ISFSArray subArray = new SFSArray(); ISFSArray subCustomArray = new SFSArray(); SFSObject subObj1 = new SFSObject(); SFSObject subCustomObj = new SFSObject(); subObj1.putLong("longField", 30L); subCustomObj.putLong("longField", 99L); subArray.addSFSObject(subObj1); subCustomArray.addSFSObject(subCustomObj); entityObj.putSFSArray("wildcardList", subArray); entityObj.putSFSArray("wildcardedListCustom", subCustomArray); Entity entity = new Entity(); sfsSerializer.deserialize(entity, entityObj); assertNotNull("Wildcarded deserialized list must not be null!", entity.getWildcardList()); assertFalse( "Wildcarded deserialized list must not be empty!", entity.getWildcardList().isEmpty()); assertEquals(30L, ((SubEntity) entity.getWildcardList().get(0)).getLongField().longValue()); assertEquals( 99L, ((SubEntity) entity.getWildcardedListCustom().get(0)).getLongField().longValue()); }
public void handleServerEvent(ISFSEvent event) throws SFSException { User user = (User) event.getParameter(SFSEventParam.USER); trace("ClueGameExtension UserDisconnectEventHandler got user: "******"room"); if (uv == null) return; Room room = (Room) getParentExtension().getParentZone().getRoomByName(uv.getStringValue()); trace("ClueGameExtension UserDisconnectEventHandler got room: " + room); // handles both the user disconnected event and the user exit room events if (room != null) { // set the game to over if (room.getUserList().size() < 3) { room.removeVariable("isGameStarted"); } } // send the note to all other players List<User> users = room.getUserList(); ISFSObject resObj = SFSObject.newInstance(); resObj.putUtfString( "message", "Game is over because there are less than 3 players. A player has disconnected from the server."); send("gameOver", resObj, users); }
private void customRoomLogin(ISFSEvent event) throws SFSLoginException { trace("Game Login by session"); DBCollection users = AuthorizeExtension.users; BasicDBObject query = new BasicDBObject(); query.put("session", generateSession); DBCursor cursor = users.find(query); if (!cursor.hasNext()) { trace("Game Login User not found!", generateSession); SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD); data.addParameter(userName); throw new SFSLoginException("Login failed for user: "******"Game Login User logged in!", generateSession); document = cursor.next(); ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA); outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, document.get("nickname").toString()); Boolean isGuest = (Boolean) document.get("is_guest"); if (isGuest == null) { isRegistered = true; } else { isRegistered = false; } } }
@Test public void testBasicTransportObject() { SFSSerializer serializer = new SFSSerializer(); ISFSObject input = new SFSObject(); input.putUtfString("name", "John"); input.putUtfString("surname", "Smith"); input.putLong("age", 50L); BasicTransportObject to = serializer.deserialize(BasicTransportObject.class, input); assertEquals("John", to.get("name")); assertEquals("Smith", to.get("surname")); assertEquals(50L, to.get("age")); }
@Override public void handleClientRequest(User paramUser, ISFSObject paramISFSObject) { String data = paramISFSObject.getUtfString("data"); ISFSObject parserObj = SFSObject.newFromJsonData(data); System.out.println("username : "******"username")); System.out.println("address :" + parserObj.getUtfString("address")); // String from = "*****@*****.**"; // String to = "*****@*****.**"; // Email myEmail = new SFSEmail(from, to, "Test mail", "Hello from SFS2X"); // try { // SmartFoxServer.getInstance().getMailService().sendMail(myEmail); // } catch (MessagingException e) { // e.printStackTrace(); // trace(e.getMessage()); // } send("zcq", parserObj, paramUser); }
@Override public void handleClientRequest(User user, ISFSObject data) { MainExtension extension = ((MainExtension) getParentExtension()); Pawn owner = (Pawn) extension.viewManager.getView(data.getInt("pawnId")); if (owner != null) { if (!owner.isOwner(user)) { return; } } WeaponModel weaponModel = (WeaponModel) data.getClass("weapon"); Weapon weapon = new Weapon(weaponModel); extension.viewManager.addView(weapon); if (owner == null) { weapon.lateId = data.getInt("pawnId"); extension.earlyWeaponAdd(weapon); } else { extension.SendWeaponRequest(user, owner, weapon); } }
private void handleGuest(ISFSEvent event) throws NoSuchElementException { trace("Guest is loggin in", userName); DBCollection users = AuthorizeExtension.users; // Create random num 0..99 Random randomGenerator = new Random(); userName = userName + randomGenerator.nextInt(100); // Set new Name ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA); outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, userName); // Insert DB BasicDBObject newUser = new BasicDBObject(); newUser.put("nickname", userName); newUser.put("session", generateSession); newUser.put("is_guest", true); users.insert(newUser); isRegistered = false; }
@Test public void testSerializeCustom() { SFSSerializer sfsSerializer = new SFSSerializer(); Entity entity = new Entity(); Entity subEntity = new Entity(); Entity subEntityCustom = new Entity(); subEntity.setIntField(10); subEntityCustom.setIntField(99); entity.setWildcardList(Arrays.asList(subEntity)); entity.setWildcardedListCustom(Arrays.asList(subEntityCustom)); sfsSerializer.registerPostProcessor( new SerializePostProcessor() { @Override public <ST, DT extends TransportObject> void process(ST result, DT sourceObject) { if (sourceObject instanceof Entity) ((ISFSObject) result).putBool("postProcessed", true); } }); sfsSerializer.registerPreProcessor( new SerializePreProcessor() { @Override public <DT> void process(DT sourceObject) { if (sourceObject instanceof Entity) ((Entity) sourceObject).setPreProcessed(true); } }); ISFSObject sObj = sfsSerializer.serialize(entity); assertNotNull(sObj.getSFSArray("wildcardList")); assertTrue(sObj.getBool("postProcessed")); assertTrue(sObj.getBool("preProcessed")); Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("wildcardList").iterator(); assertEquals( subEntity.getIntField() * 2, ((SFSObject) iterator.next().getObject()).getInt("intField").intValue()); Iterator<SFSDataWrapper> customIterator = sObj.getSFSArray("wildcardedListCustom").iterator(); assertEquals( subEntityCustom.getIntField(), ((SFSObject) customIterator.next().getObject()).getInt("intField")); }
@Override public void handleClientRequest(User user, ISFSObject isfso) { trace("isplay: " + user.getVariable("isplay")); Boolean isplay = user.getVariable("isplay").getBoolValue(); if (isplay == true) { if (user.getVariable("ischuong").getBoolValue()) { ISFSObject obj = new SFSObject(); obj.putBool("status", false); obj.putUtfString("message", "Chương không có quyền đặt tiền"); send("betgame", obj, user); } else { Double bet = 0.0; bet = isfso.getDouble("betmoney"); trace(bet); SFSUserVariable money = SFSUserVariable.newInstance("bet", bet); try { user.setVariable(money); } catch (SFSVariableException ex) { } trace("Thong tin user sau bet game: " + user.getVariable("bet")); ISFSObject obj = new SFSObject(); obj.putBool("status", true); obj.putInt("betid", user.getId()); obj.putDouble("betmoney", bet); send("betgame", obj, this.getParentExtension().getParentRoom().getUserList()); } } else { ISFSObject obj = new SFSObject(); obj.putBool("status", false); obj.putUtfString("message", "Không có quyền chơi, chờ ván sau"); send("betgame", obj, user); } }
@Test public void testDeserialize() { SFSSerializer sfsSerializer = new SFSSerializer(); ISFSObject subEntityObj = new SFSObject(); subEntityObj.putLong("longField", 20L); ISFSObject entityObj = new SFSObject(); entityObj.putInt("intField", 10); entityObj.putSFSObject("subEntity", subEntityObj); entityObj.putUtfString("notDeserializable", "value1"); entityObj.putUtfString("notSerializable", "value2"); entityObj.putUtfString("enumField", "white"); entityObj.putUtfString("changedName", "changedValue"); entityObj.putUtfString("fieldWithoutGetter", "value"); entityObj.putInt("fieldCustomSerializable", 10); entityObj.putUtfString("totallyIgnoredField", "value"); entityObj.putUtfStringArray("colors", Arrays.asList("black", "black", "white")); ISFSArray subArray = new SFSArray(); SFSObject subObj1 = new SFSObject(); subObj1.putLong("longField", 30L); subObj1.putUtfStringArray("stringsList", Arrays.asList("testString")); SFSObject subObj2 = new SFSObject(); subArray.addSFSObject(subObj1); subArray.addSFSObject(subObj2); entityObj.putSFSArray("subEntities", subArray); Entity entity = sfsSerializer.deserialize(Entity.class, entityObj); assertEquals(Entity.Color.white, entity.getEnumField()); assertEquals(Entity.Color.white, entity.getColors().get(2)); assertEquals(entityObj.getInt("intField"), entity.getIntField()); assertEquals((Long) 10L, entity.fieldCustomSerializable); assertEquals(entityObj.getUtfString("fieldWithoutGetter"), entity.fieldWithoutGetter); assertEquals(entityObj.getUtfString("notSerializable"), entity.getNotSerializable()); assertEquals(entityObj.getUtfString("changedName"), entity.getNameToBeChanged()); assertNull(entity.getNotDeserializable()); assertNull(entity.totallyIgnoredField); assertEquals(subEntityObj.getLong("longField"), entity.getSubEntity().getLongField()); assertNotNull(entity.getSubEntities()); assertFalse(entity.getSubEntities().isEmpty()); assertEquals(30L, entity.getSubEntities().get(0).getLongField().longValue()); assertEquals("testString", entity.getSubEntities().get(0).getStringsList().get(0)); }
@CustomListItemDeserializer(listName = "wildcardList") public TransportObject deserializeWildcardItem(ISFSObject object) { SubEntity res = new SubEntity(); res.setLongField(object.getLong("longField")); return res; }
@Test public void testSerialize() { SFSSerializer sfsSerializer = new SFSSerializer(); SubEntity subEntity = new SubEntity(); subEntity.setLongField(10L); subEntity.setStringsList(Arrays.asList("testString")); Entity entity = new Entity(); entity.setIntField(100); entity.setStringField("string"); entity.setSubEntity(subEntity); entity.setSubEntities(Arrays.asList(new SubEntity(100L), new SubEntity(100L))); entity.setNotDeserializable("value1"); entity.fieldWithoutGetter = "value"; entity.setNotSerializable("value2"); entity.setEnumField(Entity.Color.black); entity.setColors(Arrays.asList(Entity.Color.black, Entity.Color.white)); entity.fieldCustomSerializable = 20L; entity.totallyIgnoredField = "value"; ISFSObject sObj = sfsSerializer.serialize(entity); assertEquals(entity.getIntField(), sObj.getInt("intField")); assertEquals(entity.fieldWithoutGetter, sObj.getUtfString("fieldWithoutGetter")); assertEquals(entity.getEnumField().name(), sObj.getUtfString("enumField")); assertEquals(entity.getNameToBeChanged(), sObj.getUtfString("changedName")); assertEquals(entity.getNotDeserializable(), sObj.getUtfString("notDeserializable")); assertEquals(20, sObj.getInt("fieldCustomSerializable").intValue()); Collection<String> colors = sObj.getUtfStringArray("colors"); assertTrue(colors.contains(entity.getColors().get(1).name())); assertNull(sObj.getUtfString("totallyIgnoredField")); assertNull(sObj.getUtfString("notSerializable")); assertNull(sObj.getUtfString("ignoredField")); assertEquals(entity.getStringField(), sObj.getUtfString("stringField")); assertNotNull(sObj.getSFSObject("subEntity")); assertEquals(subEntity.getLongField(), sObj.getSFSObject("subEntity").getLong("longField")); assertNotNull(sObj.getSFSArray("subEntities")); Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("subEntities").iterator(); assertEquals(100L, ((SFSObject) iterator.next().getObject()).getLong("longField").longValue()); assertEquals( "testString", sObj.getSFSObject("subEntity").getUtfStringArray("stringsList").iterator().next()); }