Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions Assets/Talo Game Services/Talo/Runtime/APIs/ChannelsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,27 @@ public string ToQueryString()
}
}

public class CreateChannelOptions
{
public string name;
public (string, string)[] props = Array.Empty<(string, string)>();

public bool autoCleanup = false;
public bool isPrivate = false;
public bool temporaryMembership = false;
}

public enum ChannelLeavingReason
{
Default,
TemporaryMembership
}

public class ChannelsAPI : BaseAPI
{
public event Action<Channel, PlayerAlias, string> OnMessageReceived;
public event Action<Channel, PlayerAlias> OnChannelJoined;
public event Action<Channel, PlayerAlias> OnChannelLeft;
public event Action<Channel, PlayerAlias, ChannelLeavingReason> OnChannelLeft;
public event Action<Channel, PlayerAlias> OnOwnershipTransferred;
public event Action<Channel> OnChannelDeleted;
public event Action<Channel, string[]> OnChannelUpdated;
Expand All @@ -63,7 +79,7 @@ public ChannelsAPI() : base("v1/game-channels")
else if (response.GetResponseType() == "v1.channels.left")
{
var data = response.GetData<ChannelLeftResponse>();
OnChannelLeft?.Invoke(data.channel, data.playerAlias);
OnChannelLeft?.Invoke(data.channel, data.playerAlias, data.meta.reason);
}
else if (response.GetResponseType() == "v1.channels.ownership-transferred")
{
Expand Down Expand Up @@ -113,39 +129,57 @@ public async Task<Channel[]> GetSubscribedChannels(GetSubscribedChannelsOptions
return res.channels;
}

private async Task<Channel> SendCreateChannelRequest(
string name,
bool autoCleanup = false,
bool isPrivate = false,
params (string, string)[] propTuples
)
private async Task<Channel> SendCreateChannelRequest(CreateChannelOptions options)
{
Talo.IdentityCheck();

var props = propTuples.Select((propTuple) => new Prop(propTuple)).ToArray();
var props = options.props.Select((propTuple) => new Prop(propTuple)).ToArray();

var uri = new Uri(baseUrl);
var content = JsonUtility.ToJson(new ChannelsCreateRequest
{
name = name,
autoCleanup = autoCleanup,
name = options.name,
autoCleanup = options.autoCleanup,
props = props,
@private = isPrivate
@private = options.isPrivate,
temporaryMembership = options.temporaryMembership
});
var json = await Call(uri, "POST", content);

var res = JsonUtility.FromJson<ChannelResponse>(json);
return res.channel;
}

public async Task<Channel> Create(CreateChannelOptions options)
{
options ??= new CreateChannelOptions();
return await SendCreateChannelRequest(options);
}

[Obsolete("Use Create(CreateChannelOptions options) instead.")]
public async Task<Channel> Create(string name, bool autoCleanup = false, params (string, string)[] propTuples)
{
return await SendCreateChannelRequest(name, autoCleanup, false, propTuples);
var options = new CreateChannelOptions
{
name = name,
autoCleanup = autoCleanup,
props = propTuples,
isPrivate = false
};
return await SendCreateChannelRequest(options);
}

[Obsolete("Use Create(CreateChannelOptions options) instead.")]
public async Task<Channel> CreatePrivate(string name, bool autoCleanup = false, params (string, string)[] propTuples)
{
return await SendCreateChannelRequest(name, autoCleanup, true, propTuples);
var options = new CreateChannelOptions
{
name = name,
autoCleanup = autoCleanup,
props = propTuples,
isPrivate = true
};
return await SendCreateChannelRequest(options);
}

public async Task<Channel> Join(int channelId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class ChannelsCreateRequest
public bool autoCleanup;
public Prop[] props;
public bool @private;
public bool temporaryMembership;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ public class ChannelLeftResponse
{
public Channel channel;
public PlayerAlias playerAlias;
public ChannelLeftResponseMetadata meta;
}

[System.Serializable]
public class ChannelLeftResponseMetadata
{
public ChannelLeavingReason reason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private async void OnCreateChannelClick()
return;
}

var channel = await Talo.Channels.Create(channelName);
var channel = await Talo.Channels.Create(new CreateChannelOptions() { name = channelName, autoCleanup = true });
AddChannelToList(channel);
channelNameField.value = "";
activeChannelId = channel.id;
Expand Down