List of usage examples for com.mongodb DBCursor next
@Override
public DBObject next()
From source file:com.avanza.ymer.MirrorEnvironment.java
License:Apache License
public void removeFormatVersion(Class<?> dataType, Object id) { String collectionName = dataType.getSimpleName().substring(0, 1).toLowerCase() + dataType.getSimpleName().substring(1); DBCollection collection = getMongoDb().getCollection(collectionName); DBObject idQuery = BasicDBObjectBuilder.start("_id", id).get(); DBCursor cursor = collection.find(idQuery); cursor.next(); DBObject obj = cursor.curr();/*w w w . j a v a 2 s . c om*/ cursor.close(); obj.removeField("_formatVersion"); collection.update(idQuery, obj); }
From source file:com.aw.app.action.BuildingAction.java
/** * /*w ww . j a va2 s .c o m*/ * @param uid * @return */ public List getUserBuildings(long uid) { List buildings = new ArrayList(); DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject("uid", uid); DBCursor cursor = table.find(query); while (cursor.hasNext()) { DBObject object = cursor.next(); buildings.add(object); } return buildings; }
From source file:com.aw.app.action.BuildingAction.java
/** * /*from www . ja va2 s . c o m*/ * @param uid * @return */ public long getTotalHousingSpace(long uid) { long campSpace = 0; DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base"); BasicDBObject query = new BasicDBObject(); query.put("tid", HOUSING_TID); query.put("uid", uid); DBCursor cursor = table.find(query); while (cursor.hasNext()) { DBObject building = cursor.next(); if (building.containsField("buildingArmies")) { /// In complete Code } } return campSpace; }
From source file:com.aw.app.action.BuildingAction.java
/** * //from w w w . j a va 2 s . co 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.StrategyAction.java
/** * /*w w w.j a va2 s.c om*/ * @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.TroopsAction.java
/** * //from w w w . j a v a 2 s . c o 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 w w w .j av a2s. c om * @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
/** * //from w w w.ja v a 2s .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
/** * //from ww w .j a v a2s.c o m * @param uid * @param action * @return */ public List getUserTroopsData(long uid, String action) { List troopsData = new ArrayList(); String owner = ""; if ("self".equalsIgnoreCase(action)) owner = "1"; if ("".equalsIgnoreCase(action)) owner = "0"; DBCursor userTroopsResult = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops") .find(new BasicDBObject("uid", uid).append("owner", owner)); while (userTroopsResult.hasNext()) { DBObject userTroop = userTroopsResult.next(); troopsData.add(userTroop); } return troopsData; }
From source file:com.aw.app.action.TroopsAction.java
/** * //from w w w .j a v a 2 s . 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; }