using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SocketIO;
using SocketMessages;
public class SocketManager : MonoBehaviour
{
[Header("Managers")]
public AppManager _AppManager;
[Space(8)]
public CharacterMatchingManager _CharacterMatchingManager;
public UserDataPanelManager _UserDataPanelManager;
public EffectsManager _EffectsManager;
[Header("Socket")]
public string serverIP;
public string serverPort;
public bool isConnected;
Dictionary<string, string> data;
[Space(8)]
[Header("UI")]
public GameObject connectionErrorPanel;
public InputField inputIPAddress;
public InputField inputPortAddress;
[Space(8)]
[Header("Socket_Scripts")]
public SocketSettings _SocketSettings;
public SocketIOComponent socket;
public SocketEmitMessageList socketEmitMessageList;
private void Awake()
{
// Load Socket Settings.
LoadSettings();
}
public void Start()
{
if (_AppManager.serverDebugging) return;
// Connect
socket.gameObject.SetActive(true);
socket.Connect();
socket.On("open", TestOpen);
socket.On("error", TestError);
socket.On("close", TestClose);
socket.On("mirror", TaggingListener);
socket.On("emotion", GetFacialEmotionData);
data = new Dictionary<string, string>();
}
public void Update()
{
if (_AppManager.serverDebugging) return;
if (Input.GetKeyDown(KeyCode.E))
{
if (isConnected)
{
EndOfExperience();
}
else
{
Debug.Log("Not Connected to Server");
}
}
// Display Error setting Pannel to configure IP adress of nodejs server
if (isConnected && connectionErrorPanel.activeSelf)
{
connectionErrorPanel.SetActive(false);
}
else if (!isConnected && !connectionErrorPanel.activeSelf)
{
connectionErrorPanel.SetActive(true);
inputIPAddress.placeholder.GetComponent<Text>().text = serverIP;
inputPortAddress.placeholder.GetComponent<Text>().text = serverPort;
}
}
#region Socket Settings Function
public void UpdateServerAdress()
{
socket.url = "ws://" + serverIP + ":" + serverPort + "/socket.io/?EIO=4&transport=websocket";
Debug.Log("Server Address Updated : " + socket.url);
}
#endregion
#region Node JS Common Function
public void TaggingListener(SocketIOEvent e)
{
Debug.Log("[SocketIO] User Tagged");
Debug.Log(e.data);
isConnected = true;
// Now The Interaction Begins.
_AppManager.isOccupied = true;
if (e.data == null)
{
Debug.Log("There is no data from API");
return;
}
if (e.data.GetField("username") != null && e.data.GetField("characterId") != null && e.data.GetField("language") != null)
{
string createdAt = "2019";
if (e.data.GetField("createdAt") != null)
{
createdAt = e.data.GetField("createdAt").str;
}
string username = e.data.GetField("username").str;
string characterIdStr = e.data.GetField("characterId").str;
int characterId = int.Parse(characterIdStr);
string userLang = e.data.GetField("language").str;
// Do something with this user dataset.
_CharacterMatchingManager.currentCharacterIndex = characterId;
_UserDataPanelManager.UpdateUserData(username, createdAt);
_UserDataPanelManager.lang = userLang;
_UserDataPanelManager.UpdateUserTagline(characterId);
_EffectsManager.CurrentCharacterRenderer(characterId);
// To Interaction Scene, Send Data To The Script Also.
_AppManager.ToInteractionScene();
}
}
public void EndOfExperience()
{
if (_AppManager.serverDebugging)
{
Debug.Log("Experience ends, but in server debugging mode");
return;
}
EndExperience newEndExperience = new EndExperience();
newEndExperience.experience = "mirror";
newEndExperience.data = "experience";
string json = JsonUtility.ToJson(newEndExperience);
Debug.Log(json);
SendMessageToNode("experience", json);
}
public void SendMessageToNode(string _message, string _contents)
{
if (_AppManager.serverDebugging)
{
Debug.Log("in Server debugging mode");
return;
}
JSONObject jsonContents = new JSONObject(_contents);
socket.Emit(_message, jsonContents);
}
public void TestOpen(SocketIOEvent e)
{
Debug.Log("[SocketIO] Open received: " + e.name + " " + e.data);
isConnected = true;
Debug.Log(isConnected);
}
public void TestError(SocketIOEvent e)
{
Debug.Log("[SocketIO] Error received: " + e.name + " " + e.data);
if (isConnected) isConnected = false;
}
public void TestClose(SocketIOEvent e)
{
Debug.Log("[SocketIO] Close received: " + e.name + " " + e.data);
isConnected = false;
}
public void ReloadApp(SocketIOEvent e)
{
//Debug Message and Data
//Debug.Log("[SocketIO]Reload: " + e.name);
// StartCoroutine(gameManager.ReloadScene());
}
public void QuitApp(SocketIOEvent e)
{
//Debug Message and Data
//Debug.Log("[SocketIO]Quit: " + e.name);
// StartCoroutine(gameManager.QuitScene());
}
#endregion
#region Face Analysis Functions
public void GetFacialEmotionData(SocketIOEvent e)
{
if (e.data == null) { return; }
if (e.data.GetField("class") != null)
{
if (e.data.GetField("class"))
{
Debug.Log("Server said, " + e.data.GetField("class"));
// Invoke CharacterFaceSwap Here
string currentFaceIndexString = e.data.GetField("class").ToString();
if (int.TryParse(currentFaceIndexString, out int _currentFaceIndex))
{
if (_currentFaceIndex < 5)
{
if (_currentFaceIndex != 2)
{
_CharacterMatchingManager.currentFaceIndex = _currentFaceIndex;
//TODO fix character emotions(how many?)
}
}
}
else
{
Debug.Log("It should be a number");
}
}
}
}
#endregion
#region UI Socket Settings
public void ChangeIpAdress(string _ip, string _port)
{
serverIP = _ip;
serverPort = _port;
UpdateServerAdress();
}
public void UpdateIpAdress(string _ip, string _port)
{
// ChangeIpAdress(_ip, _port);
SaveSettings(_ip, _port);
connectionErrorPanel.SetActive(false);
}
public void SaveInputButton()
{
string _newIp = inputIPAddress.text;
string _newPort = inputPortAddress.text;
if (_newIp == "")
_newIp = inputIPAddress.placeholder.GetComponent<Text>().text;
if (_newPort == "")
_newPort = inputPortAddress.placeholder.GetComponent<Text>().text;
UpdateIpAdress(_newIp, _newPort);
}
#endregion
#region Load & Save Socket Settings
public void LoadSettings()
{
_SocketSettings.LoadFromJson();
if (_SocketSettings.ipAddress != null && _SocketSettings.port != null)
{
Debug.Log(_SocketSettings.ipAddress + ":" + _SocketSettings.port);
ChangeIpAdress(_SocketSettings.ipAddress, _SocketSettings.port);
return;
}
else
UpdateIpAdress("127.0.0.1", "80");
UpdateServerAdress();
}
public void SaveSettings(string _newIp, string _newPort)
{
_SocketSettings.ipAddress = _newIp;
_SocketSettings.port = _newPort;
_SocketSettings.SaveToJson();
_AppManager.ReloadScene();
}
#endregion
}