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.

235 lines
8.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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, SendShipPacket};
  5. use elseware::entity::character::{CharacterClass, SectionID};
  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_mag_feed() {
  13. let mut entity_gateway = InMemoryGateway::new();
  14. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  15. let mag = entity_gateway.create_item(
  16. item::NewItemEntity {
  17. item: item::ItemDetail::Mag(
  18. item::mag::Mag::baby_mag(0)
  19. ),
  20. location: item::ItemLocation::Inventory {
  21. character_id: char1.id,
  22. //equipped: true,
  23. }
  24. }).await.unwrap();
  25. let mut monomates = Vec::new();
  26. for _ in 0..7usize {
  27. monomates.push(entity_gateway.create_item(
  28. item::NewItemEntity {
  29. item: item::ItemDetail::Tool(
  30. item::tool::Tool {
  31. tool: item::tool::ToolType::Monomate,
  32. }
  33. ),
  34. location: item::ItemLocation::Inventory {
  35. character_id: char1.id,
  36. }
  37. }).await.unwrap());
  38. }
  39. let equipped = item::EquippedEntity {
  40. weapon: None,
  41. armor: None,
  42. shield: None,
  43. unit: [None; 4],
  44. mag: Some(mag.id),
  45. };
  46. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  47. let mut inventory = Vec::new();
  48. inventory.push(mag.into());
  49. inventory.push(item::InventoryItemEntity::Stacked(monomates));
  50. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
  51. let mut ship = Box::new(ShipServerState::builder()
  52. .gateway(entity_gateway.clone())
  53. .build());
  54. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  55. join_lobby(&mut ship, ClientId(1)).await;
  56. create_room(&mut ship, ClientId(1), "room", "").await;
  57. for _ in 0..7usize {
  58. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(PlayerFeedMag {
  59. client: 0,
  60. target: 0,
  61. mag_id: 0x10000,
  62. item_id: 0x10001,
  63. })))).await.unwrap().for_each(drop);
  64. }
  65. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  66. assert_eq!(inventory_items.items.len(), 1);
  67. inventory_items.items[0].with_individual(|item| {
  68. match &item.item {
  69. item::ItemDetail::Mag(mag) => {
  70. println!("mag! {:?}", mag);
  71. assert!(mag.level() == 7);
  72. assert!(mag.def() == 5);
  73. assert!(mag.pow() == 2);
  74. assert!(mag.dex() == 0);
  75. assert!(mag.mind() == 0);
  76. }
  77. _ => panic!()
  78. }
  79. }).unwrap();
  80. }
  81. #[async_std::test]
  82. async fn test_mag_change_owner() {
  83. let mut entity_gateway = InMemoryGateway::new();
  84. let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  85. let (_user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
  86. char1.char_class = CharacterClass::RAmarl;
  87. char1.section_id = SectionID::Redria;
  88. entity_gateway.save_character(&char1).await.unwrap();
  89. char2.char_class = CharacterClass::FOmarl;
  90. char2.section_id = SectionID::Whitill;
  91. entity_gateway.save_character(&char2).await.unwrap();
  92. let mag = entity_gateway.create_item(
  93. item::NewItemEntity {
  94. item: item::ItemDetail::Mag(
  95. item::mag::Mag::baby_mag(0)
  96. ),
  97. location: item::ItemLocation::Inventory {
  98. character_id: char1.id,
  99. }
  100. }).await.unwrap();
  101. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![mag])).await.unwrap();
  102. let mut ship = Box::new(ShipServerState::builder()
  103. .gateway(entity_gateway.clone())
  104. .build());
  105. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  106. log_in_char(&mut ship, ClientId(2), "a2", "a").await;
  107. join_lobby(&mut ship, ClientId(1)).await;
  108. join_lobby(&mut ship, ClientId(2)).await;
  109. create_room(&mut ship, ClientId(1), "room", "").await;
  110. join_room(&mut ship, ClientId(2), 0).await;
  111. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
  112. client: 0,
  113. target: 0,
  114. unknown1: 0,
  115. map_area: 0,
  116. item_id: 0x10000,
  117. x: 0.0,
  118. y: 0.0,
  119. z: 0.0,
  120. })))).await.unwrap().for_each(drop);
  121. ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
  122. client: 0,
  123. target: 0,
  124. item_id: 0x10000,
  125. map_area: 0,
  126. unknown: [0; 3]
  127. })))).await.unwrap().for_each(drop);
  128. let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
  129. assert_eq!(inventory_items.items.len(), 1);
  130. inventory_items.items[0].with_individual(|item| {
  131. match &item.item {
  132. item::ItemDetail::Mag(mag) => {
  133. assert!(mag.class == CharacterClass::FOmarl);
  134. assert!(mag.id == SectionID::Whitill);
  135. }
  136. _ => panic!()
  137. }
  138. }).unwrap();
  139. }
  140. #[async_std::test]
  141. async fn test_mag_cell() {
  142. let mut entity_gateway = InMemoryGateway::new();
  143. let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
  144. let mag = entity_gateway.create_item(
  145. item::NewItemEntity {
  146. item: item::ItemDetail::Mag(
  147. item::mag::Mag::baby_mag(0)
  148. ),
  149. location: item::ItemLocation::Inventory {
  150. character_id: char1.id,
  151. }
  152. }).await.unwrap();
  153. for _ in 0..1000usize {
  154. let fed_tool = entity_gateway.create_item(
  155. item::NewItemEntity {
  156. item: item::ItemDetail::Tool (
  157. item::tool::Tool {
  158. tool: item::tool::ToolType::Monomate,
  159. }
  160. ),
  161. location: item::ItemLocation::FedToMag {
  162. mag: mag.id,
  163. }
  164. }).await.unwrap();
  165. entity_gateway.feed_mag(&mag.id, &fed_tool.id).await.unwrap();
  166. }
  167. let mag_cell = entity_gateway.create_item(
  168. item::NewItemEntity {
  169. item: item::ItemDetail::Tool(
  170. item::tool::Tool {
  171. tool: item::tool::ToolType::CellOfMag502,
  172. }
  173. ),
  174. location: item::ItemLocation::Inventory {
  175. character_id: char1.id,
  176. }
  177. }).await.unwrap();
  178. let equipped = item::EquippedEntity {
  179. weapon: None,
  180. armor: None,
  181. shield: None,
  182. unit: [None; 4],
  183. mag: Some(mag.id),
  184. };
  185. entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
  186. entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![mag, mag_cell])).await.unwrap();
  187. let mut ship = Box::new(ShipServerState::builder()
  188. .gateway(entity_gateway.clone())
  189. .build());
  190. log_in_char(&mut ship, ClientId(1), "a1", "a").await;
  191. join_lobby(&mut ship, ClientId(1)).await;
  192. create_room(&mut ship, ClientId(1), "room", "").await;
  193. ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerUseItem(PlayerUseItem {
  194. client: 0,
  195. target: 0,
  196. item_id: 0x10001,
  197. })))).await.unwrap().for_each(drop);
  198. let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
  199. inventory_items.items[0].with_individual(|item| {
  200. match &item.item {
  201. item::ItemDetail::Mag(mag) => {
  202. assert_eq!(mag.mag, item::mag::MagType::Soniti);
  203. }
  204. _ => panic!()
  205. }
  206. }).unwrap();
  207. }