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.BuildingAction.java

/**
 * //from  w  ww  . j a va  2 s  .  c  o m
 * @param uid
 * @param buildingId
 * @param time
 * @return 
 */
public Map updateBuildingCollection(long uid, long buildingId, long time) {
    Map status = new HashMap();
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject().append("uid", uid).append("building_id", buildingId));
    if (buildingBase != null) {
        if (buildingBase.containsField("building_class")
                && buildingBase.get("building_class").toString().equalsIgnoreCase("Resource")
                && this.isCollectionBuilding(buildingBase.get("tid"))) {
            if (buildingBase.containsField("buildingResources")) {
                List<BuildingResource> buildingResources = (List<BuildingResource>) buildingBase
                        .get("buildingResources");
                BuildingResource resource = buildingResources.get(0);
                if (resource != null) {
                    status = updateBuildingStorageResource(resource, buildingBase.get("tid"),
                            new Date().getTime());
                } else {
                    status.put("status", false);
                    status.put("errorMessage", "Resource not found.");
                }
            }
        } else {
            status.put("errorMessage", "Building is not a resource collector.");
        }
    } else {
        status.put("errorMessage", "Building Not Found.");
    }
    return status;
}

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

/**
 * /*from   w w  w . j a  v a 2 s.c  o m*/
 * @param uid
 * @param buildingId
 * @param time
 * @return 
 */
public Map instantFinishBuilding(long uid, long buildingId, long time) {
    Map status = new HashMap();
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject().append("uid", uid).append("building_id", buildingId));
    if (buildingBase != null) {
        int updating = (Integer) buildingBase.get("is_upgrading");
        int constructing = (Integer) buildingBase.get("is_constructing");
        if (updating == 1 || constructing == 1) {
            DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"),
                    (Long) buildingBase.get("level") + 1);
            if (updating == 1 && nextLevelConfig == null) {
                status.put("errorMessage", "Next level of building is not present.");
                return status;
            }
            long spenAmount = this.calculateInstantFinishAmount(buildingId, time);
            long userSpens = new UserAction().getUserResource(uid, "spens");
            if (userSpens >= spenAmount) {
                UserAction action = new UserAction();
                if (action.updateUserResource(uid, spenAmount, "spens", "decrease")) {
                    buildingBase.put("is_upgrading", 0);
                    buildingBase.put("upgrade_start_time", 0);
                    buildingBase.put("level", (Long) buildingBase.get("level") + 1);
                    if (AWUtil.isInArray(collectionResource, buildingBase.get("tid").toString())) {
                        long lastCollectionTime = time;
                        List<BuildingResource> resources = (List<BuildingResource>) buildingBase
                                .get("buildingResources");
                        BuildingResource resource = resources.get(0);
                        if (resource != null) {
                            resource.put("last_collection_time", lastCollectionTime);
                            // update resource here...
                        }
                    }
                    // update building base here....
                }
            } else {
                status.put("errorMessage",
                        "Not enough resources in the storage to finish this building upgrade.");
            }

        } else {
            status.put("errorMessage", "Building is not upgrading, you have made bad request.");
        }
    } else {
        status.put("errorMessage", "Building Not Found.");
    }
    return status;
}

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

/**
 * //from w  ww. j a  v a 2 s  .  co m
 * @param uid
 * @param buildingId
 * @param time
 * @return 
 */
public Map upgradeBuilding(long uid, long buildingId, long time) {
    Map status = new HashMap();
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject().append("uid", uid).append("building_id", buildingId));
    if (buildingBase != null) {
        int upgrading = (Integer) buildingBase.get("");
        if (upgrading == 1) {
            status.put("errorMessage", "Building is already upgrading.");
            return status;
        }
        int constructing = (Integer) buildingBase.get("");
        if (constructing == 1) {
            status.put("errorMessage", "Building is under construction.");
            return status;
        }
        DBObject nextLevelConfig = this.getBuildingConfiguration(buildingBase.get("tid"),
                (Long) buildingBase.get("level") + 1);
        boolean isAllowed = TownHallAction.isAllowedBuilding(buildingBase.get("tid"), uid,
                (Long) buildingBase.get("level") + 1);
        if (isAllowed)
            status.put("status", true);
        if (nextLevelConfig == null) {
            status.put("errorMessage", "Next level of building is not present.");
        }

        buildingBase.put("is_upgrading", 1);
        buildingBase.put("upgrade_start_time", time);
        if (new UserAction().updateUserResource(uid, nextLevelConfig.get("build_cost"),
                (String) nextLevelConfig.get("build_resource"), "decrease")) {
            if (buildingBase.get("tid").toString().equals(collectionResource[0])
                    || buildingBase.get("tid").toString().equals(collectionResource[1])) {
                long lastCollectionTime = time + ApplicationSettings.convertToTimeStamp(nextLevelConfig);
                if (buildingBase.containsField("buildingResources")) {
                    List<BuildingResource> buildingResources = (List<BuildingResource>) buildingBase
                            .get("buildingResources");
                    if (buildingResources != null && buildingResources.size() > 0) {
                        BuildingResource resource = buildingResources.get(0);
                        resource.put("last_collection_time", lastCollectionTime);
                        // update resource object here.
                    }
                }
            }
            // update building base here
        } else {
            status.put("errorMessage", "Building Not Found.");
        }

    }
    return status;
}

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

