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.

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