Teams #

Read more about the Teams system in Hiro here.

Initializing the teams system #

The teams system relies on the Nakama System and an ILogger, both must be passed in as dependencies via the constructor.

1
2
var teamsSystem = new TeamsSystem(logger, nakamaSystem);
systems.Add(teamsSystem);

Subscribing to changes in the teams system #

You can listen for changes in the teams system so that you can respond appropriately, such as updating the UI, by implementing the IObserver pattern, or use the SystemObserver<T> type which handles it for you.

1
2
3
4
5
var disposer = SystemObserver<TeamsSystem>.Create(teamsSystem, system => {
    Instance.Logger.Info($"System updated.");

    // Update UI elements etc as necessary here...
});

Refreshing the teams system #

To ensure the teams system has the latest information from Nakama you can refresh it.

1
await teamsSystem.RefreshAsync();

Getting the user’s current team #

You can get the user’s current team.

1
2
var team = teamsSystem.Team;
Debug.Log($"Current Team: {team.Name}. Total members: {team.EdgeCount}");

You can also determine if the user is an Admin or Superadmin of their current team.

1
2
var isAdmin = teamsSystem.IsAdmin;
var isSuperAdmin = teamsSystem.IsSuperAdmin;

Getting the user’s current team members #

You can get the user’s current team members.

1
2
3
4
5
6
foreach (var teamMemberKvp in teamsSystem.TeamMembers)
{
    var teamMember = teamMemberKvp.Value;
    var online = teamMember.User.Online ? "Online" : "Offline";
    Debug.Log($"{teamMember.User.Username} ({online})");
}

Getting team members for other teams #

You can get the team members in other teams by passing the team id or a team object.

1
2
var memberList = await teamsSystem.GetTeamMembersAsync("<teamId>");
var memberList = await teamsSystem.GetTeamMembersAsync(team);

Getting the chat history for the user’s team #

You can get the chat history for the user’s team.

1
2
3
4
foreach (var message in teamsSystem.ChatHistory)
{
    Debug.Log($"{message.User.Username}: {message.Content}");
}

Creating a team #

You can create a team for the user. You must provide a function that returns a boolean value indicating whether or not the user can create a team.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var name = "MyTeam";
var description = "My team description";
var open = true;
var icon = "<iconUrl>";
var setupMetadata = "<metadata>";
var team = await teamsSystem.CreateTeamAsync(name, description, open, icon, setupMetadata, (client, session) => {
    // Check if the user is eligible to create a team
    // e.g. Are they high enough level?
    return Task.FromResult(true);
});

Listing available teams #

You can list available teams by location.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var location = "uk";
var limit = 10;
var cursor = "<optionalCursor>";

var teamList = await teamsSystem.ListTeamsAsync(location, limit, cursor);

foreach (var team in teamList.Teams)
{
    Debug.Log($"Team: {team.Name}. Total members: {team.EdgeCount}");
}

Searching for teams #

You can search for teams based on their name or shortcode.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var name = "<name>";
var shortcode = "<optionalShortcode>";
var limit = 10;

var teamList = await teamsSystem.SearchTeamsAsync(name, shortcode, limit);

foreach (var team in teamList.Teams)
{
    Debug.Log($"Team: {team.Name}. Total members: {team.EdgeCount}");
}

Joining a team #

You can join a team for the user either by passing the team id or by passing a team object.

1
2
await teamsSystem.JoinTeamAsync("<teamId>");
await teamsSystem.JoinTeamAsync(team);

Listing join requests for the user’s current team #

You can list join requests for the user’s current team

1
2
3
4
foreach (var joinRequest in teamsSystem.JoinRequests)
{
    Debug.Log($"{joinRequest.User.Username} requested to join.");
}

Approving team join requests #

You can approve a single join request, either by passing the user id of the user requesting to join, or by directly passing the group user object.

1
2
await teamsSystem.ApproveJoinRequestAsync("<teamId>", "<userId>");
await teamsSystem.ApproveJoinRequestAsync("<teamId>", groupUser);

You can also approve multiple join requests at once.

1
await teamsSystem.ApproveJoinRequestsAsync("<teamId>", new[] { "<userId1>", "<userId2>" });

Rejecting team join requests #

You can reject a single join request, either by passing the user id of the user requesting to join, or by directly passing the group user object.

1
2
await teamsSystem.RejectJoinRequestAsync("<teamId>", "<userId>");
await teamsSystem.RejectJoinRequestAsync("<teamId>", groupUser);

You can also reject multiple join requests at once.

1
await teamsSystem.RejectJoinRequestsAsync("<teamId>", new[] { "<userId1>", "<userId2>" });

Promoting team members #

You can promote team members if the user has sufficient priviledges.

1
await teamsSystem.PromoteUsersAsync("<teamId>", new[] { "<userId1>", "<userId2>" });

Leaving a team #

You can leave a team for the user either by passing the team id.

1
await teamsSystem.LeaveTeamAsync("<teamId>");

Or by passing a team object.

1
await teamsSystem.LeaveTeamAsync(team);

Deleting a team #

You can delete a team, providing the user has sufficient priviledges.

1
await teamsSystem.DeleteTeamAsync("<teamId>");

Sending a chat message to the user’s team #

You can send a chat message to the user’s team.

1
var messageAck = await teamsSystem.SendChatMessageAsync("<teamId>", "<contentJson>");