Skip to content

Commit

Permalink
Merge pull request #140 from Mushroom/player-user-names
Browse files Browse the repository at this point in the history
Add player username syncing
  • Loading branch information
hubastard authored Apr 13, 2021
2 parents fe1753f + 65459b6 commit 7f2167d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NebulaClient/MultiplayerClientSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void ClientSocket_OnOpen(object sender, System.EventArgs e)
serverConnection = new NebulaConnection(clientSocket, serverEndpoint, PacketProcessor);
IsConnected = true;
//TODO: Maybe some challenge-response authentication mechanism?
SendPacket(new HandshakeRequest(CryptoUtils.GetPublicKey(CryptoUtils.GetOrCreateUserCert())));
SendPacket(new HandshakeRequest(CryptoUtils.GetPublicKey(CryptoUtils.GetOrCreateUserCert()), GameMain.data.account.userName));
}

private void ClientSocket_OnClose(object sender, CloseEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion NebulaHost/MultiplayerHostSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void StartServer(int port, bool loadSaveFile = false)
LocalPlayer.IsMasterClient = true;

// TODO: Load saved player info here
LocalPlayer.SetPlayerData(new PlayerData(PlayerManager.GetNextAvailablePlayerId(), GameMain.localPlanet?.id ?? -1, new Float3(1.0f, 0.6846404f, 0.243137181f)));
LocalPlayer.SetPlayerData(new PlayerData(PlayerManager.GetNextAvailablePlayerId(), GameMain.localPlanet?.id ?? -1, new Float3(1.0f, 0.6846404f, 0.243137181f), AccountData.me.userName));
}

private void StopServer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public void ProcessPacket(HandshakeRequest packet, NebulaConnection conn)
playerManager.SavedPlayerData.Add(clientCertHash, player.Data);
}

// Add the username to the player data
player.Data.Username = packet.Username;

