395 lines
11 KiB
C#
395 lines
11 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using DG.Tweening;
|
|
using FishNet.Managing;
|
|
using FishNet.Object;
|
|
using FishNet.Transporting;
|
|
using Heathen.SteamworksIntegration;
|
|
using Obvious.Soap;
|
|
using Steamworks;
|
|
using TMPro;
|
|
using TankAndHealerStudioAssets;
|
|
using UnityEngine;
|
|
|
|
public class UI_MultiplayerChatbox : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public struct ChatMessageData
|
|
{
|
|
public string username;
|
|
|
|
public string message;
|
|
|
|
public int styleIndex;
|
|
|
|
public ChatMessageData(string username, string message, UltimateChatBox.ChatStyle chatStyle)
|
|
{
|
|
this.username = username;
|
|
this.message = message;
|
|
styleIndex = GetStyleIndex(chatStyle);
|
|
}
|
|
|
|
public UltimateChatBox.ChatStyle GetStyle()
|
|
{
|
|
return styleIndex switch
|
|
{
|
|
0 => UltimateChatBoxStyles.boldUsername,
|
|
1 => UltimateChatBoxStyles.errorUsername,
|
|
2 => UltimateChatBoxStyles.greenUsername,
|
|
_ => UltimateChatBoxStyles.boldUsername,
|
|
};
|
|
}
|
|
|
|
public static int GetStyleIndex(UltimateChatBox.ChatStyle chatStyle)
|
|
{
|
|
if (chatStyle.Equals(UltimateChatBoxStyles.boldUsername))
|
|
{
|
|
return 0;
|
|
}
|
|
if (chatStyle.Equals(UltimateChatBoxStyles.errorUsername))
|
|
{
|
|
return 1;
|
|
}
|
|
if (chatStyle.Equals(UltimateChatBoxStyles.greenUsername))
|
|
{
|
|
return 2;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private LobbyManager lobbyManager;
|
|
|
|
[SerializeField]
|
|
private ScriptableLobbyDataVariable scriptable_variable_LobbyData;
|
|
|
|
[SerializeField]
|
|
private UltimateChatBox ultimateChatBox;
|
|
|
|
[SerializeField]
|
|
private CanvasGroup chatCanvasGroup;
|
|
|
|
[SerializeField]
|
|
private BoolVariable isChatButtonPressed;
|
|
|
|
[SerializeField]
|
|
private BoolVariable isChatCancelButtonPressed;
|
|
|
|
[SerializeField]
|
|
private ScriptableEventFishData onFishCatch;
|
|
|
|
[SerializeField]
|
|
private BoolVariable IsChatboxActive;
|
|
|
|
[SerializeField]
|
|
private Transform fadingMessageParent;
|
|
|
|
[SerializeField]
|
|
private TMP_Text fadingMessage;
|
|
|
|
private bool chatIsVisible;
|
|
|
|
private LobbyManager LobbyManager
|
|
{
|
|
get
|
|
{
|
|
lobbyManager = lobbyManager ?? (lobbyManager = UnityEngine.Object.FindFirstObjectByType<LobbyManager>());
|
|
return lobbyManager;
|
|
}
|
|
}
|
|
|
|
private NetworkManager NetworkManager
|
|
{
|
|
get
|
|
{
|
|
if (NetworkManager.Instances.Count <= 0)
|
|
{
|
|
return null;
|
|
}
|
|
return NetworkManager.Instances[0];
|
|
}
|
|
}
|
|
|
|
private void IsChatButtonPressed_OnValueChanged(bool isPressed)
|
|
{
|
|
if (isPressed)
|
|
{
|
|
if (chatIsVisible)
|
|
{
|
|
chatIsVisible = false;
|
|
ultimateChatBox.DisableInputField();
|
|
DOTween.Kill(chatCanvasGroup);
|
|
chatCanvasGroup.DOFade(0f, 0.5f);
|
|
}
|
|
else
|
|
{
|
|
chatIsVisible = true;
|
|
ultimateChatBox.DisableInputField();
|
|
DOTween.Kill(chatCanvasGroup);
|
|
chatCanvasGroup.DOFade(1f, 0.5f);
|
|
ClearFadingMessages();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void IsChatCancelButtonPressed_OnValueChanged(bool isPressed)
|
|
{
|
|
if (IsChatboxActive.Value)
|
|
{
|
|
DOTween.Kill(this);
|
|
DOTween.Sequence().AppendInterval(Time.deltaTime).AppendCallback(delegate
|
|
{
|
|
ultimateChatBox.DisableInputField();
|
|
IsChatboxActive.Value = false;
|
|
})
|
|
.SetTarget(this);
|
|
}
|
|
}
|
|
|
|
private void UltimateChatBox_OnInputFieldSubmitted(string text)
|
|
{
|
|
if (text.Length > 50)
|
|
{
|
|
text = text.Substring(0, 50) + "...";
|
|
}
|
|
string username = "Unknown Player";
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
username = UserData.Me.Name;
|
|
}
|
|
ultimateChatBox.RegisterChat(username, text, UltimateChatBoxStyles.boldUsername);
|
|
if (LobbyManager.HasLobby)
|
|
{
|
|
string message = JsonUtility.ToJson(new ChatMessageData(username, text, UltimateChatBoxStyles.boldUsername));
|
|
LobbyManager.SendChatMessage(message);
|
|
}
|
|
}
|
|
|
|
private void UltimateChatBox_OnInputFieldEnabled()
|
|
{
|
|
if (!chatIsVisible)
|
|
{
|
|
ultimateChatBox.DisableInputField();
|
|
}
|
|
else
|
|
{
|
|
IsChatboxActive.Value = true;
|
|
}
|
|
}
|
|
|
|
private void UltimateChatBox_OnInputFieldDisabled()
|
|
{
|
|
IsChatboxActive.Value = false;
|
|
}
|
|
|
|
private void OnUserJoinedLobby(UserData user)
|
|
{
|
|
ultimateChatBox.RegisterChat("System", user.Name + " has joined the lobby.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
|
|
private void OnUserLeftLobby(UserLobbyLeaveData leaveData)
|
|
{
|
|
ultimateChatBox.RegisterChat("System", leaveData.user.Name + " has left the lobby.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
|
|
private void OnChatMsgReceived(LobbyChatMsg msg)
|
|
{
|
|
CSteamID cSteamID = default(CSteamID);
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
cSteamID = UserData.Me.id;
|
|
}
|
|
if (msg.sender.id == cSteamID)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
ChatMessageData chatMessageData = JsonUtility.FromJson<ChatMessageData>(Encoding.UTF8.GetString(msg.data));
|
|
if (!string.IsNullOrWhiteSpace(chatMessageData.username) && !string.IsNullOrWhiteSpace(chatMessageData.message))
|
|
{
|
|
ultimateChatBox.RegisterChat(chatMessageData.username, chatMessageData.message, chatMessageData.GetStyle());
|
|
if (!chatIsVisible)
|
|
{
|
|
AddFadingMessage(chatMessageData.username, chatMessageData.message, chatMessageData.GetStyle());
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning("Failed to parse chat message: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void ClientManager_OnRemoteConnectionState(RemoteConnectionStateArgs remoteConnection)
|
|
{
|
|
StartCoroutine(RemoteConnectionStateCoroutine(remoteConnection));
|
|
IEnumerator RemoteConnectionStateCoroutine(RemoteConnectionStateArgs remoteConnectionStateArgs)
|
|
{
|
|
ulong steamID = 0uL;
|
|
float timeout = 3f;
|
|
float elapsed = 0f;
|
|
float interval = 0.2f;
|
|
if (NetworkManager.ClientManager.Clients.TryGetValue(remoteConnectionStateArgs.ConnectionId, out var client))
|
|
{
|
|
while (elapsed < timeout)
|
|
{
|
|
FishNetSpawnerProxy fishNetSpawnerProxy = client.Objects.FirstOrDefault((NetworkObject obj) => obj.GetComponent<FishNetSpawnerProxy>() != null)?.GetComponent<FishNetSpawnerProxy>();
|
|
if (fishNetSpawnerProxy != null && fishNetSpawnerProxy.SteamID != 0)
|
|
{
|
|
steamID = fishNetSpawnerProxy.SteamID;
|
|
break;
|
|
}
|
|
elapsed += interval;
|
|
yield return new WaitForSeconds(interval);
|
|
}
|
|
}
|
|
if (steamID != 0)
|
|
{
|
|
LobbyMemberData lobbyMemberData = scriptable_variable_LobbyData.Value.Members.FirstOrDefault((LobbyMemberData element) => element.user.id.m_SteamID == steamID);
|
|
if (!string.IsNullOrEmpty(lobbyMemberData.user.Name))
|
|
{
|
|
if (remoteConnectionStateArgs.ConnectionState == RemoteConnectionState.Started)
|
|
{
|
|
ultimateChatBox.RegisterChat("System", lobbyMemberData.user.Name + " has connected to server.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
else
|
|
{
|
|
ultimateChatBox.RegisterChat("System", lobbyMemberData.user.Name + " has left the server.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MultiplayerManager_OnInSession()
|
|
{
|
|
string text = "Unknown Player";
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
text = UserData.Me.Name;
|
|
}
|
|
ultimateChatBox.RegisterChat("System", text + " has connected to server.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
|
|
private void MultiplayerManager_OnLeaveingSession()
|
|
{
|
|
string text = "Unknown Player";
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
text = UserData.Me.Name;
|
|
}
|
|
ultimateChatBox.RegisterChat("System", text + " has left the server.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
|
|
private void MultiplayerManager_OnHostChange()
|
|
{
|
|
ultimateChatBox.RegisterChat("System", "Lobby owner left, starting host migration.", UltimateChatBoxStyles.errorUsername);
|
|
}
|
|
|
|
private void OnFishCatch_OnRaised(FishData fishData)
|
|
{
|
|
string username = "Unknown Player";
|
|
if (SteamSettings.Initialized)
|
|
{
|
|
_ = UserData.Me;
|
|
username = UserData.Me.Name;
|
|
}
|
|
string message = $"Caught a {fishData.FishName}, weighing {fishData.Weight:F2} kg";
|
|
ultimateChatBox.RegisterChat(username, message, UltimateChatBoxStyles.greenUsername);
|
|
if (LobbyManager.HasLobby)
|
|
{
|
|
string message2 = JsonUtility.ToJson(new ChatMessageData(username, message, UltimateChatBoxStyles.greenUsername));
|
|
LobbyManager.SendChatMessage(message2);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
ultimateChatBox.DisableInputField();
|
|
chatCanvasGroup.alpha = 0f;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
isChatButtonPressed.OnValueChanged += IsChatButtonPressed_OnValueChanged;
|
|
isChatCancelButtonPressed.OnValueChanged += IsChatCancelButtonPressed_OnValueChanged;
|
|
ultimateChatBox.OnInputFieldSubmitted += UltimateChatBox_OnInputFieldSubmitted;
|
|
ultimateChatBox.OnInputFieldEnabled += UltimateChatBox_OnInputFieldEnabled;
|
|
ultimateChatBox.OnInputFieldDisabled += UltimateChatBox_OnInputFieldDisabled;
|
|
LobbyManager.evtUserJoined.AddListener(OnUserJoinedLobby);
|
|
LobbyManager.evtUserLeft.AddListener(OnUserLeftLobby);
|
|
LobbyManager.evtChatMsgReceived.AddListener(OnChatMsgReceived);
|
|
if ((bool)NetworkManager)
|
|
{
|
|
NetworkManager.ClientManager.OnRemoteConnectionState += ClientManager_OnRemoteConnectionState;
|
|
}
|
|
MultiplayerManager.OnInSession += MultiplayerManager_OnInSession;
|
|
MultiplayerManager.OnLeaveingSession += MultiplayerManager_OnLeaveingSession;
|
|
MultiplayerManager.OnHostChange += MultiplayerManager_OnHostChange;
|
|
onFishCatch.OnRaised += OnFishCatch_OnRaised;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
isChatButtonPressed.OnValueChanged -= IsChatButtonPressed_OnValueChanged;
|
|
isChatCancelButtonPressed.OnValueChanged -= IsChatCancelButtonPressed_OnValueChanged;
|
|
ultimateChatBox.OnInputFieldSubmitted -= UltimateChatBox_OnInputFieldSubmitted;
|
|
ultimateChatBox.OnInputFieldEnabled -= UltimateChatBox_OnInputFieldEnabled;
|
|
ultimateChatBox.OnInputFieldDisabled -= UltimateChatBox_OnInputFieldDisabled;
|
|
LobbyManager.evtUserJoined.RemoveListener(OnUserJoinedLobby);
|
|
LobbyManager.evtUserLeft.RemoveListener(OnUserLeftLobby);
|
|
LobbyManager.evtChatMsgReceived.RemoveListener(OnChatMsgReceived);
|
|
if ((bool)NetworkManager)
|
|
{
|
|
NetworkManager.ClientManager.OnRemoteConnectionState -= ClientManager_OnRemoteConnectionState;
|
|
}
|
|
MultiplayerManager.OnInSession -= MultiplayerManager_OnInSession;
|
|
MultiplayerManager.OnLeaveingSession -= MultiplayerManager_OnLeaveingSession;
|
|
MultiplayerManager.OnHostChange -= MultiplayerManager_OnHostChange;
|
|
onFishCatch.OnRaised -= OnFishCatch_OnRaised;
|
|
}
|
|
|
|
private void ClearFadingMessages()
|
|
{
|
|
for (int num = fadingMessageParent.childCount - 1; num >= 0; num--)
|
|
{
|
|
Transform child = fadingMessageParent.GetChild(num);
|
|
if (!(child == fadingMessage.transform))
|
|
{
|
|
DOTween.Kill(child.gameObject);
|
|
UnityEngine.Object.Destroy(child.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddFadingMessage(string username, string message, UltimateChatBox.ChatStyle style)
|
|
{
|
|
TMP_Text newMsg = UnityEngine.Object.Instantiate(fadingMessage, fadingMessageParent);
|
|
newMsg.gameObject.SetActive(value: true);
|
|
newMsg.text = username + ": " + message;
|
|
Vector3 localPosition = fadingMessage.transform.localPosition;
|
|
float num = 400f;
|
|
float num2 = 6f;
|
|
Sequence sequence = DOTween.Sequence();
|
|
sequence.SetTarget(newMsg.gameObject);
|
|
sequence.Append(newMsg.transform.DOLocalMoveY(localPosition.y + num, num2).SetEase(Ease.OutCubic));
|
|
sequence.Join(newMsg.DOFade(0f, num2 * 0.8f).SetDelay(num2 * 0.2f));
|
|
sequence.OnComplete(delegate
|
|
{
|
|
UnityEngine.Object.Destroy(newMsg.gameObject);
|
|
});
|
|
}
|
|
|
|
public void Debug_AddFadingMessage()
|
|
{
|
|
AddFadingMessage("Debugger", "This is a test fading message.", UltimateChatBoxStyles.boldUsername);
|
|
}
|
|
}
|