2020-08-07 01:27:08 +02:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-07 00:57:40 +02:00
|
|
|
|
using System;
|
2020-08-07 01:27:08 +02:00
|
|
|
|
using UnityEngine;
|
2020-08-07 00:57:40 +02:00
|
|
|
|
using NeonTea.Quakeball.Interface;
|
2020-08-08 02:35:19 +02:00
|
|
|
|
using NeonTea.Quakeball.Networking.Instances;
|
2020-08-07 00:57:40 +02:00
|
|
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
|
|
|
|
|
2020-08-07 20:20:13 +02:00
|
|
|
|
namespace NeonTea.Quakeball.Networking {
|
2020-08-07 00:57:40 +02:00
|
|
|
|
public class NetChaperone : MonoBehaviour, PeerMessageListener {
|
|
|
|
|
|
2020-08-07 03:46:09 +02:00
|
|
|
|
public GameObject SpawnedRemotePlayer;
|
|
|
|
|
|
2020-08-07 00:57:40 +02:00
|
|
|
|
private Terminal Terminal;
|
|
|
|
|
|
2020-08-07 01:27:08 +02:00
|
|
|
|
private Queue<string> MessageQueue = new Queue<string>();
|
|
|
|
|
|
2020-08-07 04:11:53 +02:00
|
|
|
|
public GameObject SpawnPlayer(Vector3 location) {
|
|
|
|
|
GameObject obj = GameObject.Instantiate(SpawnedRemotePlayer);
|
|
|
|
|
obj.transform.position = location;
|
|
|
|
|
return obj;
|
2020-08-07 03:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 00:57:40 +02:00
|
|
|
|
private void Start() {
|
|
|
|
|
if (Terminal.Singleton != null) {
|
|
|
|
|
Terminal = Terminal.Singleton;
|
2020-08-08 19:14:34 +02:00
|
|
|
|
Terminal.RegisterCommand("host", Host, "host <nickname> [port] [address] - Hosts server at given address and port.");
|
|
|
|
|
Terminal.RegisterCommand("join", Join, "join <nickname> [address] [port] - Tries to join a server at given address and port.");
|
2020-08-08 02:35:19 +02:00
|
|
|
|
Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s).");
|
2020-08-08 03:03:08 +02:00
|
|
|
|
Terminal.RegisterCommand("disconnect", Disconnect, "disconnect - Stop current server, or close current client abruptly.");
|
2020-08-07 01:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
2020-08-07 04:31:49 +02:00
|
|
|
|
if (Net.Singleton.Instance != null) {
|
|
|
|
|
Net.Singleton.Instance.Peer.Update();
|
2020-08-08 02:35:19 +02:00
|
|
|
|
Net.Singleton.Instance.Update();
|
2020-08-07 04:31:49 +02:00
|
|
|
|
}
|
2020-08-07 01:27:08 +02:00
|
|
|
|
if (Terminal != null && Terminal.isActiveAndEnabled) {
|
|
|
|
|
while (MessageQueue.Count > 0) {
|
|
|
|
|
Terminal.Println(MessageQueue.Dequeue());
|
|
|
|
|
}
|
2020-08-07 00:57:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Host(string[] args) {
|
2020-08-08 19:14:34 +02:00
|
|
|
|
if (args.Length > 3) {
|
2020-08-08 21:33:43 +02:00
|
|
|
|
Terminal.Println($"<color={Terminal.ERROR_COLOR}>Can't accept more than 3 arguments.</color>");
|
2020-08-07 00:57:40 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string addr = "0.0.0.0";
|
|
|
|
|
string portstr = "8080";
|
2020-08-08 19:14:34 +02:00
|
|
|
|
string nick = args[0];
|
2020-08-07 01:27:08 +02:00
|
|
|
|
if (args.Length > 1) {
|
2020-08-08 19:14:34 +02:00
|
|
|
|
portstr = args[1];
|
|
|
|
|
}
|
|
|
|
|
if (args.Length > 2) {
|
|
|
|
|
addr = args[2];
|
2020-08-07 01:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
int port;
|
|
|
|
|
if (!Int32.TryParse(portstr, out port)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-08 19:14:34 +02:00
|
|
|
|
Net.Singleton.StartServer(addr, port, nick, this);
|
2020-08-07 01:27:08 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Join(string[] args) {
|
2020-08-08 21:33:43 +02:00
|
|
|
|
if (args.Length > 3) {
|
|
|
|
|
Terminal.Println($"<color={Terminal.ERROR_COLOR}>Can't accept more than 3 arguments.</color>");
|
2020-08-07 01:27:08 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
string addr = "127.0.0.1";
|
|
|
|
|
string portstr = "8080";
|
2020-08-08 19:14:34 +02:00
|
|
|
|
string nick = args[0];
|
2020-08-07 01:27:08 +02:00
|
|
|
|
if (args.Length > 1) {
|
2020-08-08 19:14:34 +02:00
|
|
|
|
addr = args[1];
|
|
|
|
|
}
|
|
|
|
|
if (args.Length > 2) {
|
|
|
|
|
portstr = args[2];
|
2020-08-07 00:57:40 +02:00
|
|
|
|
}
|
|
|
|
|
int port;
|
|
|
|
|
if (!Int32.TryParse(portstr, out port)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-08 19:14:34 +02:00
|
|
|
|
Net.Singleton.StartClient(addr, port, nick, this);
|
2020-08-07 00:57:40 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 02:35:19 +02:00
|
|
|
|
private bool Ping(string[] args) {
|
|
|
|
|
if (Net.Singleton.Instance == null) {
|
|
|
|
|
Terminal.Println("No hosting or connecting has been done!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (Net.Singleton.Instance is Client) {
|
2020-08-08 19:45:59 +02:00
|
|
|
|
Client c = (Client)Net.Singleton.Instance;
|
|
|
|
|
foreach (NetPlayer p in c.Players.Values) {
|
|
|
|
|
int ping = (int)(p.Ping * 1000);
|
|
|
|
|
Terminal.Println($"{p.Nick}: {ping}ms");
|
|
|
|
|
}
|
2020-08-08 02:35:19 +02:00
|
|
|
|
} else if (Net.Singleton.Instance is Server) {
|
|
|
|
|
Server s = (Server)Net.Singleton.Instance;
|
|
|
|
|
foreach (NetPlayer p in s.PlayerList) {
|
|
|
|
|
int ping = (int)(p.Ping * 1000);
|
2020-08-08 19:45:59 +02:00
|
|
|
|
Terminal.Println($"{p.Nick}: {ping}ms");
|
2020-08-08 02:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 03:03:08 +02:00
|
|
|
|
private bool Disconnect(string[] args) {
|
|
|
|
|
if (Net.Singleton.Instance == null) {
|
|
|
|
|
Terminal.Println("No hosting or connecting has been done!");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Net.Singleton.Instance.Stop();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 00:57:40 +02:00
|
|
|
|
public void Message(string msg) {
|
2020-08-07 01:27:08 +02:00
|
|
|
|
MessageQueue.Enqueue(msg);
|
2020-08-07 00:57:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Err(string err) { }
|
|
|
|
|
}
|
|
|
|
|
}
|