Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
MPAI-MMM
mmm-tec-unity
Commits
9264793f
Commit
9264793f
authored
Apr 02, 2025
by
cann-alberto
Browse files
Release pre-MMM-API
parent
b909d464
Changes
10
Hide whitespace changes
Inline
Side-by-side
Assets/Scenes/LoginScene.unity
View file @
9264793f
...
...
@@ -357,9 +357,10 @@ MonoBehaviour:
personaUrl
:
humanID
:
userID
:
ipAddress
:
127.0.0.1
port
:
8081
pathToRoomExe
:
C:\\Users\\lab2a\\Documents\\
Unity Projects\\
MPAI-MMM\\Builds\\MPAI-MMM.exe
currentPlayer
Posi
tion
:
pathToRoomExe
:
C:\\Users\\lab2a\\Documents\\MPAI-MMM
-Unity
\\Builds\\MPAI-MMM.exe
currentPlayer
Loca
tion
:
localPlayerPrefab
:
{
fileID
:
0
}
---
!u!4
&1142055672
Transform
:
...
...
Assets/Scripts/GameManager.cs
View file @
9264793f
...
...
@@ -9,7 +9,9 @@ public class GameManager : MonoBehaviour
public
string
personaUrl
;
public
string
humanID
;
public
string
userID
;
[
Tooltip
(
"Port for the Communication Server"
)]
[
Tooltip
(
"IpAddress of the Communication Server"
)]
public
string
ipAddress
;
[
Tooltip
(
"Port of the Communication Server"
)]
public
int
port
;
[
Tooltip
(
"Path to the Room.exe file"
)]
public
string
pathToRoomExe
;
...
...
Assets/Scripts/InGameMenuEvents.cs
View file @
9264793f
...
...
@@ -7,6 +7,7 @@ using System.Diagnostics;
using
System.Runtime.InteropServices.WindowsRuntime
;
using
UnityEngine
;
using
UnityEngine.UIElements
;
using
static
UnityEngine
.
UIElements
.
UxmlAttributeDescription
;
public
class
InGameMenuEvents
:
MonoBehaviour
{
...
...
@@ -117,24 +118,72 @@ public class InGameMenuEvents : MonoBehaviour
private
void
SendMsg
(
ClickEvent
evt
)
{
UnityEngine
.
Debug
.
Log
(
$"S-Process: User1;\nAction: MM-Send; \nS-Compl: Message;\nD-Process: To User2"
);
//
TextField
toTextField
=
_document
.
rootVisualElement
.
Q
<
TextField
>(
"toTextField"
);
TextField
msgTextField
=
_document
.
rootVisualElement
.
Q
<
TextField
>(
"msgTextField"
);
// Retrieve data from the GUI
TextField
toTextField
=
_document
.
rootVisualElement
.
Q
(
"toTextField"
)
as
TextField
;
TextField
msgTextField
=
_document
.
rootVisualElement
.
Q
(
"msgTextField"
)
as
TextField
;
if
(
toTextField
==
null
||
msgTextField
==
null
)
{
UnityEngine
.
Debug
.
LogError
(
"Input fields not found."
);
return
;
}
// Create Json data to be sent
string
actionDataJson
=
CreateMessageJson
(
toTextField
.
value
,
msgTextField
.
value
);
StartCoroutine
(
GameManager
.
Instance
.
WebAPIManager
.
Upload
(
"Communication/messages"
,
actionDataJson
,
HandleResponse
));
// Send message to the target user
SendTextMessage
(
toTextField
.
value
,
actionDataJson
);
UnityEngine
.
Debug
.
Log
(
"Message sent to "
+
toTextField
.
value
);
// Retrieve comInfo by humanID
UnityEngine
.
Debug
.
Log
(
"S-Process: User1;\nAction: MM-Send;\nS-Compl: Message;\nD-Process: ACSrvc"
);
//User1 checks User2s whereabout
StartCoroutine
(
RetrieveComInfo
(
toTextField
.
value
,
(
comInfo
)
=>
{
if
(
string
.
IsNullOrEmpty
(
comInfo
))
{
UnityEngine
.
Debug
.
LogError
(
"Failed to retrieve communication info."
);
return
;
}
// Send the message
UnityEngine
.
Debug
.
Log
(
"S-Process: User1;\nAction: MM-Send;\nS-Compl: Message;\nD-Process: To User2"
);
// User1 sends a message to User2
UnityEngine
.
Debug
.
Log
(
"after"
+
comInfo
);
string
actionDataJson
=
CreateMessageJson
(
comInfo
,
msgTextField
.
value
);
StartCoroutine
(
GameManager
.
Instance
.
WebAPIManager
.
Upload
(
"Communication/messages"
,
actionDataJson
,
HandleResponse
));
SendTextMessage
(
comInfo
,
actionDataJson
);
UnityEngine
.
Debug
.
Log
(
"Message sent to "
+
comInfo
);
MessageInfo
myMessage
=
new
MessageInfo
(
DateTime
.
Now
,
GameManager
.
Instance
.
userID
,
comInfo
,
msgTextField
.
value
);
UpdateMessageLabel
(
myMessage
);
msgTextField
.
value
=
""
;
}));
}
private
IEnumerator
RetrieveComInfo
(
string
humanID
,
Action
<
string
>
onCompleted
)
{
User
targetUser
=
null
;
yield
return
GameManager
.
Instance
.
WebAPIManager
.
GetRequest
(
"Activity/user/humanid/"
+
humanID
,
(
responseText
)
=>
{
if
(!
string
.
IsNullOrEmpty
(
responseText
))
{
targetUser
=
JsonUtility
.
FromJson
<
User
>(
responseText
);
}
else
{
UnityEngine
.
Debug
.
LogError
(
"Error while retrieving the account."
);
}
});
if
(
targetUser
==
null
)
{
UnityEngine
.
Debug
.
LogError
(
"Account is null."
);
onCompleted
?.
Invoke
(
null
);
yield
break
;
}
UnityEngine
.
Debug
.
Log
(
targetUser
.
comIp
);
UnityEngine
.
Debug
.
Log
(
targetUser
.
comPort
);
MessageInfo
myMessage
=
new
MessageInfo
(
DateTime
.
Now
,
GameManager
.
Instance
.
userID
,
toTextField
.
value
,
msgTextField
.
value
);
UpdateMessageLabel
(
myMessage
);
// Reset the TextField
msgTextField
.
value
=
""
;
string
comInfo
=
targetUser
.
comIp
+
":"
+
targetUser
.
comPort
;
UnityEngine
.
Debug
.
Log
(
"before"
+
comInfo
);
onCompleted
?.
Invoke
(
comInfo
);
}
private
void
HandleResponse
(
string
response
)
...
...
Assets/Scripts/MainMenuEvents.cs
View file @
9264793f
using
System
;
using
System.Collections
;
using
System.Net
;
using
UnityEngine
;
using
UnityEngine.UIElements
;
...
...
@@ -79,12 +80,43 @@ public class MainMenuEvents : MonoBehaviour
// 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
;
GameManager
.
Instance
.
userID
=
userAccount
.
users
[
0
].
id
;
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
;
_networkClientManager
.
ConnectToServer
();
// Start Mirror Server
GameManager
.
Instance
.
ServerSocket
.
StartServer
(
GameManager
.
Instance
.
port
);
// Start Communication Server
GameManager
.
Instance
.
ServerSocket
.
StartServer
(
GameManager
.
Instance
.
ipAddress
,
GameManager
.
Instance
.
port
);
// Start Communication Server
}
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
;
}
#
endregion
#
region
Registration
...
...
@@ -139,10 +171,10 @@ public class MainMenuEvents : MonoBehaviour
string
personalDataJson
=
CreatePersonalDataJson
();
// Start the process with the profile creation
StartCoroutine
(
CreateProfileAndAccount
(
personalDataJson
));
StartCoroutine
(
RegisterHuman
(
personalDataJson
));
}
private
IEnumerator
CreateProfileAndAccount
(
string
personalDataJson
)
private
IEnumerator
RegisterHuman
(
string
personalDataJson
)
{
// Step 1: Create the profile
Profile
newProfile
=
null
;
...
...
@@ -178,10 +210,10 @@ public class MainMenuEvents : MonoBehaviour
if
(
newAccount
==
null
)
yield
break
;
Debug
.
Log
(
"S-Process: RGSrvc;\nS-Compl: Account;\nD-Process: human"
);
// Step 3: Create the user
User
newUser
=
null
;
yield
return
GameManager
.
Instance
.
WebAPIManager
.
Upload
(
"Registration/users"
,
"{}"
,
(
responseText
)
=>
User
newUser
=
null
;
string
newUserJson
=
CreateNewUser
();
yield
return
GameManager
.
Instance
.
WebAPIManager
.
Upload
(
"Registration/users"
,
newUserJson
,
(
responseText
)
=>
{
if
(
responseText
!=
null
)
{
...
...
@@ -193,8 +225,8 @@ public class MainMenuEvents : MonoBehaviour
Debug
.
LogError
(
"Error while creating the user"
);
}
});
if
(
newUser
==
null
)
yield
break
;
if
(
newUser
==
null
)
yield
break
;
// Step 4: Update the account
bool
accountUpdated
=
false
;
TextField
personaTextField
=
_document
.
rootVisualElement
.
Q
(
"personaTextField"
)
as
TextField
;
...
...
@@ -213,11 +245,38 @@ public class MainMenuEvents : MonoBehaviour
});
if
(!
accountUpdated
)
yield
break
;
GameManager
.
Instance
.
personaUrl
=
GameManager
.
Instance
.
WebAPIManager
.
BASE_URL
+
"Registration/avatars/"
+
personaTextField
.
value
;
GameManager
.
Instance
.
userID
=
newUser
.
id
;
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
;
// Step
5
: Connect to the server
// Step
6
: Connect to the server
_networkClientManager
.
ConnectToServer
();
GameManager
.
Instance
.
ServerSocket
.
StartServer
(
GameManager
.
Instance
.
port
);
// Start Communication Server
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
;
}
private
string
CreatePersonalDataJson
()
...
...
@@ -283,7 +342,9 @@ public class MainMenuEvents : MonoBehaviour
" \"humanID\": \""
+
accountToUpdate
.
humanID
+
"\", "
+
" \"personalProfileID\": \""
+
accountToUpdate
.
personalProfileID
+
"\", "
+
" \"rights\": [],"
+
" \"users\": [{\"id\": \""
+
newUser
.
id
+
"\"}],"
+
" \"users\": [{"
+
"\"userID\": \""
+
newUser
.
userID
+
"\", "
+
"\"humanID\": \""
+
newUser
.
humanID
+
"\"}],"
+
" \"personae\": [{\"model\": \""
+
persona
+
"\"}],"
+
"\"descrMetadata\": \""
+
DateTime
.
Now
+
"\"}"
;
Debug
.
Log
(
jsonData
);
...
...
Assets/NetInfo.cs
→
Assets/
Scripts/
NetInfo.cs
View file @
9264793f
File moved
Assets/NetInfo.cs.meta
→
Assets/
Scripts/
NetInfo.cs.meta
View file @
9264793f
File moved
Assets/Scripts/PlayerTracker.cs
View file @
9264793f
...
...
@@ -2,7 +2,6 @@ using Mirror;
using
System
;
using
System.Diagnostics
;
using
UnityEngine
;
using
UnityEngine.UIElements
;
public
class
PlayerTracker
:
NetworkBehaviour
...
...
Assets/Scripts/RoomGameManager.cs
View file @
9264793f
...
...
@@ -54,8 +54,8 @@ public class RoomGameManager : MonoBehaviour
writer
.
WriteLine
(
DateTime
.
Now
+
": Server started at port "
+
_transport
.
port
);
}
// Start Communication Server
_serverSocket
.
StartServer
(
_transport
.
port
+
1
);
// Start Communication Server
_serverSocket
.
StartServer
(
"0.0.0.0"
,
_transport
.
port
+
1
);
//TODO specify the ip address of the comunication server for the room
}
}
else
// A client is connecting to the room
...
...
Assets/Scripts/ServerSocket.cs
View file @
9264793f
using
ReadyPlayerMe.Core
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Net
;
...
...
@@ -17,11 +16,12 @@ public class ServerSocket : MonoBehaviour
private
ConcurrentQueue
<
MessageInfo
>
messageQueue
=
new
ConcurrentQueue
<
MessageInfo
>();
// Thread-safe queue
public
event
Action
<
MessageInfo
>
OnMessageReceived
;
public
void
StartServer
(
int
port
)
public
void
StartServer
(
string
ipAddress
,
int
port
)
{
try
{
_server
=
new
TcpListener
(
IPAddress
.
Any
,
port
);
//_server = new TcpListener(IPAddress.Any, port);
_server
=
new
TcpListener
(
IPAddress
.
Parse
(
ipAddress
),
port
);
_server
.
Start
();
_isRunning
=
true
;
Debug
.
Log
(
"Server socket started. Waiting for client..."
);
...
...
Assets/Scripts/WebAPIManager.cs
View file @
9264793f
...
...
@@ -4,7 +4,6 @@ using System.Collections.Generic;
using
UnityEngine
;
using
UnityEngine.Networking
;
public
class
WebAPIManager
:
MonoBehaviour
{
[
SerializeField
]
...
...
@@ -181,7 +180,10 @@ public class Right
[
Serializable
]
public
class
User
{
public
string
id
;
public
string
userID
;
public
string
humanID
;
public
string
comIp
;
public
string
comPort
;
}
[
Serializable
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment