the-marenga / sf-api

Manage Shakes & Fidget characters via simple commands. Handles encryption, response parsing and everything else for you
MIT License
18 stars 3 forks source link

dungeons.get_light(dungeon_name) sometimes crashes #54

Closed tiquis0290 closed 6 months ago

tiquis0290 commented 6 months ago

i tried iterating thru lightdungeons and getting them but sometimes it crashes (output1) but sometimes dont (output2). for me it looks like sometimes it thinks that only 26 dungeons exist (probably error on loading data?)

any idea how to fix it?

os: wsl2, linux ubuntu 22.04.3 LTS

my code:

288 let mut i = 0; //counter for my better understanding of problem 289 let dungeons = game_state.unlocks.dungeons.clone(); //getting dungeons from gamestate 290 for dungeon_name in LightDungeon::iter() { 291 println!("{}", i); 292 let d = dungeons.get_light(dungeon_name); 293 i += 1; 294 //here i have my code but commented out

...

304 }

output1: //only few lines

... 23 //print of i 24 25 thread 'main' panicked at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sf-api-0.1.2/src/gamestate/dungeons.rs:210:28: index out of bounds: the len is 26 but the index is 26 stack backtrace: 0: rust_begin_unwind at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5 1: core::panicking::panic_fmt at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14 2: core::panicking::panic_bounds_check at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:208:5 3: <usize as core::slice::index::SliceIndex<[T]>>::index at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:255:10 4: core::slice::index::<impl core::ops::index::Index for [T]>::index at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/slice/index.rs:18:9 5: <alloc::vec::Vec<T,A> as core::ops::index::Index>::index at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/alloc/src/vec/mod.rs:2770:9 6: sf_api::gamestate::dungeons::Dungeons::get_light at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sf-api-0.1.2/src/gamestate/dungeons.rs:210:28 7: sf_bot::dungeons_attack::{{closure}} at ./src/main.rs:292:21 8: sf_bot::login::{{closure}} at ./src/main.rs:121:56 9: sf_bot::main::{{closure}} at ./src/main.rs:44:45 10: tokio::runtime::park::CachedParkThread::block_on::{{closure}} at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/park.rs:281:63 11: tokio::runtime::coop::with_budget at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/coop.rs:107:5 12: tokio::runtime::coop::budget at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/coop.rs:73:5 13: tokio::runtime::park::CachedParkThread::block_on at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/park.rs:281:31 14: tokio::runtime::context::blocking::BlockingRegionGuard::block_on at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/context/blocking.rs:66:9 15: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::{{closure}} at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/scheduler/multi_thread/mod.rs:87:13 16: tokio::runtime::context::runtime::enter_runtime at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/context/runtime.rs:65:16 17: tokio::runtime::scheduler::multi_thread::MultiThread::block_on at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/scheduler/multi_thread/mod.rs:86:9 18: tokio::runtime::runtime::Runtime::block_on at /home/tiquis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.37.0/src/runtime/runtime.rs:351:45 19: sf_bot::main at ./src/main.rs:43:5 20: core::ops::function::FnOnce::call_once at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5

output2: ... 23 24 25 26 27 28

... //my program

the-marenga commented 6 months ago

Thanks for the report. Seems like the part of the server reponse, that contains the levels of the dungeon now only contains entries for dungeons, that are unlocked. Previously these would be padded to the size of all dungeons.

I am going to make sure that gets handled propery in the next release

For now, you best bet would maybe be to use panic::catch_unwind to handle that panic

tiquis0290 commented 6 months ago

I think it's not the case you specified because on this account i only have unlocked first 5, so it will have to crash earlier and also sometimes it works

the-marenga commented 6 months ago

hmm.. then there might be multiple reasons as to why they can be too short. In any case, I will just switch to a fixed size of dungeons (all of them) in my code, instead of relying on the server to send me all of them. That way, the get_ functions will be guaranteed to never crash