/**
* Copyright 2019 Heroic Labs and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using UnityEngine;
using System;
namespace DemoGame.Scripts.Chat
{
///
/// Bypass between ChatManager and ChatChannel UI.
///
public class ChatChannel
{
#region public variables
///
/// Id of chat channel used to communicate with channel instance on server
///
public readonly string Id;
///
/// Name of channel, could be nickname of user in 1-on-1 chat or name of clan in group chat
///
public string ChannelName;
///
/// Cursor for next message in channel history. Used for loading more channel history.
///
public string NextCursor;
#endregion
#region public properties
///
/// List of active users usernames
///
public List ActiveUsersUsernames
{
get
{
List usernames = new List();
foreach (var user in _users)
{
usernames.Add(user.Value.Username);
}
return usernames;
}
}
#endregion
#region public events
///
/// An event fired when new message was added by any user.
/// Params: messageId, userid, username, content, create time, is loaded from history
///
public event Action OnChatMessage = delegate { };
///
/// An event fired when new message was edited by any user.
/// Params: messageId, content, create time
///
public event Action OnChatUpdate = delegate { };
///
/// An event fired when new message was removed by any user.
/// Params: messageIds
///
public event Action OnChatRemove = delegate { };
///
/// An event fired when received new message from server.
/// Used when any player joined group, left group, was added to group, was kicked from group and was promoted in group
/// Params: messageId, content
///
public event Action OnServerMessage = delegate { };
///
/// An event fired when any player joined the channel.
///
public event Action OnJoinedChannel = delegate { };
///
/// An event fired when any player left the channel.
///
public event Action OnLeftChannel = delegate { };
#endregion
#region private variables
///
/// List of active channel users
///
private Dictionary _users = new Dictionary();
#endregion
#region Constructor
///
/// Creates new ChatChannel with given Id
///
///
public ChatChannel(string channelId)
{
Id = channelId;
}
#endregion
#region public methods
///
/// Fires OnChatMessage event, when new message comes
///
public void ChatMessage(string messageId, string userId, string username, string content, string createTime, bool historical = false)
{
OnChatMessage(messageId, userId, username, content, createTime, historical);
}
///
/// Fires OnChatUpdate event when update for message comes
///
public void ChatUpdate(string messageId, string content, string createTime)
{
OnChatUpdate(messageId, content);
}
///
/// Fires OnChatRemove event when remove message information comes
///
public void ChatRemove(string messageId)
{
OnChatRemove(messageId);
}
///
/// Fires OnServerMessage event with username and information about joining
///
public void JoinedGroup(string messageId, string username, bool historical = false)
{
OnServerMessage(messageId, username + " has joined", historical);
}
///
/// Fires OnServerMessage event with username and infrmation about adding
///
public void AddedToGroup(string messageId, string username, bool historical = false)
{
OnServerMessage(messageId, username + " was added", historical);
}
///
/// Fires OnServerMessage event with username and information about leaving
///
public void LeftGroup(string messageId, string username, bool historical = false)
{
OnServerMessage(messageId, username + " has left", historical);
}
///
/// Fires OnServerMessage event with username and information about kicking
///
public void KickedFromGroup(string messageId, string username, bool historical = false)
{
OnServerMessage(messageId, username + " was kicked", historical);
}
///
/// Fires OnServerMessage event with username and information about promoting
///
public void PromotedInGroup(string messageId, string username, bool historical = false)
{
OnServerMessage(messageId, username + " was promoted", historical);
}
public void JoinedChannel(string userId, string username, string avatar = null)
{
Debug.Log("User joined: " + username);
ChatUser newUser = new ChatUser(userId, username, avatar);
_users.Add(userId, newUser);
OnJoinedChannel(newUser);
}
///
/// Fires OnLeftChannel event with ChatUser and then removes this user from _users dictionary
///
///
public void LeftChannel(string userId)
{
Debug.Log("User left: " + _users[userId].Username);
OnLeftChannel(_users[userId]);
_users.Remove(userId);
}
#endregion
}
}