Skip to content
shakty edited this page Oct 5, 2022 · 1 revision

Group matching consist in assigning roles to players in the same group.

Currently the Matcher API supports automatic matching for pairs only, but it easy to assign roles in larger groups.

Step 1: Create a new a stage to assign roles

Ideally, you place this stage right before the stages in which roles are needed.

File: game.stages.js

   // ... previus steps.

   stager.stage('assignRoles')

   // ... steps in which roles are needed.

Step 2: Assign roles to players

In this example, we randomly assign role "BIDDER" to one player and role "RESPONDER" to the others.

File: logic.js

 stager.extendStep('assignRoles', {
        cb: function() {

            // Prepare for role assignment.
            let counter = 0;
            let roles = [ 'BIDDER', 'RESPONDER' ];
            // Random idx for role BIDDER.
            let R = J.randomInt(node.game.playerList.size());

            // Loop through connected players and send a message with role.
            node.game.playerList.each(player => {

                // Draw role and send message.
                let role = ++counter === R ? roles[0] : roles[1];
                node.say("ROLE", player.id, role);

                // Reconnecting clients receive role automatically.
                node.game.matcher.lastMatchesById[player.id] = {
                    role: role
                };

            });
        }
    });

Step 3: Store the role

The player waiting for role assignment must first set its own role in step 'assignRoles', and then carry over the role in the next step, where it is needed.

File: player.js

    stager.extendStep('assignRoles', {
        cb: function() {
            node.on.data('ROLE', msg => {
                // Manually set role.
                this.role = msg.data;
                // Mark done, so it can go to next step.
                node.done();
            })
        }
    });

    stager.extendStage('stage_with_roles', {
        // This line carries over the role set in previous stage.
        role: function() { return this.role; }
        // ...other step properties as needed (e.g., property roles must be set).
    });
Clone this wiki locally