Rename and clean up MapleStorageInventory

This commit is contained in:
P0nk
2021-09-09 22:53:16 +02:00
parent e9c8a82b47
commit 701d735202
2 changed files with 202 additions and 172 deletions

View File

@@ -274,7 +274,7 @@ public class Storage {
public void arrangeItems(Client c) { public void arrangeItems(Client c) {
lock.lock(); lock.lock();
try { try {
MapleStorageInventory msi = new MapleStorageInventory(c, items); StorageInventory msi = new StorageInventory(c, items);
msi.mergeItems(); msi.mergeItems();
items = msi.sortItems(); items = msi.sortItems();

View File

@@ -28,161 +28,19 @@ import constants.inventory.ItemConstants;
import java.util.*; import java.util.*;
/** /**
*
* @author RonanLana * @author RonanLana
*/ */
public class StorageInventory {
class PairedQuicksort { private final Client c;
private int i = 0;
private int j = 0;
private final ArrayList<Integer> intersect;
ItemInformationProvider ii = ItemInformationProvider.getInstance();
private void PartitionByItemId(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (x.getItemId() > A.get(i).getItemId()) i++;
while (x.getItemId() < A.get(j).getItemId()) j--;
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByName(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(i).getItemId())) > 0) i++;
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(j).getItemId())) < 0) j--;
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByQuantity(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (x.getQuantity() > A.get(i).getQuantity()) i++;
while (x.getQuantity() < A.get(j).getQuantity()) j--;
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByLevel(int Esq, int Dir, ArrayList<Item> A) {
Equip x, w, eqpI, eqpJ;
i = Esq;
j = Dir;
x = (Equip)(A.get((i + j) / 2));
do {
eqpI = (Equip)A.get(i);
eqpJ = (Equip)A.get(j);
while (x.getLevel() > eqpI.getLevel()) i++;
while (x.getLevel() < eqpJ.getLevel()) j--;
if (i <= j) {
w = (Equip)A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
void MapleQuicksort(int Esq, int Dir, ArrayList<Item> A, int sort) {
switch(sort) {
case 3:
PartitionByLevel(Esq, Dir, A);
break;
case 2:
PartitionByName(Esq, Dir, A);
break;
case 1:
PartitionByQuantity(Esq, Dir, A);
break;
default:
PartitionByItemId(Esq, Dir, A);
}
if (Esq < j) MapleQuicksort(Esq, j, A, sort);
if (i < Dir) MapleQuicksort(i, Dir, A, sort);
}
public PairedQuicksort(ArrayList<Item> A, int primarySort, int secondarySort) {
intersect = new ArrayList<>();
if(A.size() > 0) MapleQuicksort(0, A.size() - 1, A, primarySort);
intersect.add(0);
for(int ind = 1; ind < A.size(); ind++) {
if(A.get(ind - 1).getItemId() != A.get(ind).getItemId()) {
intersect.add(ind);
}
}
intersect.add(A.size());
for(int ind = 0; ind < intersect.size() - 1; ind++) {
if(intersect.get(ind + 1) > intersect.get(ind)) MapleQuicksort(intersect.get(ind), intersect.get(ind + 1) - 1, A, secondarySort);
}
}
}
public class MapleStorageInventory {
private Client c;
private Map<Short, Item> inventory = new LinkedHashMap<>(); private Map<Short, Item> inventory = new LinkedHashMap<>();
private byte slotLimit; private final byte slotLimit;
public MapleStorageInventory(Client c, List<Item> toSort) { public StorageInventory(Client c, List<Item> toSort) {
this.inventory = new LinkedHashMap<>(); this.inventory = new LinkedHashMap<>();
this.slotLimit = (byte)toSort.size(); this.slotLimit = (byte) toSort.size();
this.c = c; this.c = c;
for(Item item : toSort) { for (Item item : toSort) {
this.addItem(item); this.addItem(item);
} }
} }
@@ -244,7 +102,7 @@ public class MapleStorageInventory {
if (src < 0 || dst < 0) { if (src < 0 || dst < 0) {
return; return;
} }
if(dst > this.getSlotLimit()) { if (dst > this.getSlotLimit()) {
return; return;
} }
@@ -299,16 +157,24 @@ public class MapleStorageInventory {
ItemInformationProvider ii = ItemInformationProvider.getInstance(); ItemInformationProvider ii = ItemInformationProvider.getInstance();
Item srcItem, dstItem; Item srcItem, dstItem;
for(short dst = 1; dst <= this.getSlotLimit(); dst++) { for (short dst = 1; dst <= this.getSlotLimit(); dst++) {
dstItem = this.getItem(dst); dstItem = this.getItem(dst);
if(dstItem == null) continue; if (dstItem == null) {
continue;
}
for(short src = (short)(dst + 1); src <= this.getSlotLimit(); src++) { for (short src = (short) (dst + 1); src <= this.getSlotLimit(); src++) {
srcItem = this.getItem(src); srcItem = this.getItem(src);
if(srcItem == null) continue; if (srcItem == null) {
continue;
}
if(dstItem.getItemId() != srcItem.getItemId()) continue; if (dstItem.getItemId() != srcItem.getItemId()) {
if(dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) break; continue;
}
if (dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) {
break;
}
moveItem(src, dst); moveItem(src, dst);
} }
@@ -344,7 +210,7 @@ public class MapleStorageInventory {
for (short i = 1; i <= this.getSlotLimit(); i++) { for (short i = 1; i <= this.getSlotLimit(); i++) {
Item item = this.getItem(i); Item item = this.getItem(i);
if (item != null) { if (item != null) {
itemarray.add(item.copy()); itemarray.add(item.copy());
} }
} }
@@ -360,3 +226,167 @@ public class MapleStorageInventory {
return itemarray; return itemarray;
} }
} }
class PairedQuicksort {
private int i = 0;
private int j = 0;
private final ArrayList<Integer> intersect;
ItemInformationProvider ii = ItemInformationProvider.getInstance();
private void PartitionByItemId(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (x.getItemId() > A.get(i).getItemId()) {
i++;
}
while (x.getItemId() < A.get(j).getItemId()) {
j--;
}
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByName(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(i).getItemId())) > 0) {
i++;
}
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(j).getItemId())) < 0) {
j--;
}
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByQuantity(int Esq, int Dir, ArrayList<Item> A) {
Item x, w;
i = Esq;
j = Dir;
x = A.get((i + j) / 2);
do {
while (x.getQuantity() > A.get(i).getQuantity()) {
i++;
}
while (x.getQuantity() < A.get(j).getQuantity()) {
j--;
}
if (i <= j) {
w = A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
private void PartitionByLevel(int Esq, int Dir, ArrayList<Item> A) {
Equip x, w, eqpI, eqpJ;
i = Esq;
j = Dir;
x = (Equip) (A.get((i + j) / 2));
do {
eqpI = (Equip) A.get(i);
eqpJ = (Equip) A.get(j);
while (x.getLevel() > eqpI.getLevel()) {
i++;
}
while (x.getLevel() < eqpJ.getLevel()) {
j--;
}
if (i <= j) {
w = (Equip) A.get(i);
A.set(i, A.get(j));
A.set(j, w);
i++;
j--;
}
} while (i <= j);
}
void MapleQuicksort(int Esq, int Dir, ArrayList<Item> A, int sort) {
switch (sort) {
case 3:
PartitionByLevel(Esq, Dir, A);
break;
case 2:
PartitionByName(Esq, Dir, A);
break;
case 1:
PartitionByQuantity(Esq, Dir, A);
break;
default:
PartitionByItemId(Esq, Dir, A);
}
if (Esq < j) {
MapleQuicksort(Esq, j, A, sort);
}
if (i < Dir) {
MapleQuicksort(i, Dir, A, sort);
}
}
public PairedQuicksort(ArrayList<Item> A, int primarySort, int secondarySort) {
intersect = new ArrayList<>();
if (A.size() > 0) {
MapleQuicksort(0, A.size() - 1, A, primarySort);
}
intersect.add(0);
for (int ind = 1; ind < A.size(); ind++) {
if (A.get(ind - 1).getItemId() != A.get(ind).getItemId()) {
intersect.add(ind);
}
}
intersect.add(A.size());
for (int ind = 0; ind < intersect.size() - 1; ind++) {
if (intersect.get(ind + 1) > intersect.get(ind)) {
MapleQuicksort(intersect.get(ind), intersect.get(ind + 1) - 1, A, secondarySort);
}
}
}
}