You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Multiplayer
{
public class LoginManager
{
private ServerManager ServerManager;
public LoginManager(ServerManager ServerManager)
{
this.ServerManager = ServerManager;
RegisterHandles();
}
public void RegisterHandles()
{
ServerManager.Server.RegisterHandler(LoginMsgID.Name, OnPlayerLogin);
ServerManager.Server.RegisterHandler(MsgType.Connect, OnConnect);
ServerManager.Server.RegisterHandler(MsgType.Disconnect, OnDisconnect);
}
//Called when a player logs in
public void OnPlayerLogin(NetworkMessage msg)
{
StringMsg stringMsg;
if (!Utility.ReadMessage<StringMsg>(msg, out stringMsg))
return;
Debug.Log("Player login: " + stringMsg.String);
if (ServerManager.AllPlayers.ContainsKey(stringMsg.ID))
{
Debug.Log(stringMsg.String + " already logged in");
return;
}
Player newPlayer = new Player(msg.conn, stringMsg.String);
ServerManager.AllPlayers.Add(newPlayer.ID, newPlayer);
}
private void OnConnect(NetworkMessage msg)
{
Debug.Log("New Connection: \n Address: " + msg.conn.address + "\n ID: " + msg.conn.connectionId);
msg.conn.Send(LoginMsgID.QueryName, new PlayerMsg((byte)msg.conn.connectionId));
}
private void OnDisconnect(NetworkMessage msg)
{
if (ServerManager.AllPlayers.ContainsKey(msg.conn.connectionId))
{
Debug.Log(ServerManager.AllPlayers[msg.conn.connectionId].Name + "has disconnected.");
ServerManager.AllPlayers.Remove(msg.conn.connectionId);
}
else
Debug.Log("Unkown player has disconnected.");
}
}
}