PlayerTracker.cs 6.42 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
using Mirror;
using System;
using System.Diagnostics;
using UnityEngine;
cann-alberto's avatar
cann-alberto committed
5
6
using UnityEngine.UIElements;

cann-alberto's avatar
cann-alberto committed
7
8
9
10
11

public class PlayerTracker : NetworkBehaviour
{    
    public GameObject roomPrefab;
    public event Action OnRoomInstantiated;  // Define an event
cann-alberto's avatar
cann-alberto committed
12
    private bool hasTriedToConnect = false;    
cann-alberto's avatar
cann-alberto committed
13

cann-alberto's avatar
cann-alberto committed
14
    int port = 0; 
cann-alberto's avatar
cann-alberto committed
15
16

    void Start()
cann-alberto's avatar
cann-alberto committed
17
18
19
    {        
        hasTriedToConnect = false;                
    }    
cann-alberto's avatar
cann-alberto committed
20
21
22
23
24
25
26

    private void OnTriggerEnter(Collider other)
    {
        if (isLocalPlayer)
        {
            if (other.CompareTag("Parcel"))
            {
cann-alberto's avatar
cann-alberto committed
27
28
29
30
31
32
                UnityEngine.Debug.Log("Player enter: " + other.gameObject.name);
                UnityEngine.Debug.Log($"S-Process: User;\nAction: MM-Move; \nS-Compl: Persona From MLoc To MLoc({other.gameObject.name}) With SA ;\nD-Process: LOSrvc"); //Persona moved close to Parcel                

                string moveDataJson = CreateMoveDataJson(other.gameObject.name);
                StartCoroutine(GameManager.Instance.WebAPIManager.Upload("Location/action-request", moveDataJson, HandleResponse));
                GameManager.Instance.currentPlayerLocation = other.gameObject.name;
cann-alberto's avatar
cann-alberto committed
33
34
35
36
37
38
            }
            if (other.CompareTag("RoomPlaceholder") && !hasTriedToConnect)
            {
                hasTriedToConnect = true; // To avoid trying multiple conenction ot the same room
                NetInfo netInfo = other.GetComponent<NetInfo>();
                UnityEngine.Debug.Log("NetInfo[Mirror Server]: " + netInfo.IpAddress + ":" + netInfo.Port + "\nNetInfo[Communication Server]: " + netInfo.IpAddressComServer + ":" + netInfo.PortComServer);
cann-alberto's avatar
cann-alberto committed
39
40
41

                RoomData.roomName = "Room_[" + netInfo.IpAddress+":"+netInfo.Port+"]";
                RoomData.netInfo = netInfo;                                
cann-alberto's avatar
cann-alberto committed
42
43
44
45
46
47
48
49

                StartCoroutine(GameManager.Instance.ClientSocket.TryConnectToServerCoroutine(netInfo.IpAddressComServer, netInfo.PortComServer, 10, 2f));
            }
        }

    }

    private void OnTriggerExit(Collider other)
cann-alberto's avatar
cann-alberto committed
50
51
52
53
54
55
    {
        if (isLocalPlayer)
        {
            if (other.CompareTag("Parcel"))
                UnityEngine.Debug.Log("Exit from:" + other.gameObject.name);
        }
cann-alberto's avatar
cann-alberto committed
56
    }
cann-alberto's avatar
cann-alberto committed
57
    
cann-alberto's avatar
cann-alberto committed
58
59
    private void HandleResponse(string response)
    {
cann-alberto's avatar
cann-alberto committed
60
        UnityEngine.Debug.Log("Received response: " + response); // Log the response received from the server        
cann-alberto's avatar
cann-alberto committed
61
62
    }

cann-alberto's avatar
cann-alberto committed
63
64
65
66
    private string CreateMoveDataJson(string ParcelName)
    {                
        string position = transform.position.ToString().Replace(",", ".");        
        string rotation = transform.localRotation.ToString().Replace(",", ".");       
cann-alberto's avatar
cann-alberto committed
67
68
        string jsonData = "{" +
            "\"time\": \"" + DateTime.Now + "\"," +
cann-alberto's avatar
cann-alberto committed
69
70
71
72
73
74
75
            "\"action\": \"MM-Move\"," +
            "\"sProcess\": \"" + GameManager.Instance.userID + "\"," +
            "\"sComplements\": \"Persona From " + GameManager.Instance.currentPlayerLocation + " To " + ParcelName + " With SA position: " + position + " rotation: " + rotation + "\"," +
            "\"dProcess\": \"Location Service\"" +
            "}";        
        return jsonData;       
    }    
cann-alberto's avatar
cann-alberto committed
76
77
78
79
80
81

