use elseware::common::serverstate::{ClientId, ServerState}; use elseware::entity::character::{CharacterClass}; use elseware::entity::gateway::{EntityGateway, InMemoryGateway}; use elseware::common::leveltable::CharacterLevelTable; use elseware::ship::ship::{ShipServerState, SendShipPacket, RecvShipPacket}; use elseware::ship::monster::MonsterType; use elseware::entity::item; use elseware::ship::room::{Difficulty}; use libpso::packet::ship::*; use libpso::packet::messages::*; #[path = "common.rs"] mod common; use common::*; #[async_std::test] async fn test_character_gains_exp() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let (enemy_id, exp) = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let (enemy_id, map_enemy) = (0..).filter_map(|i| { room.maps.enemy_by_id(i).map(|enemy| { (i, enemy) }).ok() }).next().unwrap(); let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap(); (enemy_id, map_enemy_stats.exp) }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp{ client: enemy_id as u8, target: 16, enemy_id: enemy_id as u16, client_id: 0, unused: 0, last_hitter: 1, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(exp == c1.character.exp); } #[async_std::test] async fn test_character_levels_up() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; char1.exp = 49; entity_gateway.save_character(&char1).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); (0..).filter_map(|i| { room.maps.enemy_by_id(i).map(|_| { i }).ok() }).next().unwrap() }; let levelup_pkt = ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp{ client: enemy_id as u8, target: 16, enemy_id: enemy_id as u16, client_id: 0, unused: 0, last_hitter: 1, })))).await.unwrap().collect::>(); assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 2, ..})}))); let leveltable = CharacterLevelTable::default(); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(leveltable.get_level_from_exp(c1.character.char_class, c1.character.exp) == 2); } #[async_std::test] async fn test_character_levels_up_multiple_times() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let (enemy_id, exp) = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let (enemy_id, map_enemy) = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::DarkFalz2 { Some((i, enemy)) } else { None } }) }).next().unwrap(); let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap(); (enemy_id, map_enemy_stats.exp) }; let levelup_pkt = ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp{ client: enemy_id as u8, target: 16, enemy_id: enemy_id as u16, client_id: 0, unused: 0, last_hitter: 1, })))).await.unwrap().collect::>(); assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 8, ..})}))); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(exp == c1.character.exp); } #[async_std::test] async fn test_one_character_gets_full_exp_and_other_attacker_gets_partial() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, _char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user2, _char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await; let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(1)).await; join_lobby(&mut ship, ClientId(2)).await; create_room(&mut ship, ClientId(1), "room", "").await; join_room(&mut ship, ClientId(2), 0).await; let (enemy_id, exp) = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let (enemy_id, map_enemy) = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::DarkFalz2 { Some((i, enemy)) } else { None } }) }).next().unwrap(); let map_enemy_stats = room.monster_stats.get(&map_enemy.monster).unwrap(); (enemy_id, map_enemy_stats.exp) }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp{ client: enemy_id as u8, target: 16, enemy_id: enemy_id as u16, client_id: 0, unused: 0, last_hitter: 1, })))).await.unwrap().for_each(drop); ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp{ client: enemy_id as u8, target: 16, enemy_id: enemy_id as u16, client_id: 0, unused: 0, last_hitter: 0, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); let c2 = ship.clients.get(&ClientId(2)).unwrap(); assert!(c1.character.exp == exp); assert!(c2.character.exp == (exp as f32 * 0.8) as u32); } #[async_std::test] async fn test_exp_steal_min_1() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 1); } #[async_std::test] async fn test_exp_steal_max_80() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; char1.exp = 80000000; char1.char_class = CharacterClass::HUcast; entity_gateway.save_character(&char1).await.unwrap(); let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 80000080); } #[async_std::test] async fn test_exp_steal_android_boost_in_ultimate() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; char1.exp = 80000000; char1.char_class = CharacterClass::HUcast; entity_gateway.save_character(&char1).await.unwrap(); let (_user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await; char2.exp = 80000000; char2.char_class = CharacterClass::HUmar; entity_gateway.save_character(&char2).await.unwrap(); let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut p2_inv = Vec::new(); p2_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p2_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char2.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(2)).await; join_room(&mut ship, ClientId(2), 0).await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); let c2 = ship.clients.get(&ClientId(2)).unwrap(); println!("c1 exp: {:?}, c2 exp: {:?}", c1.character.exp, c2.character.exp); assert!(c1.character.exp == 80000080); assert!(c2.character.exp == 80000032); } #[async_std::test] async fn test_exp_steal_no_android_boost_in_vhard() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; char1.exp = 80000000; char1.char_class = CharacterClass::HUcast; entity_gateway.save_character(&char1).await.unwrap(); let (_user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await; char2.exp = 80000000; char2.char_class = CharacterClass::HUmar; entity_gateway.save_character(&char2).await.unwrap(); let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut p2_inv = Vec::new(); p2_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p2_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char2.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::VeryHard).await; log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(2)).await; join_room(&mut ship, ClientId(2), 0).await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); let c2 = ship.clients.get(&ClientId(2)).unwrap(); println!("c1 exp: {:?}, c2 exp: {:?}", c1.character.exp, c2.character.exp); assert!(c1.character.exp == 80000010); assert!(c2.character.exp == 80000010); } #[async_std::test] async fn test_exp_steal_multihit_penalty() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; char1.exp = 80000000; char1.char_class = CharacterClass::HUcast; entity_gateway.save_character(&char1).await.unwrap(); let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Dagger, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Mechgun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 80000040); // change equipped item ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerEquipItem(PlayerEquipItem { client: 0, target: 0, item_id: 0x10001, sub_menu: 9, unknown1: 0, })))).await.unwrap().for_each(drop); ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 80000066); } #[async_std::test] async fn test_cannot_steal_exp_from_boss() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Dragon { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 0); } #[async_std::test] async fn test_exp_steal_doesnt_exceed_100p() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room(&mut ship, ClientId(1), "room", "").await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; for _ in 0..10 { ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); } let c1 = ship.clients.get(&ClientId(1)).unwrap(); assert!(c1.character.exp == 5); } #[async_std::test] async fn test_each_client_can_steal_full_exp_from_same_enemy() { let mut entity_gateway = InMemoryGateway::default(); let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await; let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await; entity_gateway.save_character(&char1).await.unwrap(); entity_gateway.save_character(&char2).await.unwrap(); let mut p1_inv = Vec::new(); p1_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p1_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap(); let mut p2_inv = Vec::new(); p2_inv.push(entity_gateway.create_item( item::NewItemEntity { item: item::ItemDetail::Weapon( item::weapon::Weapon { weapon: item::weapon::WeaponType::Raygun, grind: 5, special: Some(item::weapon::WeaponSpecial::Kings), attrs: [Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Hit, value: 100}), Some(item::weapon::WeaponAttribute{attr: item::weapon::Attribute::Dark, value: 30}), None,], tekked: true, } ), }).await.unwrap()); let equipped = item::EquippedEntity { weapon: Some(p2_inv[0].id), armor: None, shield: None, unit: [None; 4], mag: None, }; entity_gateway.set_character_equips(&char2.id, &equipped).await.unwrap(); entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap(); let mut ship = Box::new(ShipServerState::builder() .gateway(entity_gateway.clone()) .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Normal).await; log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(2)).await; join_room(&mut ship, ClientId(2), 0).await; let enemy_id = { let room = ship.blocks.0[0].rooms[0].as_ref().unwrap(); let enemy_id = (0..).filter_map(|i| { room.maps.enemy_by_id(i).ok().and_then(|enemy| { if enemy.monster == MonsterType::Booma { Some(i) } else { None } }) }).next().unwrap(); enemy_id }; for _ in 0..10 { ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::ExperienceSteal(ExperienceSteal{ client: 0, target: 0, client2: enemy_id as u8, target2: 16, enemy_id: enemy_id as u16, })))).await.unwrap().for_each(drop); } let c1 = ship.clients.get(&ClientId(1)).unwrap(); let c2 = ship.clients.get(&ClientId(2)).unwrap(); println!("c1 exp: {:?}, c2 exp: {:?}", c1.character.exp, c2.character.exp); assert!(c1.character.exp == 5); assert!(c2.character.exp == 5); }