public User getUserById(String nickname) {
Session session = null;
Transaction tx = null;
User user = null;
if ((user = this.getUserByIdFromCache(nickname)) != null) {
return user;
} else {
try {
session = HibernateService.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Query query = session.createQuery(“FROM User U WHERE U.nickname = :nickname”);
query.setString(“nickname”, nickname);
user = (User)query.uniqueResult();
tx.commit();
} catch (Exception var6) {
if (tx.wasRolledBack()) {
tx.rollback();
}
Assistant
To update the value of isAchievementCompleted field in the database table to true (1), you can modify the update() method in the DatabaseManagerImpl class as follows:
java
public void update(Garage garage) {
Session session = null;
Transaction tx = null;
try {
session = HibernateService.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
// Update the field "isAchievementCompleted" to true (1)
SQLQuery query = session.createSQLQuery("UPDATE garage SET isAchievementCompleted = 1 WHERE garageId = :garageId");
query.setParameter("garageId", garage.getGarageId());
query.executeUpdate();
session.update(garage);
tx.commit();
} catch (Exception var5) {
if (tx.wasRolledBack()) {
tx.rollback();
}
var5.printStackTrace();
RemoteDatabaseLogger.error(var5);
}
}
User
onTryBuyItem МЕТОД ЭТОТ ДОЛЖЕН МЕНЯТЬ ЗНАЧЕНИЕ ПОСЛЕ ПОКУПКИ
Assistant
To modify the onTryBuyItem method to update the value of isAchievementCompleted after a purchase, you can make the following changes:
java
User
как сделать показ окна this.send(Type.LOBBY, "complete_achievement", String.valueOf(0)); если в базе данных у isAchievementCompleted стоит 0
Assistant
To show the window this.send(Type.LOBBY, "complete_achievement", String.valueOf(0)); only if the value of isAchievementCompleted in the database is set to 0, you can modify the method as follows:
public List collectGarages() {
Session session = null;
List garages = null;
try {
session = HibernateService.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
garages = session.createCriteria(Garage.class).list();
tx.commit();
return garages;
} catch (Exception var5) {
var5.printStackTrace();
RemoteDatabaseLogger.error(var5);
return null;
}
}
}
СДЕЛАЙ НАКОНЕЦ ЧТОБЫ ЭТОТ МЕТОД public synchronized void onTryBuyItem(String itemId, int count) {
if (count > 0 && count <= 9999) {
Item item = (Item)GarageItemsLoader.items.get(itemId.substring(0, itemId.length() - 3));
Item fromUser = null;
int price = item.price * count;
int itemRang = item.modifications[0].rank;
if (this.checkMoney(price)) {
if (this.getLocalUser().getRang() + 1 < itemRang) {
return;
}
if ((fromUser = this.localUser.getGarage().buyItem(itemId, count, 0)) != null) {
this.send(Type.GARAGE, “buy_item”, StringUtils.concatStrings(item.id, “_m”, String.valueOf(item.modificationIndex)), JSONUtils.parseItemInfo(fromUser));
this.addCrystall(-price);
this.localUser.getGarage().parseJSONData();
database.update(this.localUser); // Обновите информацию о пользователе, чтобы изменить значение поля “isAchievementCompleted”
this.send(Type.LOBBY, “complete_achievement”, String.valueOf(0));
} else {
this.send(Type.GARAGE, “try_buy_item_NO”);
}
}
} else {
this.crystallToZero();
}
}
МЕНЯЛ ИМЕННО ЗНАЧЕНИЕ isAchievementCompleted В ТАБЛИЦЕ БАЗЫ ДАННЫХ НА TRUE ТОЕСТЬ НА 1
Here, the SQL query has been modified to update the field "isAchievementCompleted" to 1 (true) based on the garageId.
public synchronized void onTryBuyItem(String itemId, int count) {
if (count > 0 && count <= 9999) {
Item item = (Item) GarageItemsLoader.items.get(itemId.substring(0, itemId.length() - 3));
Item fromUser = null;
int price = item.price * count;
int itemRang = item.modifications[0].rank;
if (this.checkMoney(price)) {
if (this.getLocalUser().getRang() + 1 < itemRang) {
return;
}
if ((fromUser = this.localUser.getGarage().buyItem(itemId, count, 0)) != null) {
this.send(Type.GARAGE, "buy_item", StringUtils.concatStrings(item.id, "_m", String.valueOf(item.modificationIndex)), JSONUtils.parseItemInfo(fromUser));
this.addCrystall(-price);
this.localUser.getGarage().parseJSONData();
database.update(this.localUser); // Update user information to modify the value of "isAchievementCompleted" field
// Set the value of "isAchievementCompleted" to true (1)
this.localUser.isAchievementCompleted(true);
database.update(this.localUser); // Update user information to save the change
this.send(Type.LOBBY, "complete_achievement", String.valueOf(0));
} else {
this.send(Type.GARAGE, "try_buy_item_NO");
}
}
} else {
this.crystallToZero();
}
}
In this modified method, after purchasing the item, the value of isAchievementCompleted is set to true (1) using this.localUser.isAchievementCompleted(true), and then the database.update(this.localUser) method is called to update the user information in the database and save the change.
onTryBuyItem
java
public synchronized void onTryBuyItem(String itemId, int count) {
if (count > 0 && count <= 9999) {
Item item = (Item) GarageItemsLoader.items.get(itemId.substring(0, itemId.length() - 3));
Item fromUser = null;
int price = item.price * count;
int itemRang = item.modifications[0].rank;
if (this.checkMoney(price)) {
if (this.getLocalUser().getRang() + 1 < itemRang) {
return;
}
if ((fromUser = this.localUser.getGarage().buyItem(itemId, count, 0)) != null) {
this.send(Type.GARAGE, "buy_item", StringUtils.concatStrings(item.id, "_m", String.valueOf(item.modificationIndex)), JSONUtils.parseItemInfo(fromUser));
this.addCrystall(-price);
this.localUser.getGarage().parseJSONData();
database.update(this.localUser);
boolean isAchievementCompletedBeforeUpdate = this.localUser.isAchievementCompleted();
this.localUser.isAchievementCompleted(true);
database.update(this.localUser);
boolean isAchievementCompletedAfterUpdate = this.localUser.isAchievementCompleted();
// Check if the value of isAchievementCompleted changed from 0 to 1
if (isAchievementCompletedBeforeUpdate == false && isAchievementCompletedAfterUpdate == true) {
this.send(Type.LOBBY, "complete_achievement", String.valueOf(0));
}
} else {
this.send(Type.GARAGE, "try_buy_item_NO");
}
}
} else {
this.crystallToZero();
}
}
In this modified code, before updating the user information in the database to set isAchievementCompleted to true, the current value of isAchievementCompleted is saved in isAchievementCompletedBeforeUpdate. After the update, the new value of isAchievementCompleted is obtained in isAchievementCompletedAfterUpdate. Then, it checks if the value changed from 0 to 1, and if so, it sends the complete_achievement message to the LOBBY with String.valueOf(0).