2020-10-31 23:18:25 -06:00
use elseware ::common ::serverstate ::{ ClientId , ServerState } ;
use elseware ::entity ::gateway ::{ EntityGateway , InMemoryGateway } ;
use elseware ::entity ::item ;
use elseware ::ship ::ship ::{ ShipServerState , RecvShipPacket , SendShipPacket } ;
2022-10-18 04:46:21 -06:00
use elseware ::ship ::location ::RoomId ;
2020-10-31 23:18:25 -06:00
use libpso ::packet ::ship ::* ;
2022-02-19 20:29:46 +00:00
//use libpso::packet::messages::*;
2020-10-31 23:18:25 -06:00
#[ path = " common.rs " ]
mod common ;
use common ::* ;
#[ async_std::test ]
async fn test_item_ids_reset_when_rejoining_rooms ( ) {
2021-06-18 19:52:33 -06:00
let mut entity_gateway = InMemoryGateway ::default ( ) ;
2020-10-31 23:18:25 -06:00
2022-02-19 20:29:46 +00:00
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 ;
2020-10-31 23:18:25 -06:00
2020-11-05 16:36:39 -07:00
let mut p1_inv = Vec ::new ( ) ;
for _ in 0 .. 3 usize {
p1_inv . push ( entity_gateway . create_item (
2020-10-31 23:18:25 -06:00
item ::NewItemEntity {
item : item ::ItemDetail ::Weapon (
item ::weapon ::Weapon {
weapon : item ::weapon ::WeaponType ::Saber ,
grind : 0 ,
special : None ,
attrs : [ None , None , None ] ,
tekked : true ,
}
) ,
2020-11-05 16:36:39 -07:00
} ) . await . unwrap ( ) ) ;
2020-10-31 23:18:25 -06:00
}
2020-11-05 16:36:39 -07:00
let mut p2_inv = Vec ::new ( ) ;
for _ in 0 .. 10 usize {
p2_inv . push ( entity_gateway . create_item (
2020-10-31 23:18:25 -06:00
item ::NewItemEntity {
item : item ::ItemDetail ::Weapon (
item ::weapon ::Weapon {
weapon : item ::weapon ::WeaponType ::Saber ,
grind : 0 ,
special : None ,
attrs : [ None , None , None ] ,
tekked : true ,
}
) ,
2020-11-05 16:36:39 -07:00
} ) . await . unwrap ( ) ) ;
2020-10-31 23:18:25 -06:00
}
2020-11-05 16:36:39 -07:00
entity_gateway . set_character_inventory ( & char1 . id , & item ::InventoryEntity ::new ( p1_inv ) ) . await . unwrap ( ) ;
entity_gateway . set_character_inventory ( & char2 . id , & item ::InventoryEntity ::new ( p2_inv ) ) . await . unwrap ( ) ;
let mut ship = Box ::new ( ShipServerState ::builder ( )
2020-10-31 23:18:25 -06:00
. gateway ( entity_gateway . clone ( ) )
2020-11-05 16:36:39 -07:00
. build ( ) ) ;
2020-10-31 23:18:25 -06:00
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 ;
2022-10-18 04:46:21 -06:00
let p = ship . handle ( ClientId ( 2 ) , RecvShipPacket ::MenuSelect ( MenuSelect {
2020-10-31 23:18:25 -06:00
menu : ROOM_MENU_ID ,
item : 0 ,
2022-10-18 04:46:21 -06:00
} ) ) . await . unwrap ( ) ;
ship . handle ( ClientId ( 2 ) , RecvShipPacket ::DoneBursting ( DoneBursting { } ) ) . await . unwrap ( ) ;
2020-10-31 23:18:25 -06:00
match & p [ 1 ] . 1 {
SendShipPacket ::AddToRoom ( add_to ) = > {
println! ( " addto {:?} " , add_to ) ;
assert_eq! ( add_to . playerinfo . inventory . items . iter ( ) . map ( | k | k . item_id ) . collect ::< Vec < _ > > ( ) ,
vec! [ 0x210000 , 0x210001 , 0x210002 , 0x210003 , 0x210004 , 0x210005 , 0x210006 , 0x210007 , 0x210008 , 0x210009 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
} ,
_ = > panic! ( ) ,
}
leave_room ( & mut ship , ClientId ( 2 ) ) . await ;
2022-10-18 04:46:21 -06:00
let p = ship . handle ( ClientId ( 2 ) , RecvShipPacket ::MenuSelect ( MenuSelect {
2020-10-31 23:18:25 -06:00
menu : ROOM_MENU_ID ,
item : 0 ,
2022-10-18 04:46:21 -06:00
} ) ) . await . unwrap ( ) ;
2020-10-31 23:18:25 -06:00
match & p [ 1 ] . 1 {
SendShipPacket ::AddToRoom ( add_to ) = > {
assert_eq! ( add_to . playerinfo . inventory . items . iter ( ) . map ( | k | k . item_id ) . collect ::< Vec < _ > > ( ) ,
vec! [ 0x210000 , 0x210001 , 0x210002 , 0x210003 , 0x210004 , 0x210005 , 0x210006 , 0x210007 , 0x210008 , 0x210009 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
} ,
_ = > panic! ( ) ,
}
}
2021-07-26 01:21:29 +00:00
2023-01-29 00:56:56 -07:00
/*
2021-07-26 01:21:29 +00:00
#[ async_std::test ]
async fn test_load_rare_monster_default_appear_rates ( ) {
let mut entity_gateway = InMemoryGateway ::default ( ) ;
2022-02-19 20:29:46 +00:00
let ( _user1 , _char1 ) = new_user_character ( & mut entity_gateway , " a1 " , " a " , 1 ) . await ;
2021-07-26 01:21:29 +00:00
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 ;
// assume episode 1
2022-10-18 04:46:21 -06:00
ship . blocks . 0 [ 0 ] . rooms . with ( RoomId ( 0 ) , | room | Box ::pin ( async move {
let rates = & * room . rare_monster_table ;
for ( _monster , rate ) in rates . clone ( ) . appear_rate {
assert_eq! ( rate , 0.001953125 f32 ) ; // 1/512 = 0.001953125
}
} ) ) . await . unwrap ( ) ;
2022-02-07 03:01:29 +00:00
}
2023-01-29 00:56:56 -07:00
* /
2022-02-07 03:01:29 +00:00
#[ async_std::test ]
async fn test_set_valid_quest_group ( ) {
let mut entity_gateway = InMemoryGateway ::default ( ) ;
2022-05-10 00:27:49 +00:00
let ( _user1 , _char1 ) = new_user_character ( & mut entity_gateway , " a1 " , " a " , 1 ) . await ;
2022-02-07 03:01:29 +00:00
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 ;
2022-10-18 04:46:21 -06:00
let packets = ship . handle ( ClientId ( 1 ) , RecvShipPacket ::RequestQuestList ( RequestQuestList { flag : 0 } ) ) . await . unwrap ( ) ;
2022-02-07 03:01:29 +00:00
match & packets [ 0 ] . 1 {
SendShipPacket ::QuestCategoryList ( quest_cat ) = > {
assert! ( String ::from_utf16_lossy ( & quest_cat . quest_categories [ 0 ] . name ) . starts_with ( " Retrieval " ) ) ;
} ,
_ = > panic! ( " Wrong quest category " ) ,
}
}
#[ async_std::test ]
async fn test_set_invalid_quest_group ( ) {
let mut entity_gateway = InMemoryGateway ::default ( ) ;
2022-05-10 00:27:49 +00:00
let ( _user1 , _char1 ) = new_user_character ( & mut entity_gateway , " a1 " , " a " , 1 ) . await ;
2022-02-07 03:01:29 +00:00
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 ;
2022-10-18 04:46:21 -06:00
let packets = ship . handle ( ClientId ( 1 ) , RecvShipPacket ::RequestQuestList ( RequestQuestList { flag : 100 } ) ) . await . unwrap ( ) ;
2022-02-07 03:01:29 +00:00
match & packets [ 0 ] . 1 {
SendShipPacket ::QuestCategoryList ( quest_cat ) = > {
// flag > quest category length should take the highest value allowed for quest category which is 1 in multimode (for govt quests) and 0 in other modes.
// assuming we create an ep1 room in multimode, we should load the government quests in this test case
assert! ( String ::from_utf16_lossy ( & quest_cat . quest_categories [ 0 ] . name ) . starts_with ( " Government " ) ) ;
} ,
_ = > panic! ( " Wrong quest category " ) ,
}
2022-07-11 03:48:21 +00:00
}
#[ async_std::test ]
async fn test_get_room_info ( ) {
let mut entity_gateway = InMemoryGateway ::default ( ) ;
let ( _user1 , mut _char1 ) = new_user_character ( & mut entity_gateway , " a1 " , " a " , 1 ) . await ;
_char1 . name = String ::from ( " GODmar " ) ;
entity_gateway . save_character ( & _char1 ) . await . unwrap ( ) ;
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 ;
let _expectedmsg = String ::from ( " 1 Lv1 GODmar \n HUmar Pioneer 2 \n " ) ;
2022-10-18 04:46:21 -06:00
let packets = ship . handle ( ClientId ( 2 ) , RecvShipPacket ::MenuDetail ( MenuDetail { menu : 3 , item : 0 } ) ) . await . unwrap ( ) ;
2022-07-11 03:48:21 +00:00
assert! ( matches! ( & packets [ 0 ] , ( ClientId ( 2 ) , SendShipPacket ::SmallLeftDialog ( SmallLeftDialog {
padding : [ 17664 , 1157645568 ] ,
2022-07-11 20:28:03 +00:00
msg : _expectedmsg ,
2022-07-11 03:48:21 +00:00
} ) ) ) ) ;
}
#[ async_std::test ]
async fn test_cannot_get_room_info_after_room_is_closed ( ) {
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 ;
leave_room ( & mut ship , ClientId ( 1 ) ) . await ;
let _expectedmsg = String ::from ( " Game is no longer active! \0 " ) ;
2022-10-18 04:46:21 -06:00
let packets = ship . handle ( ClientId ( 2 ) , RecvShipPacket ::MenuDetail ( MenuDetail { menu : 3 , item : 0 } ) ) . await . unwrap ( ) ;
2022-07-11 03:48:21 +00:00
assert! ( matches! ( & packets [ 0 ] , ( ClientId ( 2 ) , SendShipPacket ::SmallLeftDialog ( SmallLeftDialog {
padding : [ 17664 , 1157645568 ] ,
msg : _expectedmsg ,
} ) ) ) ) ;
}
#[ async_std::test ]
async fn test_cannot_join_room_after_its_closed ( ) {
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 ;
leave_room ( & mut ship , ClientId ( 1 ) ) . await ;
let _expectedmsg = String ::from ( " This room no longer exists! \0 " ) ;
2022-10-18 04:46:21 -06:00
let packets = ship . handle ( ClientId ( 2 ) , RecvShipPacket ::MenuSelect ( MenuSelect { menu : 3 , item : 0 } ) ) . await . unwrap ( ) ;
2022-07-11 03:48:21 +00:00
assert! ( matches! ( & packets [ 0 ] , ( ClientId ( 2 ) , SendShipPacket ::SmallDialog ( SmallDialog {
padding : [ 0 , 0 ] ,
msg : _expectedmsg , // wow yes cool rust is so great literally the best i can't put a String::from() directly in here.
} ) ) ) ) ;
}