Author Topic: Game action synchronization with DateTime for RFCs viable?  (Read 4254 times)

pahe

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 37
    • View Profile
Game action synchronization with DateTime for RFCs viable?
« on: January 24, 2016, 12:56:25 PM »
Hi guys.

I have the problem that I want to synchronize my game action (for a real time strategy game) by sending a DateTime with the response from the server, so each client will later know when the server had send it to everybody. As I don't have the possibility to add the server DateTime to RFCs, I'm wondering how I can do this:

  1.         internal void SendReleaseSkill(BattleSide battleSide, BattleSkillBase skill)
  2.         {
  3.             tnObject.Send(12, Target.All, TNManager.playerID, (int)battleSide, (int)skill.SkillName, DateTime.UtcNow.ToLongTimeString());
  4.         }
  5.  
  6.         [RFC(12)]
  7.         void OnTriggerSkill(int playerId, int battleSide, int skillType, string triggerDate)
  8.         {
  9.             battleView.battleLogic.ReleaseSkill((BattleSide)battleSide, associatedPlayer, (SkillName)skillType, triggerDate);
  10.         }
  11.  

I don't think that this would be a good solution to send the clients DateTime.UtcNow with it, as I think this will be very prone to cheating by the client. Would the solution be here to change from RFCs to own packets where I have more control over what the server sends to the clients?

Rough gameserver response code example:
  1.             case Packet.TriggerSkillRequest:
  2.             {
  3.                 var packetWriter = BeginSend(Packet.TriggerSkillResponse);
  4.                 packetWriter.Write(playerId);
  5.                 packetWriter.Write(battleSide);
  6.                 packetWriter.Write(skillType);
  7.                 packetWriter.Write(DateTime.UtcNow.ToLongTimeString());
  8.                 EndSend(true, player.channel, null);
  9.                 break;
  10.             }
  11.  

Thanks for advice.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Game action synchronization with DateTime for RFCs viable?
« Reply #1 on: January 25, 2016, 10:52:44 PM »
You should not be sending local time. You have TNManager.serverTime to work with. Use that instead. It's going to be the same across all clients.

pahe

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 37
    • View Profile
Re: Game action synchronization with DateTime for RFCs viable?
« Reply #2 on: January 26, 2016, 08:44:11 AM »
You should not be sending local time. You have TNManager.serverTime to work with. Use that instead. It's going to be the same across all clients.

I'm not sure if I understand correctly. Please let me resummarize my problem, maybe it was not clear enough.

The scenario is:

1. Player A (host) triggers a skill (fireball) and sends the RFC request all players.
2. Player A gets the response instantly (as its the host) and triggers the skill.
3. Player B gets the RFC 500 ms later and triggers the skill.

Now they are out of sync. That's why I would like to send the time when the skill was triggered with the RFC.

How can I use the TNManager.serverTime to know when Player A triggered his skill for Player B?

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Game action synchronization with DateTime for RFCs viable?
« Reply #3 on: January 27, 2016, 12:49:20 AM »
How can I use the TNManager.serverTime to know when Player A triggered his skill for Player B?

  1. [RFC(NET_ID.SkillTrigger)]
  2. public void Net_SkillTriggered(SkillType skill, long timestamp)
  3. {
  4.     DateTime forhumaneyes = new DateTime(timestamp);
  5.     Debug.Log("Skill " + skill.ToString() + " triggered at " + forhumaneyes.ToString());
  6. }
  7.  
  8. public void CastFireball()
  9. {
  10.     tno.Send((byte)NET_ID.SkillTrigger, Target.All, SkillType.Fireball, TNManager.serverTime);
  11. }
  12.  

It's been a while since I've wrote any code but you get the idea ^.^

I think you're approaching it the wrong way though. You shouldn't be designing your game around a 500ms ping. That is incredibly high even for players on opposite sides of the planet, and adding a timestamp to the packet isn't going to do much because you can't travel back in time. Not to mention a timestamp can only be so precise.

My head is loopy right now so maybe Aren can provide some design advice. If not then you could look up deterministic lockstep. I don't think your problem would require such a solution, but the principle could be helpful. Some interesting words thrown together: "playout delay buffer". As summarized by Glenn Fiedler at this page, "[I]n short, what you want to do is buffer packets for a short amount of time so they appear to be arriving at a steady rate even though in reality they arrive somewhat jittered".

Note: Not sure if links to external sites are allowed. My apologies if they are not. I felt the need to cite the author of the quote for moral reasons.

pahe

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 37
    • View Profile
Re: Game action synchronization with DateTime for RFCs viable?
« Reply #4 on: January 27, 2016, 04:22:39 PM »
Thanks for the advice! I'll try it out and see if it will solve my problem. Seems to be a good one though.

The 500 ms lag is definitely too much and was just for describing the problem. I hope to see lags in maybe 10-50ms, but have no good results yet. Currently I have the problem that my clients to out of sync at some point and I need to compensate that.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Game action synchronization with DateTime for RFCs viable?
« Reply #5 on: January 28, 2016, 01:38:45 AM »
There is no cure for half a second lag. The lagging player will see things delayed, but how will he know that they are delayed? He won't. You will only know that they're delayed if you sit next to him, in which case there is no reason for you to have any lag.

In short, simply don't worry about it at all.

Also, serverTime already considers player lag.

The best way to avoid visible lag is to let clients do prediction. Sync input frequently -- every 5-10% change in Input.GetAxis is what I recommend (but no more than 5-8 times per second). Sync pos/rot/velocity infrequently -- once every couple of seconds. In Windward I sync every 3-4 seconds at most. The rest of the time I just let client ships keep moving based on the latest input data. The infrequent sync periodically corrects the simulation.