    [Command]
    public void CmdInstantiateRoom(Vector3 position, Quaternion orientation)
    {
        if (!isServer) return;

cann-alberto's avatar
cann-alberto committed
82
83
        // Search for a free port
        int port = PortManager.Instance.FindFreePort();
cann-alberto's avatar
cann-alberto committed
84
85
86
87
88
89
90
91
92
93
94
        if (port == -1)
        {
            UnityEngine.Debug.LogError("No available ports to create a new room.");
            return;
        }

        GameObject roomObject = Instantiate(roomPrefab, position, orientation);
        NetInfo netInfo = roomObject.GetComponent<NetInfo>();        
        NetworkServer.Spawn(roomObject);
        netInfo.SetNetworkInfo("127.0.0.1", port, "127.0.0.1", port + 1); // TODO: replace the ipAddresses

cann-alberto's avatar
cann-alberto committed
95
96
97
        // Store the room in the dictionary
        PortManager.Instance.TrackRoom(port, roomObject);        

cann-alberto's avatar
cann-alberto committed
98
        UnityEngine.Debug.Log("Launching the server...");        
cann-alberto's avatar
cann-alberto committed
99
        LaunchRoomServer(port); // Launch the Mirror server
cann-alberto's avatar
cann-alberto committed
100
101
102
103
104
105
106
107
108
109
110
111
        RpcNotifyRoomCreated(); // Notify clients
    }

    [ClientRpc]
    void RpcNotifyRoomCreated()
    {
        OnRoomInstantiated?.Invoke(); // Invoke event when room is instatiated
    }

    public void InstantiateRoomPrefab()
    {
        if (isLocalPlayer)
cann-alberto's avatar
cann-alberto committed
112
113
114
115
        {
            UnityEngine.Debug.Log($"S-Process: User;\nAction: MM-Add; \nS-Compl:Room At MLoc With SA;\nD-Process: LOSrvc"); //User1 places Room on Parcel        
            string addDataJson = CreateAddDataJson();
            StartCoroutine(GameManager.Instance.WebAPIManager.Upload("Location/action-request", addDataJson, HandleResponse));
cann-alberto's avatar
cann-alberto committed
116
117
118
119
120
121
122
123
            CmdInstantiateRoom(transform.position, transform.localRotation);
        }
        else
        {
            UnityEngine.Debug.LogWarning("RequestRoomCreation called on a non-local player.");
        }
    }

cann-alberto's avatar
cann-alberto committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    private string CreateAddDataJson()
    {
        string position = transform.position.ToString().Replace(",", ".");
        string rotation = transform.localRotation.ToString().Replace(",", ".");
        string jsonData = "{" +
            "\"time\": \"" + DateTime.Now + "\"," +
            "\"action\": \"MM-Add\"," +
            "\"sProcess\": \"" + GameManager.Instance.userID + "\"," +
            "\"sComplements\": \"Room At " + GameManager.Instance.currentPlayerLocation + " With SA position: " + position + " rotation: " + rotation + "\"," +
            "\"dProcess\": \"Location Service\"" +
            "}";
        return jsonData;        
    }

cann-alberto's avatar
cann-alberto committed
138
139
140
141
142
143
    private void LaunchRoomServer(int port)
    {
        //Launch the server        
        string sceneName = "Room Server";

        ProcessStartInfo startInfo = new ProcessStartInfo();
cann-alberto's avatar
cann-alberto committed
144
        startInfo.FileName = GameManager.Instance.pathToRoomExe;        
cann-alberto's avatar
cann-alberto committed
145
146
147
148
149
        startInfo.Arguments = $"-scene {sceneName} -port {port}";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.CreateNoWindow = true;
cann-alberto's avatar
cann-alberto committed
150
        
cann-alberto's avatar
cann-alberto committed
151
152
        Process serverProcess = new Process();
        serverProcess.StartInfo = startInfo;
cann-alberto's avatar
cann-alberto committed
153
154
155
        serverProcess.EnableRaisingEvents = true;
        serverProcess.Exited += (sender, args) => OnRoomServerClosed(port); // Attach event        
        serverProcess.Start();        
cann-alberto's avatar
cann-alberto committed
156
    }
cann-alberto's avatar
cann-alberto committed
157
158
159
160
161
    
    public void OnRoomServerClosed(int port)
    {        
        UnityEngine.Debug.Log($"Room server closed for port {port}");
        PortManager.Instance.PortsToDestroy.Enqueue(port);        
cann-alberto's avatar
cann-alberto committed
162
163
164
    }

}