Android Open Source - android-levelup World Storage






From Project

Back to project page android-levelup.

License

The source code is released under:

MIT License

If you think the Android project android-levelup listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012-2014 Soomla Inc.//  ww w  .  j a v a2  s . c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.soomla.levelup.data;

import android.text.TextUtils;

import com.soomla.BusProvider;
import com.soomla.Soomla;
import com.soomla.SoomlaUtils;
import com.soomla.data.KeyValueStorage;
import com.soomla.levelup.LevelUp;
import com.soomla.levelup.events.LevelUpInitializedEvent;
import com.soomla.levelup.events.WorldAssignedRewardEvent;
import com.soomla.levelup.events.WorldCompletedEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class WorldStorage {

    private static String keyWorlds(String worldId, String postfix) {
        return DB_WORLD_KEY_PREFIX + worldId + "." + postfix;
    }

    private static String keyWorldCompleted(String worldId) {
        return keyWorlds(worldId, "completed");
    }

    private static String keyReward(String worldId) {
        return keyWorlds(worldId, "assignedReward");
    }

    public static void initLevelUp() {
        BusProvider.getInstance().post(new LevelUpInitializedEvent());
    }

    public static void setCompleted(String worldId, boolean completed) {
        setCompleted(worldId, completed, true);
    }

    public static void setCompleted(String worldId, boolean completed, boolean notify) {

        String key = keyWorldCompleted(worldId);

        if (completed) {
            KeyValueStorage.setValue(key, "yes");

            if (notify) {
                BusProvider.getInstance().post(new WorldCompletedEvent(worldId));
            }
        } else {
            KeyValueStorage.deleteKeyValue(key);
        }
    }

    public static boolean isCompleted(String worldId) {
        String key = keyWorldCompleted(worldId);
        String val = KeyValueStorage.getValue(key);
        return !TextUtils.isEmpty(val);
    }


    /**
     * World Reward *
     */

    public static void setReward(String worldId, String rewardId) {
        setReward(worldId, rewardId, true);
    }

    public static void setReward(String worldId, String rewardId, boolean notify) {

        String key = keyReward(worldId);
        if (!TextUtils.isEmpty(rewardId)) {
            KeyValueStorage.setValue(key, rewardId);
        } else {
            KeyValueStorage.deleteKeyValue(key);
        }

        if (notify) {
            // Notify world was assigned a reward
            BusProvider.getInstance().post(new WorldAssignedRewardEvent(worldId));
        }
    }

    public static String getAssignedReward(String worldId) {
        String key = keyReward(worldId);
        return KeyValueStorage.getValue(key);
    }

    public static boolean isLevel(String worldId) {
        JSONObject model = LevelUp.getLevelUpModel();
        if (model != null) {
            HashMap<String, JSONObject> worlds = LevelUp.getWorlds(model);
            JSONObject world = worlds.get(worldId);
            if (world != null) {
                try {
                    if (world.getString("itemId").equals(worldId)) {
                        return (world.getString("className").equals("Level"));
                    }
                }
                catch (JSONException ex) {
                    SoomlaUtils.LogDebug(TAG, "Model JSON is malformed " + ex.getMessage());
                }
            }
        }

        return false;
    }

    public static final String DB_WORLD_KEY_PREFIX = LevelUp.DB_KEY_PREFIX + "worlds.";
    private static final String TAG = "SOOMLA WorldStorage";
}




Java Source Code List

com.soomla.levelup.LevelUp.java
com.soomla.levelup.data.GateStorage.java
com.soomla.levelup.data.LUJSONConsts.java
com.soomla.levelup.data.LevelStorage.java
com.soomla.levelup.data.MissionStorage.java
com.soomla.levelup.data.ScoreStorage.java
com.soomla.levelup.data.WorldStorage.java
com.soomla.levelup.events.GateClosedEvent.java
com.soomla.levelup.events.GateOpenedEvent.java
com.soomla.levelup.events.LatestScoreChangedEvent.java
com.soomla.levelup.events.LevelEndedEvent.java
com.soomla.levelup.events.LevelStartedEvent.java
com.soomla.levelup.events.LevelUpInitializedEvent.java
com.soomla.levelup.events.MissionCompletedEvent.java
com.soomla.levelup.events.MissionCompletionRevokedEvent.java
com.soomla.levelup.events.ScoreRecordChangedEvent.java
com.soomla.levelup.events.ScoreRecordReachedEvent.java
com.soomla.levelup.events.WorldAssignedRewardEvent.java
com.soomla.levelup.events.WorldCompletedEvent.java