-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added the autoshop task #208
Conversation
Reviewer's Guide by SourceryThis pull request introduces a new AutoShop task that allows users to automatically buy items from the shop based on specified conditions. It includes configurations for setting the item to buy, quantity, and conditions for purchase. It also adds new configurations and enums to support the new task. Sequence diagram for AutoShop item purchase processsequenceDiagram
participant AutoShop
participant BackpageAPI
participant StatsAPI
participant HeroItemsAPI
AutoShop->>StatsAPI: Check credits/uridium
alt Not enough credits/uridium
AutoShop->>AutoShop: Update label with error
else Sufficient funds
AutoShop->>HeroItemsAPI: Check item quantity conditions
alt Conditions met
AutoShop->>BackpageAPI: Send purchase request
BackpageAPI-->>AutoShop: Purchase response
alt Purchase successful
AutoShop->>AutoShop: Update label with success
else Purchase failed
AutoShop->>AutoShop: Update label with error
end
end
end
Class diagram for AutoShop and related classesclassDiagram
class AutoShop {
-BackpageAPI backpage
-PluginAPI api
-StatsAPI stats
-HeroItemsAPI items
-Config config
-JLabel label
+setConfig(ConfigSetting)
+beforeConfig()
+onTickTask()
-check(BuyItem)
-buyItem(ItemSupported, int)
}
class BuyItem {
+boolean enable
+int timeToCheck
+int quantity
+String itemToBuy
+Condition condition
+QuantityCondition quantityCondition
}
class QuantityCondition {
+boolean active
+String item
+int quantity
}
class ItemSupported {
-String id
-String category
-double creditsPrice
-double uridiumPrice
+getId()
+getCategory()
+getCreditsPrice()
+getUridiumPrice()
}
class Config {
+BuyItem item1
+BuyItem item2
+BuyItem item3
+BuyItem item4
+BuyItem item5
}
AutoShop ..|> Task
AutoShop ..|> Configurable
AutoShop ..|> InstructionProvider
AutoShop --> Config
Config --> BuyItem
BuyItem --> QuantityCondition
AutoShop ..> ItemSupported
State diagram for AutoShop purchase statesstateDiagram-v2
[*] --> CheckConditions
CheckConditions --> CheckCredits: Conditions met
CheckConditions --> Wait: Conditions not met
CheckCredits --> CheckUridium: Has enough credits
CheckCredits --> NO_CREDITS: Not enough credits
CheckUridium --> Purchase: Has enough uridium
CheckUridium --> NO_URI: Not enough uridium
Purchase --> PURCHASE_SUCCESS: Success
Purchase --> PURCHASE_ERROR: Error
NO_CREDITS --> Wait
NO_URI --> Wait
PURCHASE_ERROR --> Wait
PURCHASE_SUCCESS --> Wait
Wait --> CheckConditions: After timeToCheck
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dm94 - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟡 Review instructions: 8 issues found
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
return; | ||
} | ||
|
||
updateCheckTime(itemConfig); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Move the updateCheckTime call after successful purchase
Updating the check time before attempting the purchase means we'll have to wait for the next interval even if the purchase fails. Consider moving this after the successful purchase.
Suggested implementation:
if (itemConfig.quantity <= 0) {
return;
}
if (itemConfig.itemToBuy != null && checkNormalCondition(itemConfig)
The updateCheckTime(itemConfig)
call should be moved to after the successful purchase is completed. This would likely be:
- After all purchase conditions are checked
- After the credits are verified to be sufficient
- After the actual purchase transaction succeeds
- Before any final success state updates
You'll need to add the updateCheckTime(itemConfig)
call just before the method returns successfully after completing the purchase.
this.creditsPrice = creditsPrice; | ||
this.uridiumPrice = uridiumPrice; | ||
|
||
if (id.contains("ammunition_laser_")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider using a more robust category determination system
The current string-based category determination is fragile and duplicates information. Consider using a dedicated category field in the enum or a more structured approach.
Suggested implementation:
public enum ItemCategory {
BATTERY,
SPECIAL,
ROCKET
}
ItemSupported(String id, double creditsPrice, double uridiumPrice, ItemCategory category) {
this.id = id;
this.creditsPrice = creditsPrice;
this.uridiumPrice = uridiumPrice;
this.category = category.name().toLowerCase();
}
This change will require:
- Updating all places where ItemSupported is instantiated to include the category parameter
- If you need to maintain backward compatibility, you could add a factory method or keep the old constructor and mark it as @deprecated:
@Deprecated
ItemSupported(String id, double creditsPrice, double uridiumPrice) {
this(id, creditsPrice, uridiumPrice, determineCategory(id));
}
private static ItemCategory determineCategory(String id) {
if (id.contains("ammunition_laser_")) {
return ItemCategory.BATTERY;
} else if (id.contains("equipment_extra_cpu_")) {
return ItemCategory.SPECIAL;
}
return ItemCategory.ROCKET;
}
} | ||
} | ||
|
||
private ItemSupported getItemById(String id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): Consider using a static Map for item lookup instead of iteration
The current implementation iterates through all items on every lookup. Consider using a static Map<String, ItemSupported> for O(1) lookups.
Suggested implementation:
.getConnection();
if (conn.getResponseCode() != 200) {
throw new UnsupportedOperationException("Can't connect when sid is invalid");
}
}
private static final Map<String, ItemSupported> ITEM_MAP;
static {
ITEM_MAP = Arrays.stream(ItemSupported.values())
.collect(Collectors.toMap(
ItemSupported::getId,
item -> item
));
}
private ItemSupported getItemById(String id) {
return ITEM_MAP.get(id);
}
You'll need to add these imports if not already present:
- import java.util.Map;
- import java.util.Arrays;
- import java.util.stream.Collectors;
check(this.config.item5); | ||
} | ||
|
||
private void check(BuyItem itemConfig) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider extracting the purchase logic in the check() method into a separate tryPurchaseItem() method to improve readability and reduce nesting.
The check()
method mixes purchase validation with execution logic. Extract the purchase-related code into a dedicated method to reduce nesting and improve readability:
private void check(BuyItem itemConfig) {
if (!itemConfig.enable || itemConfig.nextCheck > System.currentTimeMillis()
|| itemConfig.quantity <= 0) {
return;
}
updateCheckTime(itemConfig);
if (itemConfig.itemToBuy != null && checkNormalCondition(itemConfig)
&& checkQuantityCondition(itemConfig.quantityCondition)) {
tryPurchaseItem(itemConfig);
}
}
private void tryPurchaseItem(BuyItem itemConfig) {
ItemSupported itemSelected = getItemById(itemConfig.itemToBuy);
if (itemSelected == null) return;
if (this.stats.getTotalCredits() < itemSelected.getCreditsPrice() * itemConfig.quantity) {
updateLabel(itemConfig, State.NO_CREDITS);
return;
}
if (this.stats.getTotalUridium() < itemSelected.getUridiumPrice() * itemConfig.quantity) {
updateLabel(itemConfig, State.NO_URI);
return;
}
try {
buyItem(itemSelected, itemConfig.quantity);
updateLabel(itemConfig, State.PURCHASE_SUCCESS);
} catch (Exception e) {
updateLabel(itemConfig, State.PURCHASE_ERROR);
e.printStackTrace();
}
}
|
||
@Override | ||
public void onTickTask() { | ||
if (!this.backpage.isInstanceValid() || !this.backpage.getSidStatus().contains("OK")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using early return to reduce nesting and improve readability.
Using early return can help in reducing the nesting level of the code, making it more readable and maintainable.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
return; | ||
} | ||
|
||
if (itemConfig.quantity <= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using early return to reduce nesting and improve readability.
Using early return can help in reducing the nesting level of the code, making it more readable and maintainable.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
|
||
ItemSupported itemSelected = getItemById(itemConfig.itemToBuy); | ||
|
||
if (itemSelected == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using early return to reduce nesting and improve readability.
Using early return can help in reducing the nesting level of the code, making it more readable and maintainable.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
return; | ||
} | ||
|
||
if (this.stats.getTotalCredits() < itemSelected.getCreditsPrice() * itemConfig.quantity) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using early return to reduce nesting and improve readability.
Using early return can help in reducing the nesting level of the code, making it more readable and maintainable.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
return; | ||
} | ||
|
||
if (this.stats.getTotalUridium() < itemSelected.getUridiumPrice() * itemConfig.quantity) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using early return to reduce nesting and improve readability.
Using early return can help in reducing the nesting level of the code, making it more readable and maintainable.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
updateLabel(itemConfig, State.PURCHASE_SUCCESS); | ||
} catch (Exception e) { | ||
updateLabel(itemConfig, State.PURCHASE_ERROR); | ||
e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Avoid using printStackTrace; consider logging the exception instead.
Using a logging framework instead of printStackTrace allows for better control over logging levels and outputs.
Review instructions:
Path patterns: *.java
Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.
Summary by Sourcery
New Features: