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.

333 lines
13 KiB

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