Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setters #14

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,24 @@ Its constructor takes [an object of the form](https://github.com/MaxOhn/rosu-pp-

Its only method is `build(): BeatmapAttributes`.

## Example
## Setters

For the `Difficulty`, `Performance`, and `BeatmapAttributesBuilder` classes, each field of their constructor's argument object is also available as a setter afterwards which takes either a value of the field's type or `undefined` to unset the previously set value.

```js
import * as rosu from "rosu-pp-js";

// Either given in the constructor
let difficulty = new rosu.Difficulty({ clockRate: 1.23 });

// Or adjusted afterwards
difficulty.mods = 8;

// Or even reset entirely
difficulty.clockRate = undefined;
```

## Examples

### Calculating performance

Expand Down
138 changes: 138 additions & 0 deletions rosu_pp_js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,45 @@ export class BeatmapAttributesBuilder {
* @returns {BeatmapAttributes}
*/
build(): BeatmapAttributes;
/**
*/
ar?: number;
/**
*/
arWithMods?: boolean;
/**
*/
clockRate?: number;
/**
*/
cs?: number;
/**
*/
csWithMods?: boolean;
/**
*/
hp?: number;
/**
*/
hpWithMods?: boolean;
/**
*/
isConvert?: boolean;
/**
*/
map?: Beatmap;
/**
*/
mode?: GameMode;
/**
*/
mods?: number;
/**
*/
od?: number;
/**
*/
odWithMods?: boolean;
}
/**
* Builder for a difficulty calculation.
Expand Down Expand Up @@ -410,6 +449,42 @@ export class Difficulty {
* @returns {GradualPerformance}
*/
gradualPerformance(map: Beatmap): GradualPerformance;
/**
*/
ar?: number;
/**
*/
arWithMods?: boolean;
/**
*/
clockRate?: number;
/**
*/
cs?: number;
/**
*/
csWithMods?: boolean;
/**
*/
hardrockOffsets?: boolean;
/**
*/
hp?: number;
/**
*/
hpWithMods?: boolean;
/**
*/
mods?: number;
/**
*/
od?: number;
/**
*/
odWithMods?: boolean;
/**
*/
passedObjects?: number;
}
/**
* The result of a difficulty calculation.
Expand Down Expand Up @@ -655,6 +730,69 @@ export class Performance {
* @returns {PerformanceAttributes}
*/
calculate(args: MapOrAttributes): PerformanceAttributes;
/**
*/
accuracy?: number;
/**
*/
ar?: number;
/**
*/
arWithMods?: boolean;
/**
*/
clockRate?: number;
/**
*/
combo?: number;
/**
*/
cs?: number;
/**
*/
csWithMods?: boolean;
/**
*/
hardrockOffsets?: boolean;
/**
*/
hitresultPriority?: HitResultPriority;
/**
*/
hp?: number;
/**
*/
hpWithMods?: boolean;
/**
*/
misses?: number;
/**
*/
mods?: number;
/**
*/
n100?: number;
/**
*/
n300?: number;
/**
*/
n50?: number;
/**
*/
nGeki?: number;
/**
*/
nKatu?: number;
/**
*/
od?: number;
/**
*/
odWithMods?: boolean;
/**
*/
passedObjects?: number;
}
/**
* The result of a performance calculation.
Expand Down
37 changes: 37 additions & 0 deletions src/args/beatmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::{Formatter, Result as FmtResult};

use rosu_pp::model::beatmap::BeatmapAttributesBuilder;
use serde::de;
use wasm_bindgen::{__rt::RefMut, prelude::wasm_bindgen};

Expand Down Expand Up @@ -101,6 +102,42 @@ pub struct BeatmapAttributesArgs {
pub map: Option<RefMut<'static, JsBeatmap>>,
}

impl BeatmapAttributesArgs {
pub fn as_builder(&self) -> BeatmapAttributesBuilder {
let mut builder = BeatmapAttributesBuilder::new();

if let Some(ref map) = self.map {
builder = builder.map(&map.inner);
}

if let Some(mode) = self.mode {
builder = builder.mode(mode.into(), self.is_convert);
}

if let Some(clock_rate) = self.clock_rate {
builder = builder.clock_rate(clock_rate);
}

if let Some(ar) = self.ar {
builder = builder.ar(ar, self.ar_with_mods);
}

if let Some(cs) = self.cs {
builder = builder.cs(cs, self.cs_with_mods);
}

if let Some(hp) = self.hp {
builder = builder.hp(hp, self.hp_with_mods);
}

if let Some(od) = self.od {
builder = builder.od(od, self.od_with_mods);
}

builder.mods(self.mods)
}
}

fn deser_maybe_map<'de, D: de::Deserializer<'de>>(
d: D,
) -> Result<Option<RefMut<'static, JsBeatmap>>, D::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/args/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface DifficultyArgs extends CommonArgs {
hardrockOffsets?: boolean;
}"#;

#[derive(Default, serde::Deserialize)]
#[derive(Clone, Default, serde::Deserialize)]
#[serde(rename_all = "camelCase", rename = "Object")]
pub struct DifficultyArgs {
#[serde(default)]
Expand Down
9 changes: 9 additions & 0 deletions src/args/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ pub enum JsHitResultPriority {
WorstCase,
}

impl From<JsHitResultPriority> for HitResultPriority {
fn from(priority: JsHitResultPriority) -> Self {
match priority {
JsHitResultPriority::BestCase => Self::BestCase,
JsHitResultPriority::WorstCase => Self::WorstCase,
}
}
}

impl JsHitResultPriority {
fn deserialize<'de, D: de::Deserializer<'de>>(d: D) -> Result<HitResultPriority, D::Error> {
let priority = match <u8 as de::Deserialize>::deserialize(d) {
Expand Down
Loading
Loading