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.

334 lines
13 KiB

  1. use networking::serverstate::{ClientId, ServerState};
  2. use entity::gateway::{EntityGateway, InMemoryGateway};
  3. use entity::item;
  4. use libpso::item::weapon;
  5. use ship_server::RecvShipPacket;
  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_after_leaving_and_rejoining_room() {
  13. let mut entity_gateway = InMemoryGateway::default();
  14. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  15. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  16. let mut p1_items = Vec::new();
  17. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  18. let mut item = Vec::new();
  19. for _ in 0..2usize {
  20. item.push(entity_gateway.create_item(
  21. ItemBuilder::tool(tool)
  22. .as_new()
  23. ).await.unwrap());
  24. }
  25. p1_items.push(item::InventoryItemEntity::Stacked(item));
  26. }
  27. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  28. let mut p2_items = Vec::new();
  29. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  30. let mut item = Vec::new();
  31. for _ in 0..2usize {
  32. item.push(entity_gateway.create_item(
  33. ItemBuilder::tool(tool)
  34. .as_new()
  35. ).await.unwrap());
  36. }
  37. p2_items.push(item::InventoryItemEntity::Stacked(item));
  38. }
  39. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_items)).await.unwrap();
  40. let mut ship = standard_ship(entity_gateway.clone());
  41. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  42. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  43. join_lobby(&mut ship, ClientId(1)).await;
  44. join_lobby(&mut ship, ClientId(2)).await;
  45. create_room(&mut ship, ClientId(1), "room", "").await;
  46. join_room(&mut ship, ClientId(2), 0).await;
  47. leave_room(&mut ship, ClientId(2)).await;
  48. join_room(&mut ship, ClientId(2), 0).await;
  49. leave_room(&mut ship, ClientId(2)).await;
  50. join_room(&mut ship, ClientId(2), 0).await;
  51. leave_room(&mut ship, ClientId(2)).await;
  52. join_room(&mut ship, ClientId(2), 0).await;
  53. leave_room(&mut ship, ClientId(1)).await;
  54. join_room(&mut ship, ClientId(1), 0).await;
  55. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  56. client: 0,
  57. target: 0,
  58. item_id: 0x10003,
  59. })))).await.unwrap();
  60. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  61. client: 0,
  62. target: 0,
  63. item_id: 0x210006,
  64. })))).await.unwrap();
  65. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  66. assert_eq!(inventory_items.items.len(), 2);
  67. inventory_items.items[0].with_stacked(|items| {
  68. assert_eq!(items.len(), 2)
  69. }).unwrap();
  70. inventory_items.items[1].with_stacked(|items| {
  71. assert_eq!(items.len(), 1)
  72. }).unwrap();
  73. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  74. assert_eq!(inventory_items.items.len(), 2);
  75. inventory_items.items[0].with_stacked(|items| {
  76. assert_eq!(items.len(), 1)
  77. }).unwrap();
  78. inventory_items.items[1].with_stacked(|items| {
  79. assert_eq!(items.len(), 2)
  80. }).unwrap();
  81. }
  82. #[async_std::test]
  83. async fn test_using_some_monomates_after_a_convoluted_series_of_leaves_and_joins() {
  84. let mut entity_gateway = InMemoryGateway::default();
  85. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  86. let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  87. let (_user3, char3) = new_user_character(&mut entity_gateway, "a3", "a").await;
  88. let mut p1_items = Vec::new();
  89. for tool in vec![item::tool::ToolType::Monofluid, item::tool::ToolType::Difluid, item::tool::ToolType::Trifluid].into_iter() {
  90. let mut item = Vec::new();
  91. for _ in 0..2usize {
  92. item.push(entity_gateway.create_item(
  93. ItemBuilder::tool(tool)
  94. .as_new()
  95. ).await.unwrap());
  96. }
  97. p1_items.push(item::InventoryItemEntity::Stacked(item));
  98. }
  99. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  100. let mut p2_items = Vec::new();
  101. for tool in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter() {
  102. let mut item = Vec::new();
  103. for _ in 0..6usize {
  104. item.push(entity_gateway.create_item(
  105. ItemBuilder::tool(tool)
  106. .as_new()
  107. ).await.unwrap());
  108. }
  109. p2_items.push(item::InventoryItemEntity::Stacked(item));
  110. }
  111. entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_items)).await.unwrap();
  112. let mut p3_items = Vec::new();
  113. for _ in 0..5usize {
  114. p3_items.push(
  115. item::InventoryItemEntity::Individual(
  116. entity_gateway.create_item(
  117. ItemBuilder::weapon(weapon::WeaponType::Saber)
  118. .as_new()
  119. ).await.unwrap()
  120. ));
  121. }
  122. entity_gateway.set_character_inventory(&char3.id, &item::InventoryEntity::new(p3_items)).await.unwrap();
  123. let mut ship = standard_ship(entity_gateway.clone());
  124. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  125. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  126. log_in_char(&mut ship, ClientId(3), "a3", "a").await;
  127. join_lobby(&mut ship, ClientId(1)).await;
  128. join_lobby(&mut ship, ClientId(2)).await;
  129. join_lobby(&mut ship, ClientId(3)).await;
  130. // so lets trace the item_ids here as it is dumb:
  131. create_room(&mut ship, ClientId(1), "room", "").await;
  132. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  133. // g2 : ; 0x210000
  134. // g3 : ; 0x410000
  135. join_room(&mut ship, ClientId(2), 0).await;
  136. // g1/p1: 0x010000 0x010001 0x010002 ; 0x10003
  137. // g2/p2: 0x210000 0x210001 ; 0x210002
  138. // g3 : ; 0x410000
  139. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  140. client: 0,
  141. target: 0,
  142. item_id: 0x210000,
  143. })))).await.unwrap();
  144. join_room(&mut ship, ClientId(3), 0).await;
  145. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  146. // g2/p2: 0x210000 0x210001 ; 0x210002
  147. // g3/p3: 0x410000 0x410001 0x410002 0x410003 0x0410004 ; 0x410005
  148. leave_room(&mut ship, ClientId(2)).await;
  149. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  150. // g2 : ; 0x210002
  151. // g3/p3: 0x410000 0x410001 0x410002 0x410003 0x410004 ; 0x410005
  152. join_room(&mut ship, ClientId(2), 0).await;
  153. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  154. // g2/p2: 0x210002 0x210003 ; 0x210004
  155. // g3/p3: 0x410000 0x410001 0x410002 0x410003 0x410004 ; 0x410005
  156. leave_room(&mut ship, ClientId(2)).await;
  157. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  158. // g2 : ; 0x210004
  159. // g3/p3: 0x410000 0x410001 0x410002 0x410003 0x410004 ; 0x410005
  160. leave_room(&mut ship, ClientId(3)).await;
  161. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  162. // g2 : ; 0x210004
  163. // g3 : ; 0x410005
  164. join_room(&mut ship, ClientId(3), 0).await;
  165. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  166. // g2/p3: 0x210004 0x210005 0x210006 0x210007 0x210008 ; 0x210009
  167. // g3 : ; 0x410007
  168. join_room(&mut ship, ClientId(2), 0).await;
  169. // g1/p1: 0x010000 0x010001 0x010002 ; 0x010003
  170. // g2/p3: 0x210004 0x210005 0x210006 0x210007 0x210008 ; 0x210009
  171. // g3/p2: 0x410005 0x410006 ; 0x410007
  172. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  173. client: 0,
  174. target: 0,
  175. item_id: 0x410005,
  176. })))).await.unwrap();
  177. leave_room(&mut ship, ClientId(1)).await;
  178. leave_room(&mut ship, ClientId(2)).await;
  179. // g1 : ; 0x010003
  180. // g2/p3: 0x210004 0x210005 0x210006 0x210007 0x210008 ; 0x210009
  181. // g3 : ; 0x410007
  182. join_room(&mut ship, ClientId(2), 0).await;
  183. // g1/p2: 0x010003 0x010004 ; 0x010005
  184. // g2/p3: 0x210004 0x210005 0x210006 0x210007 0x210008 ; 0x210009
  185. // g3 : ; 0x410007
  186. join_room(&mut ship, ClientId(1), 0).await;
  187. // g1/p2: 0x010003 0x010004 ; 0x010005
  188. // g2/p3: 0x210004 0x210005 0x210006 0x210007 0x210008 ; 0x210009
  189. // g3/p1: 0x410008 0x410009 0x41000A ; 0x41000B
  190. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  191. client: 0,
  192. target: 0,
  193. item_id: 0x010003,
  194. })))).await.unwrap();
  195. leave_room(&mut ship, ClientId(2)).await;
  196. leave_room(&mut ship, ClientId(3)).await;
  197. join_room(&mut ship, ClientId(3), 0).await;
  198. join_room(&mut ship, ClientId(2), 0).await;
  199. // g1/p3: 0x010005 0x010006 0x010007 0x010008 0x010009 ; 0x010009
  200. // g2/p2: 0x210009 0x21000A ; 0x21000B
  201. // g3/p1: 0x410008 0x410009 0x41000A ; 0x41000B
  202. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  203. client: 0,
  204. target: 0,
  205. item_id: 0x210009,
  206. })))).await.unwrap();
  207. leave_room(&mut ship, ClientId(2)).await;
  208. join_room(&mut ship, ClientId(2), 0).await;
  209. // g1/p3: 0x010005 0x010006 0x010007 0x010008 0x010009 ; 0x010009
  210. // g2/p2: 0x21000B 0x21000C ; 0x21000D
  211. // g3/p1: 0x410008 0x410009 0x401000A ; 0x41000B
  212. ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  213. client: 0,
  214. target: 0,
  215. item_id: 0x21000B,
  216. })))).await.unwrap();
  217. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  218. assert_eq!(inventory_items.items.len(), 2);
  219. inventory_items.items[0].with_stacked(|items| {
  220. assert_eq!(items.len(), 1)
  221. }).unwrap();
  222. inventory_items.items[1].with_stacked(|items| {
  223. assert_eq!(items.len(), 6)
  224. }).unwrap();
  225. }
  226. #[async_std::test]
  227. async fn test_depositing_a_full_stack_then_withdrawing_part() {
  228. let mut entity_gateway = InMemoryGateway::default();
  229. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  230. let mut p1_items = Vec::new();
  231. for tool in vec![item::tool::ToolType::Monofluid, item::tool::ToolType::Difluid, item::tool::ToolType::Trifluid].into_iter() {
  232. let mut item = Vec::new();
  233. for _ in 0..5usize {
  234. item.push(entity_gateway.create_item(
  235. ItemBuilder::tool(tool)
  236. .as_new()
  237. ).await.unwrap());
  238. }
  239. p1_items.push(item::InventoryItemEntity::Stacked(item));
  240. }
  241. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
  242. let mut monomates = Vec::new();
  243. for _ in 0..3usize {
  244. monomates.push(entity_gateway.create_item(
  245. ItemBuilder::tool(item::tool::ToolType::Monomate)
  246. .as_new()
  247. ).await.unwrap());
  248. }
  249. entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), &item::BankIdentifier::Character).await.unwrap();
  250. let mut ship = standard_ship(entity_gateway.clone());
  251. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  252. join_lobby(&mut ship, ClientId(1)).await;
  253. create_room(&mut ship, ClientId(1), "room", "").await;
  254. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
  255. client: 0,
  256. target: 0,
  257. unknown: 0,
  258. })))).await.unwrap();
  259. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  260. client: 0,
  261. target: 0,
  262. item_id: 0x10001,
  263. action: 0,
  264. item_amount: 5,
  265. meseta_amount: 0,
  266. unknown: 0,
  267. })))).await.unwrap();
  268. ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
  269. client: 0,
  270. target: 0,
  271. item_id: 0x10001,
  272. action: 1,
  273. item_amount: 3,
  274. meseta_amount: 0,
  275. unknown: 0,
  276. })))).await.unwrap();
  277. ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  278. client: 0,
  279. target: 0,
  280. item_id: 0x20001,
  281. })))).await.unwrap();
  282. }