-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerCode.cs
333 lines (293 loc) · 9.93 KB
/
PlayerCode.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using TMPro;
public class PlayerCode : NetworkBehaviour
{
public string PlayerUID;
public string PlayerName;
public string PlayerTitle;
public string MyMemeText;
public string VotedMemeMadeBy = "";
public Text MyMainMemeText;
public float ServerUpdateFreq = 3f;
public bool ReadyToTerminate = false;
public AudioClip roundWin;
public AudioClip roundLost;
public bool HostDecendant = false;
private TMP_InputField MyInputField;
private GameObject gameManager;
private TurnManager myTurnManager;
private float serverUpdateOffset;
private bool updatedTextYet = false;
private bool checkLocalPlayer = false;
// This important script is attached to all Player Gameobjects. This is the unique object that identifies them in matches.
// It contains logic for local player behavior, and stores player data
//Upon boot, update the server and all other players of your presence and your data
void Start()
{
if (isServer)
Debug.Log("I am the server!");
if (isLocalPlayer)
{
//This is code that only works if this is the local player!
/*
PlayerUID = PlayerPrefs.GetInt("MM_UID", Random.Range(0, 10000000)).ToString();// GenerateRandomUID();
PlayerName = PlayerPrefs.GetString("MM_Username", GenerateRandomName());
PlayerTitle = PlayerPrefs.GetString("MM_PlayerTitle", "");
*/
///*
//TESTING CODE
PlayerUID = GenerateRandomUID();// GenerateRandomUID();
PlayerName = GenerateRandomName();
PlayerTitle = GenerateRandomTitle();
//END OF TESTING CODE
//*/
serverUpdateOffset = Random.Range(0f, 1 / ServerUpdateFreq);
UpdatePlayerObjectName();
CmdUpdateNewPlayerDetails(PlayerName, PlayerTitle, PlayerUID, ReadyToTerminate);
}
gameManager = GameObject.Find("ManagerMatch");
myTurnManager = gameManager.GetComponentInChildren<TurnManager>();
if (isLocalPlayer)
{
PlayerPrefs.SetString("MM_CrashRoomcode", GameObject.FindGameObjectWithTag("MatchManager").GetComponent<RoomHandlerCode>().RoomCodeConnectedTo);
}
}
//When the object is enabled, prepare match objects accordingly
void OnEnable()
{
Start();
myTurnManager.CmdSetupMainMemeForMe(PlayerUID);
myTurnManager.CmdGetMyTurnStage(PlayerUID);
checkLocalPlayer = true;
}
//Using update, we know when to enable or disable certain functionality depending on where we are in the match
void Update()
{
if (isLocalPlayer)
{
if (checkLocalPlayer)
{
checkLocalPlayer = false;
if (myTurnManager.TurnStage == 5 || myTurnManager.TurnStage == 7 || myTurnManager.TurnStage == 9)
{
Debug.Log("Local player is turning waiting for next round panel on");
myTurnManager.IsStageEndLoadingPanelEnabled(true);
}
myTurnManager.GetComponent<MatchManager>().CmdUpdatePlayerListUI("");
}
//This is code that only works if this is the local player!
if (MyMainMemeText == null)
{
try
{
MyMainMemeText = GameObject.Find("MemeTextNormalMain").GetComponent<Text>();
}
catch
{
}
}
else if (myTurnManager.TurnStage == 3)
MyMainMemeText.text = MyMemeText;
if (MyInputField == null)
{
try
{
MyInputField = GameObject.Find("InputFieldMeme").GetComponent<TMP_InputField>();
MyInputField.interactable = true;
}
catch
{
}
}
else if (myTurnManager.TurnStage == 3)
{
UpdateMemeText(MyInputField.text);
}
}
}
//When player disconnects, notify the UI manager
void OnDisable()
{
myTurnManager.gameObject.GetComponent<MatchManager>().CmdUpdatePlayerListUI(PlayerUID);
}
/// <summary>
/// These are functions for testing, and should not be used in the final version.
/// Instead, they are here for testing purposes only.
/// Use them as intended!
/// </summary>
///
private string GenerateRandomName()
{
string[] Names = new string[10] { "Nimrod", "Inbar", "Gal", "Shachar", "Naama", "Robin", "Ilay", "Omri", "Amit", "Batman" };
return (Names[Random.Range(0, Names.Length)]);
}
private string GenerateRandomUID()
{
return (Random.Range(1, 9999999).ToString());
}
private string GenerateRandomTitle()
{
string[] Titles = new string[10] { "The Great", "The Terrible", "The Wicked", "The Good", "The Bad", "The Ugly", "The Master", "The Unknown", "The Unready", "The Hilarious" };
return (Titles[Random.Range(0, Titles.Length)]);
}
/// <summary>
/// End of testing funcs
/// </summary>
[Command]
void CmdUpdateNewPlayerDetails(string plName, string plTitle, string plUID, bool plTerminate)
{
PlayerName = plName;
PlayerTitle = plTitle;
PlayerUID = plUID;
ReadyToTerminate = plTerminate;
UpdatePlayerObjectName();
RpcUpdateNewPlayerDetails(plName, plTitle, plUID ,plTerminate);
}
[ClientRpc]
void RpcUpdateNewPlayerDetails(string plName, string plTitle, string plUID, bool plTerminate)
{
if (!hasAuthority)
{
PlayerName = plName;
PlayerTitle = plTitle;
PlayerUID = plUID;
ReadyToTerminate = plTerminate;
UpdatePlayerObjectName();
}
}
void UpdatePlayerObjectName()
{
gameObject.name = "PlayerObject [" + PlayerName + " " + PlayerTitle + "]";
}
public void SetAsHostDecendant()
{
RpcSetAsHostDecendant();
}
[ClientRpc]
private void RpcSetAsHostDecendant()
{
if (IsLocalPlayer())
{
HostDecendant = true;
GameObject.Find("ManagerMatch").GetComponent<MatchManager>().HostDecendant = HostDecendant;
}
}
public void UpdateMemeText(string memeText)
{
if (!isLocalPlayer)
return;
MyMemeText = memeText;
if (((myTurnManager.GetTime() + serverUpdateOffset) % (1f / ServerUpdateFreq) < (1f / ServerUpdateFreq) / 2f && updatedTextYet == false) || myTurnManager.GetTime() == myTurnManager.GetWaitingForInputTime())
{
updatedTextYet = true;
CmdUpdateMemeText(MyMemeText);
}
else if ((myTurnManager.GetTime() + serverUpdateOffset) % (1f / ServerUpdateFreq) > (1f / ServerUpdateFreq) / 2f && updatedTextYet == true)
{
updatedTextYet = false;
}
}
public void LockSubmitButtonPress()
{
if (IsLocalPlayer())
{
CmdLockSubmitbuttonPress();
MyInputField.interactable = false;
}
}
[Command]
public void CmdLockSubmitbuttonPress()
{
CmdUpdateMemeText(MyMemeText);
myTurnManager.LockPlayer(PlayerUID);
}
[Command]
public void CmdLockVoteButtonPress()
{
myTurnManager.LockPlayer(PlayerUID);
}
[Command]
public void CmdUnlockSubmitButtonPress()
{
myTurnManager.UnlockPlayer(PlayerUID);
}
public void UnlockSubmitButtonPress()
{
if (IsLocalPlayer())
{
CmdUnlockSubmitButtonPress();
if (MyInputField != null)
MyInputField.interactable = true;
}
}
public void PayPlayer(string compressedPlayerList)
{
if (!isLocalPlayer)
return;
List<string> playerRankList = TurnManager.StringDeseparator(compressedPlayerList);
for (int i = 0; i < playerRankList.Count; i++)
{
if (playerRankList[i] == (PlayerName + " " + PlayerTitle))
{
PlayerPrefs.SetInt("MM_MoneyEarned", Mathf.Clamp((playerRankList.Count - i), 0, 5));
PlayerPrefs.SetInt("MM_Money", PlayerPrefs.GetInt("MM_Money", 0) + Mathf.Clamp((playerRankList.Count - i), 0, 5));
break;
}
}
}
public void PlayRoundEndMusic(string winnerUID)
{
if (!isLocalPlayer)
{
Debug.Log("Not local player. Not playing round end music");
return;
}
if (winnerUID == PlayerUID)
{
this.GetComponent<AudioSource>().PlayOneShot(roundWin);
Debug.Log("Won round, so playing winner music");
}
else
{
this.GetComponent<AudioSource>().PlayOneShot(roundLost);
Debug.Log("Lost round, so playing losing music");
}
}
[Command]
void CmdUpdateMemeText(string memeText)
{
MyMemeText = memeText;
//RpcUpdateMemeText(memeText);//I don't want to update meme text on all clients, because this adds heaps of traffic to the server
}
[ClientRpc]
void RpcUpdateMemeText(string memeText)
{
//This happens on all clients
if (!hasAuthority)
{
//But this happens only on the clients that didn't cause the update
MyMemeText = memeText;
}
}
public void ChooseMemeToVoteFor(string memeWriter)
{
if (isLocalPlayer)
{
VotedMemeMadeBy = memeWriter;
CmdChooseMemeToVoteFor(memeWriter);
}
}
[Command]
void CmdChooseMemeToVoteFor(string memeWriter)
{
VotedMemeMadeBy = memeWriter;
}
public bool IsLocalPlayer()
{
return isLocalPlayer;
}
}