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.

365 lines
14 KiB

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