-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerGameMasterTool.cs
More file actions
213 lines (176 loc) · 6.48 KB
/
PlayerGameMasterTool.cs
File metadata and controls
213 lines (176 loc) · 6.48 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// player game master stats / actions / controls.
using UnityEditor;
using UnityEngine;
using Mirror;
namespace uMMORPG
{
public class PlayerGameMasterTool : NetworkBehaviour
{
[Header("Components")]
public Player player;
// note: isGameMaster flag is in Player.cs
// server data via SyncVar and SyncToOwner is the easiest solution
[HideInInspector, SyncVar] public int connections;
[HideInInspector, SyncVar] public int maxConnections;
[HideInInspector, SyncVar] public int onlinePlayers;
[HideInInspector, SyncVar] public float uptime;
[HideInInspector, SyncVar] public int tickRate;
// tick rate helpers
int tickRateCounter;
double tickRateStart;
// server data /////////////////////////////////////////////////////////////
public override void OnStartServer()
{
// validate: only for GMs
if (!player.isGameMaster) return;
// send data to client every few seconds. use syncInterval for it.
InvokeRepeating(nameof(RefreshData), syncInterval, syncInterval);
}
[ServerCallback]
void Update()
{
// validate: only for GMs
if (!player.isGameMaster) return;
// measure tick rate to get an idea of server load
++tickRateCounter;
if (NetworkTime.time >= tickRateStart + 1)
{
// save tick rate. will be synced to client automatically.
tickRate = tickRateCounter;
// start counting again
tickRateCounter = 0;
tickRateStart = NetworkTime.time;
}
}
[Server]
void RefreshData()
{
// validate: only for GMs
if (!player.isGameMaster) return;
// refresh sync vars. will be synced to client automatically.
connections = NetworkServer.connections.Count;
maxConnections = NetworkManager.singleton.maxConnections;
onlinePlayers = Player.onlinePlayers.Count;
uptime = Time.realtimeSinceStartup;
}
[Command]
public void CmdSendGlobalMessage(string message)
{
// validate: only for GMs
if (!player.isGameMaster) return;
player.chat.SendGlobalMessage(message);
}
[Command]
public void CmdShutdown()
{
// validate: only for GMs
if (!player.isGameMaster) return;
NetworkManagerMMO.Quit();
}
// character ///////////////////////////////////////////////////////////////
[Command]
public void CmdSetCharacterInvincible(bool value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
player.combat.invincible = value;
}
[Command]
public void CmdSetCharacterLevel(int value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
player.level.current = Mathf.Clamp(value, 1, player.level.max);
}
[Command]
public void CmdSetCharacterExperience(long value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
player.experience.current = Utils.Clamp(value, 0, player.experience.max);
}
[Command]
public void CmdSetCharacterSkillExperience(long value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
if (value > 0)
((PlayerSkills)player.skills).skillExperience = value;
}
[Command]
public void CmdSetCharacterGold(long value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
if (value > 0)
player.gold = value;
}
[Command]
public void CmdSetCharacterCoins(long value)
{
// validate: only for GMs
if (!player.isGameMaster) return;
if (value > 0)
player.itemMall.coins = value;
}
// player actions //////////////////////////////////////////////////////////
[Command]
public void CmdWarp(string otherPlayer)
{
// validate: only for GMs
if (!player.isGameMaster) return;
// warp self to other
if (Player.onlinePlayers.TryGetValue(otherPlayer, out Player other))
player.movement.Warp(other.transform.position);
}
[Command]
public void CmdSummon(string otherPlayer)
{
// validate: only for GMs
if (!player.isGameMaster) return;
// summon other to self and add chat message so the player knows why
// it happened
if (Player.onlinePlayers.TryGetValue(otherPlayer, out Player other))
{
other.movement.Warp(player.transform.position);
other.chat.TargetMsgInfo("A GM summoned you.");
}
}
[Command]
public void CmdKill(string otherPlayer)
{
// validate: only for GMs
if (!player.isGameMaster) return;
// kill other and add chat message so the player knows why it happened
if (Player.onlinePlayers.TryGetValue(otherPlayer, out Player other))
{
other.health.current = 0;
other.chat.TargetMsgInfo("A GM killed you.");
}
}
[Command]
public void CmdKick(string otherPlayer)
{
// validate: only for GMs
if (!player.isGameMaster) return;
// kick other
if (Player.onlinePlayers.TryGetValue(otherPlayer, out Player other))
// TODO add a reason for kick so people don't think they were disconnected
other.connectionToClient.Disconnect();
}
// validation //////////////////////////////////////////////////////////////
protected override void OnValidate()
{
base.OnValidate();
// gm tool data should only ever be synced to owner!
// observers should not know about it!
if (syncMode != SyncMode.Owner)
{
syncMode = SyncMode.Owner;
#if UNITY_EDITOR
Undo.RecordObject(this, name + " " + GetType() + " component syncMode changed to Owner.");
#endif
}
}
}
}