Class MatchExpression
- All Implemented Interfaces:
Serializable
Overview
Match Expressions are conditional expressions used to filter Users or Rooms in specific API calls. They work like queries in a database and can be used to search for Rooms or Users using custom criteria. These expressions are very easy to build and concatenate to generate custom filters used to Match Users and Rooms.Here's a quick example to get started:
MatchExpression exp = new MatchExpression('rank', NumberMatch.GREATER_THAN, 5).and('country', StringMatch.EQUALS, 'Italy')
Expressions are made of three elements:
- Variable name
- Match operator
- Value
The search options are not just limited to User/Room Variables name. In fact the Matching engine provides two extra classes, RoomProperties and UserProperties, where you can access other specific attributes of the Room and User class.
This is an example of matching specific Room properties and Variables:
// Prepare match expression
MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true).and
(RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true).and
("isGameStarted", BoolMatch.EQUALS, false);
// Search Rooms
List<Rooms> joinableRooms = sfsApi.findRooms(zone.getRoomListFromGroup("chess"), exp, 0);
Advanced features
the Match expression offer advanced capabilities of searching through nested data structures such as SFSObject and SFSArray. This is done via a very simple dot-syntax. Here's an example of how it works:
MatchExpression exp = new MatchExpression("europe.italy.capital", StringMatch.EQUALS, "Rome")
The above example goes down deep into an SFSObject called europe, taking the italy object (another SFSObject) and finally reading its String field capital and matching it with another String. Here is one more examples using SFSObject and SFSArray:
MatchExpression exp = new MatchExpression("europe.italy.majorCities.3.name", StringMatch.EQUALS, "Milan")
From the italy object we obtain a majorCities SFSArray and we grab the third item in it (the .3 expression means 'give me the element at index == 3'). The item is again an SFSObject whose name property we finally compare to a String.
The power of Match Expression doesn't end here. You can run multiple passes of matching if you need complex searches to be performed. For example you can run a first match and obtain a list of filtered Rooms and then use it to apply another expression to further refine your search, and so on and so forth.
Also you will learn more interesting usages of Match Expressions in conjunction with the SFSGame class later in this very article.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMatchExpression(String varName, com.smartfoxserver.entities.match.IMatcher condition, Object value) Example of matching a User to its variables: -
Method Summary
Modifier and TypeMethodDescriptionConcatenates two expressions with the logical AND operator.static MatchExpressionfromSFSArray(ISFSArray sfsa) booleanhasNext()next()Concatenates two expressions with the logical OR operator.rewind()toString()
-
Constructor Details
-
MatchExpression
public MatchExpression(String varName, com.smartfoxserver.entities.match.IMatcher condition, Object value) Example of matching a User to its variables:
This will search for all Users in the Zone that have a UserVariable called avgScore greater than 20, or a variable "rank" whose value is greater than 5.var matchExp = MatchExpression("avgScore", NumberMatch.GREATER_THAN, 20) .or("rank", NumberMatch.GREATER_THAN, 5); var res = getApi().findUsers(getParentZone.getUserList(), matchExp, 10);Example of matching a User to its properties:
This will search for all Users in the Extension's Room whose name contains Great and that is set as player in that Room.var matchExp = MatchExpression(UserProperties.NAME, StringMatch.CONTAINS, "Great") .and(UserProperties.IS_PLAYER, BoolMatch.EQUALS, true); var res = getApi().findUsers(getParentRoom.getUserList(), matchExp, 10);- Parameters:
varName- name of a Room/User variable or Room/User propertycondition- a condition to be matchedvalue- a value to be compared
-
-
Method Details
-
and
public MatchExpression and(String varName, com.smartfoxserver.entities.match.IMatcher condition, Object value) Concatenates two expressions with the logical AND operator.- Parameters:
varName- name of the variable or propertycondition- the match conditionvalue- the value to match- Returns:
- the new resulting MatchExpression
- See Also:
-
or
public MatchExpression or(String varName, com.smartfoxserver.entities.match.IMatcher condition, Object value) Concatenates two expressions with the logical OR operator.- Parameters:
varName- name of the variable or propertycondition- the match conditionvalue- the value to match- Returns:
- the new resulting MatchExpression
- See Also:
-
toString
-
hasNext
public boolean hasNext()- Internal
-
next
- Internal
-
rewind
- Internal
-
fromSFSArray
- Internal
-
toSFSArray
- Internal
-