using System;
using System.Collections.Generic;
using System.Linq;
using StardewValley.Network;
namespace StardewModdingAPI.Framework.Networking
{
/// Metadata about a connected player.
internal class MultiplayerPeer : IMultiplayerPeer
{
/*********
** Fields
*********/
/// A method which sends a message to the peer.
private readonly Action SendMessageImpl;
/*********
** Accessors
*********/
///
public long PlayerID { get; }
///
public bool IsHost { get; }
///
public bool IsSplitScreen => this.ScreenID != null;
///
public bool HasSmapi => this.ApiVersion != null;
///
public int? ScreenID { get; }
///
public GamePlatform? Platform { get; }
///
public ISemanticVersion GameVersion { get; }
///
public ISemanticVersion ApiVersion { get; }
///
public IEnumerable Mods { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The player's unique ID.
/// The player's screen ID, if applicable.
/// The metadata to copy.
/// A method which sends a message to the peer.
/// Whether this is a connection to the host player.
public MultiplayerPeer(long playerID, int? screenID, RemoteContextModel model, Action sendMessage, bool isHost)
{
this.PlayerID = playerID;
this.ScreenID = screenID;
this.IsHost = isHost;
if (model != null)
{
this.Platform = model.Platform;
this.GameVersion = model.GameVersion;
this.ApiVersion = model.ApiVersion;
this.Mods = model.Mods.Select(mod => new MultiplayerPeerMod(mod)).ToArray();
}
this.SendMessageImpl = sendMessage;
}
///
public IMultiplayerPeerMod GetMod(string id)
{
if (string.IsNullOrWhiteSpace(id) || this.Mods == null || !this.Mods.Any())
return null;
id = id.Trim();
return this.Mods.FirstOrDefault(mod => mod.ID != null && mod.ID.Equals(id, StringComparison.OrdinalIgnoreCase));
}
/// Send a message to the given peer, bypassing the game's normal validation to allow messages before the connection is approved.
/// The message to send.
public void SendMessage(OutgoingMessage message)
{
this.SendMessageImpl(message);
}
}
}