You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
9.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. use elseware::common::serverstate::{ClientId, ServerState};
  2. use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
  3. use elseware::entity::item;
  4. use elseware::ship::ship::{ShipServerState, RecvShipPacket};
  5. //use elseware::ship::items::{ClientItemId, ActiveItemEntityId, HeldItemType, FloorItemType};
  6. use libpso::packet::ship::*;
  7. use libpso::packet::messages::*;
  8. #[path = "common.rs"]
  9. mod common;
  10. use common::*;
  11. #[async_std::test]
  12. async fn test_use_monomate() {
  13. let mut entity_gateway = InMemoryGateway::new();
  14. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  15. let mut p1_items = Vec::new();
  16. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  17. let mut item = Vec::new();
  18. for _ in 0..2usize {
  19. item.push(entity_gateway.create_item(
  20. item::NewItemEntity {
  21. item: item::ItemDetail::Tool(
  22. item::tool::Tool {
  23. tool: tool
  24. }
  25. ),
  26. location: item::ItemLocation::Inventory {
  27. character_id: char1.id,
  28. }
  29. }).await.unwrap());
  30. }
  31. p1_items.push(item::InventoryItemEntity::Stacked(item));
  32. }
  33. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  34. let mut ship = Box::new(ShipServerState::builder()
  35. .gateway(entity_gateway.clone())
  36. .build());
  37. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  38. join_lobby(&mut ship, ClientId(1)).await;
  39. create_room(&mut ship, ClientId(1), "room", "").await;
  40. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  41. client: 0,
  42. target: 0,
  43. item_id: 0x10000,
  44. })))).await.unwrap().for_each(drop);
  45. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  46. assert_eq!(inventory_items.items.len(), 2);
  47. inventory_items.items[0].with_stacked(|items| {
  48. assert_eq!(items.len(), 1)
  49. }).unwrap();
  50. inventory_items.items[1].with_stacked(|items| {
  51. assert_eq!(items.len(), 2)
  52. }).unwrap();
  53. }
  54. #[async_std::test]
  55. async fn test_use_monomate_twice() {
  56. let mut entity_gateway = InMemoryGateway::new();
  57. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  58. let mut p1_items = Vec::new();
  59. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  60. let mut item = Vec::new();
  61. for _ in 0..3usize {
  62. item.push(entity_gateway.create_item(
  63. item::NewItemEntity {
  64. item: item::ItemDetail::Tool(
  65. item::tool::Tool {
  66. tool: tool
  67. }
  68. ),
  69. location: item::ItemLocation::Inventory {
  70. character_id: char1.id,
  71. }
  72. }).await.unwrap());
  73. }
  74. p1_items.push(item::InventoryItemEntity::Stacked(item));
  75. }
  76. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  77. let mut ship = Box::new(ShipServerState::builder()
  78. .gateway(entity_gateway.clone())
  79. .build());
  80. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  81. join_lobby(&mut ship, ClientId(1)).await;
  82. create_room(&mut ship, ClientId(1), "room", "").await;
  83. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  84. client: 0,
  85. target: 0,
  86. item_id: 0x10000,
  87. })))).await.unwrap().for_each(drop);
  88. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  89. client: 0,
  90. target: 0,
  91. item_id: 0x10000,
  92. })))).await.unwrap().for_each(drop);
  93. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  94. assert_eq!(inventory_items.items.len(), 2);
  95. inventory_items.items[0].with_stacked(|items| {
  96. assert_eq!(items.len(), 1)
  97. }).unwrap();
  98. inventory_items.items[1].with_stacked(|items| {
  99. assert_eq!(items.len(), 3)
  100. }).unwrap();
  101. }
  102. #[async_std::test]
  103. async fn test_use_last_monomate() {
  104. let mut entity_gateway = InMemoryGateway::new();
  105. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  106. let mut p1_inv = Vec::new();
  107. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  108. p1_inv.push(item::InventoryItemEntity::Stacked(vec![entity_gateway.create_item(
  109. item::NewItemEntity {
  110. item: item::ItemDetail::Tool(
  111. item::tool::Tool {
  112. tool: tool
  113. }
  114. ),
  115. location: item::ItemLocation::Inventory {
  116. character_id: char1.id,
  117. }
  118. }).await.unwrap()]));
  119. }
  120. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  121. let mut ship = Box::new(ShipServerState::builder()
  122. .gateway(entity_gateway.clone())
  123. .build());
  124. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  125. join_lobby(&mut ship, ClientId(1)).await;
  126. create_room(&mut ship, ClientId(1), "room", "").await;
  127. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  128. client: 0,
  129. target: 0,
  130. item_id: 0x10000,
  131. })))).await.unwrap().for_each(drop);
  132. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  133. assert_eq!(inventory_items.items.len(), 1);
  134. inventory_items.items[0].with_stacked(|items| {
  135. assert_eq!(items.len(), 1);
  136. assert_eq!(items[0].item.item_type(), item::ItemType::Tool(item::tool::ToolType::Monofluid));
  137. }).unwrap();
  138. }
  139. #[async_std::test]
  140. async fn test_use_nonstackable_tool() {
  141. let mut entity_gateway = InMemoryGateway::new();
  142. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  143. let mut p1_items = Vec::new();
  144. p1_items.push(entity_gateway.create_item(
  145. item::NewItemEntity {
  146. item: item::ItemDetail::Tool(
  147. item::tool::Tool {
  148. tool: item::tool::ToolType::MagicStoneIritista,
  149. }
  150. ),
  151. location: item::ItemLocation::Inventory {
  152. character_id: char1.id,
  153. }
  154. }).await.unwrap());
  155. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  156. let mut ship = Box::new(ShipServerState::builder()
  157. .gateway(entity_gateway.clone())
  158. .build());
  159. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  160. join_lobby(&mut ship, ClientId(1)).await;
  161. create_room(&mut ship, ClientId(1), "room", "").await;
  162. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  163. client: 0,
  164. target: 0,
  165. item_id: 0x10000,
  166. })))).await.unwrap().for_each(drop);
  167. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  168. assert_eq!(inventory_items.items.len(), 0);
  169. }
  170. #[async_std::test]
  171. async fn test_use_materials() {
  172. let mut entity_gateway = InMemoryGateway::new();
  173. let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  174. let mut p1_inv = Vec::new();
  175. for tool in vec![item::tool::ToolType::PowerMaterial, item::tool::ToolType::LuckMaterial].into_iter() {
  176. let mut item = Vec::new();
  177. for _ in 0..5usize {
  178. item.push(entity_gateway.create_item(
  179. item::NewItemEntity {
  180. item: item::ItemDetail::Tool(
  181. item::tool::Tool {
  182. tool: tool
  183. }
  184. ),
  185. location: item::ItemLocation::Inventory {
  186. character_id: char1.id,
  187. }
  188. }).await.unwrap());
  189. }
  190. p1_inv.push(item::InventoryItemEntity::Stacked(item));
  191. }
  192. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
  193. let mut ship = Box::new(ShipServerState::builder()
  194. .gateway(entity_gateway.clone())
  195. .build());
  196. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  197. join_lobby(&mut ship, ClientId(1)).await;
  198. create_room(&mut ship, ClientId(1), "room", "").await;
  199. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  200. client: 0,
  201. target: 0,
  202. item_id: 0x10000,
  203. })))).await.unwrap().for_each(drop);
  204. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  205. client: 0,
  206. target: 0,
  207. item_id: 0x10001,
  208. })))).await.unwrap().for_each(drop);
  209. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  210. client: 0,
  211. target: 0,
  212. item_id: 0x10001,
  213. })))).await.unwrap().for_each(drop);
  214. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  215. assert_eq!(inventory_items.items.len(), 2);
  216. inventory_items.items[0].with_stacked(|items| {
  217. assert_eq!(items.len(), 4)
  218. }).unwrap();
  219. inventory_items.items[1].with_stacked(|items| {
  220. assert_eq!(items.len(), 3)
  221. }).unwrap();
  222. let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
  223. let char = characters[0].as_ref().unwrap();
  224. assert!(char.materials.power == 1);
  225. assert!(char.materials.luck == 2);
  226. }