PlayerTracker.cs 6.39 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

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

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

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

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

    private void OnTriggerEnter(Collider other)
    {
        if (isLocalPlayer)
        {
            if (other.CompareTag("Parcel"))
            {
cann-alberto's avatar
cann-alberto committed
26
27
28
29
30
31
                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
32
33
34
35
36
37
            }
            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
38
39
40

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

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

    }

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

cann-alberto's avatar
cann-alberto committed
62
63
64
65
    private string CreateMoveDataJson(string ParcelName)
    {                
        string position = transform.position.ToString().Replace(",", ".");        
        string rotation = transform.localRotation.ToString().Replace(",", ".");       
cann-alberto's avatar
cann-alberto committed
66
67
        string jsonData = "{" +
            "\"time\": \"" + DateTime.Now + "\"," +
cann-alberto's avatar
cann-alberto committed
68
69
70
71
72
73
74
            "\"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
75
76
77
78
79
80

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

cann-alberto's avatar
cann-alberto committed
81
82
        // Search for a free port
        int port = PortManager.Instance.FindFreePort();
cann-alberto's avatar
cann-alberto committed
83
84
85
86
87
88
89
90
91
92
93
        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
94
95
96
        // Store the room in the dictionary
        PortManager.Instance.TrackRoom(port, roomObject);        

cann-alberto's avatar
cann-alberto committed
97
        UnityEngine.Debug.Log("Launching the server...");        
cann-alberto's avatar
cann-alberto committed
98
        LaunchRoomServer(port); // Launch the Mirror server
cann-alberto's avatar
cann-alberto committed
99
100
101
102
103
104
105
106
107
108
109
110
        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
111
112
113
114
        {
            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
115
116
117
118
119
120
121
122
            CmdInstantiateRoom(transform.position, transform.localRotation);
        }
        else
        {
            UnityEngine.Debug.LogWarning("RequestRoomCreation called on a non-local player.");
        }
    }

cann-alberto's avatar
cann-alberto committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    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
137
138
139
140
141
142
    private void LaunchRoomServer(int port)
    {
        //Launch the server        
        string sceneName = "Room Server";

        ProcessStartInfo startInfo = new ProcessStartInfo();
cann-alberto's avatar
cann-alberto committed
143
        startInfo.FileName = GameManager.Instance.pathToRoomExe;        
cann-alberto's avatar
cann-alberto committed
144
145
146
147
148
        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
149
        
cann-alberto's avatar
cann-alberto committed
150
151
        Process serverProcess = new Process();
        serverProcess.StartInfo = startInfo;
cann-alberto's avatar
cann-alberto committed
152
153
154
        serverProcess.EnableRaisingEvents = true;
        serverProcess.Exited += (sender, args) => OnRoomServerClosed(port); // Attach event        
        serverProcess.Start();        
cann-alberto's avatar
cann-alberto committed
155
    }
cann-alberto's avatar
cann-alberto committed
156
157
158
159
160
    
    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
161
162
163
    }

}