public void updateUser(User user) {
userDAO.updateUser(user);
}
public void userCraft(long id, String recipeId) {
User user = getUser(id);
Recipe recipe = getRecipe(recipeId);
List<ItemTemplate> ingredients = recipe.getIngredients();
List<String> ingrNames = new ArrayList<String>();
for (ItemTemplate item : ingredients) {
ingrNames.add(item.getId());
}
// check that user has all ingredients
Set<String> itemKeys = user.getItems().keySet();
if (!itemKeys.containsAll(ingrNames)) {
return;
}
// apply craft
Map<String, Long> items = user.getItems();
for (ItemTemplate ingr : ingredients) {
Long cnt = items.get(ingr.getId());
cnt--;
if (cnt == 0L) {
items.remove(ingr.getId());
} else {
items.put(ingr.getId(), cnt);
}
}
ItemTemplate res = recipe.getResult();
if (items.containsKey(res.getId())) {
items.put(res.getId(), items.get(res.getId()) + 1);
} else {
items.put(res.getId(), 1L);
}
user.setItems(items);
updateUser(user);
}