question

Talha avatar image
Talha asked

Unity Internet Time issue, Php file hosting on server

Hi, this is my first post.

I am creating a unity game.

I would like to know how can i implement the internet time solution to the cheating problem.

I have a timer in my game. 15 lives after 4 hours. How can i implement it in the game. I know the code, how to get the time and parse it and show it BUT Hosting? what's up with that?

What url should i pass in my code? is it possible to use PlayFab's Servers to get the current time of the player? Do I have to upload a php file to Player'sData in PlayFab?

How does that work? I am new, kindly help me out, Mr.Google is saying I need a website and have to host the php file there? is there another alternative?

What will I pass in string _url in the scenario where PlayFab servers (GetTime) function is used?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class TimeManager : MonoBehaviour {
	/* 
	  	necessary variables to hold all the things we need.
		php url
		timedata, the data we get back
		current time
		current date
	*/ 


	public static TimeManager sharedInstance = null;
	private string _url = "http://localhost/Wireal/Time.php";
	private string _timeData;
	private string _currentTime;
	private string _currentDate;




	//make sure there is only one instance of this always.
	void Awake() {
		if (sharedInstance == null) {
			sharedInstance = this;
		} else if (sharedInstance != this) {
			Destroy (gameObject);  
		}
		DontDestroyOnLoad(gameObject);
	}




	//time fether coroutine
	public IEnumerator getTime()
	{
		Debug.Log ("==> step 1. Getting info from internet now!");
		WWW www = new WWW (_url);
		yield return www;
		Debug.Log ("==> step 2. Got the info from internet!");
		_timeData = www.text;
		string[] words = _timeData.Split('/');	
		//timerTestLabel.text = www.text;
		Debug.Log ("The date is : "+words[0]);
		Debug.Log ("The time is : "+words[1]);


		//setting current time
		_currentDate = words[0];
		_currentTime = words[1];
	}




	//get the current time at startup
	void Start()
	{
		Debug.Log ("==> TimeManager script is Ready.");
		StartCoroutine ("getTime");
	}


	//get the current date
	public string getCurrentDateNow()
	{
		return _currentDate;
	}




	//get the current Time
	public string getCurrentTimeNow()
	{
		return _currentTime;
	}




}

Custom Game Servers
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Hernando avatar image
Hernando answered

I don't know the meaning of "15 lives after 4 hours", but I guess you don't want players to end the game ahead of time by modifying the local device time. In fact, its solution is similar to that provided on Google, but PlayFab provides CloudScript, that offers a fast, secure and scalable alternative to dedicated servers(more information please see: https://api.playfab.com/docs/tutorials/landing-automation/using-cloud-script & https://api.playfab.com/docs/tutorials/landing-automation/writing-custom-cloud-script). So, you don't need to build your own PHP server, but use CloudScript to record the server time in your title.

If you want to avoid player cheats by synchronizing the client and server time, you can get the time in CloudScript by calling the API Server.GetTime at the beginning of the game and record the result as the start time in PlayerReadOnlyData.

After the client finishes timing, the request for the end of the game sent to the server. At this time, CloudSciprt calls the API Server.GetTime again and calculates with the start time. If the time is reached, you can end the game. If not, the player has the possibility of cheating.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Talha avatar image Talha commented ·

@Hernando Thankyou for your reply mate.

"15 lives after 4 hours" means that the player gets 15 lives after every 4 hours,(like in candy crush where (oops You've ran out of lives) player has to wait 4 hours or so and after that he can play again, if he does not want to wait then he must buy lives from Store using real currency).

You have put a lot of my weight back in by saying i dont need to build a php server. Could you please elaborate a little more how we may achieve that? because it is continuous. After every 4 hours Player gets 15 lives(hearts).

What happens if the Player is not connected to the internet?

0 Likes 0 ·
brendan avatar image brendan Talha commented ·

The simplest thing to do would be to create that as a Virtual Currency in PlayFab. Just set it to a recharge rate of 90 (the rate is per 24 hours), with a max recharge of 15. That way, the VC will never recharge to more than 15, and it'll recharge at a rate of 15 per 4 hours.

But if the player isn't connected to the internet, I'm afraid no server, PlayFab, php, or otherwise, is going to help you to have an authoritative way to state how many lives the player currently has. If you want to allow offline play, you'll have to let the client decide how many lives the player has based upon the time, and report anything the player did once they're back online (and use some server side code to try to catch them when they cheat by advancing the clock on their local device).

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.