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.

296 lines
9.8 KiB

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