@ServicesInject(target = DatabaseManagerImpl.class)
private DatabaseManager database = DatabaseManagerImpl.instance();
private List<String> loadedGifts = readLoadedGiftsFromFile();
public static GiftsServices instance() {
return INSTANCE;
}
private List<String> readLoadedGiftsFromFile() {
List<String> loadedGifts = new ArrayList<>();
try (FileReader reader = new FileReader("gift.json")) {
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
for (Object obj : jsonArray) {
loadedGifts.add(obj.toString());
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return loadedGifts;
}
public void userOnGiftsWindowOpen(LobbyManager lobby) {
lobby.send(Type.LOBBY, "show_gifts_window", String.valueOf(this.loadedGifts), String.valueOf((lobby.getLocalUser().getGarage().getItemById("gift")).count));
}
public void tryRollItem(LobbyManager lobby) {
String itemName, itemId = null;
int countItems = 0;
int rarity = 0;
int offsetCrystalls = 0;
try {
JSONArray jsonArray = parseJsonArray(String.valueOf(this.loadedGifts));
JSONObject randomItem = pickRandomItem(jsonArray);
itemId = (String)randomItem.get("item_id");
rarity = ((Long)randomItem.get("rarity")).intValue();
countItems = ((Long)randomItem.get("count")).intValue();
} catch (Exception e) {
e.printStackTrace();
}
if (itemId.startsWith("set_")) {
String countString = itemId.substring(4);
int setItemCount = Integer.parseInt(countString);
itemName = "+" + setItemCount;
addBonusItemsToGarage(lobby.getLocalUser(), setItemCount);
} else if (itemId.equals("crystalls")) {
itemName = "x" + countItems;
lobby.dummyAddCrystall(countItems);
offsetCrystalls = countItems;
} else {
itemName = getItemNameWithCount(lobby, itemId, countItems);
offsetCrystalls = getOffsetCrystalls(lobby, (Item)GarageItemsLoader.items.get(itemId));
rewardGiftItemToUser(lobby, (Item)GarageItemsLoader.items.get(itemId));
}
Logger.debug("User " + lobby.getLocalUser().getNickname() + " added item " + itemName + " to garage");
Item item = lobby.getLocalUser().getGarage().getItemById("gift");
updateInventory(lobby, item, 1);
lobby.send(Type.LOBBY, "item_rolled", itemId, "" + countItems + ";" + countItems, itemName + ";" + itemName);
}
public void rollItems(LobbyManager lobby, int rollCount) {
JSONArray jsonArrayGift = new JSONArray();
int offsetCrystalls = 0;
StringBuilder resultLogs = new StringBuilder("[GIFT_SYSTEM_LOG_OUT]: Details:");
resultLogs.append(" Nickname: ").append(lobby.getLocalUser().getNickname());
resultLogs.append(" gifts opened count: ").append(rollCount).append("\n");
try {
JSONArray jsonArray = parseJsonArray(String.valueOf(this.loadedGifts));
for (int i = 0; i < rollCount; i++) {
String itemName;
JSONObject randomItem = pickRandomItem(jsonArray);
String itemId = (String)randomItem.get("item_id");
int rarity = ((Long)randomItem.get("rarity")).intValue();
int countItems = ((Long)randomItem.get("count")).intValue();
JSONArray numInventoryCounts = new JSONArray();
for (int j = 0; j < 5; j++)
numInventoryCounts.add(Integer.valueOf(0));
if (itemId.startsWith("set_")) {
String countString = itemId.substring(4);
int setItemCount = Integer.parseInt(countString);
itemName = "+" + setItemCount;
addBonusItemsToGarage(lobby.getLocalUser(), setItemCount);
} else if (itemId.equals("crystalls")) {
itemName = "x" + countItems;
lobby.dummyAddCrystall(countItems);
} else {
itemName = getItemNameWithCount(lobby, itemId, countItems);
offsetCrystalls = getOffsetCrystalls(lobby, (Item)GarageItemsLoader.items.get(itemId));
rewardGiftItemToUser(lobby, (Item)GarageItemsLoader.items.get(itemId));
}
resultLogs.append("===================================\n");
resultLogs.append("[GIFT_SYSTEM_LOG_OUT]: Prize: ").append(itemName).append("\n");
JSONObject newItem = new JSONObject();
newItem.put("itemId", itemId);
newItem.put("visualItemName", itemName);
newItem.put("rarity", Integer.valueOf(rarity));
newItem.put("offsetCrystalls", Integer.valueOf(offsetCrystalls));
newItem.put("numInventoryCounts", numInventoryCounts);
jsonArrayGift.add(newItem);
}
} catch (Exception e) {
e.printStackTrace();
}
Logger.debug(String.valueOf(resultLogs));
Item item = lobby.getLocalUser().getGarage().getItemById("gift");
updateInventory(lobby, item, rollCount);
lobby.send(Type.LOBBY,"items_rolled", String.valueOf(jsonArrayGift));
}
private void updateInventory(LobbyManager lobby, Item item, int amountToRemove) {
item.count -= amountToRemove;
if (item.count <= 0)
(lobby.getLocalUser().getGarage()).items.remove(item);
lobby.getLocalUser().getGarage().parseJSONData();
this.database.update(lobby.getLocalUser().getGarage());
}
private String getItemNameWithCount(LobbyManager lobby, String itemId, int countItems) {
String itemName = ((Item)GarageItemsLoader.items.get(itemId)).name.localizatedString(lobby.getLocalUser().getLocalization());
List<String> specialItemIds = Arrays.asList("mine", "n2o", "health", "armor", "double_damage");
if (specialItemIds.contains(itemId)) {
Item bonusItem = lobby.getLocalUser().getGarage().getItemById(itemId);
if (bonusItem == null) {
bonusItem = ((Item)GarageItemsLoader.items.get(itemId)).clone();
(lobby.getLocalUser().getGarage()).items.add(bonusItem);
}
bonusItem.count += countItems;
itemName = itemName + " x" + countItems;
}
return itemName;
}
private void rewardGiftItemToUser(LobbyManager lobby, Item item) {
boolean containsItem = lobby.getLocalUser().getGarage().containsItem(item.id);
if (containsItem) {
if (item.itemType != ItemType.INVENTORY) {
lobby.dummyAddCrystall(item.price / 2);
Logger.log("User " + lobby.getLocalUser().getNickname() + " contains item in garage reward: " + item.price / 2 + " crystals.");
}
} else {
(lobby.getLocalUser().getGarage()).items.add((Item)GarageItemsLoader.items.get(item.id));
}
}
private int getOffsetCrystalls(LobbyManager lobby, Item item) {
boolean containsItem = lobby.getLocalUser().getGarage().containsItem(item.id);
int offsetCrystalls = 0;
if (containsItem &&
item.itemType != ItemType.INVENTORY) {
offsetCrystalls = item.price / 2;
Logger.log("User " + lobby.getLocalUser().getNickname() + " offsetCrystalls: " + offsetCrystalls);
}
return offsetCrystalls;
}
public static JSONArray parseJsonArray(String jsonArrayString) throws ParseException {
JSONParser jsonParser = new JSONParser();
return (JSONArray)jsonParser.parse(jsonArrayString);
}
private void addBonusItemsToGarage(User localUser, int setItemCount) {
Logger.log("addBonusItemsToGarage()::setItemCount: " + setItemCount);
List<String> bonusItemIds = Arrays.asList("n2o", "double_damage", "armor", "mine", "health");
for (String bonusItemId : bonusItemIds) {
Item bonusItem = localUser.getGarage().getItemById(bonusItemId);
if (bonusItem == null) {
bonusItem = ((Item)GarageItemsLoader.items.get(bonusItemId)).clone();
(localUser.getGarage()).items.add(bonusItem);
}
bonusItem.count += setItemCount;
}
}
public static JSONObject pickRandomItem(JSONArray jsonArray) {
Random random = new Random();
int[] rarityProbabilities = { 50, 34, 10, 5, 1 };
int totalProbabilitySum = 0;
for (int i = 0; i < rarityProbabilities.length; i++)
totalProbabilitySum += rarityProbabilities[i];
int randomValue = random.nextInt(totalProbabilitySum);
int cumulativeProbability = 0;
int rarity = 0;
for (int j = 0; j < rarityProbabilities.length; j++) {
cumulativeProbability += rarityProbabilities[j];
if (randomValue < cumulativeProbability) {
rarity = j;
break;
}
}
ArrayList<JSONObject> itemsWithRarity = new ArrayList<>();
for (int k = 0; k < jsonArray.size(); k++) {
JSONObject item = (JSONObject)jsonArray.get(k);
int itemRarity = ((Long)item.get("rarity")).intValue();
if (itemRarity == rarity)
itemsWithRarity.add(item);
}
if (itemsWithRarity.size() > 0) {
int randomIndex = random.nextInt(itemsWithRarity.size());
return itemsWithRarity.get(randomIndex);
}
return (JSONObject)jsonArray.get(0);
}