Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #153 from Zhangyr2022/main
Browse files Browse the repository at this point in the history
Fix bugs of ticks in client
  • Loading branch information
Zhangyr2022 authored May 9, 2024
2 parents f74eb01 + c735d51 commit 2394c76
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 207 deletions.
2 changes: 1 addition & 1 deletion client/Assets/Scripts/Classes/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void UpdatePosition(Position pos)
TryGetPlayerAnimations();
playerAnimations?.WalkTo(playerObj.transform.position, newPos);
playerObj.transform.position = newPos;
Debug.Log(newPos);
//Debug.Log(newPos);
}
public void CreateTitleUI()
{
Expand Down
124 changes: 71 additions & 53 deletions client/Assets/Scripts/ReplayLoader/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

public class Record : MonoBehaviour
{
long begin ;



public const float ObjPrefabScaling=0.4f;
public enum PlayState
{
Expand Down Expand Up @@ -40,7 +44,7 @@ public class RecordInfo
/// <summary>
/// Contains all the item in the game
/// </summary>
public float NowFrameTime
public double NowFrameTime
{
get
{
Expand Down Expand Up @@ -207,6 +211,7 @@ private void Start()
{
_stopButton.GetComponent<Image>().sprite = _stopButtonSprite;
_recordInfo.NowPlayState = PlayState.Play;
_recordInfo.NowTime = System.DateTime.Now.Ticks;
}
});

Expand Down Expand Up @@ -313,14 +318,14 @@ private JArray LoadRecordData()
{
JObject recordJsonObject = JsonUtility.UnzipRecord(_recordFile);
// Load the record array
JArray recordArray = (JArray)recordJsonObject["records"];
JArray recordArray = (JArray)recordJsonObject["records"];

if (recordArray == null)
{
Debug.Log("Record file is empty!");
return null;
}
return recordArray;
return recordArray;
}

#region Event Definition
Expand Down Expand Up @@ -493,12 +498,13 @@ private void UpdatePlayers(JArray players)
_ => ArmorTypes.NoArmor
},
player["speed"].ToObject<float>(),
player["firearm"].ToString() switch
player["firearm"]["name"].ToString() switch
{
"S686" => FirearmTypes.S686,
"AWM" => FirearmTypes.Awm,
"VECTOR" => FirearmTypes.Vector,
"FISTS" => FirearmTypes.Fists,
"M16" => FirearmTypes.M16,
_ => FirearmTypes.Fists,
},
inventory,
Expand Down Expand Up @@ -600,56 +606,62 @@ private void UpdateTick()
{
//try
//{
if (_recordInfo.RecordSpeed > 0)
if (_recordInfo.RecordSpeed < 0)
{
return;
}

int recordTick = _recordInfo.NowTick;
while (recordTick == _recordInfo.NowTick) {
if (_recordArray[_recordInfo.NowRecordNum].Value<string>("currentTicks") != null &&
_recordArray[_recordInfo.NowRecordNum]["messageType"].ToString() == "COMPETITION_UPDATE")
{
if (_recordArray[_recordInfo.NowRecordNum].Value<string>("currentTicks") != null &&
_recordArray[_recordInfo.NowRecordNum]["messageType"].ToString() == "COMPETITION_UPDATE")
//Debug.Log(_recordArray[_recordInfo.NowRecordNum]["currentTicks"].ToString());
UpdatePlayers((JArray)_recordArray[_recordInfo.NowRecordNum]["data"]["players"]);
_recordInfo.NowTick = (int)(_recordArray[_recordInfo.NowRecordNum]["currentTicks"]);
_currentTickText.text = $"Ticks: {_recordInfo.NowTick}";
JArray events = (JArray)_recordArray[_recordInfo.NowRecordNum]["data"]["events"];
if (events != null)
{
//Debug.Log(_recordArray[_recordInfo.NowRecordNum]["currentTicks"].ToString());
UpdatePlayers((JArray)_recordArray[_recordInfo.NowRecordNum]["data"]["players"]);
_recordInfo.NowTick = (int)(_recordArray[_recordInfo.NowRecordNum]["currentTicks"]);
_currentTickText.text = $"Ticks: {_recordInfo.NowTick}";
JArray events = (JArray)_recordArray[_recordInfo.NowRecordNum]["data"]["events"];
if (events != null)
foreach (JObject eventJson in events)
{
foreach (JObject eventJson in events)
JObject eventJsonInfo = (JObject)eventJson["Json"];
switch(eventJson["Json"]["eventType"].ToString())
{
JObject eventJsonInfo = (JObject)eventJson["Json"];
switch(eventJson["Json"]["eventType"].ToString())
{
case "PLAYER_ATTACK":
AfterPlayerAttackEvent(eventJsonInfo);
break;
case "PLAYER_SWITCH_ARM":
AfterPlayerSwitchArmEvent(eventJsonInfo);
break;
case "PLAYER_PICK_UP":
AfterPlayerPickUpEvent(eventJsonInfo);
break;
case "PLAYER_USE_MEDICINE":
AfterPlayerUseMedicineEvent(eventJsonInfo);
break;
case "PLAYER_USE_GRENADE":
AfterPlayerUseGrenadeEvent(eventJsonInfo);
break;
case "PLAYER_ABANDON":
AfterPlayerAbandonEvent(eventJsonInfo);
break;
case "PLAYER_PREPARE":
break;
default:
break;
}
case "PLAYER_ATTACK":
AfterPlayerAttackEvent(eventJsonInfo);
break;
case "PLAYER_SWITCH_ARM":
AfterPlayerSwitchArmEvent(eventJsonInfo);
break;
case "PLAYER_PICK_UP":
AfterPlayerPickUpEvent(eventJsonInfo);
break;
case "PLAYER_USE_MEDICINE":
AfterPlayerUseMedicineEvent(eventJsonInfo);
break;
case "PLAYER_USE_GRENADE":
AfterPlayerUseGrenadeEvent(eventJsonInfo);
break;
case "PLAYER_ABANDON":
AfterPlayerAbandonEvent(eventJsonInfo);
break;
case "PLAYER_PREPARE":
break;
default:
break;
}
}
}
if (_recordArray[_recordInfo.NowRecordNum]["messageType"].ToString() == "SAFE_ZONE")
{
UpdateCircle(new Vector2((float)_recordArray[_recordInfo.NowRecordNum]["data"]["center"]["x"], (float)_recordArray[_recordInfo.NowRecordNum]["data"]["center"]["y"]),
(float)_recordArray[_recordInfo.NowRecordNum]["data"]["radius"]);
}
_recordInfo.NowRecordNum++;

}
if (_recordArray[_recordInfo.NowRecordNum]["messageType"].ToString() == "SAFE_ZONE")
{
UpdateCircle(new Vector2((float)_recordArray[_recordInfo.NowRecordNum]["data"]["center"]["x"], (float)_recordArray[_recordInfo.NowRecordNum]["data"]["center"]["y"]),
(float)_recordArray[_recordInfo.NowRecordNum]["data"]["radius"]);
}
_recordInfo.NowRecordNum++;
}
//}
//catch
//{
Expand All @@ -659,14 +671,20 @@ private void UpdateTick()

private void FixedUpdate()
{
if ((_recordInfo.NowPlayState == PlayState.Play && _recordInfo.NowTick < _recordInfo.MaxTick) || (_recordInfo.NowPlayState == PlayState.Jump))

if (!(_recordInfo.NowPlayState == PlayState.Play && _recordInfo.NowTick < _recordInfo.MaxTick))
{
if ((float)(System.DateTime.Now.Ticks - _recordInfo.NowTime)/1e7 > _recordInfo.NowFrameTime || _recordInfo.NowPlayState == PlayState.Jump)
{
_recordInfo.NowTime = System.DateTime.Now.Ticks;
UpdateTick();
_recordInfo.NowDeltaTime = 0;
}
return;
}

if ((float)(System.DateTime.Now.Ticks - _recordInfo.NowTime)/1e7 > _recordInfo.NowFrameTime)
{
long finishedTime = System.DateTime.Now.Ticks;
Debug.Log($"Finished: {(finishedTime - begin) / 1e7} | {System.DateTime.Now.ToString("o")}");
begin = System.DateTime.Now.Ticks;
_recordInfo.NowTime = _recordInfo.NowTime + (long)(_recordInfo.NowFrameTime*1e7);
UpdateTick();
_recordInfo.NowDeltaTime = 0;
}
}
}
23 changes: 15 additions & 8 deletions client/Assets/Scripts/Utility/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,19 @@ public static JObject UnzipRecord(string path)
}
else
{
JValue tick = (JValue)records[0]["currentTicks"];
if (tick != null)
{
// The first tick
indexAndTicks[nowRecordIndex].Item2 = (int)tick;
}
else
foreach (JToken recordInfo in records)
{
indexAndTicks[nowRecordIndex].Item2 = -2;
JValue tick = (JValue)recordInfo["currentTicks"];
if (tick != null)
{
// The first tick
indexAndTicks[nowRecordIndex].Item2 = (int)tick;
break;
}
else
{
indexAndTicks[nowRecordIndex].Item2 = -2;
}
}
}
}
Expand All @@ -162,6 +166,9 @@ public static JObject UnzipRecord(string path)
List<(int, int)> indexAndTicksList = indexAndTicks.ToList<(int, int)>();
indexAndTicksList.Sort((x, y) => x.Item2.CompareTo(y.Item2));

foreach ((int, int) it in indexAndTicksList) {
Debug.Log($"RecordInfo: {it.Item1},{it.Item2}");
}

// Write the json obj according to the order
JObject recordJsonObject = new()
Expand Down
Loading

0 comments on commit 2394c76

Please sign in to comment.