Files

189 lines
5.8 KiB
C#
Raw Permalink Normal View History

2022-12-10 23:45:16 +02:00
using System.Collections.Generic;
2022-12-04 19:19:07 +02:00
using UnityEngine;
using UnityEngine.UI;
2022-12-05 03:15:48 +02:00
using TMPro;
2022-12-10 23:45:16 +02:00
using System.Net;
using System;
2023-01-01 12:50:59 +02:00
using System.Net.Sockets;
2022-12-10 23:45:16 +02:00
2022-12-05 03:15:48 +02:00
namespace SharedGame
{
2022-12-04 19:19:07 +02:00
2022-12-05 03:15:48 +02:00
public class ConnectionWidget : MonoBehaviour
{
public TMP_InputField[] inpIps;
2022-12-04 19:19:07 +02:00
public Toggle[] tglSpectators;
2022-12-05 03:15:48 +02:00
public TMP_InputField inpPlayerIndex;
2022-12-04 19:19:07 +02:00
public Toggle tgLocal;
public Button btnConnect;
private GameManager gameManager => GameManager.Instance;
2022-12-05 03:15:48 +02:00
private void Awake()
{
2022-12-04 19:19:07 +02:00
gameManager.OnRunningChanged += OnRunningChanged;
btnConnect.onClick.AddListener(OnConnect);
var connections = new List<Connections>();
2022-12-05 03:15:48 +02:00
connections.Add(new Connections()
{
2023-01-01 12:50:59 +02:00
ip = PrivateIP(),
2022-12-04 19:19:07 +02:00
port = 7000,
2022-12-06 12:22:07 +02:00
spectator = false,
2022-12-04 19:19:07 +02:00
});
2022-12-05 03:15:48 +02:00
connections.Add(new Connections()
{
2023-01-01 12:50:59 +02:00
ip = PrivateIP(),
2022-12-04 19:19:07 +02:00
port = 7001,
2022-12-06 12:22:07 +02:00
spectator = false,
2022-12-04 19:19:07 +02:00
});
inpPlayerIndex.text = "0";
LoadConnectionInfo(connections);
}
2023-01-01 12:50:59 +02:00
private string PrivateIP()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
2022-12-10 23:45:16 +02:00
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
tgLocal.isOn = !tgLocal.isOn;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
if (inpPlayerIndex.text == "0")
{
inpPlayerIndex.text = "1";
}
else
{
inpPlayerIndex.text = "0";
}
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
inpIps[0].Select();
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
inpIps[1].Select();
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
OnConnect();
}
}
2022-12-05 03:15:48 +02:00
private void OnConnect()
{
if (tgLocal.isOn)
{
2022-12-05 19:45:10 +02:00
NetworkInput.IS_LOCAL = true;
2022-12-04 19:19:07 +02:00
gameManager.StartLocalGame();
}
2022-12-05 03:15:48 +02:00
else
{
SceneSettings.IsOnline = true;
2022-12-05 19:45:10 +02:00
NetworkInput.IS_LOCAL = false;
2022-12-04 19:19:07 +02:00
var connectionInfo = GetConnectionInfo();
var perf = FindObjectOfType<GgpoPerformancePanel>();
perf.Setup();
var playerIndex = int.Parse(inpPlayerIndex.text);
SceneSettings.OnlineIndex = playerIndex;
2022-12-04 19:19:07 +02:00
gameManager.StartGGPOGame(perf, connectionInfo, playerIndex);
}
}
2023-02-12 16:35:54 +02:00
public void RematchConnection()
2023-01-30 15:36:37 +02:00
{
NetworkInput.IS_LOCAL = false;
var connectionInfo = GetConnectionInfo();
var perf = FindObjectOfType<GgpoPerformancePanel>();
perf.Setup();
2023-02-12 16:35:54 +02:00
if (SceneSettings.OnlineIndex == -1)
{
SceneSettings.OnlineIndex = int.Parse(inpPlayerIndex.text);
}
gameManager.StartGGPOGame(perf, connectionInfo, SceneSettings.OnlineIndex);
2023-01-30 15:36:37 +02:00
}
2022-12-04 19:19:07 +02:00
2023-01-01 12:50:59 +02:00
public void StartGGPO(string ipOne, string ipTwo, string privateIpOne, string privateIpTwo, int portOne, int portTwo, int index)
2022-12-07 12:13:57 +02:00
{
2023-01-01 12:50:59 +02:00
string ipOneUsed = ipOne;
string ipTwoUsed = ipTwo;
if (ipOne == ipTwo)
{
ipOneUsed = privateIpOne;
ipTwoUsed = privateIpTwo;
}
2022-12-07 12:13:57 +02:00
var connections = new List<Connections>();
connections.Add(new Connections()
{
2023-01-01 12:50:59 +02:00
ip = ipOneUsed,
port = (ushort)portOne,
2022-12-07 12:13:57 +02:00
spectator = false,
});
connections.Add(new Connections()
{
2023-01-01 12:50:59 +02:00
ip = ipTwoUsed,
port = (ushort)portTwo,
2022-12-07 12:13:57 +02:00
spectator = false,
});
LoadConnectionInfo(connections);
NetworkInput.IS_LOCAL = false;
var connectionInfo = GetConnectionInfo();
var perf = FindObjectOfType<GgpoPerformancePanel>();
perf.Setup();
2022-12-08 14:35:27 +02:00
var playerIndex = index;
2023-02-22 20:02:39 +02:00
Printer.Log($"P1:" + ipOneUsed + "," + portOne);
Printer.Log($"P2:" + ipTwoUsed + "," + portTwo);
2022-12-07 12:13:57 +02:00
gameManager.StartGGPOGame(perf, connectionInfo, playerIndex);
}
2022-12-05 03:15:48 +02:00
private void OnDestroy()
{
2022-12-05 16:04:05 +02:00
if (gameManager != null)
{
gameManager.OnRunningChanged -= OnRunningChanged;
}
2022-12-04 19:19:07 +02:00
btnConnect.onClick.RemoveListener(OnConnect);
}
2022-12-05 03:15:48 +02:00
private void OnRunningChanged(bool obj)
{
2022-12-04 19:19:07 +02:00
gameObject.SetActive(!obj);
}
2022-12-05 03:15:48 +02:00
public void LoadConnectionInfo(IList<Connections> connections)
{
for (int i = 0; i < connections.Count; ++i)
{
2022-12-04 19:19:07 +02:00
inpIps[i].text = connections[i].ip + ":" + connections[i].port;
tglSpectators[i].isOn = connections[i].spectator;
}
}
2022-12-05 03:15:48 +02:00
public IList<Connections> GetConnectionInfo()
{
2022-12-04 19:19:07 +02:00
var connections = new List<Connections>(inpIps.Length);
2022-12-05 03:15:48 +02:00
for (int i = 0; i < inpIps.Length; ++i)
{
2022-12-04 19:19:07 +02:00
var split = inpIps[i].text.Split(':');
2022-12-05 03:15:48 +02:00
connections.Add(new Connections()
{
2022-12-04 19:19:07 +02:00
ip = split[0],
port = ushort.Parse(split[1]),
2022-12-06 12:22:07 +02:00
spectator = false,
2022-12-04 19:19:07 +02:00
});
}
return connections;
}
}
}