MainMenuEvents.cs 14.5 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
using System;
using System.Collections;
cann-alberto's avatar
cann-alberto committed
3
using System.Net;
cann-alberto's avatar
cann-alberto committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
using UnityEngine.UIElements;


public class MainMenuEvents : MonoBehaviour
{    
    [SerializeField]
    private NetworkClientManager _networkClientManager; // Interface to MMM server through Mirror lib

    private UIDocument _document;
    private Button _buttonLogin;
    private Button _buttonRegister;
    private VisualElement _registrationForm;
    private VisualElement _loginForm;    

    private void Awake()
    {
        _document = GetComponent<UIDocument>();

        _buttonLogin = _document.rootVisualElement.Q("LoginButton") as Button;
        _buttonLogin.RegisterCallback<ClickEvent>(OnLoginButtonClick);
        
        _buttonRegister = _document.rootVisualElement.Q("RegisterButton") as Button;
        _buttonRegister.RegisterCallback<ClickEvent>(OnRegisterButtonClick);

        _loginForm = _document.rootVisualElement.Q("LoginForm") as VisualElement;
        _registrationForm = _document.rootVisualElement.Q("RegistrationForm") as VisualElement;
    }

    private void OnDisable()
    {
        _buttonLogin.UnregisterCallback<ClickEvent>(OnLoginButtonClick);
        _buttonRegister.UnregisterCallback <ClickEvent>(OnRegisterButtonClick);        
    }
cann-alberto's avatar
cann-alberto committed
38
39
    
    #region Login
cann-alberto's avatar
cann-alberto committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    private void OnLoginButtonClick(ClickEvent evt)
    {
        _registrationForm.style.visibility = Visibility.Hidden; 
        _loginForm.style.visibility = Visibility.Visible;
        Button submitLoginButton = _document.rootVisualElement.Q("submitLoginButton") as Button;
        submitLoginButton.RegisterCallback<ClickEvent>(OnSubmitLoginButtonClick);
           
    }

    private void OnSubmitLoginButtonClick(ClickEvent evt)
    {
        // Get HumanID
        TextField humanIdTextField = _document.rootVisualElement.Q("loginHumanIdTextField") as TextField;
        StartCoroutine(LoginUser(humanIdTextField.value));                                       
    }

    private IEnumerator LoginUser(string humanID)
    {
        Account userAccount = null;

        // Wait for the request to complete
        yield return GameManager.Instance.WebAPIManager.GetRequest("Registration/accounts/" + humanID, (responseText) =>
        {
            if (responseText != null)
            {                
                userAccount = JsonUtility.FromJson<Account>(responseText);                
            }
            else
            {
                Debug.LogError("Error while retrieving the account.");
            }
        });

        // Ensure the account data is available before proceeding
        if (userAccount == null)
        {
            Debug.LogError("Failed to login. Account is null.");
            yield break;
        }
cann-alberto's avatar
cann-alberto committed
79
                
cann-alberto's avatar
cann-alberto committed
80
81
82
        // Update the DataManager with the retrieved account data
        GameManager.Instance.humanID = humanID;
        GameManager.Instance.personaUrl = GameManager.Instance.WebAPIManager.BASE_URL + "Registration/avatars/" + userAccount.personae[0].model;
cann-alberto's avatar
cann-alberto committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
        GameManager.Instance.userID = userAccount.users[0].userID;

        bool userUpdated = false;
        string userJson = CreateUpdatedUserJson(userAccount.users[0].userID, humanID, GameManager.Instance.ipAddress, GameManager.Instance.port);
        
        // Send information for comunication
        yield return GameManager.Instance.WebAPIManager.PutRequest("Activity/users/" + userAccount.users[0].userID + "/comInfo", userJson, (responseText) =>
        {
            if (responseText != null)
            {
                Debug.Log("User updated successfully for userID: " + userAccount.users[0].userID);
                userUpdated = true;
            }
            else
            {
                Debug.LogError("Error while updating user.");
            }
        });
        if (!userUpdated) yield break;
cann-alberto's avatar
cann-alberto committed
102
103

        _networkClientManager.ConnectToServer(); // Start Mirror Server
cann-alberto's avatar
cann-alberto committed
104
        GameManager.Instance.ServerSocket.StartServer(GameManager.Instance.ipAddress, GameManager.Instance.port); // Start Communication Server        
cann-alberto's avatar
cann-alberto committed
105
106
    }

cann-alberto's avatar
cann-alberto committed
107
108
109
110
111
112
113
114
115
116
117
118
119
    private string CreateUpdatedUserJson(string userId, string humanID, string iPAddress, int port)
    {
        string jsonData = "{" +
            " \"userID\": \"" + userId + "\", " +
            " \"humanID\": \"" + humanID + "\"," +
            " \"comIp\": \"" + iPAddress.ToString() + "\", " +
            " \"comPort\": \"" + port + "\"" +            
            "}";        
        Debug.Log(jsonData);
        return jsonData;
    }


cann-alberto's avatar
cann-alberto committed
120
121
122
    #endregion

    #region Registration
cann-alberto's avatar
cann-alberto committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    private void OnRegisterButtonClick(ClickEvent evt)
    {
        _loginForm.style.visibility = Visibility.Hidden;
        _registrationForm.style.visibility = Visibility.Visible;
        Button submitRegistrationButton = _document.rootVisualElement.Q("submitRegistrationButton") as Button;
        submitRegistrationButton.RegisterCallback<ClickEvent>(OnSubmitRegisterButtonClick);

        
        Button maleButton = _document.rootVisualElement.Q("AvatarButton0") as Button;
        maleButton.RegisterCallback<ClickEvent>(OnAvatar0ButtonClick);
        Button male2Button = _document.rootVisualElement.Q("AvatarButton1") as Button;
        male2Button.RegisterCallback<ClickEvent>(OnAvatar1ButtonClick);
        Button femaleButton = _document.rootVisualElement.Q("AvatarButton2") as Button;
        femaleButton.RegisterCallback<ClickEvent>(OnAvatar2ButtonClick);
        Button female2Button = _document.rootVisualElement.Q("AvatarButton3") as Button;
        female2Button.RegisterCallback<ClickEvent>(OnAvatar3ButtonClick);

    }

    private void OnAvatar3ButtonClick(ClickEvent evt)
    {
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;
        personaTextField.value = "Female2";
    }

    private void OnAvatar2ButtonClick(ClickEvent evt)
    {
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;
        personaTextField.value = "Female";
    }

    private void OnAvatar1ButtonClick(ClickEvent evt)
    {
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;
        personaTextField.value = "Male2";
    }

    private void OnAvatar0ButtonClick(ClickEvent evt)
    {
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;
        personaTextField.value = "Male";
    }

