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.

286 lines
9.5 KiB

  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. for (slot, tool) in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter().enumerate() {
  16. for _ in 0..2 {
  17. entity_gateway.create_item(
  18. item::NewItemEntity {
  19. item: item::ItemDetail::Tool(
  20. item::tool::Tool {
  21. tool: tool
  22. }
  23. ),
  24. location: item::ItemLocation::Inventory {
  25. character_id: char1.id,
  26. slot: slot,
  27. equipped: false,
  28. }
  29. }).await;
  30. }
  31. }
  32. let mut ship = ShipServerState::new(entity_gateway.clone());
  33. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  34. join_lobby(&mut ship, ClientId(1)).await;
  35. create_room(&mut ship, ClientId(1), "room", "").await;
  36. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  37. client: 0,
  38. target: 0,
  39. item_id: 0x10000,
  40. })))).await.unwrap().for_each(drop);
  41. let items = entity_gateway.get_items_by_character(&char1).await;
  42. assert!(items.iter().filter(|item| {
  43. if let item::ItemDetail::Tool(t) = item.item {
  44. t.tool == item::tool::ToolType::Monomate
  45. }
  46. else {
  47. false
  48. }
  49. }).count() == 1);
  50. assert!(items.iter().filter(|item| {
  51. if let item::ItemDetail::Tool(t) = item.item {
  52. t.tool == item::tool::ToolType::Monofluid
  53. }
  54. else {
  55. false
  56. }
  57. }).count() == 2);
  58. }
  59. #[async_std::test]
  60. async fn test_use_monomate_twice() {
  61. let mut entity_gateway = InMemoryGateway::new();
  62. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  63. for (slot, tool) in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter().enumerate() {
  64. for _ in 0..3 {
  65. entity_gateway.create_item(
  66. item::NewItemEntity {
  67. item: item::ItemDetail::Tool(
  68. item::tool::Tool {
  69. tool: tool
  70. }
  71. ),
  72. location: item::ItemLocation::Inventory {
  73. character_id: char1.id,
  74. slot: slot,
  75. equipped: false,
  76. }
  77. }).await;
  78. }
  79. }
  80. let mut ship = ShipServerState::new(entity_gateway.clone());
  81. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  82. join_lobby(&mut ship, ClientId(1)).await;
  83. create_room(&mut ship, ClientId(1), "room", "").await;
  84. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  85. client: 0,
  86. target: 0,
  87. item_id: 0x10000,
  88. })))).await.unwrap().for_each(drop);
  89. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  90. client: 0,
  91. target: 0,
  92. item_id: 0x10000,
  93. })))).await.unwrap().for_each(drop);
  94. let items = entity_gateway.get_items_by_character(&char1).await;
  95. assert!(items.iter().filter(|item| {
  96. if let item::ItemDetail::Tool(t) = item.item {
  97. t.tool == item::tool::ToolType::Monomate
  98. }
  99. else {
  100. false
  101. }
  102. }).count() == 1);
  103. assert!(items.iter().filter(|item| {
  104. if let item::ItemDetail::Tool(t) = item.item {
  105. t.tool == item::tool::ToolType::Monofluid
  106. }
  107. else {
  108. false
  109. }
  110. }).count() == 3);
  111. }
  112. #[async_std::test]
  113. async fn test_use_last_monomate() {
  114. let mut entity_gateway = InMemoryGateway::new();
  115. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  116. for (slot, tool) in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter().enumerate() {
  117. for _ in 0..1 {
  118. entity_gateway.create_item(
  119. item::NewItemEntity {
  120. item: item::ItemDetail::Tool(
  121. item::tool::Tool {
  122. tool: tool
  123. }
  124. ),
  125. location: item::ItemLocation::Inventory {
  126. character_id: char1.id,
  127. slot: slot,
  128. equipped: false,
  129. }
  130. }).await;
  131. }
  132. }
  133. let mut ship = ShipServerState::new(entity_gateway.clone());
  134. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  135. join_lobby(&mut ship, ClientId(1)).await;
  136. create_room(&mut ship, ClientId(1), "room", "").await;
  137. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  138. client: 0,
  139. target: 0,
  140. item_id: 0x10000,
  141. })))).await.unwrap().for_each(drop);
  142. let items = entity_gateway.get_items_by_character(&char1).await;
  143. assert!(items.iter().filter(|item| {
  144. if let item::ItemDetail::Tool(t) = item.item {
  145. t.tool == item::tool::ToolType::Monomate
  146. }
  147. else {
  148. false
  149. }
  150. }).count() == 0);
  151. assert!(items.iter().filter(|item| {
  152. if let item::ItemDetail::Tool(t) = item.item {
  153. t.tool == item::tool::ToolType::Monofluid
  154. }
  155. else {
  156. false
  157. }
  158. }).count() == 1);
  159. }
  160. #[async_std::test]
  161. async fn test_use_nonstackable_tool() {
  162. let mut entity_gateway = InMemoryGateway::new();
  163. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  164. entity_gateway.create_item(
  165. item::NewItemEntity {
  166. item: item::ItemDetail::Tool(
  167. item::tool::Tool {
  168. tool: item::tool::ToolType::MagicStoneIritista,
  169. }
  170. ),
  171. location: item::ItemLocation::Inventory {
  172. character_id: char1.id,
  173. slot: 0,
  174. equipped: false,
  175. }
  176. }).await;
  177. let mut ship = ShipServerState::new(entity_gateway.clone());
  178. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  179. join_lobby(&mut ship, ClientId(1)).await;
  180. create_room(&mut ship, ClientId(1), "room", "").await;
  181. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  182. client: 0,
  183. target: 0,
  184. item_id: 0x10000,
  185. })))).await.unwrap().for_each(drop);
  186. let items = entity_gateway.get_items_by_character(&char1).await;
  187. assert!(items.len() == 0);
  188. }
  189. #[async_std::test]
  190. async fn test_use_materials() {
  191. let mut entity_gateway = InMemoryGateway::new();
  192. let (user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  193. for (slot, tool) in vec![item::tool::ToolType::PowerMaterial, item::tool::ToolType::LuckMaterial].into_iter().enumerate() {
  194. for _ in 0..5 {
  195. entity_gateway.create_item(
  196. item::NewItemEntity {
  197. item: item::ItemDetail::Tool(
  198. item::tool::Tool {
  199. tool: tool
  200. }
  201. ),
  202. location: item::ItemLocation::Inventory {
  203. character_id: char1.id,
  204. slot: slot,
  205. equipped: false,
  206. }
  207. }).await;
  208. }
  209. }
  210. let mut ship = ShipServerState::new(entity_gateway.clone());
  211. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  212. join_lobby(&mut ship, ClientId(1)).await;
  213. create_room(&mut ship, ClientId(1), "room", "").await;
  214. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  215. client: 0,
  216. target: 0,
  217. item_id: 0x10000,
  218. })))).await.unwrap().for_each(drop);
  219. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  220. client: 0,
  221. target: 0,
  222. item_id: 0x10001,
  223. })))).await.unwrap().for_each(drop);
  224. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  225. client: 0,
  226. target: 0,
  227. item_id: 0x10001,
  228. })))).await.unwrap().for_each(drop);
  229. let items = entity_gateway.get_items_by_character(&char1).await;
  230. assert!(items.iter().filter(|item| {
  231. if let item::ItemDetail::Tool(t) = item.item {
  232. t.tool == item::tool::ToolType::PowerMaterial
  233. }
  234. else {
  235. false
  236. }
  237. }).count() == 4);
  238. assert!(items.iter().filter(|item| {
  239. if let item::ItemDetail::Tool(t) = item.item {
  240. t.tool == item::tool::ToolType::LuckMaterial
  241. }
  242. else {
  243. false
  244. }
  245. }).count() == 3);
  246. let characters = entity_gateway.get_characters_by_user(&user1).await;
  247. let char = characters[0].as_ref().unwrap();
  248. assert!(char.materials.power == 1);
  249. assert!(char.materials.luck == 2);
  250. }