Skip to content

Commit

Permalink
Chnage reputation score and min reputation
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJannsen committed Dec 8, 2023
1 parent 6f82785 commit c0d7b7d
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private View(Model model,
Label simHeadline = new Label(Res.get("user.reputation.sim.headline"));
simHeadline.getStyleClass().addAll("bisq-text-1");
ageField = getInputField("user.reputation.sim.age");
simAgeSlider = new AgeSlider(0, 1000, 0);
simAgeSlider = new AgeSlider(0, (int) AccountAgeService.MAX_DAYS_AGE_SCORE, 0);
simScore = getField(Res.get("user.reputation.sim.score"));
VBox.setMargin(simAgeSlider.getView().getRoot(), new Insets(15, 0, 0, 0));
root.getChildren().addAll(simHeadline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@

import bisq.common.util.MathUtils;
import bisq.desktop.components.controls.MaterialTextField;
import bisq.desktop.main.content.user.reputation.components.AgeSlider;
import bisq.i18n.Res;
import bisq.user.reputation.BondedReputationService;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
import org.fxmisc.easybind.monadic.MonadicBinding;

public class BondScoreSimulation {

Expand All @@ -53,47 +47,30 @@ public static class Controller implements bisq.desktop.common.view.Controller {
@Getter
private final View view;
private final Model model;
private Subscription agePin, ageAsStringPin, scorePin;
private Subscription scorePin;

private Controller() {
model = new Model();
view = new View(model, this);

model.getAmount().set("100");
model.getLockTime().set("10000");
model.getAgeAsInt().set(0);
model.getAge().set("0");
}

@Override
public void onActivate() {
agePin = EasyBind.subscribe(model.getAgeAsInt(), age -> model.getAge().set(String.valueOf(age)));
ageAsStringPin = EasyBind.subscribe(model.getAge(), ageAsString -> {
try {
model.getAgeAsInt().set(Integer.parseInt(ageAsString));
} catch (Exception e) {
}
});

MonadicBinding<String> binding = EasyBind.combine(model.getAmount(), model.getLockTime(), model.getAgeAsInt(), this::calculateSimScore);
scorePin = EasyBind.subscribe(binding, score -> model.getScore().set(score));
scorePin = EasyBind.subscribe(model.getAmount(), amount -> model.getScore().set(calculateSimScore(amount)));
}

@Override
public void onDeactivate() {
agePin.unsubscribe();
ageAsStringPin.unsubscribe();
scorePin.unsubscribe();
}

private String calculateSimScore(String amount, String lockTime, Number age) {
private String calculateSimScore(String amount) {
try {
// amountAsLong is the smallest unit of BSQ (100 = 1 BSQ)
long amountAsLong = Math.max(0, MathUtils.roundDoubleToLong(Double.parseDouble(amount) * 100));
long lockTimeAsLong = Long.parseLong(lockTime);
lockTimeAsLong = Math.min(100_000, Math.max(10_000, lockTimeAsLong));
long ageInDays = Math.max(0, age.intValue());
long totalScore = BondedReputationService.doCalculateScore(amountAsLong, lockTimeAsLong, ageInDays);
long totalScore = BondedReputationService.doCalculateScore(amountAsLong);
return String.valueOf(totalScore);
} catch (Exception e) {
return "";
Expand All @@ -103,16 +80,12 @@ private String calculateSimScore(String amount, String lockTime, Number age) {

@Getter
private static class Model implements bisq.desktop.common.view.Model {
private final IntegerProperty ageAsInt = new SimpleIntegerProperty();
private final StringProperty lockTime = new SimpleStringProperty();
private final StringProperty age = new SimpleStringProperty();
private final StringProperty amount = new SimpleStringProperty();
private final StringProperty score = new SimpleStringProperty();
}

private static class View extends bisq.desktop.common.view.View<VBox, Model, Controller> {
private final MaterialTextField amount, score, lockTime, age;
private final AgeSlider ageSlider;
private final MaterialTextField amount, score;

private View(Model model,
Controller controller) {
Expand All @@ -124,34 +97,20 @@ private View(Model model,
int width = 195;
amount.setMinWidth(width);
amount.setMaxWidth(width);
lockTime = getInputField("user.reputation.sim.lockTime");
lockTime.setMinWidth(width);
lockTime.setMaxWidth(width);
age = getInputField("user.reputation.sim.age");
ageSlider = new AgeSlider(0, 1000, 0);
score = getField(Res.get("user.reputation.sim.score"));
VBox.setMargin(ageSlider.getView().getRoot(), new Insets(15, 0, 0, 0));
root.getChildren().addAll(simHeadline,
new HBox(10, amount, lockTime),
age,
ageSlider.getView().getRoot(),
amount,
score);
}

@Override
protected void onViewAttached() {
ageSlider.valueProperty().bindBidirectional(model.getAgeAsInt());
lockTime.textProperty().bindBidirectional(model.getLockTime());
age.textProperty().bindBidirectional(model.getAge());
amount.textProperty().bindBidirectional(model.getAmount());
score.textProperty().bind(model.getScore());
}

@Override
protected void onViewDetached() {
ageSlider.valueProperty().unbindBidirectional(model.getAgeAsInt());
lockTime.textProperty().unbindBidirectional(model.getLockTime());
age.textProperty().unbindBidirectional(model.getAge());
amount.textProperty().unbindBidirectional(model.getAmount());
score.textProperty().unbind();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public BondedReputationTab2View(BondedReputationTab2Model model, BondedReputatio
formulaHeadline.getStyleClass().addAll("bisq-text-1");
VBox formulaBox = new VBox(10, formulaHeadline,
getField("weight", String.valueOf(BondedReputationService.WEIGHT)),
getField("score"),
getField("ageScore"),
getField("totalScore"));

HBox hBox = new HBox(20, formulaBox, simulation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public BurnBsqTab2View(BurnBsqTab2Model model, BurnBsqTab2Controller controller,
formulaHeadline.getStyleClass().addAll("bisq-text-1");
VBox formulaBox = new VBox(10, formulaHeadline,
getField(Res.get("user.reputation.weight"), String.valueOf(ProofOfBurnService.WEIGHT)),
getFormulaField("score"),
getFormulaField("ageScore"),
getFormulaField("totalScore"));

HBox hBox = new HBox(20, formulaBox, simulation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@

import bisq.common.util.MathUtils;
import bisq.desktop.components.controls.MaterialTextField;
import bisq.desktop.main.content.user.reputation.components.AgeSlider;
import bisq.i18n.Res;
import bisq.user.reputation.ProofOfBurnService;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
import org.fxmisc.easybind.monadic.MonadicBinding;

public class BurnScoreSimulation {

Expand All @@ -52,44 +47,30 @@ public static class Controller implements bisq.desktop.common.view.Controller {
@Getter
private final View view;
private final Model model;
private Subscription agePin, ageAsStringPin, scorePin;
private Subscription scorePin;

private Controller() {
model = new Model();
view = new View(model, this);

model.getAmount().set("100");
model.getAge().set(0);
model.getAgeAsString().set("0");
}

@Override
public void onActivate() {
agePin = EasyBind.subscribe(model.getAge(), age -> model.getAgeAsString().set(String.valueOf(age)));
ageAsStringPin = EasyBind.subscribe(model.getAgeAsString(), ageAsString -> {
try {
model.getAge().set(Integer.parseInt(ageAsString));
} catch (Exception e) {
}
});

MonadicBinding<String> binding = EasyBind.combine(model.getAmount(), model.getAge(), this::calculateSimScore);
scorePin = EasyBind.subscribe(binding, score -> model.getScore().set(score));
scorePin = EasyBind.subscribe(model.getAmount(), amount -> model.getScore().set(calculateSimScore(amount)));
}

@Override
public void onDeactivate() {
agePin.unsubscribe();
ageAsStringPin.unsubscribe();
scorePin.unsubscribe();
}

private String calculateSimScore(String amount, Number age) {
private String calculateSimScore(String amount) {
try {
// amountAsLong is the smallest unit of BSQ (100 = 1 BSQ)
long amountAsLong = Math.max(0, MathUtils.roundDoubleToLong(Double.parseDouble(amount) * 100));
long ageInDays = Math.max(0, age.intValue());
long totalScore = ProofOfBurnService.doCalculateScore(amountAsLong, ageInDays);
long totalScore = ProofOfBurnService.doCalculateScore(amountAsLong);
return String.valueOf(totalScore);
} catch (Exception e) {
return "";
Expand All @@ -99,17 +80,13 @@ private String calculateSimScore(String amount, Number age) {

@Getter
private static class Model implements bisq.desktop.common.view.Model {
private final IntegerProperty age = new SimpleIntegerProperty();
private final StringProperty ageAsString = new SimpleStringProperty();
private final StringProperty amount = new SimpleStringProperty();
private final StringProperty score = new SimpleStringProperty();
}

private static class View extends bisq.desktop.common.view.View<VBox, Model, Controller> {
private final MaterialTextField amount;
private final AgeSlider ageSlider;
private final MaterialTextField score;
private final MaterialTextField age;

private View(Model model,
Controller controller) {
Expand All @@ -118,29 +95,20 @@ private View(Model model,
Label simHeadline = new Label(Res.get("user.reputation.sim.headline"));
simHeadline.getStyleClass().addAll("bisq-text-1");
amount = getInputField("user.reputation.sim.burnAmount");
age = getInputField("user.reputation.sim.age");
ageSlider = new AgeSlider(0, 1000, 0);
score = getField(Res.get("user.reputation.sim.score"));
VBox.setMargin(ageSlider.getView().getRoot(), new Insets(15, 0, 0, 0));
root.getChildren().addAll(simHeadline,
amount,
age,
ageSlider.getView().getRoot(),
score);
}

@Override
protected void onViewAttached() {
ageSlider.valueProperty().bindBidirectional(model.getAge());
age.textProperty().bindBidirectional(model.getAgeAsString());
amount.textProperty().bindBidirectional(model.getAmount());
score.textProperty().bind(model.getScore());
}

@Override
protected void onViewDetached() {
ageSlider.valueProperty().unbindBidirectional(model.getAge());
age.textProperty().unbindBidirectional(model.getAgeAsString());
amount.textProperty().unbindBidirectional(model.getAmount());
score.textProperty().unbind();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private View(Model model,
Label simHeadline = new Label(Res.get("user.reputation.sim.headline"));
simHeadline.getStyleClass().addAll("bisq-text-1");
ageField = getInputField("user.reputation.sim.age");
simAgeSlider = new AgeSlider(0, 1000, 0);
simAgeSlider = new AgeSlider(0, (int) SignedWitnessService.MAX_DAYS_AGE_SCORE, 0);
simScore = getField(Res.get("user.reputation.sim.score"));
VBox.setMargin(simAgeSlider.getView().getRoot(), new Insets(15, 0, 0, 0));
root.getChildren().addAll(simHeadline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
package bisq.desktop.main.content.user.reputation.signedAccount.tab2;

import bisq.bisq_easy.NavigationTarget;
import bisq.common.util.MathUtils;
import bisq.desktop.ServiceProvider;
import bisq.desktop.common.Browser;
import bisq.desktop.common.view.Controller;
import bisq.desktop.common.view.Navigation;
import bisq.user.reputation.ProofOfBurnService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -46,18 +44,6 @@ public void onActivate() {
public void onDeactivate() {
}

private static String calculateSimScore(String amount, Number age) {
try {
// amountAsLong is the smallest unit of BSQ (100 = 1 BSQ)
long amountAsLong = MathUtils.roundDoubleToLong(Double.parseDouble(amount) * 100);
long ageInDays = age.intValue();
long totalScore = ProofOfBurnService.doCalculateScore(amountAsLong, ageInDays);
return String.valueOf(totalScore);
} catch (Exception e) {
return "";
}
}

void onBack() {
Navigation.navigateTo(NavigationTarget.SIGNED_WITNESS_TAB_1);
}
Expand Down
Loading

0 comments on commit c0d7b7d

Please sign in to comment.