Add nicknames

This commit is contained in:
Sofia 2020-08-08 20:14:34 +03:00
parent d2e74fce02
commit 2777f85d61
11 changed files with 98 additions and 38 deletions

View File

@ -23,12 +23,12 @@ namespace NeonTea.Quakeball.Networking {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "0.0.0.0" : HostAddr.text; string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "0.0.0.0" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text; string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Debug.Log(addr); Debug.Log(addr);
Net.Singleton.StartServer(addr, Int32.Parse(port), this); Net.Singleton.StartServer(addr, Int32.Parse(port), "", this);
}); });
Join.onClick.AddListener(() => { Join.onClick.AddListener(() => {
string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "127.0.0.1" : HostAddr.text; string addr = String.IsNullOrWhiteSpace(HostAddr.text) ? "127.0.0.1" : HostAddr.text;
string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text; string port = String.IsNullOrWhiteSpace(Port.text) ? "8080" : Port.text;
Net.Singleton.StartClient(addr, Int32.Parse(port), this); Net.Singleton.StartClient(addr, Int32.Parse(port), "", this);
}); });
Stop.onClick.AddListener(() => { Stop.onClick.AddListener(() => {
Net.Quit(); Net.Quit();

View File

@ -21,6 +21,7 @@ namespace NeonTea.Quakeball.Networking {
RegisterPacket(typeof(PingPckt)); RegisterPacket(typeof(PingPckt));
RegisterPacket(typeof(SpawnPckt)); RegisterPacket(typeof(SpawnPckt));
RegisterPacket(typeof(SelfIdentPckt)); RegisterPacket(typeof(SelfIdentPckt));
RegisterPacket(typeof(NicknamePckt));
RegisterPacket(typeof(PlayerUpdatePckt)); RegisterPacket(typeof(PlayerUpdatePckt));
RegisterPacket(typeof(PlayerActionPckt)); RegisterPacket(typeof(PlayerActionPckt));
RegisterPacket(typeof(PlayerSyncPacket)); RegisterPacket(typeof(PlayerSyncPacket));

View File

@ -28,7 +28,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
} }
public override void Start(string address, int port, PeerMessageListener listener) { public override void Start(string address, int port, string nick, PeerMessageListener listener) {
if (Peer.Running) { if (Peer.Running) {
return; return;
} }
@ -37,6 +37,8 @@ namespace NeonTea.Quakeball.Networking.Instances {
Peer.Start(0); Peer.Start(0);
byte ident = Peer.RegisterProtocol(new GameProtocol(this)); byte ident = Peer.RegisterProtocol(new GameProtocol(this));
Peer.Connect(address, port, ident); Peer.Connect(address, port, ident);
LocalPlayer.Nick = nick;
} }
public override void OnStop() { public override void OnStop() {
@ -84,8 +86,16 @@ namespace NeonTea.Quakeball.Networking.Instances {
Players.Add(LocalPlayer.Id, LocalPlayer); Players.Add(LocalPlayer.Id, LocalPlayer);
SelfIdentified = true; SelfIdentified = true;
NicknamePckt nickpckt = new NicknamePckt(LocalPlayer.Nick);
Peer.SendReliable(Server.uid, nickpckt);
SpawnPckt spawn = new SpawnPckt(LocalPlayer.Controlled.transform.position); SpawnPckt spawn = new SpawnPckt(LocalPlayer.Controlled.transform.position);
Peer.SendReliable(Server.uid, spawn); Peer.SendReliableLater(Server.uid, spawn);
} else if (packet is NicknamePckt) {
NicknamePckt nick = (NicknamePckt)packet;
if (nick.PlayerId != LocalPlayer.Id) {
Players[nick.PlayerId].Nick = nick.Nick;
}
} else if (packet is SpawnPckt) { } else if (packet is SpawnPckt) {
SpawnPckt spawn = (SpawnPckt)packet; SpawnPckt spawn = (SpawnPckt)packet;
if (spawn.PlayerId == LocalPlayer.Id && spawn.IsInitial) { if (spawn.PlayerId == LocalPlayer.Id && spawn.IsInitial) {

View File

@ -19,7 +19,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
Peer = new Peer(Fingerprint); Peer = new Peer(Fingerprint);
} }
public abstract void Start(string address, int port, PeerMessageListener listener); public abstract void Start(string address, int port, string nick, PeerMessageListener listener);
public abstract void OnStop(); public abstract void OnStop();
public abstract void Connected(Connection conn); public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn); public abstract void Disconnected(Connection conn);

View File

@ -24,7 +24,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
LocalPlayer = new NetPlayer(ulong.MaxValue); LocalPlayer = new NetPlayer(ulong.MaxValue);
} }
public override void Start(string address, int port, PeerMessageListener listener) { public override void Start(string address, int port, string nick, PeerMessageListener listener) {
if (Peer.Running) { if (Peer.Running) {
return; return;
} }
@ -38,6 +38,7 @@ namespace NeonTea.Quakeball.Networking.Instances {
Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>(); Player obj = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
LocalPlayer.Controlled = obj; LocalPlayer.Controlled = obj;
LocalPlayer.Nick = nick;
AddPlayer(LocalPlayer); AddPlayer(LocalPlayer);
} }
@ -59,6 +60,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
if (p.Controlled == null) { // Not yet initialized, sending later. if (p.Controlled == null) { // Not yet initialized, sending later.
continue; continue;
} }
NicknamePckt nick = new NicknamePckt(p.Nick, p.Id);
Peer.SendReliableLater(conn.uid, nick);
SpawnPckt spawn = new SpawnPckt(p.Controlled.transform.position, p.Id); SpawnPckt spawn = new SpawnPckt(p.Controlled.transform.position, p.Id);
Peer.SendReliableLater(conn.uid, spawn); Peer.SendReliableLater(conn.uid, spawn);
} }
@ -78,7 +82,12 @@ namespace NeonTea.Quakeball.Networking.Instances {
} }
public override void Handle(Connection conn, Packet packet) { public override void Handle(Connection conn, Packet packet) {
if (packet is SpawnPckt) { if (packet is NicknamePckt) {
NicknamePckt nick = (NicknamePckt)packet;
Players[conn.uid].Nick = nick.Nick;
nick.PlayerId = conn.uid;
SendReliableToAll(nick);
} else if (packet is SpawnPckt) {
SpawnPckt spawn = (SpawnPckt)packet; SpawnPckt spawn = (SpawnPckt)packet;
if (Players[conn.uid].Controlled == null && spawn.IsInitial) { if (Players[conn.uid].Controlled == null && spawn.IsInitial) {
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>(); Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();

View File

@ -11,14 +11,14 @@ namespace NeonTea.Quakeball.Networking {
public NetInstance Instance; public NetInstance Instance;
public void StartClient(string address, int port, PeerMessageListener listener) { public void StartClient(string address, int port, string nick, PeerMessageListener listener) {
Instance = new Client(); Instance = new Client();
Instance.Start(address, port, listener); Instance.Start(address, port, nick, listener);
} }
public void StartServer(string address, int port, PeerMessageListener listener) { public void StartServer(string address, int port, string nick, PeerMessageListener listener) {
Instance = new Server(); Instance = new Server();
Instance.Start(address, port, listener); Instance.Start(address, port, nick, listener);
} }
public static void Quit() { public static void Quit() {

View File

@ -23,8 +23,8 @@ namespace NeonTea.Quakeball.Networking {
private void Start() { private void Start() {
if (Terminal.Singleton != null) { if (Terminal.Singleton != null) {
Terminal = Terminal.Singleton; Terminal = Terminal.Singleton;
Terminal.RegisterCommand("host", Host, "host [port] [address] - Hosts server at given address and port."); Terminal.RegisterCommand("host", Host, "host <nickname> [port] [address] - Hosts server at given address and port.");
Terminal.RegisterCommand("join", Join, "join [address] [port] - Tries to join a server at given address and port."); Terminal.RegisterCommand("join", Join, "join <nickname> [address] [port] - Tries to join a server at given address and port.");
Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s)."); Terminal.RegisterCommand("ping", Ping, "ping - return the current ping to any connection(s).");
Terminal.RegisterCommand("disconnect", Disconnect, "disconnect - Stop current server, or close current client abruptly."); Terminal.RegisterCommand("disconnect", Disconnect, "disconnect - Stop current server, or close current client abruptly.");
} }
@ -43,23 +43,24 @@ namespace NeonTea.Quakeball.Networking {
} }
private bool Host(string[] args) { private bool Host(string[] args) {
if (args.Length > 2) { if (args.Length > 3) {
Terminal.Println($"<color={Terminal.ERROR_COLOR}>Can't accept more than 2 arguments.</color>"); Terminal.Println($"<color={Terminal.ERROR_COLOR}>Can't accept more than 2 arguments.</color>");
return false; return false;
} }
string addr = "0.0.0.0"; string addr = "0.0.0.0";
string portstr = "8080"; string portstr = "8080";
if (args.Length > 0) { string nick = args[0];
portstr = args[0];
}
if (args.Length > 1) { if (args.Length > 1) {
addr = args[1]; portstr = args[1];
}
if (args.Length > 2) {
addr = args[2];
} }
int port; int port;
if (!Int32.TryParse(portstr, out port)) { if (!Int32.TryParse(portstr, out port)) {
return false; return false;
} }
Net.Singleton.StartServer(addr, port, this); Net.Singleton.StartServer(addr, port, nick, this);
return true; return true;
} }
@ -70,17 +71,18 @@ namespace NeonTea.Quakeball.Networking {
} }
string addr = "127.0.0.1"; string addr = "127.0.0.1";
string portstr = "8080"; string portstr = "8080";
if (args.Length > 0) { string nick = args[0];
addr = args[0];
}
if (args.Length > 1) { if (args.Length > 1) {
portstr = args[1]; addr = args[1];
}
if (args.Length > 2) {
portstr = args[2];
} }
int port; int port;
if (!Int32.TryParse(portstr, out port)) { if (!Int32.TryParse(portstr, out port)) {
return false; return false;
} }
Net.Singleton.StartClient(addr, port, this); Net.Singleton.StartClient(addr, port, nick, this);
return true; return true;
} }

View File

@ -24,12 +24,14 @@ namespace NeonTea.Quakeball.Networking {
} }
} }
} }
public string Nick;
public bool Unsynced = false; public bool Unsynced = false;
public float Ping = 0; public float Ping = 0;
public NetPlayer(ulong id, Player obj = null) { public NetPlayer(ulong id, Player obj = null) {
Id = id; Id = id;
Controlled = obj; Controlled = obj;
Nick = id.ToString();
} }
} }
} }

View File

@ -0,0 +1,39 @@
using UnityEngine;
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Networking.Packets {
public class SelfIdentPckt : Packet {
public ulong PlayerId;
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
}
public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
}
}
public class NicknamePckt : Packet {
public ulong PlayerId;
public string Nick;
public NicknamePckt() { }
public NicknamePckt(string nick, ulong id = 0) {
Nick = nick;
PlayerId = id;
}
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
Nick = buffer.ReadString();
}
public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
buffer.Write(Nick);
}
}
}

View File

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

View File

@ -50,18 +50,4 @@ namespace NeonTea.Quakeball.Networking.Packets {
buffer.Write(KillerPlayerId); buffer.Write(KillerPlayerId);
} }
} }
public class SelfIdentPckt : Packet {
public ulong PlayerId;
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
}
public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
}
}
} }