Fix not using the selected Cash shop surprise
If you have multiple, it would always use the first one. Now it uses whichever you select (as expected).
This commit is contained in:
@@ -42,7 +42,8 @@ public class CashShopSurpriseHandler extends AbstractPacketHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<CashShopSurpriseResult> result = cs.openCashShopSurprise();
|
long cashId = p.readLong();
|
||||||
|
Optional<CashShopSurpriseResult> result = cs.openCashShopSurprise(cashId);
|
||||||
if (result.isEmpty()) {
|
if (result.isEmpty()) {
|
||||||
c.sendPacket(PacketCreator.onCashItemGachaponOpenFailed());
|
c.sendPacket(PacketCreator.onCashItemGachaponOpenFailed());
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import client.inventory.Pet;
|
|||||||
import config.YamlConfig;
|
import config.YamlConfig;
|
||||||
import constants.id.ItemId;
|
import constants.id.ItemId;
|
||||||
import constants.inventory.ItemConstants;
|
import constants.inventory.ItemConstants;
|
||||||
|
import net.jcip.annotations.GuardedBy;
|
||||||
import net.server.Server;
|
import net.server.Server;
|
||||||
import provider.Data;
|
import provider.Data;
|
||||||
import provider.DataProvider;
|
import provider.DataProvider;
|
||||||
@@ -408,17 +409,6 @@ public class CashShop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getItemsSize() {
|
|
||||||
int size = 0;
|
|
||||||
lock.lock();
|
|
||||||
try {
|
|
||||||
size = inventory.size();
|
|
||||||
} finally {
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Integer> getWishList() {
|
public List<Integer> getWishList() {
|
||||||
return wishList;
|
return wishList;
|
||||||
}
|
}
|
||||||
@@ -537,46 +527,58 @@ public class CashShop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Item getCashShopItemByItemid(int itemid) {
|
public Optional<CashShopSurpriseResult> openCashShopSurprise(long cashId) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
for (Item it : inventory) {
|
Optional<Item> maybeCashShopSurprise = getItemByCashId(cashId);
|
||||||
if (it.getItemId() == itemid) {
|
if (maybeCashShopSurprise.isEmpty() ||
|
||||||
return it;
|
maybeCashShopSurprise.get().getItemId() != ItemId.CASH_SHOP_SURPRISE) {
|
||||||
}
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Item cashShopSurprise = maybeCashShopSurprise.get();
|
||||||
|
if (cashShopSurprise.getQuantity() <= 0) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getItemsSize() >= 100) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
CashItem cashItemReward = CashItemFactory.getRandomCashItem();
|
||||||
|
if (cashItemReward == null) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
short newQuantity = (short) (cashShopSurprise.getQuantity() - 1);
|
||||||
|
cashShopSurprise.setQuantity(newQuantity);
|
||||||
|
if (newQuantity <= 0) {
|
||||||
|
removeFromInventory(cashShopSurprise);
|
||||||
|
}
|
||||||
|
|
||||||
|
Item itemReward = cashItemReward.toItem();
|
||||||
|
addToInventory(itemReward);
|
||||||
|
|
||||||
|
return Optional.of(new CashShopSurpriseResult(cashShopSurprise, itemReward));
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Optional<CashShopSurpriseResult> openCashShopSurprise() {
|
@GuardedBy("lock")
|
||||||
Item cashShopSurprise = getCashShopItemByItemid(ItemId.CASH_SHOP_SURPRISE);
|
private Optional<Item> getItemByCashId(long cashId) {
|
||||||
if (cashShopSurprise == null || cashShopSurprise.getQuantity() <= 0) {
|
return inventory.stream()
|
||||||
return Optional.empty();
|
.filter(item -> item.getCashId() == cashId)
|
||||||
|
.findAny();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getItemsSize() {
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
return inventory.size();
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getItemsSize() >= 100) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
CashItem cashItemReward = CashItemFactory.getRandomCashItem();
|
|
||||||
if (cashItemReward == null) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
short newQuantity = (short) (cashShopSurprise.getQuantity() - 1);
|
|
||||||
cashShopSurprise.setQuantity(newQuantity);
|
|
||||||
if (newQuantity <= 0) {
|
|
||||||
removeFromInventory(cashShopSurprise);
|
|
||||||
}
|
|
||||||
|
|
||||||
Item itemReward = cashItemReward.toItem();
|
|
||||||
addToInventory(itemReward);
|
|
||||||
|
|
||||||
return Optional.of(new CashShopSurpriseResult(cashShopSurprise, itemReward));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item generateCouponItem(int itemId, short quantity) {
|
public static Item generateCouponItem(int itemId, short quantity) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.server.channel.handlers;
|
|||||||
|
|
||||||
import client.inventory.Item;
|
import client.inventory.Item;
|
||||||
import constants.id.ItemId;
|
import constants.id.ItemId;
|
||||||
|
import net.packet.InPacket;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@@ -15,6 +16,7 @@ import tools.PacketCreator;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -31,21 +33,25 @@ class CashShopSurpriseHandlerTest extends HandlerTest {
|
|||||||
when(chr.getCashShop()).thenReturn(cashShop);
|
when(chr.getCashShop()).thenReturn(cashShop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private InPacket useCashShopSurprisePacket(long cashId) {
|
||||||
|
return Packets.buildInPacket(out -> out.writeLong(cashId));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDoNothingWhenCsIsNotOpened() {
|
void shouldDoNothingWhenCsIsNotOpened() {
|
||||||
when(cashShop.isOpened()).thenReturn(false);
|
when(cashShop.isOpened()).thenReturn(false);
|
||||||
|
|
||||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
handler.handlePacket(useCashShopSurprisePacket(123), client);
|
||||||
|
|
||||||
verify(cashShop, never()).openCashShopSurprise();
|
verify(cashShop, never()).openCashShopSurprise(anyLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldSendFailurePacketWhenFailToOpen() {
|
void shouldSendFailurePacketWhenFailToOpen() {
|
||||||
when(cashShop.isOpened()).thenReturn(true);
|
when(cashShop.isOpened()).thenReturn(true);
|
||||||
when(cashShop.openCashShopSurprise()).thenReturn(Optional.empty());
|
when(cashShop.openCashShopSurprise(anyLong())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
handler.handlePacket(useCashShopSurprisePacket(456), client);
|
||||||
|
|
||||||
verify(client).sendPacket(PacketCreator.onCashItemGachaponOpenFailed());
|
verify(client).sendPacket(PacketCreator.onCashItemGachaponOpenFailed());
|
||||||
}
|
}
|
||||||
@@ -55,10 +61,10 @@ class CashShopSurpriseHandlerTest extends HandlerTest {
|
|||||||
when(cashShop.isOpened()).thenReturn(true);
|
when(cashShop.isOpened()).thenReturn(true);
|
||||||
Item cashShopSurprise = Items.itemWithQuantity(ItemId.CASH_SHOP_SURPRISE, 3);
|
Item cashShopSurprise = Items.itemWithQuantity(ItemId.CASH_SHOP_SURPRISE, 3);
|
||||||
Item reward = Items.itemWithQuantity(5000012, 1);
|
Item reward = Items.itemWithQuantity(5000012, 1);
|
||||||
when(cashShop.openCashShopSurprise()).thenReturn(Optional.of(new CashShop.CashShopSurpriseResult(
|
when(cashShop.openCashShopSurprise(789)).thenReturn(Optional.of(new CashShop.CashShopSurpriseResult(
|
||||||
cashShopSurprise, reward)));
|
cashShopSurprise, reward)));
|
||||||
|
|
||||||
handler.handlePacket(Packets.emptyInPacket(), client);
|
handler.handlePacket(useCashShopSurprisePacket(789), client);
|
||||||
|
|
||||||
verify(client).sendPacket(PacketCreator.onCashGachaponOpenSuccess(ACCOUNT_ID, cashShopSurprise.getCashId(), 3,
|
verify(client).sendPacket(PacketCreator.onCashGachaponOpenSuccess(ACCOUNT_ID, cashShopSurprise.getCashId(), 3,
|
||||||
reward, 5000012, 1, true));
|
reward, 5000012, 1, true));
|
||||||
|
|||||||
@@ -2,11 +2,17 @@ package testutil;
|
|||||||
|
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import net.packet.ByteBufInPacket;
|
import net.packet.ByteBufInPacket;
|
||||||
|
import net.packet.ByteBufOutPacket;
|
||||||
import net.packet.InPacket;
|
import net.packet.InPacket;
|
||||||
|
import net.packet.OutPacket;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class Packets {
|
public class Packets {
|
||||||
|
|
||||||
public static InPacket emptyInPacket() {
|
public static InPacket buildInPacket(Consumer<OutPacket> contentProvider) {
|
||||||
return new ByteBufInPacket(Unpooled.buffer());
|
OutPacket builderInput = new ByteBufOutPacket();
|
||||||
|
contentProvider.accept(builderInput);
|
||||||
|
return new ByteBufInPacket(Unpooled.wrappedBuffer(builderInput.getBytes()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user