/// <summary> /// Creates a new session for your app user /// </summary> /// <param name="callback"></param> public void startSession(Action <SessionResult> callback = null) { session = new session(); components.App.startSession component = new components.App.startSession(); component.callWith(_core, (results.App.startSession r) => { SessionResult session_result = (SessionResult)r; if (session_result.success) { session = session_result.session; } else { if (session_result.error == null) { session_result.error = new error { message = "Could not connect to Newgrounds.io server" } } ; } if (callback != null) { callback(session_result); } } ); }
private void _loginSuccess(SessionResult r) { dispatchEvent <SessionResult>(loginEvent.LOGIN_SUCCESS, r); if (_onloginsuccess != null) { _onloginsuccess(); } }
/// <summary> /// Use this to cancel any pending login events that would be fired after calling requestLogin(). /// </summary> public void cancelLoginRequest() { _waiting_for_login = false; SessionResult sr = new SessionResult(); sr.session = getSessionLoader().session; dispatchEvent <SessionResult>(loginEvent.LOGIN_CANCELLED, sr); if (_onlogincancelled != null) { _onlogincancelled(); } }
/// <summary> /// Calls all handler functions in the event table attached to the given eventType. /// </summary> /// <param name="eventType">The eventType to trigger</param> /// <param name="data">Any object to pass to the handler function(s).</param> public void dispatchEvent(string eventType, SessionResult data) { Delegate d; if (eventTable.TryGetValue(eventType, out d)) { onCallResult <SessionResult> cb = (onCallResult <SessionResult>)d; if (cb != null) { cb(data); } } }
/// <summary> /// Ends current user session. /// </summary> public void logOut() { // tell the server it can kill the session getSessionLoader().endSession(); // we're manually making a result here so we can proceed independent of the server. SessionResult sr = new SessionResult(); sr.session = getSessionLoader().session; sr.success = true; dispatchEvent <SessionResult>(loginEvent.LOGGED_OUT, sr); if (_onlogout != null) { _onlogout(); } }
/// <summary> /// Clears the current active session (if any). /// </summary> /// <param name="callback"></param> public void endSession(Action <SessionResult> callback = null) { if (!string.IsNullOrEmpty(session.id)) { _core.callComponent("App.endSession"); } session = new session(); SessionResult sr = new SessionResult(); sr.session = session; if (callback != null) { callback(sr); } }
private void _loginFailed(SessionResult r) { // code 111 means the user clicked the "No Thanks" if (waiting_for_login && !r.success && r.error.code == 111) { _login_error = new objects.error(); _login_error.code = 111; _login_error.message = "Session was cancelled by the user"; cancelLoginRequest(); } else { _login_error = r.error; dispatchEvent <SessionResult>(loginEvent.LOGIN_FAILED, r); if (_onloginfailed != null) { _onloginfailed(); } } }
/// <summary> /// If a non-expired session has been loaded, this will open Newgrounds Passport in a browser window so the player may log in securely. /// At the time this class as written, Unity lacked the ability to open URLs in new tabs when built for WebGL /// When this is called from WebGL, a prompt will be drawn over your game asking the player if they want to sign in. /// This is necessary to load passport in conjunction with a mouse click so popup blockers can be bypassed. /// </summary> /// <returns></returns> public bool loadPassport() { SessionResult sr = new SessionResult(); sr.session = session; if (sr.getStatus() != SessionResult.REQUEST_LOGIN) { return(false); } if (ngioPluginLoaded()) { newgroundsioOpenPassportPrompt(session.passport_url); return(true); } else { Application.OpenURL(session.passport_url); return(true); } }
/// <summary> /// Will check the current status of a session. /// </summary> /// <param name="callback"></param> public void checkSession(Action <SessionResult> callback = null) { SessionResult sr = new SessionResult(); sr.session = session; Action <ResultModel> resultHandler = (ResultModel r) => { sr = (SessionResult)r; if (!r.success) { if (sr.error == null) { sr.error = new error { message = "Could not connect to Newgrounds.io server" } } ; if (sr.session == null) { sr.session = new session(); } } session = sr.session; if (callback != null) { callback(sr); } }; bool cooled = false; /* this makes sure we aren't spamming the newgrounds.io server (wich would get us blocked) */ if (first_check || stopwatch.ElapsedMilliseconds > 3000) { stopwatch.Reset(); stopwatch.Start(); cooled = true; first_check = false; } /* if we have an active session and our stopwatch has cooled down, reload the session from the server */ if (cooled && sr.getStatus() != SessionResult.SESSION_EXPIRED) { if (ngioPluginLoaded() && newgroundsioUserCancelledPrompt()) { SessionResult csr = new SessionResult(); csr.setIsLocal(true); session ns = new session(); ns.expired = true; ns.id = session.id; csr.session = ns; csr.success = false; csr.error.message = "User cancelled login"; if (callback != null) { callback(csr); } session = new session(); } else { _core.callComponent( "App.checkSession", null, resultHandler ); } } /* otherwise, just use the currently loaded session */ else { if (callback != null) { callback(sr); } } }