Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.aw.app.action.ApplicationSettings.java

/**
 * /*from  w w w .java2s.  c o  m*/
 * @param configuration
 * @return 
 */
public static long convertToTimeStamp(DBObject configuration) {
    long d = (Integer) configuration.get("build_time_d");
    long h = (Integer) configuration.get("build_time_h");
    long m = (Integer) configuration.get("build_time_m");
    return (d * 86400) + (h * 3600) + (m * 60);
}

From source file:com.aw.app.action.BattleAction.java

/**
 * //from  w  w  w.j  a va2  s. c o m
 * @param data
 * @param battle
 * @throws JSONException 
 */
public void processBattleTroops(JSONObject data, Battle battle) throws JSONException {
    if (data.has("troops") && data.getJSONArray("troops").length() > 0) {
        JSONArray array = data.getJSONArray("troops");
        JSONObject troopObj = null;
        for (int i = 0; i < array.length(); i++) {
            troopObj = array.getJSONObject(i);
            BasicDBObject troop = new BasicDBObject();
            troop.put("battle_id", battle.get("battle_id"));
            troop.put("is_created", new Date().getTime());
            troop.put("tid", troopObj.getString("tid"));
            troop.put("level", troopObj.get("level"));
            troop.put("count", troopObj.getLong("count"));
            WriteResult result = MongoDbUtil.saveCollection("aw_battle_troops", troop);
            if (result.getLastError().ok()) {
                long count = troopObj.getLong("count");
                for (int j = 1; j <= count; j++) {
                    DBObject troopConfig = MongoDbUtil
                            .getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration")
                            .findOne(new BasicDBObject().append("tid", troopObj.getString("tid"))
                                    .append("level", troopObj.get("level")));
                    if (troopConfig != null) {
                        DBObject userTroops = MongoDbUtil
                                .getCollection(MongoDbUtil.defaultDBName, "aw_user_troops")
                                .findOne(new BasicDBObject().append("troop_id", troopConfig.get("troop_id"))
                                        .append("uid", data.get("attacker_id")));
                        if (userTroops != null) {
                            // delete userTroops
                            MongoDbUtil.removeCollection("aw_user_troops", userTroops);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.aw.app.action.BattleAction.java

/**
 * /*w w w .  j  ava2 s.c o m*/
 * @param battle 
 */
public void updateBattleTrophy(Battle battle) {
    Map battleMap = battle.toMap();
    if (battleMap.containsKey("attacker_id") && battle.getLong("attacker_id") > 0) {
        DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user")
                .findOne(new BasicDBObject("uid", battle.get("attacker_id")));
        if (user != null && battle.getLong("attacker_trophies") > 0) {
            if (battle.getLong("attacker_won") == 1) {
                long userTrophies = (Long) user.get("trophies");
                userTrophies += battle.getLong("attacker_trophies");
                user.put("trophies", userTrophies);
            } else {
                long userTrophies = (Long) user.get("trophies");
                userTrophies -= battle.getLong("attacker_trophies");
                if (userTrophies < 0)
                    userTrophies = 0;
                user.put("trophies", userTrophies);
            }
            // Update User here
            BasicDBObject updateUser = new BasicDBObject("$set",
                    new BasicDBObject("trophies", user.get("trophies")));
            BasicDBObject query = new BasicDBObject("uid", battle.get("attacker_id"));
            MongoDbUtil.updateCollection("aw_user", query, updateUser);
        }
    }
    if (battleMap.containsKey("defender_id") && battle.getDefenderId() > 0) {
        DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user")
                .findOne(new BasicDBObject("uid", battle.get("defender_id")));
        if (user != null && battle.getLong("defender_trophies") > 0) {
            if (battle.getAttackerWon() == 1) {
                long userTrophies = (Long) user.get("trophies");
                userTrophies += battle.getLong("defender_trophies");
                user.put("trophies", userTrophies);
            } else {
                long userTrophies = (Long) user.get("trophies");
                userTrophies -= battle.getLong("defender_trophies");
                if (userTrophies < 0)
                    userTrophies = 0;
                user.put("trophies", userTrophies);
            }
            // Update User here
            BasicDBObject updateUser = new BasicDBObject("$set",
                    new BasicDBObject("trophies", user.get("trophies")));
            BasicDBObject query = new BasicDBObject("uid", battle.get("defender_id"));
            MongoDbUtil.updateCollection("aw_user", query, updateUser);
        }
    }
}

From source file:com.aw.app.action.BattleAction.java

/**
 * //  w  ww  .  j  av a2  s  . co  m
 * @param battleId
 * @return 
 */
public Map getBattleDetail(long battleId) {
    Map data = new HashMap();
    DBObject battle = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_battle")
            .findOne(new BasicDBObject().append("battle_id", battleId));
    if (battle != null) {
        if (battle.containsField("battleBuildings")) {
            List<BattleBuilding> battleBuildings = (List<BattleBuilding>) battle.get("battleBuildings");
            List battles = new ArrayList();
            for (BattleBuilding bb : battleBuildings) {
                Map bbData = new HashMap();
                bbData.put("building_id", bb.get("building_id"));
                bbData.put("is_destroyed", bb.get("is_destroyed"));
                bbData.put("resource_lost", bb.get("resource_lost"));
                battles.add(bbData);
            }
            data.put("buildings", battles);
        }
        if (battle.containsField("battleTroops")) {
            List<BattleTroops> battleTroops = (List<BattleTroops>) battle.get("battleTroops");
            List troops = new ArrayList();
            for (BattleTroops bt : battleTroops) {
                Map tData = new HashMap();
                tData.put("tid", bt.get("tid"));
                tData.put("count", bt.get("count"));
                tData.put("level", bt.get("level"));
                troops.add(tData);
            }
            data.put("troops", troops);
        }
    }
    return data;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /*from w  ww  .j  a v a 2  s . c o  m*/
 * @param buildings
 * @return 
 */
public List getBuildInfoForOwner(List buildings) {
    List buildingData = new ArrayList();
    for (Object building : buildings) {
        Map data = new HashMap();
        DBObject buildingObject = (DBObject) building;
        boolean checkStatus = checkBuildingUpgrade(buildingObject.get("uid"), buildingObject.get("building_id"),
                new Date().getTime());
        if (checkStatus) {
            DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
            buildingObject = table.findOne(new BasicDBObject("building_id", buildingObject.get("building_id")));
        }
        data.put("building_id", buildingObject.get("building_id"));
        data.put("tid", buildingObject.get("tid"));
        data.put("position_x", buildingObject.get("position_x"));
        data.put("position_y", buildingObject.get("position_y"));
        data.put("level", buildingObject.get("level"));
        data.put("is_constructing", buildingObject.get("is_constructing"));
        data.put("construction_start_time", buildingObject.get("construction_start_time"));
        data.put("is_upgrading", buildingObject.get("is_upgrading"));
        data.put("upgrade_start_time", buildingObject.get("upgrade_start_time"));
        data.put("building_class", buildingObject.get("building_class"));
        data.put("resource_available", 0);
        data.put("last_collection_time", 0);
        data.put("", building);

        if (buildingObject.containsField("building_resource")) {
            // add  additional fields to buildingData object.
            // last_collection_time
            // resource_available
        }
        if (buildingObject.containsField("building_townhalls")) {
            // add  additional fields to buildingData object.
            // gold_stored
            // mithril_stored
            // remove resource_available
            // remove last_collection_time
        }
        buildingData.add(data);
    }
    return buildingData;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /* ww  w  .j  a va  2  s.  c om*/
 * @param buildings
 * @return 
 */
public List getBuildInfoForAttack(List buildings) {
    List buildingData = new ArrayList();
    Map data = new HashMap();
    for (Object building : buildings) {
        DBObject buildingObject = (DBObject) building;
        boolean checkStatus = checkBuildingUpgrade(buildingObject.get("uid"), buildingObject.get("building_id"),
                new Date().getTime());
        if (checkStatus) {
            DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
            buildingObject = table.findOne(new BasicDBObject("building_id", buildingObject.get("building_id")));
        }
        data.put("building_id", buildingObject.get("building_id"));
        data.put("tid", buildingObject.get("tid"));
        data.put("position_x", buildingObject.get("position_x"));
        data.put("position_y", buildingObject.get("position_y"));
        data.put("level", buildingObject.get("level"));
        data.put("is_constructing", buildingObject.get("is_constructing"));
        data.put("construction_start_time", buildingObject.get("construction_start_time"));
        data.put("is_upgrading", buildingObject.get("is_upgrading"));
        // data.put("upgrade_start_time", buildingObject.get("upgrade_start_time"));
        data.put("building_class", buildingObject.get("building_class"));
        data.put("resource_available", 0);
        // data.put("last_collection_time", 0);

        if (buildingObject.containsField("building_resource")) {
            // add  additional fields to buildingData object.
            // resource_available
        }
        if (buildingObject.containsField("building_townhalls")) {
            // add  additional fields to buildingData object.
            // gold_stored
            // mithril_stored
            // remove resource_available
        }
        buildingData.add(data);
    }
    return buildingData;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /*ww w . java2  s. c  o  m*/
 * @param buildingConfiguration
 * @param data
 * @param isRegister
 * @return
 * @throws JSONException 
 */
public static Map addBuildingFromConfiguration(DBObject buildingConfiguration, JSONObject data,
        boolean isRegister) throws JSONException {
    Map statusData = new HashMap();
    if (buildingConfiguration != null && data != null) {
        BasicDBObject buildingBase = new BasicDBObject();
        long time = new Date().getTime();
        if (data.has("position_x")) {
            buildingBase.put("position_x", data.get("position_x"));
            if (data.has("position_y"))
                buildingBase.put("position_y", data.get("position_y"));

            buildingBase.put("created", time);
            String tid = (String) buildingConfiguration.get("tid");
            if (!tid.equals("TID_BUILDING_TOWN_HALL")) {
                buildingBase.put("construction_start_time", time);
                buildingBase.put("is_constructing", 1);
            }
            long lastCollectionTime = time + ApplicationSettings.convertToTimeStamp(buildingConfiguration);
            buildingBase.put("uid", data.get("uid"));
            buildingBase.put("attributes", buildingConfiguration.get("attributes"));
            boolean price = false;
            if (!isRegister) {
                UserAction userAction = new UserAction();
                if (!TownHallAction.isAllowedBuilding(buildingConfiguration.get("tid"), data.get("uid"),
                        buildingConfiguration.get("level"))) {
                    statusData.put("status", false);
                } else if (!isBuildAddAllow(data.get("uid"), buildingConfiguration.get("build_resource"),
                        buildingConfiguration.get("build_cost"))) {
                    statusData.put("status", false);
                    statusData.put("errorMessage", "Not enough resource available in storage.");
                }
                long buildCost = (Long) buildingConfiguration.get("build_cost");
                if (buildCost > 0) {
                    price = userAction.updateUserResource(data.get("uid"),
                            buildingConfiguration.get("build_cost"),
                            (String) buildingConfiguration.get("build_resource"), "decrease");
                } else {
                    price = true;
                }
            } else {
                price = true;
            }
            if (!price) {
                statusData.put("errorMessage", "Not enough resource available in storage.");
                statusData.put("status", false);
            }
            WriteResult result = MongoDbUtil.saveCollection("aw_building_base", buildingBase);
            if (result.getLastError().ok()) {
                String collectionName = "aw_building_base";
                BasicDBObject buildingObject = null;
                String buildingClass = (String) buildingConfiguration.get("building_class");
                if ("Defense".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_defense";
                    buildingObject = new BuildingDefense();
                    buildingObject.put("attributes", buildingConfiguration.get("attributes"));
                } else if ("Resource".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_resource";
                    buildingObject = new BuildingResource();
                    buildingObject.put("attributes", buildingConfiguration.get("attributes"));
                    if (containString(collectionResource, buildingConfiguration.get(tid).toString())) {
                        buildingObject.put("last_collection_time", lastCollectionTime);

                    } else
                        buildingObject.put("last_collection_time", 0);
                    buildingObject.put("resource_stored", 0);
                } else if ("Army".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_army";
                    buildingObject = new BuildingArmy();
                    buildingObject.put("attributes", buildingConfiguration.get("attributes"));
                } else if ("Town Hall".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_townhall";
                    buildingObject = new BuildingTownhall();
                    buildingObject.put("attributes", buildingConfiguration.get("attributes"));
                    buildingObject.put("gold_stored", buildingConfiguration.get("gold_stored"));
                    buildingObject.put("mithril_stored", buildingConfiguration.get("mithril_stored"));
                } else {
                    statusData.put("status", true);
                    statusData.put("building_id", buildingBase.get("building_id"));
                }
                if (buildingObject != null) {
                    buildingObject.put("building_id", buildingBase.get("building_id"));
                    WriteResult r = MongoDbUtil.saveCollection(collectionName, buildingObject);
                    if (r.getLastError().ok()) {
                        String buildingConfigTid = (String) buildingConfiguration.get("tid");
                        if (buildingConfigTid.equalsIgnoreCase("TID_BUILDING_BARRACK")) {
                            new UserAction().unlockUserTroops(data.get("uid"),
                                    buildingConfiguration.get("level"));
                        }
                        statusData.put("status", true);
                    }

                }
            } else {
                price = new UserAction().updateUserResource(data.get("uid"),
                        buildingConfiguration.get("build_cost"),
                        (String) buildingConfiguration.get("build_resource"), "increase");
            }
        }
    }
    return statusData;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * //from   ww w .j  a  v  a  2 s. c o  m
 * @param uid
 * @param buildingId
 * @param time
 * @return 
 */
public boolean checkBuildingUpgrade(Object uid, Object buildingId, long time) {
    boolean state = false;
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject();
    query.put("uid", uid);
    query.put("building_id", buildingId);
    DBCursor cursor = table.find(query);
    {
        while (cursor.hasNext()) {
            DBObject buildingBase = cursor.next();
            if (time == 0)
                time = new Date().getTime();
            boolean isConstructing = (Boolean) buildingBase.get("is_constructing");
            if (isConstructing) {
                long startTime = (Long) buildingBase.get("construction_start_time");
                long endTime = startTime + ApplicationSettings.convertToTimeStamp(buildingBase);
                if (time > endTime) {
                    BasicDBObject queryUpdate = new BasicDBObject();
                    queryUpdate.put("uid", buildingBase.get("uid"));
                    queryUpdate.put("building_id", buildingBase.get("building_id"));
                    BasicDBObject toUpdate = new BasicDBObject();
                    toUpdate.put("is_constructing", 0);
                    toUpdate.put("construction_start_time", 0);
                    BasicDBObject update = new BasicDBObject("$set", toUpdate);
                    MongoDbUtil.updateCollection("aw_building_base", queryUpdate, update);
                    return true;
                }
            }
            boolean upgrading = (Boolean) buildingBase.get("is_upgrading");
            if (upgrading) {
                long startTime = (Long) buildingBase.get("upgrade_start_time");
                DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"),
                        ((Long) buildingBase.get("level") + 1));
                long endTime = startTime + ApplicationSettings.convertToTimeStamp(nextLevelConfig);
                if (time > endTime) {
                    buildingBase.put("is_upgrading", 0);
                    buildingBase.put("upgrade_start_time", 0);
                    buildingBase.put("level", (Long) buildingBase.get("level") + 1);
                    state = this.updateBuildingFromConfiguration(buildingBase);
                }
            }
        }
    }
    return state;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * //from   w  ww. j av a2s.  com
 * @param building
 * @return 
 */
public boolean updateBuildingFromConfiguration(DBObject building) {
    boolean state = false;
    if (building != null) {
        DBObject buildingConfig = MongoDbUtil
                .getCollection(MongoDbUtil.defaultDBName, "aw_building_configuration")
                .findOne(new BasicDBObject().append("tid", building.get("tid")).append("level",
                        building.get("level")));
        if (buildingConfig != null) {
            building.put("attributes", buildingConfig.get("attributes"));
            BasicDBObject params = new BasicDBObject().append("is_upgrading", building.get("is_upgrading"))
                    .append("upgrade_start_time", building.get("upgrade_start_time"))
                    .append("level", building.get("level")).append("attributes", building.get("attributes"));
            WriteResult result = MongoDbUtil.updateCollection("aw_building_base",
                    new BasicDBObject("building_id", building.get("building_id")),
                    new BasicDBObject("$set", params));
            if (result.getLastError().ok()) {
                String buildingClass = (String) buildingConfig.get("building_class");
                DBObject specialBuildObject = null;
                String collectionName = "aw_building_base";
                if ("Defense".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_defense";
                } else if ("Resource".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_resource";
                } else if ("Army".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_army";
                } else if ("Town Hall".equalsIgnoreCase(buildingClass)) {
                    collectionName = "aw_building_townhall";
                }

                if (specialBuildObject != null) {

                }
            }
        }
    }
    return false;
}

From source file:com.aw.app.action.BuildingAction.java

/**
 * /* w w w.j  av a 2 s . c  o  m*/
 * @param uid
 * @param townGoldLoot
 * @param townMithrilLoot
 * @param isDestroyed 
 */
public void updateBattleTownHall(long uid, long townGoldLoot, long townMithrilLoot, boolean isDestroyed) {
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid)
                    .append("building_class", "Town Hall"));
    if (buildingBase != null) {
        if (buildingBase.containsField("buildingTownhalls")) {
            List<BuildingTownhall> townhalls = (List<BuildingTownhall>) buildingBase.get("buildingTownhalls");
            BuildingTownhall townHall = townhalls.get(0);
            if (townGoldLoot > 0) {
                townHall.put("gold_stored", townHall.getLong("gold_stored") - townGoldLoot);
            }
            if (townGoldLoot < 0) {
                townHall.put("gold_stored", 0);
            }
            if (townMithrilLoot > 0) {
                townHall.put("mithril_stored", townHall.getLong("mithril_stored") - townMithrilLoot);
            }
            if (townMithrilLoot < 0) {
                townHall.put("mithril_stored", 0);
            }
            // update townhall....
            BasicDBObject townhallUpdate = new BasicDBObject("$set",
                    new BasicDBObject().append("gold_stored", townHall.get("gold_stored"))
                            .append("mithril_stored", townHall.get("mithril_stored"))
                            .append("mithril_stored", townHall.get("mithril_stored")));
            BasicDBObject townhallQuery = new BasicDBObject("building_townhall_id",
                    townHall.get("building_townhall_id"));
            MongoDbUtil.updateCollection("aw_building_townhall", townhallQuery, townhallUpdate);

            if (isDestroyed) {
                buildingBase.put("is_destroyed", 1);
            }
            buildingBase.put("buildingTownhalls", townhalls);
            BasicDBObject updateObject = new BasicDBObject("$set",
                    new BasicDBObject().append("is_destroyed", buildingBase.get("buildingBase"))
                            .append("buildingTownhalls", buildingBase.get("buildingTownhalls")));
            BasicDBObject query = new BasicDBObject().append("uid", uid)
                    .append("tid", TownHallAction.townHallTid).append("building_class", "Town Hall");
            MongoDbUtil.updateCollection("aw_building_base", query, updateObject);
        }
    }
}