From afdf37a4d6016a95dad435ce4ec990820cc7bef7 Mon Sep 17 00:00:00 2001 From: Oscar Date: Sat, 19 Oct 2024 18:00:02 +0300 Subject: [PATCH] http requests --- .../Anime Girl/Model/Anime_Girl.controller | 2 +- Assets/Scenes/Menu/Menu.unity | 32 ++++- Assets/Scripts/Menu.cs | 18 --- Assets/Scripts/Menu.cs.meta | 3 - Assets/Scripts/Network.meta | 8 ++ .../Scripts/{ => Network}/ComponentsSync.cs | 0 .../{ => Network}/ComponentsSync.cs.meta | 0 .../{ => Network}/CustomNetworkManager.cs | 0 .../CustomNetworkManager.cs.meta | 0 Assets/Scripts/{ => Network}/HostCollider.cs | 0 .../{ => Network}/HostCollider.cs.meta | 0 Assets/Scripts/Network/HttpRequests.cs | 126 ++++++++++++++++++ Assets/Scripts/Network/HttpRequests.cs.meta | 3 + Assets/Scripts/{ => Network}/JoinCollider.cs | 0 .../{ => Network}/JoinCollider.cs.meta | 0 .../{ => Network}/NetworkRoomPlayerLobby.cs | 0 .../NetworkRoomPlayerLobby.cs.meta | 0 Assets/Scripts/Network/StartManager.cs | 16 +++ Assets/Scripts/Network/StartManager.cs.meta | 11 ++ 19 files changed, 194 insertions(+), 25 deletions(-) delete mode 100644 Assets/Scripts/Menu.cs delete mode 100644 Assets/Scripts/Menu.cs.meta create mode 100644 Assets/Scripts/Network.meta rename Assets/Scripts/{ => Network}/ComponentsSync.cs (100%) rename Assets/Scripts/{ => Network}/ComponentsSync.cs.meta (100%) rename Assets/Scripts/{ => Network}/CustomNetworkManager.cs (100%) rename Assets/Scripts/{ => Network}/CustomNetworkManager.cs.meta (100%) rename Assets/Scripts/{ => Network}/HostCollider.cs (100%) rename Assets/Scripts/{ => Network}/HostCollider.cs.meta (100%) create mode 100644 Assets/Scripts/Network/HttpRequests.cs create mode 100644 Assets/Scripts/Network/HttpRequests.cs.meta rename Assets/Scripts/{ => Network}/JoinCollider.cs (100%) rename Assets/Scripts/{ => Network}/JoinCollider.cs.meta (100%) rename Assets/Scripts/{ => Network}/NetworkRoomPlayerLobby.cs (100%) rename Assets/Scripts/{ => Network}/NetworkRoomPlayerLobby.cs.meta (100%) create mode 100644 Assets/Scripts/Network/StartManager.cs create mode 100644 Assets/Scripts/Network/StartManager.cs.meta diff --git a/Assets/Assets/Anime Girl/Model/Anime_Girl.controller b/Assets/Assets/Anime Girl/Model/Anime_Girl.controller index 26afa20..466fd01 100644 --- a/Assets/Assets/Anime Girl/Model/Anime_Girl.controller +++ b/Assets/Assets/Anime Girl/Model/Anime_Girl.controller @@ -413,7 +413,7 @@ AnimatorStateMachine: m_Position: {x: 190, y: 280, z: 0} - serializedVersion: 1 m_State: {fileID: -345546078774493414} - m_Position: {x: -20, y: 180, z: 0} + m_Position: {x: -180, y: 120, z: 0} - serializedVersion: 1 m_State: {fileID: 4949688742649318102} m_Position: {x: 40, y: -270, z: 0} diff --git a/Assets/Scenes/Menu/Menu.unity b/Assets/Scenes/Menu/Menu.unity index 5a9c7d4..7f74c88 100644 --- a/Assets/Scenes/Menu/Menu.unity +++ b/Assets/Scenes/Menu/Menu.unity @@ -472,7 +472,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &183742007 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1700,6 +1700,8 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 678043783} + - component: {fileID: 678043785} + - component: {fileID: 678043784} m_Layer: 0 m_Name: DOORS(StartManager) m_TagString: Untagged @@ -1741,6 +1743,30 @@ Transform: - {fileID: 5211766938632718045} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &678043784 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 678043782} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 888381d2f37742ae809685fc98b89251, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &678043785 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 678043782} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0b2d4f78ab532a34bad66ded52b86b98, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &685655167 GameObject: m_ObjectHideFlags: 0 @@ -3257,7 +3283,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 6b0fecffa3f624585964b0d0eb21b18e, type: 3} m_Name: m_EditorClassIdentifier: - port: 7777 + port: 1337 DualMode: 1 NoDelay: 1 Interval: 10 @@ -4985,7 +5011,7 @@ GameObject: - component: {fileID: 1673758692195814737} - component: {fileID: 1673758692195814738} m_Layer: 0 - m_Name: StartManager + m_Name: StartButton m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Scripts/Menu.cs b/Assets/Scripts/Menu.cs deleted file mode 100644 index 0c61354..0000000 --- a/Assets/Scripts/Menu.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Mirror; -using UnityEngine; - -public class Menu : MonoBehaviour -{ - public CustomNetworkManager networkManager; - - public void HostLobby() - { - networkManager.StartHost(); - } - - public void JoinLobby() - { - networkManager.networkAddress = "localhost"; - networkManager.StartClient(); - } -} diff --git a/Assets/Scripts/Menu.cs.meta b/Assets/Scripts/Menu.cs.meta deleted file mode 100644 index 29dcfc6..0000000 --- a/Assets/Scripts/Menu.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: fbc9d11c053e4cf9b3f111038086b924 -timeCreated: 1729108916 \ No newline at end of file diff --git a/Assets/Scripts/Network.meta b/Assets/Scripts/Network.meta new file mode 100644 index 0000000..d25d247 --- /dev/null +++ b/Assets/Scripts/Network.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9fd954bbae476f648badbc284c2e7bfe +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/ComponentsSync.cs b/Assets/Scripts/Network/ComponentsSync.cs similarity index 100% rename from Assets/Scripts/ComponentsSync.cs rename to Assets/Scripts/Network/ComponentsSync.cs diff --git a/Assets/Scripts/ComponentsSync.cs.meta b/Assets/Scripts/Network/ComponentsSync.cs.meta similarity index 100% rename from Assets/Scripts/ComponentsSync.cs.meta rename to Assets/Scripts/Network/ComponentsSync.cs.meta diff --git a/Assets/Scripts/CustomNetworkManager.cs b/Assets/Scripts/Network/CustomNetworkManager.cs similarity index 100% rename from Assets/Scripts/CustomNetworkManager.cs rename to Assets/Scripts/Network/CustomNetworkManager.cs diff --git a/Assets/Scripts/CustomNetworkManager.cs.meta b/Assets/Scripts/Network/CustomNetworkManager.cs.meta similarity index 100% rename from Assets/Scripts/CustomNetworkManager.cs.meta rename to Assets/Scripts/Network/CustomNetworkManager.cs.meta diff --git a/Assets/Scripts/HostCollider.cs b/Assets/Scripts/Network/HostCollider.cs similarity index 100% rename from Assets/Scripts/HostCollider.cs rename to Assets/Scripts/Network/HostCollider.cs diff --git a/Assets/Scripts/HostCollider.cs.meta b/Assets/Scripts/Network/HostCollider.cs.meta similarity index 100% rename from Assets/Scripts/HostCollider.cs.meta rename to Assets/Scripts/Network/HostCollider.cs.meta diff --git a/Assets/Scripts/Network/HttpRequests.cs b/Assets/Scripts/Network/HttpRequests.cs new file mode 100644 index 0000000..cf51992 --- /dev/null +++ b/Assets/Scripts/Network/HttpRequests.cs @@ -0,0 +1,126 @@ +using System.Collections; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using UnityEngine; +using UnityEngine.Networking; + +namespace Network +{ + public class HttpRequests : MonoBehaviour + { + private string baseUrl = "https://kotpilnya.xyz/slowpoker/api/"; + public Dictionary ServerListResponse; + private string _externalIP = "localhost"; + + [System.Serializable] public class ServerInfo + { + public string name; + public string address; + } + void Start() + { + StartCoroutine(GetExternalIPAddress()); + } + + public void AddServer() + { + ServerInfo serverInfo = new ServerInfo + { + name = "Oscar", + address = _externalIP + }; + + string jsonData = JsonUtility.ToJson(serverInfo); + StartCoroutine(SendPostRequest("add_server", jsonData)); + } + + public void GetServerList() + { + StartCoroutine(SendGetRequest("get_server_list")); + } + + IEnumerator GetExternalIPAddress() + { + UnityWebRequest request = UnityWebRequest.Get("https://api.ipify.org"); + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) + { + Debug.LogError("Error getting external IP address: " + request.error); + } + else + { + _externalIP = request.downloadHandler.text; + Debug.Log("External IP Address: " + _externalIP); + } + } + + IEnumerator SendGetRequest(string endpoint) + { + UnityWebRequest request = UnityWebRequest.Get(baseUrl + endpoint); + + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) + { + Debug.LogError("Error: " + request.error); + } + else + { + OnGetServerList(request.downloadHandler.text); + Debug.Log("Response: " + request.downloadHandler.text); + } + } + + IEnumerator SendPostRequest(string endpoint, string jsonData) + { + UnityWebRequest request = new UnityWebRequest(baseUrl + endpoint, "POST"); + + byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData); + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + + request.SetRequestHeader("Content-Type", "application/json"); + + request.downloadHandler = new DownloadHandlerBuffer(); + + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) + { + Debug.LogError("Error: " + request.error); + } + else + { + Debug.Log("Response: " + request.downloadHandler.text); + } + } + + IEnumerator SendDeleteRequest(string endpoint) + { + UnityWebRequest request = UnityWebRequest.Delete(baseUrl + endpoint); + + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) + { + Debug.LogError("Error: " + request.error); + } + else + { + Debug.Log("Response: " + request.downloadHandler.text); + } + } + void OnGetServerList(string response) + { + string jsonResponse = response; + ServerInfo serverInfo = JsonUtility.FromJson(jsonResponse); + + ServerListResponse = new Dictionary + { + { "name", serverInfo.name }, + { "address", serverInfo.address } + }; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Network/HttpRequests.cs.meta b/Assets/Scripts/Network/HttpRequests.cs.meta new file mode 100644 index 0000000..52b6b21 --- /dev/null +++ b/Assets/Scripts/Network/HttpRequests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 888381d2f37742ae809685fc98b89251 +timeCreated: 1729347751 \ No newline at end of file diff --git a/Assets/Scripts/JoinCollider.cs b/Assets/Scripts/Network/JoinCollider.cs similarity index 100% rename from Assets/Scripts/JoinCollider.cs rename to Assets/Scripts/Network/JoinCollider.cs diff --git a/Assets/Scripts/JoinCollider.cs.meta b/Assets/Scripts/Network/JoinCollider.cs.meta similarity index 100% rename from Assets/Scripts/JoinCollider.cs.meta rename to Assets/Scripts/Network/JoinCollider.cs.meta diff --git a/Assets/Scripts/NetworkRoomPlayerLobby.cs b/Assets/Scripts/Network/NetworkRoomPlayerLobby.cs similarity index 100% rename from Assets/Scripts/NetworkRoomPlayerLobby.cs rename to Assets/Scripts/Network/NetworkRoomPlayerLobby.cs diff --git a/Assets/Scripts/NetworkRoomPlayerLobby.cs.meta b/Assets/Scripts/Network/NetworkRoomPlayerLobby.cs.meta similarity index 100% rename from Assets/Scripts/NetworkRoomPlayerLobby.cs.meta rename to Assets/Scripts/Network/NetworkRoomPlayerLobby.cs.meta diff --git a/Assets/Scripts/Network/StartManager.cs b/Assets/Scripts/Network/StartManager.cs new file mode 100644 index 0000000..5beeaac --- /dev/null +++ b/Assets/Scripts/Network/StartManager.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class StartManager : MonoBehaviour +{ + void Start() + { + + } + + void Update() + { + + } +} diff --git a/Assets/Scripts/Network/StartManager.cs.meta b/Assets/Scripts/Network/StartManager.cs.meta new file mode 100644 index 0000000..b2a2e93 --- /dev/null +++ b/Assets/Scripts/Network/StartManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0b2d4f78ab532a34bad66ded52b86b98 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: