Example #1
0
    private bool validateDataReceived(DataReceiverAction action, Dictionary <string, object> data)
    {
        if (data == null)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, action.ToString() + " - undefined data");
            return(false);
        }

        object successObject;
        bool   keyExists = data.TryGetValue("success", out successObject);

        if (successObject == null || keyExists == false)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, action.ToString() + " - missing expected 'success' field");
            return(false);
        }

        if (!(successObject is bool))
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, action.ToString() + " - invalid success result data type: " + successObject.GetType().Name);
            return(false);
        }

        bool success = (bool)successObject;

        if (!success)
        {
            object reasonObject;
            keyExists = data.TryGetValue("reason", out reasonObject);

            if (reasonObject == null || keyExists == false)
            {
                reasonObject = "undefined failure reason";
            }

            if (!(reasonObject is string))
            {
                reasonObject = "invalid failure reason data type: " + reasonObject.GetType().Name;
            }

            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, action.ToString() + " - " + reasonObject.ToString());
        }

        return(success);
    }
Example #2
0
	private bool validateDataReceived(DataReceiverAction action, Dictionary<string, object> data)
	{
		if (data == null) {
			FuelSDKCommon.Log (FuelSDKCommon.LogLevel.ERROR, action.ToString () + " - undefined data");
			return false;
		}

		object successObject;
		bool keyExists = data.TryGetValue("success", out successObject);

		if (successObject == null || keyExists == false) {
			FuelSDKCommon.Log (FuelSDKCommon.LogLevel.ERROR, action.ToString () + " - missing expected 'success' field");
			return false;
		}

		if (!(successObject is bool)) {
			FuelSDKCommon.Log (FuelSDKCommon.LogLevel.ERROR, action.ToString () + " - invalid success result data type: " + successObject.GetType ().Name);
			return false;
		}

		bool success = (bool)successObject;

		if (!success) {
			object reasonObject;
			keyExists = data.TryGetValue("reason", out reasonObject);

			if (reasonObject == null || keyExists == false) {
				reasonObject = "undefined failure reason";
			}

			if (!(reasonObject is string)) {
				reasonObject = "invalid failure reason data type: " + reasonObject.GetType ().Name;
			}

			FuelSDKCommon.Log (FuelSDKCommon.LogLevel.ERROR, action.ToString () + " - " + reasonObject.ToString ());
		}

		return success;
	}
