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.

371 lines
15 KiB

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