    private void OnSubmitRegisterButtonClick(ClickEvent evt)
    {
cann-alberto's avatar
cann-alberto committed
168
169
        Debug.Log("S-Process: Human;\nAction: Register;\nS-Compl: With PersonalProfile;\nD-Process: RGSrvc;\nD-Compl: To human"); //human registers with M-Instance

cann-alberto's avatar
cann-alberto committed
170
171
172
173
        // Collect personal data in JSON format
        string personalDataJson = CreatePersonalDataJson();

        // Start the process with the profile creation
cann-alberto's avatar
cann-alberto committed
174
        StartCoroutine(RegisterHuman(personalDataJson));
cann-alberto's avatar
cann-alberto committed
175
176
    }

cann-alberto's avatar
cann-alberto committed
177
    private IEnumerator RegisterHuman(string personalDataJson)
cann-alberto's avatar
cann-alberto committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    {
        // Step 1: Create the profile
        Profile newProfile = null;        
        yield return GameManager.Instance.WebAPIManager.Upload("Registration/profiles", personalDataJson, (responseText) =>
        {
            if (responseText != null)
            {
                Debug.Log("Profile created successfully\n Profile:" + responseText);
                newProfile = JsonUtility.FromJson<Profile>(responseText);                
            }
            else
            {
                Debug.LogError("Error while creating the profile.");
            }
        });
        if (newProfile == null) yield break;

        // Step 2: Create the account
        Account newAccount = null;
        string accountJson = ConvertToAccount(newProfile);
        yield return GameManager.Instance.WebAPIManager.Upload("Registration/accounts", accountJson, (responseText) =>
        {
            if (responseText != null)
            {
                Debug.Log("Account created successfully\n Account:" + responseText);
                newAccount = JsonUtility.FromJson<Account>(responseText);
            }
            else
            {
                Debug.LogError("Error while creating the account.");
            }
        });
        if (newAccount == null) yield break;
cann-alberto's avatar
cann-alberto committed
211
212
        Debug.Log("S-Process: RGSrvc;\nS-Compl: Account;\nD-Process: human");

cann-alberto's avatar
cann-alberto committed
213
        // Step 3: Create the user
cann-alberto's avatar
cann-alberto committed
214
215
216
        User newUser = null;
        string newUserJson = CreateNewUser();
        yield return GameManager.Instance.WebAPIManager.Upload("Registration/users", newUserJson, (responseText) =>
cann-alberto's avatar
cann-alberto committed
217
218
219
220
221
222
223
224
225
226
227
        {
            if (responseText != null)
            {
                Debug.Log("User created successfully\n User:" + responseText);
                newUser = JsonUtility.FromJson<User>(responseText);
            }
            else
            {
                Debug.LogError("Error while creating the user");
            }
        });
cann-alberto's avatar
cann-alberto committed
228
229
        if (newUser == null) yield break;
        
cann-alberto's avatar
cann-alberto committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
        // Step 4: Update the account
        bool accountUpdated = false;
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;        
        string accoutJson = CreateUpdatedAccountJson(newAccount, newUser, personaTextField.value);
        yield return GameManager.Instance.WebAPIManager.PutRequest("Registration/accounts/" + newAccount.accountID, accoutJson, (responseText) =>
        {
            if (responseText != null)
            {
                Debug.Log("Account updated successfully for account ID: " + newAccount.accountID);
                accountUpdated = true;
            }
            else
            {
                Debug.LogError("Error while updating account.");
            }
        });
        if (!accountUpdated) yield break;
        GameManager.Instance.personaUrl = GameManager.Instance.WebAPIManager.BASE_URL + "Registration/avatars/" + personaTextField.value;
cann-alberto's avatar
cann-alberto committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        GameManager.Instance.userID = newUser.userID;


        // Step 5: Send information for comunication
        bool userUpdated = false;
        string userJson = CreateUpdatedUserJson(newUser.userID, newAccount.humanID, GameManager.Instance.ipAddress, GameManager.Instance.port);
        yield return GameManager.Instance.WebAPIManager.PutRequest("Activity/users/" + newUser.userID + "/comInfo", userJson, (responseText) =>
        {
            if (responseText != null)
            {
                Debug.Log("User updated successfully for userID: " + newUser.userID);
                userUpdated = true;
            }
            else
            {
                Debug.LogError("Error while updating user.");
            }
        });
        if (!userUpdated) yield break;
cann-alberto's avatar
cann-alberto committed
267

cann-alberto's avatar
cann-alberto committed
268
        // Step 6: Connect to the server
cann-alberto's avatar
cann-alberto committed
269
        _networkClientManager.ConnectToServer();
cann-alberto's avatar
cann-alberto committed
270
271
272
273
274
275
276
277
278
279
        GameManager.Instance.ServerSocket.StartServer(GameManager.Instance.ipAddress, GameManager.Instance.port); // Start Communication Server

    }

    private string CreateNewUser()
    {
        TextField humanIdTextField = _document.rootVisualElement.Q("humanIdTextField") as TextField;
        string jsonData = "{" +
            " \"humanID\": \"" + humanIdTextField.value + "\"}";
        return jsonData;
cann-alberto's avatar
cann-alberto committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    }

