summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/GameData.cs
blob: b67158befb7c2d3cc4c02356179dc9f3e35f1140 (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.CoreScripts;
using Hazel;
using InnerNet;
using UnityEngine;

//c 单局游戏数据,同步频率比较小。是由host玩家管理的结构。
// 单局游戏内只有一个这个结构,且clientId是host玩家的id,由host玩家所有,所以只有host玩家会序列化,其他玩家的机器deserialize
// 这样做能够保证客户端不会冲突,不会出现某台机器上某个任务完成了,但另一台机器没有完成的情况
// 所有的全局数据全部由host玩家管理。
// 类似的还有ship status
public class GameData : InnerNetObject, IDisconnectHandler
{
    // 玩家个数
	public int PlayerCount
	{
		get
		{
			return this.AllPlayers.Count;
		}
	}

	public static GameData Instance;

    // 所有角色数据,包括外观、装饰、是否是imposter、是否死亡
    public List<GameData.PlayerInfo> AllPlayers = new List<GameData.PlayerInfo>();

    // 全部任务数
	public int TotalTasks;

    // 完成了的任务数
	public int CompletedTasks;

	public const byte InvalidPlayerId = 255;

	public const byte DisconnectedPlayerId = 254;

	public class TaskInfo
	{
		public uint Id;

		public bool Complete;

		public void Serialize(MessageWriter writer)
		{
			writer.WritePacked(this.Id);
			writer.Write(this.Complete);
		}

		public void Deserialize(MessageReader reader)
		{
			this.Id = reader.ReadPackedUInt32();
			this.Complete = reader.ReadBoolean();
		}
	}

    //c 角色数据,包括外观、装饰、是否是imposter、是否死亡
	public class PlayerInfo
	{
		public PlayerControl Object
		{
			get
			{
				if (!this._object)
				{
					this._object = PlayerControl.AllPlayerControls.FirstOrDefault((PlayerControl p) => p.PlayerId == this.PlayerId);
				}
				return this._object;
			}
		}

        // 单局游戏内会给每一个玩家分配一个playerId
		public readonly byte PlayerId;

		public string PlayerName = string.Empty;

		public byte ColorId;

		public uint HatId;

		public uint PetId;

		public uint SkinId;

		public bool Disconnected;

		public List<GameData.TaskInfo> Tasks;

		public bool IsImpostor;

		public bool IsDead;

		private PlayerControl _object;

		public PlayerInfo(byte playerId)
		{
			this.PlayerId = playerId;
		}

		public PlayerInfo(PlayerControl pc) : this(pc.PlayerId)
		{
			this._object = pc;
		}

		public void Serialize(MessageWriter writer)
		{
			writer.Write(this.PlayerName);
			writer.Write(this.ColorId);
			writer.WritePacked(this.HatId);
			writer.WritePacked(this.PetId);
			writer.WritePacked(this.SkinId);
			byte b = 0;
			if (this.Disconnected)
			{
				b |= 1;
			}
			if (this.IsImpostor)
			{
				b |= 2;
			}
			if (this.IsDead)
			{
				b |= 4;
			}
			writer.Write(b);
			if (this.Tasks != null)
			{
				writer.Write((byte)this.Tasks.Count);
				for (int i = 0; i < this.Tasks.Count; i++)
				{
					this.Tasks[i].Serialize(writer);
				}
				return;
			}
			writer.Write(0);
		}

		public void Deserialize(MessageReader reader)
		{
			this.PlayerName = reader.ReadString();
			this.ColorId = reader.ReadByte();
			this.HatId = reader.ReadPackedUInt32();
			this.PetId = reader.ReadPackedUInt32();
			this.SkinId = reader.ReadPackedUInt32();
			byte b = reader.ReadByte();
			this.Disconnected = ((b & 1) > 0);
			this.IsImpostor = ((b & 2) > 0);
			this.IsDead = ((b & 4) > 0);
			byte b2 = reader.ReadByte();
			this.Tasks = new List<GameData.TaskInfo>((int)b2);
			for (int i = 0; i < (int)b2; i++)
			{
				this.Tasks.Add(new GameData.TaskInfo());
				this.Tasks[i].Deserialize(reader);
			}
		}

		public GameData.TaskInfo FindTaskById(uint taskId)
		{
			for (int i = 0; i < this.Tasks.Count; i++)
			{
				if (this.Tasks[i].Id == taskId)
				{
					return this.Tasks[i];
				}
			}
			return null;
		}
	}


	public void Awake()
	{
		if (GameData.Instance && GameData.Instance != this)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		GameData.Instance = this;
		if (AmongUsClient.Instance)
		{
			AmongUsClient.Instance.DisconnectHandlers.AddUnique(this);
		}
	}

	internal void SetDirty()
	{
		base.SetDirtyBit(uint.MaxValue);
	}

	public GameData.PlayerInfo GetHost()
	{
		ClientData host = AmongUsClient.Instance.GetHost();
		if (host != null && host.Character)
		{
			return host.Character.Data;
		}
		return null;
	}

	public sbyte GetAvailableId()
	{
		sbyte i;
		sbyte j;
		for (i = 0; i < 10; i = (sbyte)(j + 1))
		{
			if (!this.AllPlayers.Any((GameData.PlayerInfo p) => p.PlayerId == (byte)i))
			{
				return i;
			}
			j = i;
		}
		return -1;
	}

	public GameData.PlayerInfo GetPlayerById(byte id)
	{
		if (id == 255)
		{
			return null;
		}
		for (int i = 0; i < this.AllPlayers.Count; i++)
		{
			if (this.AllPlayers[i].PlayerId == id)
			{
				return this.AllPlayers[i];
			}
		}
		return null;
	}

	public void UpdateName(byte playerId, string name)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById != null)
		{
			playerById.PlayerName = name;
		}
		base.SetDirtyBit(1U << (int)playerId);
	}

	public void UpdateColor(byte playerId, byte color)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById != null)
		{
			playerById.ColorId = color;
		}
		base.SetDirtyBit(1U << (int)playerId);
	}

	public void UpdateHat(byte playerId, uint hat)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById != null)
		{
			playerById.HatId = hat;
		}
		base.SetDirtyBit(1U << (int)playerId);
	}

	public void UpdatePet(byte playerId, uint petId)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById != null)
		{
			playerById.PetId = petId;
		}
		base.SetDirtyBit(1U << (int)playerId);
	}

	public void UpdateSkin(byte playerId, uint skin)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById != null)
		{
			playerById.SkinId = skin;
		}
		base.SetDirtyBit(1U << (int)playerId);
	}

	public void AddPlayer(PlayerControl pc)
	{
		GameData.PlayerInfo item = new GameData.PlayerInfo(pc);
		this.AllPlayers.Add(item);
		base.SetDirtyBit(1U << (int)pc.PlayerId);
	}

	public bool RemovePlayer(byte playerId)
	{
		for (int i = 0; i < this.AllPlayers.Count; i++)
		{
			if (this.AllPlayers[i].PlayerId == playerId)
			{
				this.SetDirty();
				this.AllPlayers.RemoveAt(i);
				return true;
			}
		}
		return false;
	}

	public void RecomputeTaskCounts()
	{
		this.TotalTasks = 0;
		this.CompletedTasks = 0;
		for (int i = 0; i < this.AllPlayers.Count; i++)
		{
			GameData.PlayerInfo playerInfo = this.AllPlayers[i];
			if (!playerInfo.Disconnected && playerInfo.Tasks != null && playerInfo.Object && (PlayerControl.GameOptions.GhostsDoTasks || !playerInfo.IsDead) && !playerInfo.IsImpostor)
			{
				for (int j = 0; j < playerInfo.Tasks.Count; j++)
				{
					this.TotalTasks++;
					if (playerInfo.Tasks[j].Complete)
					{
						this.CompletedTasks++;
					}
				}
			}
		}
	}

	public void TutOnlyRemoveTask(byte playerId, uint taskId)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		GameData.TaskInfo item = playerById.FindTaskById(taskId);
		playerById.Tasks.Remove(item);
		this.RecomputeTaskCounts();
	}

	public void TutOnlyAddTask(byte playerId, uint taskId)
	{
		this.GetPlayerById(playerId).Tasks.Add(new GameData.TaskInfo
		{
			Id = taskId
		});
		this.TotalTasks++;
	}

	private void SetTasks(byte playerId, byte[] taskTypeIds)
	{
		GameData.PlayerInfo playerById = this.GetPlayerById(playerId);
		if (playerById == null)
		{
			Debug.Log("Could not set tasks for player id: " + playerId);
			return;
		}
		if (playerById.Disconnected)
		{
			return;
		}
		if (!playerById.Object)
		{
			Debug.Log(string.Concat(new object[]
			{
				"Could not set tasks for player (",
				playerById.PlayerName,
				"): ",
				playerId
			}));
			return;
		}
		playerById.Tasks = new List<GameData.TaskInfo>(taskTypeIds.Length);
		for (int i = 0; i < taskTypeIds.Length; i++)
		{
			playerById.Tasks.Add(new GameData.TaskInfo());
			playerById.Tasks[i].Id = (uint)i;
		}
		playerById.Object.SetTasks(taskTypeIds);
		base.SetDirtyBit(1U << (int)playerById.PlayerId);
	}

	public void CompleteTask(PlayerControl pc, uint taskId)
	{
		GameData.TaskInfo taskInfo = pc.Data.FindTaskById(taskId);
		if (taskInfo == null)
		{
			Debug.LogWarning("Couldn't find task: " + taskId);
			return;
		}
		if (!taskInfo.Complete)
		{
			taskInfo.Complete = true;
			this.CompletedTasks++;
			return;
		}
		Debug.LogWarning("Double complete task: " + taskId);
	}

	public void HandleDisconnect(PlayerControl player, DisconnectReasons reason)
	{
		if (!player)
		{
			return;
		}
		GameData.PlayerInfo playerById = this.GetPlayerById(player.PlayerId);
		if (playerById == null)
		{
			return;
		}
		if (AmongUsClient.Instance.IsGameStarted)
		{
			if (!playerById.Disconnected)
			{
				playerById.Disconnected = true;
				TempData.LastDeathReason = DeathReason.Disconnect;
				this.ShowNotification(playerById.PlayerName, reason);
			}
		}
		else if (this.RemovePlayer(player.PlayerId))
		{
			this.ShowNotification(playerById.PlayerName, reason);
		}
		this.RecomputeTaskCounts();
	}

	private void ShowNotification(string playerName, DisconnectReasons reason)
	{
		if (string.IsNullOrEmpty(playerName))
		{
			return;
		}
		if (reason <= DisconnectReasons.Banned)
		{
			if (reason == DisconnectReasons.ExitGame)
			{
				DestroyableSingleton<HudManager>.Instance.Notifier.AddItem(playerName + " left the game.");
				return;
			}
			if (reason == DisconnectReasons.Banned)
			{
				GameData.PlayerInfo data = AmongUsClient.Instance.GetHost().Character.Data;
				DestroyableSingleton<HudManager>.Instance.Notifier.AddItem(playerName + " was banned by " + data.PlayerName + ".");
				return;
			}
		}
		else if (reason == DisconnectReasons.Kicked)
		{
			GameData.PlayerInfo data2 = AmongUsClient.Instance.GetHost().Character.Data;
			DestroyableSingleton<HudManager>.Instance.Notifier.AddItem(playerName + " was kicked by " + data2.PlayerName + ".");
			return;
		}
		DestroyableSingleton<HudManager>.Instance.Notifier.AddItem(playerName + " left the game due to error.");
	}

	public void HandleDisconnect()
	{
		if (!AmongUsClient.Instance.IsGameStarted)
		{
			for (int i = this.AllPlayers.Count - 1; i >= 0; i--)
			{
				if (!this.AllPlayers[i].Object)
				{
					this.AllPlayers.RemoveAt(i);
				}
			}
		}
	}

	public override bool Serialize(MessageWriter writer, bool initialState)
	{
		if (initialState)
		{
			if (!DestroyableSingleton<Telemetry>.Instance.IsInitialized)
			{
				DestroyableSingleton<Telemetry>.Instance.Initialize();
			}
			writer.WriteBytesAndSize(DestroyableSingleton<Telemetry>.Instance.CurrentGuid.ToByteArray());
			writer.WritePacked(this.AllPlayers.Count);
			for (int i = 0; i < this.AllPlayers.Count; i++)
			{
				GameData.PlayerInfo playerInfo = this.AllPlayers[i];
				writer.Write(playerInfo.PlayerId);
				playerInfo.Serialize(writer);
			}
		}
		else
		{
			int position = writer.Position;
			byte b = 0;
			writer.Write(b);
			for (int j = 0; j < this.AllPlayers.Count; j++)
			{
				GameData.PlayerInfo playerInfo2 = this.AllPlayers[j];
				if ((this.DirtyBits & 1U << (int)playerInfo2.PlayerId) != 0U)
				{
					writer.Write(playerInfo2.PlayerId);
					playerInfo2.Serialize(writer);
					b += 1;
				}
			}
			writer.Position = position;
			writer.Write(b);
			writer.Position = writer.Length;
			this.DirtyBits = 0U;
		}
		return true;
	}

	public override void Deserialize(MessageReader reader, bool initialState)
	{
		if (initialState)
		{
			Guid gameGuid = new Guid(reader.ReadBytesAndSize());
			if (!DestroyableSingleton<Telemetry>.Instance.IsInitialized)
			{
				DestroyableSingleton<Telemetry>.Instance.Initialize(gameGuid);
			}
			int num = reader.ReadPackedInt32();
			for (int i = 0; i < num; i++)
			{
				GameData.PlayerInfo playerInfo = new GameData.PlayerInfo(reader.ReadByte());
				playerInfo.Deserialize(reader);
				this.AllPlayers.Add(playerInfo);
			}
		}
		else
		{
			byte b = reader.ReadByte();
			for (int j = 0; j < (int)b; j++)
			{
				byte b2 = reader.ReadByte();
				GameData.PlayerInfo playerInfo2 = this.GetPlayerById(b2);
				if (playerInfo2 != null)
				{
					playerInfo2.Deserialize(reader);
				}
				else
				{
					playerInfo2 = new GameData.PlayerInfo(b2);
					playerInfo2.Deserialize(reader);
					this.AllPlayers.Add(playerInfo2);
				}
			}
		}
		this.RecomputeTaskCounts();
	}

    private enum RpcCalls
    {
        SetTasks
    }

    public void RpcSetTasks(byte playerId, byte[] taskTypeIds)
	{
		if (AmongUsClient.Instance.AmClient)
		{
			this.SetTasks(playerId, taskTypeIds);
		}
		MessageWriter messageWriter = AmongUsClient.Instance.StartRpc(this.NetId, 0, SendOption.Reliable);
		messageWriter.Write(playerId);
		messageWriter.WriteBytesAndSize(taskTypeIds);
		messageWriter.EndMessage();
	}

	public override void HandleRpc(byte callId, MessageReader reader)
	{
		if (callId == 0)
		{
			this.SetTasks(reader.ReadByte(), reader.ReadBytesAndSize());
		}
	}
}