MatchExpression
A matching expression used to compare custom variables or predefined properties when searching for users or Rooms.
new MatchExpression(varName, condition, value)
Creates a new MatchExpression instance.
Matching expression are used to compare custom variables or predefined properties when searching for users or Rooms. They are built like "if" statements in any common programming language. They work like queries in a database and can be used to search for Rooms or users using custom criteria: in fact a matching expression can compare predefined properties of the Room and user entities (see the RoomProperties and UserProperties classes), but also custom Room or User Variables.
These expressions are easy to create and concatenate, and they can be used for many different filtering operations within the SmartFoxServer 2X framework, for example to invite players to join a game (see the CreateSFSGameRequest request description), to look for specific Rooms or users (see the FindRoomsRequest and FindUsersRequest requests descriptions), etc.
Additionally:
- any number of expressions can be linked together with the logical AND and OR operators to create complex queries
- searching through nested data structures such as objects and arrays can be done via a very simple dot-syntax
Examples
This example shows how to create a simple matching expression made of two concatenated conditions: it compares the custom "rank" and "country" User Variables to the passed values. This expression could be used during the creation of a Game Room, to filter the users that the server should take into account when sending the invitations to join the game (only italian users with a ranking greater than 5 - whatever this number means to our game).
var exp = new SFS2X.MatchExpression('rank', SFS2X.NumberMatch.GREATER_THAN, 5)
.and('country', SFS2X.StringMatch.EQUALS, 'Italy');
This example creates a matching expression made of three concatenated conditions which compare two predefined Room properties and the custom "isGameStarted" Room Variable to the passed values; this expression could be used to retrieve all the Game Rooms still waiting for players to join them.
var exp = new SFS2X.MatchExpression(SFS2X.RoomProperties.IS_GAME, SFS2X.BoolMatch.EQUALS, true)
.and(SFS2X.RoomProperties.HAS_FREE_PLAYER_SLOTS, SFS2X.BoolMatch.EQUALS, true)
.and('isGameStarted', SFS2X.BoolMatch.EQUALS, false);
This example creates a matching expression which compares a nested property in a complex data structure; an object called "avatarData" (could be a User Variable for example) contains the "shield" object (a nested object) which in turn contains, among others, the "inUse" property which could be used to retrieve all user whose avatars are currently equipped with a shield.
var exp = new SFS2X.MatchExpression('avatarData.shield.inUse', SFS2X.BoolMatch.EQUALS, true);
This example is similar to the previous one, but it involves an array. The "avatarData" object contains the "weapons" array, from which the expression retrieves the third element (that ".3" means "give me the element at index == 3") that we know being the weapon the user avatar has in his right hand. Again, this element is an object containing, among the others, the "name" property which can be compared to the passed string. This example could be used to retrieve all users whose avatars have the Narsil sword in the right hand.
var exp = new SFS2X.MatchExpression('avatarData.weapons.3.name', SFS2X.StringMatch.EQUALS, "Narsil");
Parameters
Name | Type | Optional | Description |
---|---|---|---|
varName |
|
|
The name of the variable or property to match. |
condition |
|
|
A matching condition among those provided by the BoolMatch, NumberMatch and StringMatch classes. |
value |
|
|
The value to compare against the variable or property during the matching. |
Properties
condition Matcher
Returns the matching criteria used during values comparison among those provided by the BoolMatch, NumberMatch and StringMatch classes.
- See also
- BoolMatch
- NumberMatch
- StringMatch
logicOp LogicOperator
In case of concatenated expressions, returns the current logical operator.
next MatchExpression
Returns the next matching expression concatenated to the current one, if existing.
value any type
Returns the value against which the variable or property corresponding to varName is compared.
varName string
Returns the name of the variable or property against which the comparison is made.
Depending what the matching expression is used for (searching a user or a Room), this can be the name of a User Variable or a Room Variable, or it can be one of the constants contained in the UserProperties or RoomProperties classes, representing some of the predefined properties of the user and Room entities respectively.
Methods
and(varName, condition, value) → MatchExpression
Concatenates the current expression with a new one using the logical AND operator.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
varName |
string |
|
Name of the additional variable or property to match. |
condition |
|
The additional matching condition among those provided by the BoolMatch, NumberMatch and StringMatch classes. |
|
value |
(boolean, number, or string) |
|
The value to compare against the additional variable or property during the matching. |
- See also
- LogicOperator.AND
- Returns
-
MatchExpression
A new MatchExpression resulting from the concatenation of the current expression with a new one generated from the specified parameters.
hasNext() → boolean
Checks if the current matching expression is concatenated to another one through a logical operator.
- Returns
-
boolean
true
if the current matching expression is concatenated to another one.
or(varName, condition, value) → MatchExpression
Concatenates the current expression with a new one using the logical OR operator.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
varName |
string |
|
Name of the additional variable or property to match. |
condition |
|
The additional matching condition among those provided by the BoolMatch, NumberMatch and StringMatch classes. |
|
value |
(boolean, number, or string) |
|
The value to compare against the additional variable or property during the matching. |
- See also
- LogicOperator.OR
- Returns
-
MatchExpression
A new MatchExpression resulting from the concatenation of the current expression with a new one generated from the specified parameters.
rewind() → MatchExpression
Moves the iterator cursor to the first matching expression in the chain.
- Returns
-
MatchExpression
The MatchExpression object at the top of the chain of matching expressions.
toString() → string
Returns a string representation of the matching expression.
- Returns
-
string
The string representation of the MatchExpression object.