//////////////////////////////////////////
//  Helpers to sync UIs.
//////////////////////////////////////////
//	A simple library that sends an integer
//	to all clients. Clients are supposed
//	to copy this value in another variable
//	that will be read and compared to the
//	first one. If these two variables are
//	equal, the client is responding.
//	Otherwise, the client is probably
//	freezing or crashing.
//////////////////////////////////////////
#Const Version			"2013-10-28"
#Const ScriptName		"UISync.Script.txt"

// ---------------------------------- //
/** Return the version number of the script
 *
 *	@return		The version number of the script
 */
Text GetScriptVersion() {
	return Version;
}

// ---------------------------------- //
/** Return the name of the script
 *
 *	@return		The name of the script
 */
Text GetScriptName() {
	return ScriptName;
}

// ---------------------------------- //
/**
 * Checks if a given player's UI is synchronized.
 *
 * @param _Player	The player to check.
 * @return True if the UI is synced, False otherwise.
 */
Boolean IsUISync(CPlayer _Player) {
	declare UI <=> UIManager.GetUI(_Player);
	if (UI == Null) return True;

	declare netwrite	Integer Net_ServerSync for Teams[0];	
	declare netread		Integer Net_UiSync for UI;

	return Net_UiSync == Net_ServerSync;
}

// ---------------------------------- //
/**
 * Synchronize all UIs.
 */
Void SyncUIs() {
	declare netwrite Integer Net_ServerSync for Teams[0];
	Net_ServerSync = Now;
}

// ---------------------------------- //
/**
 * Generates the ManiaScript.
 *
 * @return The ManiaScript text.
 */
Text ML_SyncUI() {
return """
	/*	----------	UISync.Script.txt ----------	*/

	declare netread		Integer Net_ServerSync for Teams[0];
	declare netwrite	Integer Net_UiSync for UI;
	if(Net_UiSync != Net_ServerSync) {
		Net_UiSync = Net_ServerSync;
	}

	/*	----------------------------------------	*/
""";
}