From 9df40e152e33c9c67449e9106dc47b9887d5e99e Mon Sep 17 00:00:00 2001 From: panicbit Date: Mon, 25 Jul 2022 22:57:05 +0200 Subject: [PATCH 1/3] add StarIterator --- js/model.ts | 17 ++++++++++------- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/js/model.ts b/js/model.ts index 49566e3..1d70148 100644 --- a/js/model.ts +++ b/js/model.ts @@ -1,7 +1,7 @@ export enum DayType { NoData = 0, None, Shower, Rainbow, Aurora } export enum ShowerType { NotSure = 0, Light, Heavy } -import {Hemisphere, Weather, SpecialDay, getMonthLength, Pattern, getPattern, getWeather, getWindPower, isSpecialDay, SnowLevel, CloudLevel, FogLevel, getSnowLevel, getCloudLevel, getFogLevel, checkWaterFog, getRainbowInfo, isAuroraPattern, fromLinearHour, toLinearHour, canHaveShootingStars, queryStars, getStarSecond, isLightShowerPattern, isHeavyShowerPattern, isPatternPossibleAtDate, GuessData, getPatternKind, PatternKind, SpWeatherLevel, getSpWeatherLevel, Constellation, getConstellation, getWindPowerMin, getWindPowerMax, getSpecialCloudInfo, SpecialCloud} from '../pkg' +import {Hemisphere, Weather, SpecialDay, getMonthLength, Pattern, getPattern, getWeather, getWindPower, isSpecialDay, SnowLevel, CloudLevel, FogLevel, getSnowLevel, getCloudLevel, getFogLevel, checkWaterFog, getRainbowInfo, isAuroraPattern, fromLinearHour, toLinearHour, canHaveShootingStars, queryStars, getStarSecond, isLightShowerPattern, isHeavyShowerPattern, isPatternPossibleAtDate, GuessData, getPatternKind, PatternKind, SpWeatherLevel, getSpWeatherLevel, Constellation, getConstellation, getWindPowerMin, getWindPowerMax, getSpecialCloudInfo, SpecialCloud, StarsIterator} from '../pkg' export {Hemisphere, Weather, SpecialDay, getMonthLength} export enum AmbiguousWeather { @@ -628,12 +628,15 @@ export class DayForecast { const hour = fromLinearHour(linearHour) if (canHaveShootingStars(hour, this.pattern)) { for (let minute = 0; minute < 60; minute++) { - const starCount = queryStars(seed, year, month, day, hour, minute, this.pattern) - if (starCount > 0) { - const star: StarInfo = {hour, minute, seconds: []} - for (let i = 0; i < starCount; i++) { - star.seconds.push(getStarSecond(i)) - } + const starsIterator = new StarsIterator(seed, year, month, day, hour, minute, this.pattern); + const star: StarInfo = {hour, minute, seconds: []} + let second = undefined; + + while ((second = starsIterator.next()) != undefined) { + star.seconds.push(second); + } + + if (star.seconds.length > 0) { this.shootingStars.push(star) } } diff --git a/src/lib.rs b/src/lib.rs index a8b0796..1eb5413 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -644,6 +644,51 @@ fn query_stars_internal(seed_base: u32, minute: u8, pattern: Pattern) -> Option< static mut LAST_STAR_SECONDS: [u8;8] = [0;8]; +#[wasm_bindgen] +pub struct StarsIterator { + star_count: u8, + star_field: u64, + second: u8, +} + +#[wasm_bindgen] +impl StarsIterator { + #[wasm_bindgen(constructor)] + pub fn new(seed: u32, year: u16, month: u8, day: u8, hour: u8, minute: u8, pattern: Pattern) -> Self { + let (year, month, day) = normalise_late_ymd(year, month, day, hour); + let seed = compute_seed_ymdh(seed, 0x20000, 0x2000, 0x100, 0x10000, year, month, day, hour); + + let (star_count, star_field) = query_stars_internal(seed, minute, pattern) + .unwrap_or((0, 0)); + + Self { + star_count, + star_field, + second: 0, + } + } + + #[allow(clippy::should_implement_trait)] + pub fn next(&mut self) -> Option { + if self.star_count == 0 { + return None; + } + + for second in self.second..60 { + let mask = 1u64 << second; + + if (self.star_field & mask) != 0 { + self.second = second + 1; + self.star_count -= 1; + + return Some(second); + } + } + + None + } +} + #[wasm_bindgen(js_name = queryStars)] pub fn query_stars(seed: u32, year: u16, month: u8, day: u8, hour: u8, minute: u8, pattern: Pattern) -> u8 { let (year, month, day) = normalise_late_ymd(year, month, day, hour); From 03f3805548d665f691d15f7bd4e56804fe41c4b6 Mon Sep 17 00:00:00 2001 From: panicbit Date: Mon, 25 Jul 2022 23:00:41 +0200 Subject: [PATCH 2/3] remove old shooting star functions --- js/model.ts | 2 +- src/lib.rs | 29 ----------------------------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/js/model.ts b/js/model.ts index 1d70148..3be9aef 100644 --- a/js/model.ts +++ b/js/model.ts @@ -1,7 +1,7 @@ export enum DayType { NoData = 0, None, Shower, Rainbow, Aurora } export enum ShowerType { NotSure = 0, Light, Heavy } -import {Hemisphere, Weather, SpecialDay, getMonthLength, Pattern, getPattern, getWeather, getWindPower, isSpecialDay, SnowLevel, CloudLevel, FogLevel, getSnowLevel, getCloudLevel, getFogLevel, checkWaterFog, getRainbowInfo, isAuroraPattern, fromLinearHour, toLinearHour, canHaveShootingStars, queryStars, getStarSecond, isLightShowerPattern, isHeavyShowerPattern, isPatternPossibleAtDate, GuessData, getPatternKind, PatternKind, SpWeatherLevel, getSpWeatherLevel, Constellation, getConstellation, getWindPowerMin, getWindPowerMax, getSpecialCloudInfo, SpecialCloud, StarsIterator} from '../pkg' +import {Hemisphere, Weather, SpecialDay, getMonthLength, Pattern, getPattern, getWeather, getWindPower, isSpecialDay, SnowLevel, CloudLevel, FogLevel, getSnowLevel, getCloudLevel, getFogLevel, checkWaterFog, getRainbowInfo, isAuroraPattern, fromLinearHour, toLinearHour, canHaveShootingStars, isLightShowerPattern, isHeavyShowerPattern, isPatternPossibleAtDate, GuessData, getPatternKind, PatternKind, SpWeatherLevel, getSpWeatherLevel, Constellation, getConstellation, getWindPowerMin, getWindPowerMax, getSpecialCloudInfo, SpecialCloud, StarsIterator} from '../pkg' export {Hemisphere, Weather, SpecialDay, getMonthLength} export enum AmbiguousWeather { diff --git a/src/lib.rs b/src/lib.rs index 1eb5413..6368c81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -641,9 +641,6 @@ fn query_stars_internal(seed_base: u32, minute: u8, pattern: Pattern) -> Option< } - -static mut LAST_STAR_SECONDS: [u8;8] = [0;8]; - #[wasm_bindgen] pub struct StarsIterator { star_count: u8, @@ -689,32 +686,6 @@ impl StarsIterator { } } -#[wasm_bindgen(js_name = queryStars)] -pub fn query_stars(seed: u32, year: u16, month: u8, day: u8, hour: u8, minute: u8, pattern: Pattern) -> u8 { - let (year, month, day) = normalise_late_ymd(year, month, day, hour); - let seed = compute_seed_ymdh(seed, 0x20000, 0x2000, 0x100, 0x10000, year, month, day, hour); - match query_stars_internal(seed, minute, pattern) { - None => 0, - Some((star_count, star_field)) => { - let mut index = 0; - for second in 0..60 { - let mask = 1u64 << second; - if (star_field & mask) != 0 { - unsafe { LAST_STAR_SECONDS[index] = second; } - index += 1; - } - } - star_count - } - } -} -#[wasm_bindgen(js_name = getStarSecond)] -pub fn get_star_second(index: usize) -> u8 { - unsafe { LAST_STAR_SECONDS[index] } -} - - - #[derive(Clone, Copy)] struct HourGuess { From ba1ce1b41d02aa0421cd338a61b7ad15e9e6fce3 Mon Sep 17 00:00:00 2001 From: panicbit Date: Tue, 26 Jul 2022 01:47:58 +0200 Subject: [PATCH 3/3] add StarsIterator::has_next --- js/model.ts | 5 +++-- src/lib.rs | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/js/model.ts b/js/model.ts index 3be9aef..8a8add9 100644 --- a/js/model.ts +++ b/js/model.ts @@ -630,9 +630,10 @@ export class DayForecast { for (let minute = 0; minute < 60; minute++) { const starsIterator = new StarsIterator(seed, year, month, day, hour, minute, this.pattern); const star: StarInfo = {hour, minute, seconds: []} - let second = undefined; - while ((second = starsIterator.next()) != undefined) { + while (starsIterator.hasNext()) { + const second = starsIterator.next()!; + star.seconds.push(second); } diff --git a/src/lib.rs b/src/lib.rs index 6368c81..b8acbbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -665,9 +665,14 @@ impl StarsIterator { } } + #[wasm_bindgen(js_name = hasNext)] + pub fn has_next(&self) -> bool { + self.star_count > 0 + } + #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> Option { - if self.star_count == 0 { + if !self.has_next() { return None; }