0% found this document useful (0 votes)
167 views

Userscript

This document contains code for a User class that manages user authentication and social integration. The class handles logging into Facebook, posting to Facebook, crediting/debiting coupons, getting owned coupons, loading data, logging into social platforms, signing out, showing leaderboards, and handling scores. Key functions include LogInFacebook, PostToFacebookFeed, GetOwnedCoupons, Load, AttemptLogin, SignOut, ShowLeaderboardUI, and HandleScore.

Uploaded by

api-289675854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

Userscript

This document contains code for a User class that manages user authentication and social integration. The class handles logging into Facebook, posting to Facebook, crediting/debiting coupons, getting owned coupons, loading data, logging into social platforms, signing out, showing leaderboards, and handling scores. Key functions include LogInFacebook, PostToFacebookFeed, GetOwnedCoupons, Load, AttemptLogin, SignOut, ShowLeaderboardUI, and HandleScore.

Uploaded by

api-289675854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

using UnityEngine;

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using UnityEngine.SocialPlatforms.GameCenter;

public class User : MonoSingleton<User>


{
public static bool signedIn = false;
private static bool _loggedInFacebook = false;
public static bool LoggedInFacebook
{
get { return _loggedInFacebook; }
private set { _loggedInFacebook = value; }
}

public static CacheData cache;

// Log in to facebook
public static void LogInFacebook(Facebook.FacebookDelegate callback)
{
if(LoggedInFacebook)
return;

Facebook.FacebookDelegate login = delegate(FBResult result)


{
if(string.IsNullOrEmpty(result.Error))
LoggedInFacebook = true;
else
Debug.LogError(result.Error);
};
FB.Login ("public_profile,publish_actions", callback + login);
}

// Posts to the user's feed


public static void PostToFacebookFeed(string link, string linkName, string
header, string body, string pictureURL, Facebook.FacebookDelegate callback=null)
{
if(!LoggedInFacebook)
{
Facebook.FacebookDelegate login = delegate(FBResult result)
{
if(string.IsNullOrEmpty(result.Error))
SafePostToFacebookFeed(link, linkName, header, body,
pictureURL, callback);
};
LogInFacebook(login);
}
else
{

SafePostToFacebookFeed(link, linkName, header, body,


pictureURL, callback);
}
}

// Post to facebook (won't check if user is actually logged in)


private static void SafePostToFacebookFeed(string link, string linkName, string
header, string body, string pictureURL, Facebook.FacebookDelegate callback)
{
// [

linkName (opens link when clicked)

// [ pic ]

header

// [ url ]
// [

body

FB.Feed(
link: link,
linkName: linkName,
linkCaption: header,
linkDescription: body,
picture: pictureURL,
callback: callback
);
}

public static IEnumerator Credit(int couponID, System.Action callback = null)


{
WWWForm form = new WWWForm();
form.AddField("database", AppSettings.Instance.databaseID);

form.AddField("id", SystemInfo.deviceUniqueIdentifier);
form.AddField("couponID", couponID);
form.AddField("status", 1);
WWW request = new
WWW(AppSettings.Instance.userDataSubmitForm, form);
yield return request;
if(request.error != null)
{
Debug.LogError(request.error);
OfflineRequests.Add (couponID, 1);
}
if(cache != null)
{
CouponData coupon = cache.GetCoupon(couponID);
if(coupon != null)
{
coupon.owned = true;
cache.SaveLocal ();
}
}
if(callback != null)
callback();
}

public static IEnumerator Debit(int couponID, System.Action callback = null)


{
WWWForm form = new WWWForm();

form.AddField("database", AppSettings.Instance.databaseID);
form.AddField("id", SystemInfo.deviceUniqueIdentifier);
form.AddField("couponID", couponID);
form.AddField("status", 0);
WWW request = new
WWW(AppSettings.Instance.userDataSubmitForm, form);
yield return request;
if(request.error != null)
{
Debug.LogError(request.error);
OfflineRequests.Add (couponID, 0);
}
if(cache != null)
{
CouponData coupon = cache.GetCoupon(couponID);
if(coupon != null)
{
coupon.owned = false;
cache.SaveLocal ();
}
}
if(callback != null)
callback();
}

public static CouponData[] GetOwnedCoupons()


{

List<CouponData> owned = new List<CouponData>();


foreach(CouponData c in cache.coupons)
{
if(c.owned)
owned.Add(c);
}
return owned.ToArray();
}

public static IEnumerator Load()


{
#if UNITY_ANDROID && !UNITY_EDITOR
// Google Play Games
//

PlayGamesPlatform.DebugLogEnabled = true;

PlayGamesPlatform.Activate();
#elif UNITY_IOS
#endif
yield return null;
}

public static void AttemptLogin(System.Action<bool> whenComplete=null)


{
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
Social.localUser.Authenticate((bool success) => {
Debug.Log ("Login completed! Success=" + success);
signedIn = success;

if(whenComplete != null)
whenComplete(success);
});
#endif
}

private static void SafeCall(System.Action<bool> callback)


{
if(signedIn)
callback(true);
else
AttemptLogin(callback);
}

public static void SignOut()


{
#if UNITY_ANDROID && !UNITY_EDITOR
if(signedIn)
((PlayGamesPlatform) Social.Active).SignOut();
#endif
signedIn = false;
}

public static void ShowLeaderboardUI()


{
#if UNITY_ANDROID && !UNITY_EDITOR

if(signedIn)

((PlayGamesPlatform)Social.Active).ShowLeaderboardUI("CgkIzKfp9LMREAIQBg");
#endif
}

public static void HandleScore(int score)


{
System.Action<bool> submit = delegate(bool obj)
{
if(obj)
{
#if UNITY_ANDROID && !UNITY_EDITOR
Social.ReportScore(score, "CgkIzKfp9LMREAIQBg",null);
#elif UNITY_IOS && !UNITY_EDITOR
Social.ReportScore(score, "replace me with correct
leaderboard id for iOS Game Center", null);
#endif
}
};
SafeCall(submit);
}
}

You might also like