/**
 * //from  w  w  w .ja  va 2s  . c om
 * @param buildingId
 * @param time
 * @return 
 */
private long calculateInstantFinishAmount(long buildingId, long time) {
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject().append("building_id", buildingId));
    if (buildingBase != null) {
        int upgrading = (Integer) buildingBase.get("is_upgrading");
        DBObject configObject = null;
        long startTime = 0;
        if (upgrading == 1) {
            configObject = this.getBuildingConfiguration(buildingBase.get("tid"),
                    (Long) buildingBase.get("level") + 1);
            startTime = (Long) buildingBase.get("upgrade_start_time");
        } else {
            configObject = this.getBuildingConfiguration(buildingBase.get("tid"), buildingBase.get("level"));
            startTime = (Long) buildingBase.get("construction_start_time");
        }
        if (configObject != null) {
            long endTime = startTime + ApplicationSettings.convertToTimeStamp(configObject);
            long diff = endTime - time;
            if (diff > 0) {
                long spenConsumed = StrategyAction.getInstantFinishValue("building", diff);
                return spenConsumed;
            }
        }
    }
    return 0;
}

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

/**
 * /*  ww w  .  j a v a 2 s  .  c o  m*/
 * @param uid 
 */
public void bootStrapAction(Long uid) {
    /********** Perform User Building Checking ***************/
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBCursor userBuildings = table.find(query);
    int count = 0;
    while (userBuildings.hasNext()) {
        DBObject building = userBuildings.next();
        if (building.containsField("tid")) {
            String tid = (String) building.get("tid");
            TroopsAction troopsAction = new TroopsAction();
            if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_TAVERN")) {
                troopsAction.checkToopsUpgrades(uid, building);
                count++;
            } else if (tid != null && tid.equalsIgnoreCase("TID_BUILDING_BARRACK")) {
                int buildingId = (Integer) building.get("building_id");
                troopsAction.checkToopsInBarrack(uid, buildingId);
                count++;
            }

        }
    }
}

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

/**
 * //ww  w  . j  a  va  2 s  . c  o  m
 * @param uid
 * @return 
 */
public BasicDBObject getTownHallOfUser(Object uid) {
    DBObject buildingBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
            .findOne(new BasicDBObject("uid", uid).append("tid", townHallTid));
    if (buildingBase != null) {
        if (buildingBase.containsField("buildingTownhalls")) {
            List<BuildingTownhall> townHallBuildings = (List<BuildingTownhall>) buildingBase
                    .get("buildingTownhalls");
            return townHallBuildings.get(0);
        }
    }
    return null;
}

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

/**
 * //from  w  w w .  jav  a2s .  co  m
 * @param uid
 * @param buildingId
 * @return 
 */
public boolean checkToopsInBarrack(Long uid, long buildingId) {
    boolean state = false;
    Long time = new Date().getTime();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_barrack_troops");

    BasicDBObject query = new BasicDBObject("uid", uid);
    query.put("building_id", buildingId);
    DBCursor barrackTroops = table.find(query);
    long nextTroopsTrainingTime = 0;
    if (barrackTroops != null && barrackTroops.size() > 0) {
        while (barrackTroops.hasNext()) {
            DBObject barrackTroop = barrackTroops.next();
            if (nextTroopsTrainingTime > 0 && state) {
                BasicDBObject updateQuery = new BasicDBObject();
                updateQuery.put("bc_id", barrackTroop.get("bc_id"));
                BasicDBObject updateBt = new BasicDBObject();
                Map data = new HashMap();
                data.put("is_first_in_queue", 1);
                data.put("training_start_time", nextTroopsTrainingTime);
                BasicDBObject object = new BasicDBObject(data);
                updateBt.put("$set", object);
                MongoDbUtil.updateCollection("aw_barrack_troops", updateQuery, updateBt);
            }
            Map result = processTroopTraining(barrackTroop, time);
            boolean breakState = (boolean) result.get("breakState");
            state = (boolean) result.get("status");
            long nextTrainingTime = (long) result.get("nextTrainingTime");
            if (breakState) {
                // Error Condition
            } else if (state && result.containsKey("nextTrainingTime") && nextTrainingTime > 0) {
                nextTroopsTrainingTime = nextTrainingTime;
            } else {
                nextTroopsTrainingTime = 0;
            }
        }
    }
    return state;
}

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

/**
 * //from   ww  w .  ja v a 2 s .  c  o  m
 * @param troopSet
 * @param time
 * @return 
 */
