| | 1 | | using DCL.Interface; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public interface IChatController |
| | 7 | | { |
| | 8 | | double initTime { get; } |
| | 9 | | event Action<ChatMessage> OnAddMessage; |
| | 10 | | List<ChatMessage> GetEntries(); |
| | 11 | |
|
| | 12 | | void AddMessageToChatWindow(string jsonMessage); |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class ChatController : MonoBehaviour, IChatController |
| | 16 | | { |
| 22 | 17 | | public static ChatController i { get; private set; } |
| 0 | 18 | | public double initTime { get; private set; } |
| | 19 | |
|
| | 20 | | public void Awake() |
| | 21 | | { |
| 0 | 22 | | i = this; |
| 0 | 23 | | initTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| 0 | 24 | | } |
| | 25 | |
|
| 0 | 26 | | [NonSerialized] public List<ChatMessage> entries = new List<ChatMessage>(); |
| | 27 | |
|
| | 28 | | public event Action<ChatMessage> OnAddMessage; |
| | 29 | |
|
| | 30 | | public void AddMessageToChatWindow(string jsonMessage) |
| | 31 | | { |
| 0 | 32 | | ChatMessage message = JsonUtility.FromJson<ChatMessage>(jsonMessage); |
| | 33 | |
|
| 0 | 34 | | if (message == null) |
| 0 | 35 | | return; |
| | 36 | |
|
| 0 | 37 | | entries.Add(message); |
| 0 | 38 | | OnAddMessage?.Invoke(message); |
| 0 | 39 | | } |
| | 40 | |
|
| 0 | 41 | | public List<ChatMessage> GetEntries() { return new List<ChatMessage>(entries); } |
| | 42 | |
|
| | 43 | | [ContextMenu("Fake Private Message")] |
| | 44 | | public void FakePrivateMessage() |
| | 45 | | { |
| 0 | 46 | | UserProfile ownProfile = UserProfile.GetOwnUserProfile(); |
| | 47 | |
|
| 0 | 48 | | var model = new UserProfileModel() |
| | 49 | | { |
| | 50 | | userId = "test user 1", |
| | 51 | | name = "test user 1", |
| | 52 | | }; |
| | 53 | |
|
| 0 | 54 | | UserProfileController.i.AddUserProfileToCatalog(model); |
| | 55 | |
|
| 0 | 56 | | var model2 = new UserProfileModel() |
| | 57 | | { |
| | 58 | | userId = "test user 2", |
| | 59 | | name = "test user 2", |
| | 60 | | }; |
| | 61 | |
|
| 0 | 62 | | UserProfileController.i.AddUserProfileToCatalog(model2); |
| | 63 | |
|
| 0 | 64 | | var msg = new ChatMessage() |
| | 65 | | { |
| | 66 | | body = "test message", |
| | 67 | | sender = model.userId, |
| | 68 | | recipient = ownProfile.userId, |
| | 69 | | messageType = ChatMessage.Type.PRIVATE |
| | 70 | | }; |
| | 71 | |
|
| 0 | 72 | | var msg2 = new ChatMessage() |
| | 73 | | { |
| | 74 | | body = "test message 2", |
| | 75 | | recipient = model2.userId, |
| | 76 | | sender = ownProfile.userId, |
| | 77 | | messageType = ChatMessage.Type.PRIVATE |
| | 78 | | }; |
| | 79 | |
|
| 0 | 80 | | AddMessageToChatWindow(JsonUtility.ToJson(msg)); |
| 0 | 81 | | AddMessageToChatWindow(JsonUtility.ToJson(msg2)); |
| 0 | 82 | | } |
| | 83 | | } |