http requests

This commit is contained in:
Oscar 2024-10-19 18:00:02 +03:00
parent 6b3ed6a2f4
commit afdf37a4d6
19 changed files with 194 additions and 25 deletions

View File

@ -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}

View File

@ -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

View File

@ -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();
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: fbc9d11c053e4cf9b3f111038086b924
timeCreated: 1729108916

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9fd954bbae476f648badbc284c2e7bfe
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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<string, string> 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<ServerInfo>(jsonResponse);
ServerListResponse = new Dictionary<string, string>
{
{ "name", serverInfo.name },
{ "address", serverInfo.address }
};
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 888381d2f37742ae809685fc98b89251
timeCreated: 1729347751

View File

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartManager : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0b2d4f78ab532a34bad66ded52b86b98
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: