mirror of
https://github.com/psforever/PSF-LoginServer.git
synced 2026-04-28 07:45:28 +00:00
216 lines
8.3 KiB
Scala
216 lines
8.3 KiB
Scala
|
|
// Copyright (c) 2017 PSForever
|
||
|
|
package objects
|
||
|
|
|
||
|
|
import net.psforever.objects.{AmmoBox, SimpleItem}
|
||
|
|
import net.psforever.objects.definition.SimpleItemDefinition
|
||
|
|
import net.psforever.objects.inventory.{GridInventory, InventoryItem, InventoryTile}
|
||
|
|
import net.psforever.objects.GlobalDefinitions._
|
||
|
|
import net.psforever.packet.game.PlanetSideGUID
|
||
|
|
import org.specs2.mutable._
|
||
|
|
|
||
|
|
import scala.collection.mutable.ListBuffer
|
||
|
|
import scala.util.Success
|
||
|
|
|
||
|
|
class InventoryTest extends Specification {
|
||
|
|
val bullet9mmBox1 = AmmoBox(PlanetSideGUID(1), bullet_9mm)
|
||
|
|
val bullet9mmBox2 = AmmoBox(PlanetSideGUID(2), bullet_9mm)
|
||
|
|
|
||
|
|
"GridInventory" should {
|
||
|
|
"construct" in {
|
||
|
|
val obj : GridInventory = GridInventory()
|
||
|
|
obj.TotalCapacity mustEqual 1
|
||
|
|
obj.Capacity mustEqual 1
|
||
|
|
}
|
||
|
|
|
||
|
|
"resize" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj.TotalCapacity mustEqual 54
|
||
|
|
obj.Capacity mustEqual 54
|
||
|
|
obj.Size mustEqual 0
|
||
|
|
}
|
||
|
|
|
||
|
|
"insert item" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj.CheckCollisions(23, bullet9mmBox1) mustEqual Success(Nil)
|
||
|
|
obj += 2 -> bullet9mmBox1
|
||
|
|
obj.TotalCapacity mustEqual 54
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
obj.Size mustEqual 1
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual Some(bullet9mmBox1)
|
||
|
|
obj.Clear()
|
||
|
|
obj.Size mustEqual 0
|
||
|
|
}
|
||
|
|
|
||
|
|
"check for collision with inventory border" in {
|
||
|
|
val obj : GridInventory = GridInventory(3, 3)
|
||
|
|
//safe
|
||
|
|
obj.CheckCollisionsAsList(0, 3, 3) mustEqual Success(Nil)
|
||
|
|
//right
|
||
|
|
obj.CheckCollisionsAsList(-1, 3, 3).isFailure mustEqual true
|
||
|
|
//left
|
||
|
|
obj.CheckCollisionsAsList(1, 3, 3).isFailure mustEqual true
|
||
|
|
//bottom
|
||
|
|
obj.CheckCollisionsAsList(3, 3, 3).isFailure mustEqual true
|
||
|
|
}
|
||
|
|
|
||
|
|
"check for item collision (right insert)" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 0 -> bullet9mmBox1
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
val w = bullet9mmBox2.Tile.width
|
||
|
|
val h = bullet9mmBox2.Tile.height
|
||
|
|
obj.CheckCollisionsAsList(0, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(1, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(2, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(3, w, h) mustEqual Success(Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(0, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(1, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(2, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(3, w, h) mustEqual Success(Nil)
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"check for item collision (left insert)" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 3 -> bullet9mmBox1
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
val w = bullet9mmBox2.Tile.width
|
||
|
|
val h = bullet9mmBox2.Tile.height
|
||
|
|
obj.CheckCollisionsAsList(3, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(2, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(1, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(0, w, h) mustEqual Success(Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(3, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(2, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(1, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(0, w, h) mustEqual Success(Nil)
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"check for item collision (below insert)" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 0 -> bullet9mmBox1
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
val w = bullet9mmBox2.Tile.width
|
||
|
|
val h = bullet9mmBox2.Tile.height
|
||
|
|
obj.CheckCollisionsAsList(0, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(9, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(18, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(27, w, h) mustEqual Success(Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(0, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(9, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(18, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(27, w, h) mustEqual Success(Nil)
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"check for item collision (above insert)" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 27 -> bullet9mmBox1
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
val w = bullet9mmBox2.Tile.width
|
||
|
|
val h = bullet9mmBox2.Tile.height
|
||
|
|
obj.CheckCollisionsAsList(27, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(19, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(9, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsList(0, w, h) mustEqual Success(Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(27, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(19, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(9, w, h) mustEqual Success(1 :: Nil)
|
||
|
|
obj.CheckCollisionsAsGrid(0, w, h) mustEqual Success(Nil)
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"block insertion if item collision" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 0 -> bullet9mmBox1
|
||
|
|
obj.Capacity mustEqual 45
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual Some(bullet9mmBox1)
|
||
|
|
obj += 2 -> bullet9mmBox2
|
||
|
|
obj.hasItem(PlanetSideGUID(2)) mustEqual None
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"remove item" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj += 0 -> bullet9mmBox1
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual Some(bullet9mmBox1)
|
||
|
|
obj -= PlanetSideGUID(1)
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual None
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"unblock insertion on item removal" in {
|
||
|
|
val obj : GridInventory = GridInventory(9, 6)
|
||
|
|
obj.CheckCollisions(23, bullet9mmBox1) mustEqual Success(Nil)
|
||
|
|
obj += 23 -> bullet9mmBox1
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual Some(bullet9mmBox1)
|
||
|
|
obj.CheckCollisions(23, bullet9mmBox1) mustEqual Success(1 :: Nil)
|
||
|
|
obj -= PlanetSideGUID(1)
|
||
|
|
obj.hasItem(PlanetSideGUID(1)) mustEqual None
|
||
|
|
obj.CheckCollisions(23, bullet9mmBox1) mustEqual Success(Nil)
|
||
|
|
obj.Clear()
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"attempt to fit an item" in {
|
||
|
|
val sampleDef22 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef22.Tile = InventoryTile.Tile22
|
||
|
|
val sampleDef33 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef33.Tile = InventoryTile.Tile33
|
||
|
|
val sampleDef63 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef63.Tile = InventoryTile.Tile63
|
||
|
|
|
||
|
|
val obj : GridInventory = GridInventory(9, 9)
|
||
|
|
obj += 0 -> SimpleItem(PlanetSideGUID(0), sampleDef22)
|
||
|
|
obj += 20 -> SimpleItem(PlanetSideGUID(1), sampleDef63)
|
||
|
|
obj += 56 -> SimpleItem(PlanetSideGUID(2), sampleDef33)
|
||
|
|
obj.Fit(InventoryTile.Tile33) match {
|
||
|
|
case Some(x) =>
|
||
|
|
x mustEqual 50
|
||
|
|
case None =>
|
||
|
|
ko
|
||
|
|
}
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
|
||
|
|
"attempt to fit all the items" in {
|
||
|
|
val sampleDef1 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef1.Tile = InventoryTile.Tile22
|
||
|
|
val sampleDef2 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef2.Tile = InventoryTile.Tile33
|
||
|
|
val sampleDef3 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef3.Tile = InventoryTile.Tile42
|
||
|
|
val sampleDef4 = new SimpleItemDefinition(149)
|
||
|
|
sampleDef4.Tile = InventoryTile.Tile63
|
||
|
|
|
||
|
|
val list : ListBuffer[InventoryItem] = ListBuffer()
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(0), sampleDef2), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(1), sampleDef3), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(2), sampleDef1), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(3), sampleDef4), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(4), sampleDef1), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(5), sampleDef4), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(6), sampleDef2), -1)
|
||
|
|
list += new InventoryItem(SimpleItem(PlanetSideGUID(7), sampleDef3), -1)
|
||
|
|
val obj : GridInventory = GridInventory(9, 9)
|
||
|
|
|
||
|
|
val (elements, out) = GridInventory.recoverInventory(list.toList, obj)
|
||
|
|
elements.length mustEqual 6
|
||
|
|
out.length mustEqual 2
|
||
|
|
elements.foreach(item => {
|
||
|
|
obj.Insert(item.start, item.obj) mustEqual true
|
||
|
|
})
|
||
|
|
out.head.Definition.Tile mustEqual InventoryTile.Tile22 //did not fit
|
||
|
|
out(1).Definition.Tile mustEqual InventoryTile.Tile22 //did not fit
|
||
|
|
ok
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|