    private string CreatePersonalDataJson()
    {
        TextField firstNameTextField = _document.rootVisualElement.Q("firstNameTextField") as TextField;
        TextField lastNameTextField = _document.rootVisualElement.Q("lastNameTextField") as TextField;
        IntegerField ageTextField = _document.rootVisualElement.Q("ageTextField") as IntegerField;
        TextField nationalityTextField = _document.rootVisualElement.Q("nationalityTextField") as TextField;
        TextField emailTextField = _document.rootVisualElement.Q("emailTextField") as TextField;
        TextField humanIdTextField = _document.rootVisualElement.Q("humanIdTextField") as TextField;

        string jsonData = "{" +
            " \"header\": \"MMM-ACC-V1.2\",  " +
            " \"mInstanceID\": \"MInstance00\",  " +
            " \"humanID\": \"" + humanIdTextField.value + "\", " +
            "\"personalProfileData\": {" +
            "   \"firstName\": \"" + firstNameTextField.value + "\", " +
            "   \"lastName\": \"" + lastNameTextField.value + "\", " +
            "   \"age\":" + ageTextField.value + ", " +
            "   \"nationality\": \"" + nationalityTextField.value + "\"," +
            "   \"email\": \"" + emailTextField.value + "\" " +
            "},  " +
cann-alberto's avatar
cann-alberto committed
302
            "\"descrMetadata\": \"" + DateTime.Now + "\"}";                
cann-alberto's avatar
cann-alberto committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

        // Passing data to the online scene
        GameManager.Instance.playerName = firstNameTextField.value;
        GameManager.Instance.humanID = humanIdTextField.value;        
        return jsonData;
    }

    private string ConvertToAccount(Profile newProfile)
    {
        string jsonData = "{" +
            " \"header\": \"" + newProfile.header + "\", " +
            " \"mInstanceID\": \"" + newProfile.mInstanceID + "\", " +
            " \"mEnvironmentID\": \"Environment00\",  " +            
            " \"humanID\": \"" + newProfile.humanID + "\", " +
            " \"personalProfileID\": \"" + newProfile.personalProfileID + "\", " +
            " \"rights\": []," +
            " \"users\": []," +
            " \"personae\": []," +
            "\"descrMetadata\": \"" + DateTime.Now + "\"}";               
        return jsonData;
    }

    private string CreatePersonaJsonPart ()
    {
        TextField personaTextField = _document.rootVisualElement.Q("personaTextField") as TextField;
        string jsonData = "{" +
            "\"model\": \"" + personaTextField.value + "\"}";

        GameManager.Instance.personaUrl = GameManager.Instance.WebAPIManager.BASE_URL + "Registration/avatars/" + personaTextField.value;        
        return jsonData;
    }
cann-alberto's avatar
cann-alberto committed
334
    
cann-alberto's avatar
cann-alberto committed
335
336
337
338
339
340
341
342
343
344
    private string CreateUpdatedAccountJson(Account accountToUpdate, User newUser, string persona)
    {
        string jsonData = "{" +
            " \"accountID\": \"" + accountToUpdate.accountID + "\", " +
            " \"header\": \"" + accountToUpdate.header + "\", " +
            " \"mInstanceID\": \"" + accountToUpdate.mInstanceID + "\", " +
            " \"mEnvironmentID\": \"Environment00\",  " +
            " \"humanID\": \"" + accountToUpdate.humanID + "\", " +
            " \"personalProfileID\": \"" + accountToUpdate.personalProfileID + "\", " +
            " \"rights\": []," +
cann-alberto's avatar
cann-alberto committed
345
346
347
            " \"users\": [{" +
                "\"userID\": \"" + newUser.userID + "\", " +
                "\"humanID\": \"" + newUser.humanID + "\"}]," +            
cann-alberto's avatar
cann-alberto committed
348
349
350
351
352
            " \"personae\": [{\"model\": \"" + persona + "\"}]," +
            "\"descrMetadata\": \"" + DateTime.Now + "\"}";
        Debug.Log(jsonData);
        return jsonData;        
    }
cann-alberto's avatar
cann-alberto committed
353
    #endregion
cann-alberto's avatar
cann-alberto committed
354
355
356
357
358
359
360
361

}