105 lines
4.0 KiB
C#
105 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using NeonTea.Quakeball.Networking.Packets;
|
|
using NeonTea.Quakeball.Player;
|
|
using NeonTea.Quakeball.Interface;
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
|
using NeonTea.Quakeball.TeaNet.Packets;
|
|
|
|
namespace NeonTea.Quakeball.Networking.Instances {
|
|
public class Server : NetInstance {
|
|
|
|
private NetChaperone Net;
|
|
|
|
private Dictionary<ulong, NetPlayer> Players = new Dictionary<ulong, NetPlayer>();
|
|
private ulong PlayerIdCounter;
|
|
|
|
private NetPlayer LocalPlayer = new NetPlayer(ulong.MaxValue);
|
|
|
|
public override void Start(string address, int port, PeerMessageListener listener) {
|
|
if (Peer != null) {
|
|
return;
|
|
}
|
|
Peer = new Peer(Fingerprint);
|
|
Peer.MessageListener = listener;
|
|
Peer.Start(port);
|
|
Peer.RegisterProtocol(new GameProtocol(this));
|
|
Peer.StartListen(address, port);
|
|
|
|
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
|
|
|
GameObject obj = GameObject.FindGameObjectWithTag("Player");
|
|
LocalPlayer.Controlled = obj;
|
|
Players.Add(LocalPlayer.Id, LocalPlayer);
|
|
}
|
|
|
|
public override void Connected(Connection conn) {
|
|
Terminal.Singleton.Println($"{conn.uid} Connected");
|
|
foreach (NetPlayer p in Players.Values) {
|
|
|
|
if (p.Controlled == null) { // Not yet initialized, sending later.
|
|
continue;
|
|
}
|
|
SpawnPckt spawn = new SpawnPckt();
|
|
spawn.PlayerId = p.Id;
|
|
spawn.Location = p.Controlled.transform.position;
|
|
Peer.SendReliableLater(conn.uid, spawn);
|
|
}
|
|
NetPlayer RemotePlayer = new NetPlayer(PlayerIdCounter++);
|
|
Players.Add(RemotePlayer.Id, RemotePlayer);
|
|
SelfIdentPckt ident = new SelfIdentPckt();
|
|
ident.PlayerId = RemotePlayer.Id;
|
|
Peer.SendReliable(conn.uid, ident);
|
|
}
|
|
|
|
public override void Disconnected(Connection conn) {
|
|
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
|
|
}
|
|
|
|
public override void Handle(Connection conn, Packet packet) {
|
|
if (packet is SpawnPckt) {
|
|
SpawnPckt spawn = (SpawnPckt)packet;
|
|
if (Players[conn.uid].Controlled == null) {
|
|
GameObject obj = Net.SpawnPlayer(spawn.Location);
|
|
Players[conn.uid].Controlled = obj;
|
|
|
|
spawn = new SpawnPckt();
|
|
spawn.PlayerId = conn.uid;
|
|
spawn.Location = obj.transform.position;
|
|
SendReliableToAll(spawn, except: spawn.PlayerId);
|
|
}
|
|
} else if (packet is PlayerUpdatePckt) {
|
|
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
|
|
if (Players[conn.uid].Controlled != null) {
|
|
updatePckt.PlayerId = conn.uid;
|
|
Players[conn.uid].Controlled.GetComponent<RemotePlayer>().QueuePacket(updatePckt);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SendReliableToAll(Packet packet, ulong except = ulong.MaxValue) {
|
|
foreach (NetPlayer p in Players.Values) {
|
|
if (p.Id == ulong.MaxValue || p.Id == except) {
|
|
continue;
|
|
}
|
|
Peer.SendReliable(p.Id, packet);
|
|
}
|
|
}
|
|
|
|
public void SendUnreliableToAll(Packet packet, ulong except = ulong.MaxValue) {
|
|
foreach (NetPlayer p in Players.Values) {
|
|
if (p.Id == ulong.MaxValue || p.Id == except) {
|
|
continue;
|
|
}
|
|
Peer.SendUnreliable(p.Id, packet);
|
|
}
|
|
}
|
|
|
|
public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) {
|
|
Debug.Log("Update local to everyone!");
|
|
pckt.PlayerId = LocalPlayer.Id;
|
|
SendUnreliableToAll(pckt);
|
|
}
|
|
}
|
|
} |