public Map processTroopTraining(DBObject troopSet, Long time) {
    Map result = new HashMap();
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration");
    BasicDBObject query = new BasicDBObject();
    query.put("tid", troopSet.get("tid"));
    query.put("level", troopSet.get("level"));
    DBCursor resultSet = table.find(query);
    while (resultSet.hasNext()) {
        DBObject config = resultSet.next();
        long remainingSpace = this.getRemainingTroopSpace((Long) troopSet.get("uid")); // Get from Method Later.
        if (config != null) {
            long trainingTime = (Long) config.get("training_time");
            long count = (Long) troopSet.get("count");
            long totalElapseTimeRequired = trainingTime * count;
            long trainingStartTime = (Long) troopSet.get("training_start_time");
            long endTime = trainingStartTime + totalElapseTimeRequired;
            long housingSpace = (Long) config.get("housing_space");
            long spaceRequired = count * housingSpace;
            if (endTime < new Date().getTime()) {
                if (remainingSpace >= spaceRequired) {
                    boolean isAdded = addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"),
                            (String) config.get("tid"), (Long) troopSet.get("count"));
                    if (isAdded) {
                        BasicDBObject deleteQuery = new BasicDBObject();
                        deleteQuery.put("bc_id", troopSet.get("bc_id"));
                        MongoDbUtil.removeCollection("aw_barrack_troops", deleteQuery);
                        result.put("status", true);
                        result.put("nextTrainingTime", endTime);
                    }
                } else if (remainingSpace == 0) {
                    BasicDBObject queryObject = new BasicDBObject("bc_id", troopSet.get("bc_id"));
                    BasicDBObject updateData = new BasicDBObject("training_start_time", new Date().getTime());
                    BasicDBObject updateQuery = new BasicDBObject("$set", updateData);
                    MongoDbUtil.updateCollection(MongoDbUtil.defaultDBName, queryObject, updateQuery);

                    result.put("status", false);
                    result.put("breakStatus", true);
                    result.put("reason", "Training Fields are full.");
                } else {
                    long troopCount = (long) Math.floor(remainingSpace / housingSpace);
                    this.addTroopOfUsers((Long) config.get("troop_id"), (Long) troopSet.get("uid"),
                            (String) config.get("tid"), troopCount);
                    long intialCount = (Long) troopSet.get("count");
                    long resultCount = intialCount - count;

                    Map queryMap = new HashMap();
                    queryMap.put("bc_id", troopSet.get("bc_id"));
                    Map updateData = new HashMap();
                    updateData.put("count", resultCount);
                    updateData.put("training_start_time", trainingStartTime + (trainingTime * troopCount));
                    Map dataMap = new HashMap();
                    dataMap.put("query", queryMap);
                    dataMap.put("update", updateData);
                    MongoDbUtil.updateCollection("aw_barrack_troops", updateData);
                    result.put("status", true);
                    result.put("breakStatus", true);
                    result.put("reason", "Training Fields are full.");
                }
            }
        } else {
            result.put("reason", "Configuration not found");
        }
    }
    return result;
}

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

/**
 * /*w  w  w. j a va  2 s .c  o m*/
 * @param uid
 * @return 
 */
public long getRemainingTroopSpace(long uid) {
    long totalSpace = new BuildingAction().getTotalHousingSpace(uid);
    long consumedSpace = 0;
    long remainingSpace = totalSpace;
    DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops");
    BasicDBObject query = new BasicDBObject("uid", uid);
    DBCursor cursor = table.find(query);
    Map troopMap = new HashMap();
    while (cursor.hasNext()) {
        DBObject userTroop = cursor.next();
        long troopId = (Long) userTroop.get("troop_id");

        if (troopMap.containsKey(troopId)) {
            long value = (Long) troopMap.get(troopId);
            ++value;
            troopMap.put(troopId, value);
        } else {
            troopMap.put(troopId, 1);
        }
    }
    Iterator entries = troopMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry entry = (Map.Entry) entries.next();
        long key = (Long) entry.getKey();
        long value = (Long) entry.getValue();
        DBCollection configTable = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName,
                "aw_troops_configuration");
        DBCursor configCursor = configTable.find(new BasicDBObject("troop_id", key));
        while (configCursor.hasNext()) {
            DBObject config = configCursor.next();
            long configHousingSpace = (Long) config.get("housing_space");
            consumedSpace = consumedSpace + (configHousingSpace * value);
        }
    }
    remainingSpace = remainingSpace - consumedSpace;
    if (remainingSpace < 0)
        remainingSpace = 0;
    return remainingSpace;
}

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

/**
 * /*  www .ja v  a 2s  .  c o  m*/
 * @param level
 * @return 
 */
public Map getTroopsTidLevelForBarrack(Object level) {
    DBCursor cursor = this.getTroopsForBarrackLevel(level);
    Map data = new HashMap();
    while (cursor.hasNext()) {
        DBObject object = cursor.next();
        data.put(object.get("tid"), object.get("level"));
    }
    return data;
}