// Make sure that each player that is currently in the game receives that a new player as join so they can create its RemotePlayerCharacter
PlayerData pdata = player.Data.CreateCopyWithoutMechaData(); // Remove inventory from mecha data
foreach (Player activePlayer in playerManager.GetConnectedPlayers())
Expand Down
8 changes: 6 additions & 2 deletions NebulaModel/DataStructures/PlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace NebulaModel.DataStructures
[RegisterNestedType]
public class PlayerData : INetSerializable
{
public string Username { get; set; }
public ushort PlayerId { get; set; }
public int LocalPlanetId { get; set; }
public Float3 Color { get; set; }
Expand All @@ -16,10 +17,11 @@ public class PlayerData : INetSerializable
public MechaData Mecha { get; set; }

public PlayerData() { }
public PlayerData(ushort playerId, int localPlanetId, Float3 color, Float3 localPlanetPosition = new Float3(), Double3 position = new Double3(), Float3 rotation = new Float3(), Float3 bodyRotation = new Float3())
public PlayerData(ushort playerId, int localPlanetId, Float3 color, string username = null, Float3 localPlanetPosition = new Float3(), Double3 position = new Double3(), Float3 rotation = new Float3(), Float3 bodyRotation = new Float3())
{
PlayerId = playerId;
LocalPlanetId = localPlanetId;
Username = username ?? $"Player {playerId}";
LocalPlanetPosition = localPlanetPosition;
Color = color;
UPosition = position;
Expand All @@ -30,6 +32,7 @@ public PlayerData() { }

public void Serialize(NetDataWriter writer)
{
writer.Put(Username);
writer.Put(PlayerId);
writer.Put(LocalPlanetId);
Color.Serialize(writer);
Expand All @@ -42,6 +45,7 @@ public void Serialize(NetDataWriter writer)

public void Deserialize(NetDataReader reader)
{
Username = reader.GetString();
PlayerId = reader.GetUShort();
LocalPlanetId = reader.GetInt();
Color = reader.GetFloat3();
Expand All @@ -55,7 +59,7 @@ public void Deserialize(NetDataReader reader)

public PlayerData CreateCopyWithoutMechaData()
{
return new PlayerData(PlayerId, LocalPlanetId, Color, LocalPlanetPosition, UPosition, Rotation, BodyRotation);
return new PlayerData(PlayerId, LocalPlanetId, Color, Username, LocalPlanetPosition, UPosition, Rotation, BodyRotation);
}
}
}
4 changes: 3 additions & 1 deletion NebulaModel/Packets/Session/HandshakeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
{
public class HandshakeRequest
{
public string Username { get; set; }
public string ModVersion { get; set; }
public int GameVersionSig { get; set; }
public byte[] ClientCert { get; set; }

public HandshakeRequest() { }

public HandshakeRequest(byte[] clientCert)
public HandshakeRequest(byte[] clientCert, string username)
{
this.Username = username;
this.ModVersion = Config.ModVersion.ToString();
this.GameVersionSig = GameConfig.gameVersion.sig;
this.ClientCert = clientCert;
Expand Down
4 changes: 3 additions & 1 deletion NebulaWorld/RemotePlayerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class RemotePlayerModel
{
const int PLAYER_PROTO_ID = 1;

public string Username { get; }
public ushort PlayerId { get; }
public Transform PlayerTransform { get; set; }
public Transform PlayerModelTransform { get; set; }
Expand All @@ -22,7 +23,7 @@ public class RemotePlayerModel
public Player PlayerInstance { get; set; }
public Mecha MechaInstance { get; set; }

public RemotePlayerModel(ushort playerId)
public RemotePlayerModel(ushort playerId, string username)
{
// Spawn remote player model by cloning the player prefab and replacing local player script by remote player ones.
string playerPrefabPath = LDB.players.Select(PLAYER_PROTO_ID).PrefabPath;
Expand Down Expand Up @@ -53,6 +54,7 @@ public RemotePlayerModel(ushort playerId)
MechaInstance.Init(PlayerInstance);

PlayerId = playerId;
Username = username;
}

public void Destroy()
Expand Down
9 changes: 4 additions & 5 deletions NebulaWorld/SimulatedWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using NebulaWorld.Factory;
using NebulaWorld.MonoBehaviours.Remote;
using NebulaWorld.Trash;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -80,7 +79,7 @@ public static void SpawnRemotePlayerModel(PlayerData playerData)
{
if (!remotePlayersModels.ContainsKey(playerData.PlayerId))
{
RemotePlayerModel model = new RemotePlayerModel(playerData.PlayerId);
RemotePlayerModel model = new RemotePlayerModel(playerData.PlayerId, playerData.Username);
remotePlayersModels.Add(playerData.PlayerId, model);
}

Expand Down Expand Up @@ -333,7 +332,7 @@ public static void RenderPlayerNameTagsOnStarmap(UIStarmap starmap)
{
// Make an instance of the "Icarus" text to represent the other player name
nameText = playerModel.StarmapNameText = GameObject.Instantiate(starmap_playerNameText, starmap_playerNameText.transform.parent);
nameText.text = $"Player {playerModel.PlayerId}";
nameText.text = $"{ playerModel.Username }{ playerModel.PlayerId }";
nameText.gameObject.SetActive(true);

// Make another copy of it, but just replace it with a point to represent their location
Expand All @@ -351,7 +350,7 @@ public static void RenderPlayerNameTagsOnStarmap(UIStarmap starmap)
adjustedVector = planet.uPosition;

// Add the local position of the player
Vector3 localPlanetPosition = playerModel.Movement.GetLastPosition().LocalPlanetPosition.ToUnity();
Vector3 localPlanetPosition = playerModel.Movement.GetLastPosition().LocalPlanetPosition.ToVector3();
adjustedVector += (VectorLF3)Maths.QRotate(planet.runtimeRotation, localPlanetPosition);

// Scale as required
Expand Down Expand Up @@ -423,7 +422,7 @@ public static void RenderPlayerNameTagsInGame()
TextMesh textMesh = playerNameText.AddComponent<TextMesh>();

// Set the text to be their name
textMesh.text = $"Player {playerModel.PlayerId}";
textMesh.text = $"{ playerModel.Username }{ playerModel.PlayerId }";
// Align it to be centered below them
textMesh.anchor = TextAnchor.UpperCenter;
// Copy the font over from the sail indicator
Expand Down

0 comments on commit 7f2167d

Please sign in to comment.