forked from Skippeh/Oxide.GettingOverItMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetPlayer.cs
178 lines (143 loc) · 6.07 KB
/
NetPlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using Lidgren.Network;
using ServerShared.Logging;
using UnityEngine;
using Logger = ServerShared.Logging.Logger;
namespace ServerShared.Player
{
public class NetPlayer
{
public readonly int Id;
public readonly NetConnection Peer;
public readonly ulong SteamId;
public PlayerMove Movement;
public string Name;
public NetPlayer SpectateTarget;
public bool Spectating => SpectateTarget != null;
public int Wins { get; private set; } = 0;
public float Goldness { get; private set; }
public Color PotColor { get; private set; } = Color.white;
public PlayerAccessLevelIdentity Identity { get; private set; }
public AccessLevel AccessLevel => Identity.AccessLevel;
private GameServer server;
private static int idCounter = 1;
public NetPlayer(NetConnection connection, string name, GameServer server, ulong steamId)
{
Peer = connection ?? throw new ArgumentNullException(nameof(connection));
Name = name ?? throw new ArgumentNullException(nameof(name));
SteamId = steamId;
this.server = server ?? throw new ArgumentNullException(nameof(server));
Id = idCounter++;
Identity = server.RequireSteamAuth
? new PlayerAccessLevelIdentity(steamId, AccessLevel.Player)
: new PlayerAccessLevelIdentity(connection.RemoteEndPoint.Address.ToUint32(), AccessLevel.Player);
}
/// <param name="color">If null then Color.white will be used.</param>
public void SendChatMessage(string message, UnityEngine.Color? color = null)
{
var writer = server.CreateMessage();
writer.Write(MessageType.ChatMessage);
writer.Write(0); // player id (0 = equivalent of null)
writer.Write((string) null);
writer.WriteRgbaColor(color ?? UnityEngine.Color.white);
writer.Write(message);
Peer.SendMessage(writer, NetDeliveryMethod.ReliableOrdered, 0);
}
public void SendConsoleMessage(string message, LogMessageType type)
{
// Todo: implement client console
}
public void Spectate(NetPlayer target)
{
if (SpectateTarget == target)
return;
bool wasSpectating = SpectateTarget != null;
SpectateTarget = target;
// Send message to connection to start/stop spectating target
var netMessage = server.CreateMessage();
netMessage.Write(MessageType.SpectateTarget);
netMessage.Write(target?.Id ?? 0);
Peer.SendMessage(netMessage, NetDeliveryMethod.ReliableOrdered, 0);
netMessage = server.CreateMessage();
if (target != null && !wasSpectating) // Started spectating
{
// Stop people from spectating this player
foreach (var netPlayer in server.Players.Values)
{
if (netPlayer.SpectateTarget == this)
netPlayer.Spectate(null);
}
// Broadcast message to all other peers to remove this peers player (if he wasn't spectating already, otherwise he would already be despawned)
netMessage.Write(MessageType.RemovePlayer);
netMessage.Write(Id);
server.Broadcast(netMessage, NetDeliveryMethod.ReliableOrdered, 0, Peer);
}
else if (target == null) // Stopped spectating
{
// Broadcast message to all other peers to add this peers player
netMessage.Write(MessageType.CreatePlayer);
netMessage.Write(Id);
netMessage.Write(Name);
netMessage.Write(Movement);
netMessage.Write(Wins);
netMessage.Write(Goldness);
netMessage.WriteRgbaColor(PotColor);
server.Broadcast(netMessage, NetDeliveryMethod.ReliableOrdered, 0, Peer);
}
}
public void SetPotProperties(float goldness, Color color, bool broadcastToClients = true)
{
Goldness = Mathf.Clamp01(goldness);
PotColor = color;
if (!broadcastToClients)
return;
BroadcastPotProperties();
}
public void SetGoldness(float goldness, bool broadcastToClients = true)
{
if (Math.Abs(Goldness - goldness) < 0.001f)
return;
Goldness = UnityEngine.Mathf.Clamp01(goldness);
if (!broadcastToClients)
return;
BroadcastPotProperties();
}
public void SetPotColor(Color color, bool broadcastToClients = true)
{
PotColor = color;
if (!broadcastToClients)
return;
BroadcastPotProperties();
}
public void SetWins(int wins, bool updateGoldness, bool broadcastToClients = true)
{
if (Wins == wins)
return;
Wins = wins;
if (broadcastToClients)
{
var message = server.CreateMessage();
message.Write(MessageType.PlayerWins);
message.Write(Id);
message.Write(Wins);
server.Broadcast(message, NetDeliveryMethod.ReliableOrdered, 0);
}
if (updateGoldness)
SetGoldness(Mathf.Pow(wins / 50f, 2), broadcastToClients);
}
public void SetAccessLevel(AccessLevel accessLevel)
{
Identity.AccessLevel = accessLevel;
Logger.LogInfo($"{Name} access level set to {AccessLevel}.");
}
private void BroadcastPotProperties()
{
var message = server.CreateMessage();
message.Write(MessageType.PlayerPotProperties);
message.Write(Id);
message.Write(Goldness);
message.WriteRgbaColor(PotColor);
server.Broadcast(message, NetDeliveryMethod.ReliableOrdered, 0);
}
}
}