Example #3
0
    /// <summary>
    /// Data Receiver
    /// </summary>
    /// <param name="message">data</param>
    private void DataReceiver(string message)
    {
        if (m_listener == null)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "Fuel SDK listener has not been set");
            return;
        }

        if (message == null)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "received undefined message");
            return;
        }

        object messageObject = FuelSDKCommon.Deserialize(message);

        if (messageObject == null)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "message could not be deserialized");
            return;
        }

        Dictionary <string, object> messageDictionary = null;

        try{
            messageDictionary = messageObject as Dictionary <string, object>;

            if (messageDictionary == null)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, " message data type: " + messageObject.GetType().Name);
                return;
            }
        }catch (Exception e) {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, " message data type: " + messageObject.GetType().Name + " error message : " + e.Message);
            return;
        }

        object actionObject;
        bool   keyExists = messageDictionary.TryGetValue("action", out actionObject);

        if (actionObject == null || keyExists == false)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "received undefined action for message: " + message);
            return;
        }

        if (!(actionObject is string))
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid action data type: " + actionObject.GetType().Name);
            return;
        }

        string             action             = (string)actionObject;
        DataReceiverAction dataReceiverAction = DataReceiverAction.none;

        if (!FuelSDKCommon.TryParseEnum <DataReceiverAction> (action, out dataReceiverAction))
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "unsupported action: " + action);
            return;
        }

        object dataObject;

        keyExists = messageDictionary.TryGetValue("data", out dataObject);

        if (dataObject == null || keyExists == false)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "no specific data in the response object for action: " + action);
            return;
        }

        Dictionary <string, object> data = null;

        try{
            data = dataObject as Dictionary <string, object>;

            if (data == null)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid data data type" + dataObject.GetType().Name);
                return;
            }
        }catch (Exception e) {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid data data type" + dataObject.GetType().Name + " error message : " + e.Message);
            return;
        }

        string dataString = FuelSDKCommon.Serialize(data);

        if (dataString == null)
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "data could not be serialized");
            return;
        }

        FuelSDKCommon.Log(FuelSDKCommon.LogLevel.DEBUG, "received '" + action + "': " + dataString);

        switch (dataReceiverAction)
        {
        case DataReceiverAction.none:
        {
            // noop
            break;
        }

        case DataReceiverAction.fuelSDKVirtualGoodList:
        {
            object transactionIDObject;
            keyExists = data.TryGetValue("transactionID", out transactionIDObject);

            if (transactionIDObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected transaction ID");
                break;
            }

            if (!(transactionIDObject is string))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid transaction ID data type: " + transactionIDObject.GetType().Name);
                break;
            }

            string transactionID = (string)transactionIDObject;


            object virtualGoodsObject;
            keyExists = data.TryGetValue("virtualGoods", out virtualGoodsObject);

            if (virtualGoodsObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected virtual goods list");
                break;
            }

            List <object> virtualGoods = null;

            try{
                virtualGoods = virtualGoodsObject as List <object>;

                if (virtualGoods == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid virtual goods list data type: " + virtualGoodsObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid virtual goods list data type: " + virtualGoodsObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnVirtualGoodList(transactionID, virtualGoods);
            break;
        }

        case DataReceiverAction.fuelSDKVirtualGoodRollback:
        {
            object transactionIDObject;
            keyExists = data.TryGetValue("transactionID", out transactionIDObject);

            if (transactionIDObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected transaction ID");
                break;
            }

            if (!(transactionIDObject is string))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid transaction ID data type: " + transactionIDObject.GetType().Name);
                break;
            }

            m_listener.OnVirtualGoodRollback((string)transactionIDObject);
            break;
        }

        case DataReceiverAction.fuelSDKNotificationEnabled:
        {
            object notificationTypeObject;
            keyExists = data.TryGetValue("notificationType", out notificationTypeObject);

            if (notificationTypeObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected notification type");
                break;
            }

            if (!(notificationTypeObject is long))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid notification type data type: " + notificationTypeObject.GetType().Name);
                break;
            }

            int notificationTypeValue = (int)((long)notificationTypeObject);

            if (!Enum.IsDefined(typeof(NotificationType), notificationTypeValue))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "unsuppported notification type value: " + notificationTypeValue.ToString());
                break;
            }

            m_listener.OnNotificationEnabled((NotificationType)notificationTypeValue);
            break;
        }

        case DataReceiverAction.fuelSDKNotificationDisabled:
        {
            object notificationTypeObject;
            keyExists = data.TryGetValue("notificationType", out notificationTypeObject);

            if (notificationTypeObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected notification type");
                break;
            }

            if (!(notificationTypeObject is long))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid notification type data type: " + notificationTypeObject.GetType().Name);
                break;
            }

            int notificationTypeValue = (int)((long)notificationTypeObject);

            if (!Enum.IsDefined(typeof(NotificationType), notificationTypeValue))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "unsuppported notification type value: " + notificationTypeValue.ToString());
                break;
            }

            m_listener.OnNotificationDisabled((NotificationType)notificationTypeValue);
            break;
        }

        case DataReceiverAction.fuelSDKSocialLoginRequest:
        {
            object allowCacheObject;
            keyExists = data.TryGetValue("allowCache", out allowCacheObject);

            if (allowCacheObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected allow cache flag");
                break;
            }

            if (!(allowCacheObject is bool))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid allow cache data type: " + allowCacheObject.GetType().Name);
                break;
            }

            bool allowCache = (bool)allowCacheObject;

            m_listener.OnSocialLogin(allowCache);
            break;
        }

        case DataReceiverAction.fuelSDKSocialInviteRequest:
        {
            m_listener.OnSocialInvite(FuelSDKCommon.ToStringDictionary <string, object> (data));
            break;
        }

        case DataReceiverAction.fuelSDKSocialShareRequest:
        {
            m_listener.OnSocialShare(FuelSDKCommon.ToStringDictionary <string, object> (data));
            break;
        }

        case DataReceiverAction.fuelSDKImplicitLaunchRequest:
        {
            object applicationStateObject;
            keyExists = data.TryGetValue("applicationState", out applicationStateObject);

            if (applicationStateObject == null)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected application state");
                break;
            }

            if (!(applicationStateObject is string))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid application state data type: " + applicationStateObject.GetType().Name);
                break;
            }

            string applicationStateString = (string)applicationStateObject;

            ApplicationState applicationState = ApplicationState.none;

            if (!FuelSDKCommon.TryParseEnum <ApplicationState> (applicationStateString, out applicationState))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "unsupported application state: " + applicationStateString);
                return;
            }

            m_listener.OnImplicitLaunch(applicationState);
            break;
        }

        case DataReceiverAction.fuelSDKUserValues:
        {
            object conditionsObject;
            keyExists = data.TryGetValue("dynamicConditions", out conditionsObject);

            if (conditionsObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected dynamic conditions");
                break;
            }

            Dictionary <string, object> conditions = null;

            try{
                conditions = conditionsObject as Dictionary <string, object>;

                if (conditions == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid conditions data type: " + conditionsObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid conditions data type: " + conditionsObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            object variablesObject;
            keyExists = data.TryGetValue("variables", out variablesObject);

            if (variablesObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected dynamic variables");
                break;
            }

            Dictionary <string, object> variables = null;

            try{
                variables = variablesObject as Dictionary <string, object>;

                if (variables == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid variables data type: " + variablesObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid variables data type: " + variablesObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnUserValues(FuelSDKCommon.ToStringDictionary <string, object> (conditions), FuelSDKCommon.ToStringDictionary <string, object> (variables));
            break;
        }

        case DataReceiverAction.fuelSDKCompeteChallengeCount:
        {
            object countObject;
            keyExists = data.TryGetValue("count", out countObject);

            if (countObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected challenge count");
                break;
            }

            if (!(countObject is long))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid challenge count data type: " + countObject.GetType().Name);
                break;
            }

            int count = (int)((long)countObject);

            m_listener.OnCompeteChallengeCount(count);
            break;
        }

        case DataReceiverAction.fuelSDKCompeteTournamentInfo:
        {
            m_listener.OnCompeteTournamentInfo(FuelSDKCommon.ToStringDictionary <string, object> (data));
            break;
        }

        case DataReceiverAction.fuelSDKCompeteUICompletedExit:
        {
            m_listener.OnCompeteUICompletedWithExit();
            break;
        }

        case DataReceiverAction.fuelSDKCompeteUICompletedMatch:
        {
            m_listener.OnCompeteUICompletedWithMatch(data);
            break;
        }

        case DataReceiverAction.fuelSDKCompeteUICompletedFail:
        {
            object reasonObject;
            keyExists = data.TryGetValue("message", out reasonObject);

            if (reasonObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected failure reason");
                break;
            }

            if (!(reasonObject is string))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid failure reason data type: " + reasonObject.GetType().Name);
                break;
            }

            string reason = (string)reasonObject;

            m_listener.OnCompeteUIFailed(reason);
            break;
        }

        case DataReceiverAction.fuelSDKIgniteEvents:
        {
            if (!validateDataReceived(dataReceiverAction, data))
            {
                break;
            }

            object eventsObject;
            keyExists = data.TryGetValue("events", out eventsObject);

            if (eventsObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected event list");
                break;
            }

            List <object> events = null;

            try{
                events = eventsObject as List <object>;

                if (events == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid event list data type: " + eventsObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid event list data type: " + eventsObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnIgniteEvents(events);
            break;
        }

        case DataReceiverAction.fuelSDKIgniteLeaderBoard:
        {
            if (!validateDataReceived(dataReceiverAction, data))
            {
                break;
            }

            object leaderBoardObject;
            keyExists = data.TryGetValue("leaderBoard", out leaderBoardObject);

            if (leaderBoardObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected leaderboard data");
                break;
            }

            Dictionary <string, object> leaderBoard = null;

            try{
                leaderBoard = leaderBoardObject as Dictionary <string, object>;

                if (leaderBoard == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid leaderboard data type: " + leaderBoardObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid leaderboard data type: " + leaderBoardObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnIgniteLeaderBoard(leaderBoard);
            break;
        }

        case DataReceiverAction.fuelSDKIgniteMission:
        {
            if (!validateDataReceived(dataReceiverAction, data))
            {
                break;
            }

            object missionObject;
            keyExists = data.TryGetValue("mission", out missionObject);

            if (missionObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected mission data");
                break;
            }

            Dictionary <string, object> mission = null;

            try{
                mission = missionObject as Dictionary <string, object>;

                if (mission == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid mission data type: " + missionObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid mission data type: " + missionObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnIgniteMission(mission);
            break;
        }

        case DataReceiverAction.fuelSDKIgniteQuest:
        {
            if (!validateDataReceived(dataReceiverAction, data))
            {
                break;
            }

            object questObject;
            keyExists = data.TryGetValue("quest", out questObject);

            if (questObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected quest data");
                break;
            }

            Dictionary <string, object> quest = null;

            try{
                quest = questObject as Dictionary <string, object>;

                if (quest == null)
                {
                    FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid quest data type: " + questObject.GetType().Name);
                    break;
                }
            }catch (Exception e) {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid quest data type: " + questObject.GetType().Name + " error message : " + e.Message);
                break;
            }

            m_listener.OnIgniteQuest(quest);
            break;
        }

        case DataReceiverAction.fuelSDKIgniteJoinEvent:
        {
            if (!validateDataReceived(dataReceiverAction, data))
            {
                break;
            }

            object eventIDObject;
            keyExists = data.TryGetValue("eventID", out eventIDObject);

            if (eventIDObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected event ID");
                break;
            }

            if (!(eventIDObject is string))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid event ID data type: " + eventIDObject.GetType().Name);
                break;
            }

            string eventID = (string)eventIDObject;

            object joinStatusObject;
            keyExists = data.TryGetValue("joinStatus", out joinStatusObject);

            if (joinStatusObject == null || keyExists == false)
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "missing expected join status");
                break;
            }

            if (!(joinStatusObject is bool))
            {
                FuelSDKCommon.Log(FuelSDKCommon.LogLevel.ERROR, "invalid join status data type: " + joinStatusObject.GetType().Name);
                break;
            }

            bool joinStatus = (bool)joinStatusObject;

            m_listener.OnIgniteJoinEvent(eventID, joinStatus);
            break;
        }

        default:
        {
            FuelSDKCommon.Log(FuelSDKCommon.LogLevel.WARN, "unsupported action: " + action);
            break;
        }
        }
    }