using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace Multiplayer
|
|
{
|
|
|
|
public class PlayerServerManager
|
|
{
|
|
|
|
private ServerManager ServerManager;
|
|
|
|
public PlayerServerManager(ServerManager ServerManager)
|
|
{
|
|
this.ServerManager = ServerManager;
|
|
RegisterHandlers();
|
|
}
|
|
|
|
private void RegisterHandlers()
|
|
{
|
|
ServerManager.Server.RegisterHandler(PlayerMsgID.Position, RecievePosition);
|
|
ServerManager.Server.RegisterHandler(PlayerMsgID.QueryPosition, QueryPosition);
|
|
ServerManager.Server.RegisterHandler(PlayerMsgID.Vegetable, PickUpVeg);
|
|
}
|
|
|
|
private void RecievePosition(NetworkMessage msg)
|
|
{
|
|
VectorMsg vectorMsg;
|
|
if (!Utility.ReadMessage<VectorMsg>(msg, out vectorMsg))
|
|
return;
|
|
|
|
if (!ServerManager.AllPlayers.ContainsKey(vectorMsg.ID))
|
|
{
|
|
Debug.Log("Recieved position from unknown player");
|
|
return;
|
|
}
|
|
|
|
Player player = ServerManager.AllPlayers[vectorMsg.ID];
|
|
player.Position = vectorMsg.vector;
|
|
|
|
foreach(Player otherPlayer in ServerManager.AllPlayers.Values)
|
|
{
|
|
if (otherPlayer.Equals(player))
|
|
continue;
|
|
|
|
otherPlayer.Connection.Send(PlayerMsgID.Position, vectorMsg);
|
|
}
|
|
}
|
|
|
|
private void QueryPosition(NetworkMessage msg)
|
|
{
|
|
PlayerMsg playerMsg;
|
|
if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
|
|
return;
|
|
|
|
if (!ServerManager.AllPlayers.ContainsKey(playerMsg.ID))
|
|
{
|
|
Debug.Log("Recieved query from unknown player");
|
|
return;
|
|
}
|
|
|
|
Player player = ServerManager.AllPlayers[playerMsg.ID];
|
|
|
|
foreach (Player otherPlayer in ServerManager.AllPlayers.Values)
|
|
{
|
|
if (otherPlayer.Equals(player))
|
|
continue;
|
|
|
|
VectorMsg vectorMsg = new VectorMsg(otherPlayer.ID, otherPlayer.Position);
|
|
player.Connection.Send(PlayerMsgID.Position, vectorMsg);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PickUpVeg(NetworkMessage msg)
|
|
{
|
|
VegMsg vegMsg;
|
|
if (!Utility.ReadMessage<VegMsg>(msg, out vegMsg))
|
|
return;
|
|
|
|
|
|
if (!ServerManager.AllPlayers.ContainsKey(vegMsg.ID))
|
|
{
|
|
Debug.Log("Recieved Veg from unknown player");
|
|
return;
|
|
}
|
|
|
|
Player player = ServerManager.AllPlayers[vegMsg.ID];
|
|
|
|
|
|
foreach (Player otherPlayer in ServerManager.AllPlayers.Values)
|
|
{
|
|
if (otherPlayer.Equals(player))
|
|
continue;
|
|
|
|
otherPlayer.Connection.Send(PlayerMsgID.Vegetable, vegMsg);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|