diff --git a/Dogename/dogename.iml b/Dogename/dogename.iml index bad4288..7d188d5 100644 --- a/Dogename/dogename.iml +++ b/Dogename/dogename.iml @@ -15,7 +15,7 @@ - + diff --git a/Dogename/src/main/java/me/lensferno/dogename/Main.java b/Dogename/src/main/java/me/lensferno/dogename/Main.java index 726412a..5abbac9 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/Main.java +++ b/Dogename/src/main/java/me/lensferno/dogename/Main.java @@ -67,6 +67,8 @@ public class Main extends Application { mainInterfaceController.setImg(DataReleaser.getMainPicStream()); + mainInterfaceController.init(); + primaryStage.setOnCloseRequest(event -> { configLoader.writeAllConfigToFile(configLoader.getMainConfigLocation(), configLoader.getVoiceConfigLocation()); System.exit(0); diff --git a/Dogename/src/main/java/me/lensferno/dogename/choose/Chooser.java b/Dogename/src/main/java/me/lensferno/dogename/choose/Chooser.java deleted file mode 100644 index 19574cb..0000000 --- a/Dogename/src/main/java/me/lensferno/dogename/choose/Chooser.java +++ /dev/null @@ -1,356 +0,0 @@ -package me.lensferno.dogename.choose; - -import com.jfoenix.controls.JFXButton; -import javafx.animation.AnimationTimer; -import javafx.beans.property.StringProperty; -import me.lensferno.dogename.configs.VoiceConfig; -import me.lensferno.dogename.data.History; -import me.lensferno.dogename.data.NameData; -import me.lensferno.dogename.voice.Token; -import me.lensferno.dogename.voice.VoicePlayer; - -import java.security.SecureRandom; -import java.util.Random; -import java.util.logging.Logger; - - /* - -┴┬┴┬/ ̄\_/ ̄\ -┬┴┬┴▏  ▏▔▔▔▔\ -┴┬┴/\ /      ﹨ -┬┴∕       /   ) -┴┬▏        ●  ▏ -┬┴▏           ▔█  -┴◢██◣     \___/ -┬█████◣       /   -┴█████████████◣ -◢██████████████▆▄ -█◤◢██◣◥█████████◤\ -◥◢████ ████████◤   \ -┴█████ ██████◤      ﹨ -┬│   │█████◤        ▏ -┴│   │              ▏ -┬ ∕    ∕    /▔▔▔\     ∕ -┴/___/﹨   ∕     ﹨  /\ -┬┴┬┴┬┴\    \      ﹨/   ﹨ -┴┬┴┬┴┬┴ \___\     ﹨/▔\﹨ ▔\ -▲△▲▲╓╥╥╥╥╥╥╥╥\   ∕  /▔﹨/▔﹨ - **╠╬╬╬╬╬╬╬╬*﹨  /  // - - */ -//一坨屎山,有待修改😒 - -public final class Chooser { - - Logger log =Logger.getLogger("ChooserLogger"); - - final int UPPER_LABEL_ID = 1; - final int UNDER_LABEL_ID = 2; - - - Token token; - VoicePlayer voicePlayer; - - VoiceConfig voiceConfig; - - Random random =new Random(); - - public short minNumber; - public short maxNumber; - - boolean voicePlay=true; - - int totalMaxCount =120; - int cycleMaxCount =0; - - int totalCount =0; - int totalCycleCount =0; - - int shownLabelId =1; - - String chosenName; - - boolean cycleEnd =true; - boolean ignoreTimesOut=false; - boolean ignorePast=true; - - boolean equalMode=true; - - boolean forceStop =false; - - boolean newAlgo=true; - - public JFXButton anpaiBtn; - - public short speed; - - NameData nameData; - - boolean isRunning; - - StringProperty upLabelText; - StringProperty downLabelText; - - History history; - - String speaker,speakSpeed,intonation; - - void writeIgnoreList(){ - nameData.writeIgnoreList(""); - } - - - AnimationTimer timer =new AnimationTimer() { - @Override - public void handle(long now) { - - if(forceStop){ - totalCount = totalMaxCount +1; - } - - try{ - Thread.sleep(speed); - }catch (Exception e){e.printStackTrace(); } - - if(totalCount >= totalMaxCount){ - if(!nameData.getIgnoreNameList().contains(chosenName)||!ignorePast||forceStop){ - - forceStop=false; - - if(ignorePast) - nameData.getIgnoreNameList().add(chosenName); - if(equalMode) - writeIgnoreList(); - - cycleEnd=true; - totalCount =0; - totalCycleCount =0; - ignoreTimesOut=false; - - switch (shownLabelId){ - case UPPER_LABEL_ID:{ - - if(downLabelText.get().contains("→")||downLabelText.get().contains("←")) - downLabelText.set(downLabelText.get().replace("→ ","").replace(" ←","")); - - upLabelText.set("→ "+chosenName+" ←"); - - break; - } - case UNDER_LABEL_ID:{ - if(upLabelText.get().contains("→")||upLabelText.get().contains("←")) - upLabelText.set(upLabelText.get().replace("→ ","").replace(" ←","")); - - downLabelText.set("→ "+chosenName+" ←"); - - break; - } - } - isRunning=false; - anpaiBtn.setText("安排一下"); - stop(); - System.gc(); - history.addHistory(chosenName); - if(voicePlay) - voicePlayer.playVoice(chosenName,speaker,intonation,speakSpeed); - return; - }else - ignoreTimesOut=true; - - - - } - if(totalCycleCount >= cycleMaxCount &&!ignoreTimesOut){ - cycleEnd=true; - totalCycleCount =0; - } - - if(cycleEnd){ - //times=(int)(1+Math.random()*(chosenTime-already)); - cycleMaxCount =1+random.nextInt(totalMaxCount - totalCount +1); - cycleEnd=false; - //showWhich=(int)(1+Math.random()*2); - shownLabelId =1+random.nextInt(2); - } - - - - switch (shownLabelId){ - case UPPER_LABEL_ID:{ - chosenName= nameData.randomGet(); - upLabelText.set(chosenName); - totalCount++; - totalCycleCount++; - break; - } - - case UNDER_LABEL_ID:{ - chosenName= nameData.randomGet(); - downLabelText.set(chosenName); - totalCount++; - totalCycleCount++; - break; - } - } - - - } - }; - //--------------------------------------------------------------------------------------- - SecureRandom secRandom =new SecureRandom(); - AnimationTimer numbTimer =new AnimationTimer() { - @Override - public void handle(long now) { - - if(forceStop){ - totalCount = totalMaxCount +1; - } - - try{ - Thread.sleep(speed); - }catch (Exception e){e.printStackTrace(); } - - if(totalCount >= totalMaxCount){ - if(!nameData.getIgnoreNumberList().contains(chosenName)||!ignorePast||forceStop){ - - forceStop=false; - - if(ignorePast) - nameData.getIgnoreNumberList().add(chosenName); - if(equalMode) - writeIgnoreList(); - - cycleEnd=true; - totalCount =0; - totalCycleCount =0; - ignoreTimesOut=false; - - - switch (shownLabelId){ - case UPPER_LABEL_ID:{ - if(downLabelText.get().contains("→")||downLabelText.get().contains("←")) - downLabelText.set(downLabelText.get().replace("→ ","").replace(" ←","")); - - upLabelText.set("→ "+chosenName+" ←"); - - break; - } - case UNDER_LABEL_ID:{ - if(upLabelText.get().contains("→")||upLabelText.get().contains("←")) - upLabelText.set(upLabelText.get().replace("→ ","").replace(" ←","")); - - downLabelText.set("→ "+chosenName+" ←"); - - break; - } - } - isRunning=false; - anpaiBtn.setText("安排一下"); - stop(); - System.gc(); - history.addHistory(chosenName); - if(voicePlay) - voicePlayer.playVoice(chosenName,speaker,intonation,speakSpeed); - return; - }else - ignoreTimesOut=true; - - } - - shownLabelId =1+random.nextInt(2); - //speed=(short)(65+random.nextInt(100)); - - switch (shownLabelId){ - case UPPER_LABEL_ID:{ - if(newAlgo) - chosenName=String.valueOf(minNumber+random.nextInt(maxNumber-minNumber+1)); - else - chosenName=String.valueOf(minNumber+secRandom.nextInt(maxNumber-minNumber+1)); - - upLabelText.set(chosenName); - totalCount++; - totalCycleCount++; - break; - } - - case UNDER_LABEL_ID:{ - if(newAlgo) - chosenName=String.valueOf(minNumber+random.nextInt(maxNumber-minNumber+1)); - else - chosenName=String.valueOf(minNumber+secRandom.nextInt(maxNumber-minNumber+1)); - - downLabelText.set(chosenName); - totalCount++; - totalCycleCount++; - break; - } - } - - - } - }; - - public void forceStop(){ - - } - - - - public void run(short speed,int chosenTime,boolean ignorePast,boolean equalMode,boolean voicePlay){ - - this.speed = speed; - this.totalMaxCount = chosenTime; - this.ignorePast = ignorePast; - this.equalMode = equalMode; - this.voicePlay = voicePlay; - - isRunning=true; - timer.start(); - } - - public void run(short maxNumber,short minNumber,short speed,int chosenTime,boolean ignorePast,boolean equalMode,boolean voicePlay){ - this.maxNumber = maxNumber; - this.minNumber = minNumber; - this.speed = speed; - this.totalMaxCount = chosenTime; - this.ignorePast = ignorePast; - this.equalMode = equalMode; - this.voicePlay = voicePlay; - - isRunning=true; - numbTimer.start(); - } - - public void set(StringProperty upLabelText, StringProperty downLabelText, JFXButton anpaiBtn, History history, NameData nameData, Token token, VoiceConfig voiceConfig){ - - this.upLabelText=upLabelText; - this.downLabelText=downLabelText; - - this.anpaiBtn = anpaiBtn; - this.history = history; - this.nameData = nameData; - - this.token=token; - this.voicePlayer=new VoicePlayer(token); - - this.voiceConfig=voiceConfig; - - this.speaker=String.valueOf(voiceConfig.getSpeaker()); - this.speakSpeed=String.valueOf(voiceConfig.getSpeed()); - this.intonation=String.valueOf(voiceConfig.getIntonation()); - this.speaker=voiceConfig.getSpeaker(); - } - - - public void setChoose(JFXButton anpaiBtn) { - this.anpaiBtn = anpaiBtn; - } - - public boolean isRunning() { - return isRunning; - } - - public void setForceStop(boolean forceStop) { - this.forceStop = forceStop; - } -} diff --git a/Dogename/src/main/java/me/lensferno/dogename/configs/MainConfig.java b/Dogename/src/main/java/me/lensferno/dogename/configs/MainConfig.java index ea56552..baec855 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/configs/MainConfig.java +++ b/Dogename/src/main/java/me/lensferno/dogename/configs/MainConfig.java @@ -5,214 +5,207 @@ import javafx.beans.property.*; public class MainConfig { - // ConfigValuesBean configValuesBean =new ConfigValuesBean(); - // ---------------------- Default values --------------------------------------------------------- - @Expose - private final String NEWEST_CONFIG_VERSION = "2"; - - private String currentConfigVersion = null; + private final int currentVersion = 3; @Expose - public final boolean DEFAULT_NAME_CHOOSE = true; + public static final boolean DEFAULT_NAME_CHOOSE = true; - public final int METHOD_NAME = 0; // 名字挑选法 - public final int METHOD_NUMBER = 1; // 数字挑选法 + public static final int METHOD_NAME = 0; // 名字挑选法 + public static final int METHOD_NUMBER = 1; // 数字挑选法 - public final int DEFAULT_CYCLE_TIMES = 120; // 默认轮回次数:120 - public final int DEFAULT_SPEED = 80; // 默认速度:20ms,对应滑动条80的位置 + public static final int DEFAULT_MAX_TOTAL_COUNT = 120; // 默认轮回次数:120 + public static final int DEFAULT_SPEED = 80; // 默认速度:20ms,对应滑动条80的位置 - public final boolean DEFAULT_RANDOM_TIMES = true; // 默认挑选轮回次数是否随机:ture + public static final boolean DEFAULT_RANDOM_TIMES = true; // 默认挑选轮回次数是否随机:ture - public final boolean DEFAULT_IGNORE_PAST = true; // 默认忽略已经点过的名字:ture + public static final boolean DEFAULT_IGNORE_PAST = true; // 默认忽略已经点过的名字:ture - public final boolean DEFAULT_EQUAL_MODE = true; // 默认开启"机会均等" + public static final boolean DEFAULT_EQUAL_MODE = true; // 默认开启"机会均等" - public final boolean DEFAULT_NEW_ALGO = true; // 默认使用新算法"Java sec random" - public final boolean DEFAULT_VOICE_PLAY = true; // 默认使用语音播报 + public static final boolean DEFAULT_NEW_ALGO = true; // 默认使用新算法"Java sec random" + public static final boolean DEFAULT_VOICE_PLAY = true; // 默认使用语音播报 - public final boolean DEFAULT_SHOW_SAYING = true; + public static final boolean DEFAULT_SHOW_SAYING = true; // ----------------------Properties---------------------------------------------------------------- - private SimpleBooleanProperty nameChooseProperty; + private SimpleBooleanProperty nameChoose; - private SimpleBooleanProperty randomTimesProperty; // 挑选次数是否随机 - private SimpleBooleanProperty ignorePastProperty; // 是否忽略已经被点过的名字 + private SimpleBooleanProperty randomCount; // 挑选次数是否随机 + private SimpleBooleanProperty passSelectedResult; // 是否忽略已经被点过的名字/数字 - private SimpleIntegerProperty chooseMethodProperty; // 挑选方式: 0->名字挑选法 1->数字挑选法 - private SimpleIntegerProperty cycleTimesProperty; // 挑选轮回次数,旧称"chosenTime" + private SimpleIntegerProperty chooseMethod; // 挑选方式: 0->名字挑选法 1->数字挑选法 + private SimpleIntegerProperty maxTotalCount; // 挑选轮回次数 - private SimpleIntegerProperty speedProperty; // 速度 + private SimpleIntegerProperty speed; // 速度 - private SimpleStringProperty minNumberProperty; // 最小值 - private SimpleStringProperty maxNumberProperty; // 最大值 + private SimpleStringProperty minNumber; // 最小值 + private SimpleStringProperty maxNumber; // 最大值 - private SimpleBooleanProperty equalModeProperty; // 是否开启"机会均等" + private SimpleBooleanProperty equalMode; // 是否开启"机会均等" - private SimpleBooleanProperty newAlgoProperty; // 是否使用新算法 - private SimpleBooleanProperty voicePlayProperty; // 是否使用语音播报 + private SimpleBooleanProperty secureRandom; // 是否使用secure random + private SimpleBooleanProperty voicePlay; // 是否使用语音播报 private SimpleBooleanProperty showSaying; // -------------------------- 初始化 -------------------------------------------------------------- public MainConfig() { - randomTimesProperty = new SimpleBooleanProperty(DEFAULT_RANDOM_TIMES); - ignorePastProperty = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST); + randomCount = new SimpleBooleanProperty(DEFAULT_RANDOM_TIMES); + passSelectedResult = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST); - chooseMethodProperty = new SimpleIntegerProperty(METHOD_NAME); - nameChooseProperty = new SimpleBooleanProperty(DEFAULT_NAME_CHOOSE); + chooseMethod = new SimpleIntegerProperty(METHOD_NAME); + nameChoose = new SimpleBooleanProperty(DEFAULT_NAME_CHOOSE); - cycleTimesProperty = new SimpleIntegerProperty(DEFAULT_CYCLE_TIMES); + maxTotalCount = new SimpleIntegerProperty(DEFAULT_MAX_TOTAL_COUNT); - speedProperty = new SimpleIntegerProperty(DEFAULT_SPEED); + speed = new SimpleIntegerProperty(DEFAULT_SPEED); - minNumberProperty = new SimpleStringProperty("0"); - maxNumberProperty = new SimpleStringProperty("10"); + minNumber = new SimpleStringProperty("0"); + maxNumber = new SimpleStringProperty("10"); - equalModeProperty = new SimpleBooleanProperty(DEFAULT_EQUAL_MODE); + equalMode = new SimpleBooleanProperty(DEFAULT_EQUAL_MODE); - newAlgoProperty = new SimpleBooleanProperty(DEFAULT_NEW_ALGO); - voicePlayProperty = new SimpleBooleanProperty(DEFAULT_VOICE_PLAY); + secureRandom = new SimpleBooleanProperty(DEFAULT_NEW_ALGO); + voicePlay = new SimpleBooleanProperty(DEFAULT_VOICE_PLAY); showSaying = new SimpleBooleanProperty(DEFAULT_SHOW_SAYING); - - currentConfigVersion = NEWEST_CONFIG_VERSION; } // -------------------------- Getters and Setters --------------------------------------------- - public boolean isNameChooseProperty() { - return nameChooseProperty.get(); + public boolean getNameChoose() { + return nameChoose.get(); } - public SimpleBooleanProperty nameChoosePropertyProperty() { - return nameChooseProperty; + public SimpleBooleanProperty nameChooseProperty() { + return nameChoose; } - public void setNameChooseProperty(boolean nameChooseProperty) { - this.nameChooseProperty.set(nameChooseProperty); + public void setNameChoose(boolean nameChoose) { + this.nameChoose.set(nameChoose); } - public boolean isRandomTimesProperty() { - return randomTimesProperty.get(); + public boolean getRandomCount() { + return randomCount.get(); } - public SimpleBooleanProperty randomTimesPropertyProperty() { - return randomTimesProperty; + public SimpleBooleanProperty randomCountProperty() { + return randomCount; } - public void setRandomTimesProperty(boolean randomTimesProperty) { - this.randomTimesProperty.set(randomTimesProperty); + public void setRandomCount(boolean randomCount) { + this.randomCount.set(randomCount); } - public boolean isIgnorePastProperty() { - return ignorePastProperty.get(); + public boolean getPassSelectedResult() { + return passSelectedResult.get(); } - public SimpleBooleanProperty ignorePastPropertyProperty() { - return ignorePastProperty; + public SimpleBooleanProperty passSelectedResultProperty() { + return passSelectedResult; } - public void setIgnorePastProperty(boolean ignorePastProperty) { - this.ignorePastProperty.set(ignorePastProperty); + public void setPassSelectedResult(boolean passSelectedResult) { + this.passSelectedResult.set(passSelectedResult); } - public int getChooseMethodProperty() { - return chooseMethodProperty.get(); + public int getChooseMethod() { + return chooseMethod.get(); } - public SimpleIntegerProperty chooseMethodPropertyProperty() { - return chooseMethodProperty; + public SimpleIntegerProperty chooseMethodProperty() { + return chooseMethod; } - public void setChooseMethodProperty(int chooseMethodProperty) { - this.chooseMethodProperty.set(chooseMethodProperty); + public void setChooseMethod(int chooseMethod) { + this.chooseMethod.set(chooseMethod); } - public int getCycleTimesProperty() { - return cycleTimesProperty.get(); + public int getMaxTotalCount() { + return maxTotalCount.get(); } - public SimpleIntegerProperty cycleTimesPropertyProperty() { - return cycleTimesProperty; + public SimpleIntegerProperty maxTotalCountProperty() { + return maxTotalCount; } - public void setCycleTimesProperty(int cycleTimesProperty) { - this.cycleTimesProperty.set(cycleTimesProperty); + public void setMaxTotalCount(int maxTotalCount) { + this.maxTotalCount.set(maxTotalCount); } - public int getSpeedProperty() { - return speedProperty.get(); + public int getSpeed() { + return speed.get(); } - public SimpleIntegerProperty speedPropertyProperty() { - return speedProperty; + public SimpleIntegerProperty speedProperty() { + return speed; } - public void setSpeedProperty(int speedProperty) { - this.speedProperty.set(speedProperty); + public void setSpeed(int speed) { + this.speed.set(speed); } - public String getMinNumberProperty() { - return minNumberProperty.get(); + public String getMinNumber() { + return minNumber.get(); } - public SimpleStringProperty minNumberPropertyProperty() { - return minNumberProperty; + public SimpleStringProperty minNumberProperty() { + return minNumber; } - public void setMinNumberProperty(String minNumberProperty) { - this.minNumberProperty.set(minNumberProperty); + public void setMinNumber(String minNumber) { + this.minNumber.set(minNumber); } - public String getMaxNumberProperty() { - return maxNumberProperty.get(); + public String getMaxNumber() { + return maxNumber.get(); } - public SimpleStringProperty maxNumberPropertyProperty() { - return maxNumberProperty; + public SimpleStringProperty maxNumberProperty() { + return maxNumber; } - public void setMaxNumberProperty(String maxNumberProperty) { - this.maxNumberProperty.set(maxNumberProperty); + public void setMaxNumber(String maxNumber) { + this.maxNumber.set(maxNumber); } - public boolean isEqualModeProperty() { - return equalModeProperty.get(); + public boolean getEqualMode() { + return equalMode.get(); } - public SimpleBooleanProperty equalModePropertyProperty() { - return equalModeProperty; + public SimpleBooleanProperty equalModeProperty() { + return equalMode; } - public void setEqualModeProperty(boolean equalModeProperty) { - this.equalModeProperty.set(equalModeProperty); + public void setEqualMode(boolean equalMode) { + this.equalMode.set(equalMode); } - public boolean isNewAlgoProperty() { - return newAlgoProperty.get(); + public boolean getSecureRandom() { + return secureRandom.get(); } - public SimpleBooleanProperty newAlgoPropertyProperty() { - return newAlgoProperty; + public SimpleBooleanProperty secureRandomProperty() { + return secureRandom; } - public void setNewAlgoProperty(boolean newAlgoProperty) { - this.newAlgoProperty.set(newAlgoProperty); + public void setSecureRandom(boolean secureRandom) { + this.secureRandom.set(secureRandom); } - public boolean isVoicePlayProperty() { - return voicePlayProperty.get(); + public boolean getVoicePlay() { + return voicePlay.get(); } - public SimpleBooleanProperty voicePlayPropertyProperty() { - return voicePlayProperty; + public SimpleBooleanProperty voicePlayProperty() { + return voicePlay; } - public void setVoicePlayProperty(boolean voicePlayProperty) { - this.voicePlayProperty.set(voicePlayProperty); + public void setVoicePlay(boolean voicePlay) { + this.voicePlay.set(voicePlay); } public boolean isShowSaying() { @@ -227,16 +220,8 @@ public class MainConfig { this.showSaying.set(showSaying); } - public String getCurrentConfigVersion() { - return currentConfigVersion; - } - - public void setCurrentConfigVersion(String currentConfigVersion) { - this.currentConfigVersion = currentConfigVersion; - } - - public String getNewestConfigVersion() { - return NEWEST_CONFIG_VERSION; + public int getCurrentConfigVersion() { + return currentVersion; } } diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java index dde13d7..cb8cbe6 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java @@ -12,16 +12,14 @@ import javafx.scene.image.ImageView; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.stage.StageStyle; +import me.lensferno.dogename.select.Selector; import me.lensferno.dogename.utils.DialogMaker; -import me.lensferno.dogename.choose.Chooser; import me.lensferno.dogename.configs.ConfigLoader; import me.lensferno.dogename.configs.MainConfig; import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.data.History; import me.lensferno.dogename.data.NameData; import me.lensferno.dogename.ocr.Ocr; -import me.lensferno.dogename.voice.Token; -import me.lensferno.dogename.voice.TokenManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -35,11 +33,6 @@ public final class MainInterfaceController { public JFXTextArea message; - //ConfigLoader configLoader=new ConfigLoader(); - - Token token; - TokenManager tokenManager=new TokenManager(); - Ocr ocrTool=null; History history=new History(); @@ -59,9 +52,6 @@ public final class MainInterfaceController { @FXML private JFXButton anPaiBtn; - @FXML - private JFXButton anPaiX10Btn; - @FXML private Pane mainPane; @@ -72,7 +62,7 @@ public final class MainInterfaceController { private JFXButton showNameMangerButton; @FXML - private Label chosen_1; + private Label upperLabel; @FXML private ImageView mainView; @@ -81,35 +71,27 @@ public final class MainInterfaceController { private JFXButton miniModeBtn; @FXML - private Label chosen_2; + private Label downLabel; public MainInterfaceController(){ history.loadHistory(); nameData.readIgnoreList(); - tokenManager.init(); this.ignoreNameList=nameData.getIgnoreNameList(); this.ignoreNumberList=nameData.getIgnoreNumberList(); - if(tokenManager.getTokenStatus().equals("ok")){ - token=tokenManager.getToken(); - } } MainConfig mainConfig; VoiceConfig voiceConfig; public void bindProperties(){ - nameChoose.selectedProperty().bindBidirectional(mainConfig.nameChoosePropertyProperty()); - //mainConfig.nameChoosePropertyProperty().not() + nameChoose.selectedProperty().bindBidirectional(mainConfig.nameChooseProperty()); - numbChoose.selectedProperty().bind(mainConfig.nameChoosePropertyProperty().not()); + numbChoose.selectedProperty().bind(mainConfig.nameChooseProperty().not()); numbChoose.selectedProperty().unbind(); -/* - mainConfig.nameChoosePropertyProperty().addListener((observable, oldValue, newValue) -> { - //numbChoose.selectedProperty().unbind(); - numbChoose.setSelected(oldValue); - });*/ - + mainConfig.nameChooseProperty().addListener((observable, oldValue, newValue) -> { + mainConfig.setChooseMethod(mainConfig.getNameChoose() ? MainConfig.METHOD_NAME : MainConfig.METHOD_NUMBER); + }); } public void setImg(InputStream stream){ @@ -130,7 +112,7 @@ public final class MainInterfaceController { @FXML void showNameManger(ActionEvent event) { - if (chooser.isRunning()){ + if (selector.isWorkerRunning()){ new DialogMaker(rootPane).createMessageDialog("(・。・)","安排中......\n为保证运行的稳定,此时还不能进行该操作哦。"); return; } @@ -138,12 +120,10 @@ public final class MainInterfaceController { new DialogMaker(rootPane).createDialogWithOneBtn("名单管理",nameManagerPaneController); } - - @FXML void showNunberSetting(ActionEvent event) { - if (chooser.isRunning()){ + if (selector.isWorkerRunning()){ new DialogMaker(rootPane).createMessageDialog("(・。・)","安排中......\n为保证运行的稳定,此时还不能进行该操作哦。"); return; } @@ -172,7 +152,7 @@ public final class MainInterfaceController { miniStage.initStyle(StageStyle.UNDECORATED); MiniPaneController miniPaneController=loader.getController(); - miniPaneController.setBase(history,nameData,token,voiceConfig,mainConfig); + miniPaneController.setBase(nameData, mainConfig, selector); Stage currentStage=(Stage)anPaiBtn.getScene().getWindow(); miniPaneController.setForwStage(currentStage); @@ -184,7 +164,6 @@ public final class MainInterfaceController { miniStage.show(); currentStage.hide(); - } @FXML @@ -218,48 +197,58 @@ public final class MainInterfaceController { HashSet ignoreNumberList; NameData nameData=new NameData(); - //boolean isRunning=false; - Chooser chooser=new Chooser(); + Selector selector =new Selector(); + + public void init(){ + + selector.initialVariable(mainConfig, nameData, history, upperLabel.textProperty(), downLabel.textProperty()); + selector.addStoppedEventListener((observableValue, oldValue, stop)->{ + if (stop) { + anPaiBtn.setText("安排一下"); + } else { + anPaiBtn.setText("不玩了!"); + } + }); + } @FXML void anPai() { - if(chooser.isRunning()){ - chooser.setForceStop(true); + if(selector.isWorkerRunning()){ + selector.forceStop(); anPaiBtn.setText("安排一下"); return; } - if(mainConfig.isRandomTimesProperty()) { - mainConfig.setCycleTimesProperty(100+random.nextInt(151)); + if(mainConfig.getRandomCount()) { + mainConfig.setMaxTotalCount(100+random.nextInt(151)); } - if(mainConfig.isNameChooseProperty()){ - runNameMode(chooser); + if(mainConfig.getNameChoose()){ + runNameMode(); }else { - runNumberMode(chooser); + runNumberMode(); } } - public void setToggleGroup(){ ToggleGroup toggleGroup =new ToggleGroup(); nameChoose.setToggleGroup(toggleGroup); numbChoose.setToggleGroup(toggleGroup); } - private void runNameMode(Chooser chooser){ + private void runNameMode(){ if(nameData.isEmpty()){ new DialogMaker(rootPane).createMessageDialog("哦霍~","现在名单还是空的捏~请前往名单管理添加名字 或 使用数字挑选法。"); return; } - if((nameData.getIgnoreNameList().size()>=nameData.getSize())&&mainConfig.isIgnorePastProperty()){ + if((nameData.getIgnoreNameList().size() >= nameData.getSize()) && mainConfig.getPassSelectedResult()){ - if(mainConfig.isEqualModeProperty()) { + if(mainConfig.getEqualMode()) { new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部名字都被点完啦!\n要把名字的忽略列表重置吗?", e ->nameData.clearNameIgnoreList()); }else { new DialogMaker(rootPane).createMessageDialog("啊?", "全部名字都被点完啦!\n请多添加几个名字 或 点击“机会均等”的“重置”按钮。"); @@ -269,33 +258,23 @@ public final class MainInterfaceController { anPaiBtn.setText("不玩了!"); - chooser.set(chosen_1.textProperty(),chosen_2.textProperty(),anPaiBtn,history,nameData,token,voiceConfig); - - chooser.run( - (short)(100-mainConfig.getSpeedProperty()) , - mainConfig.getCycleTimesProperty(), - mainConfig.isIgnorePastProperty(), - mainConfig.isEqualModeProperty(), - mainConfig.isVoicePlayProperty() - ); - + selector.run(); } - - private void runNumberMode(Chooser chooser){ + private void runNumberMode(){ try{ - int minNumber=Integer.parseInt(mainConfig.getMinNumberProperty()); - int maxNumber=Integer.parseInt(mainConfig.getMaxNumberProperty()); + int minNumber=Integer.parseInt(mainConfig.getMinNumber()); + int maxNumber=Integer.parseInt(mainConfig.getMaxNumber()); - if(maxNumber-minNumber<=0){ + if(maxNumber-minNumber <= 0){ new DialogMaker(rootPane).createMessageDialog("嗯哼?","数字要前小后大啊~"); return; } - if(nameData.getIgnoreNumberList().size()>=(maxNumber-minNumber+1) && mainConfig.isIgnorePastProperty()){ - if(mainConfig.isEqualModeProperty()) { + if(nameData.getIgnoreNumberList().size() >= (maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){ + if(mainConfig.getEqualMode()) { new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部数字都被点完啦!\n要把数字的忽略列表重置吗?", e ->nameData.clearNumberIgnoreList()); }else { new DialogMaker(rootPane).createMessageDialog("啊?", "全部数字都被点完啦!\n请扩大数字范围 或 点击“机会均等”的“重置”按钮。"); @@ -310,17 +289,7 @@ public final class MainInterfaceController { anPaiBtn.setText("不玩了!"); - chooser.set(chosen_1.textProperty(),chosen_2.textProperty(),anPaiBtn,history,nameData,token,voiceConfig); - - chooser.run( - Short.parseShort(mainConfig.getMaxNumberProperty()), - Short.parseShort(mainConfig.getMinNumberProperty()), - (short)(100-mainConfig.getSpeedProperty()) , - mainConfig.getCycleTimesProperty(), - mainConfig.isIgnorePastProperty(), - mainConfig.isEqualModeProperty(), - mainConfig.isVoicePlayProperty() - ); + selector.run(); } public Label getTopBar() { diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java index d8cd9ba..eefd303 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java @@ -8,15 +8,12 @@ import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.input.TouchEvent; -import javafx.scene.layout.Pane; import javafx.stage.Stage; -import me.lensferno.dogename.utils.DialogMaker; -import me.lensferno.dogename.choose.Chooser; +import me.lensferno.dogename.select.Selector; import me.lensferno.dogename.configs.MainConfig; import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.controllers.WindowListeners.MoveWindowByMouse; import me.lensferno.dogename.controllers.WindowListeners.MoveWindowByTouch; -import me.lensferno.dogename.data.History; import me.lensferno.dogename.data.NameData; import me.lensferno.dogename.voice.Token; @@ -45,10 +42,6 @@ public class MiniPaneController { private Random random=new Random(); private NameData nameData; - private Pane rootPane; - private History history; - private Token token; - private VoiceConfig voiceConfig; Stage currentStage; Scene currentScene; @@ -61,17 +54,10 @@ public class MiniPaneController { this.currentStage = currentStage; } - public void setBase(History history, NameData nameData, Token token, VoiceConfig voiceConfig, MainConfig mainConfig){ - - this.history = history; + public void setBase(NameData nameData, MainConfig mainConfig, Selector selector){ this.nameData = nameData; - - this.token=token; - - this.voiceConfig=voiceConfig; - this.mainConfig=mainConfig; - + this.selector = selector; } @FXML @@ -102,90 +88,66 @@ public class MiniPaneController { miniModeBtn.setOnTouchMoved(touchHandler); } - - private MainConfig mainConfig; - Chooser chooser=new Chooser(); + private Selector selector =new Selector(); @FXML void anPai() { - if(chooser.isRunning()){ - chooser.setForceStop(true); + + if(selector.isWorkerRunning()){ + selector.forceStop(); anPaiBtn.setText("安排一下"); return; } - if(mainConfig.isRandomTimesProperty()) { - mainConfig.setCycleTimesProperty(100+random.nextInt(151)); + if(mainConfig.getRandomCount()) { + mainConfig.setMaxTotalCount(100+random.nextInt(151)); } - if(mainConfig.isNameChooseProperty()){ - runNameMode(chooser); - }else { - runNumberMode(chooser); + if(mainConfig.getNameChoose()){ + runNameMode(); + } else { + runNumberMode(); } } - - private void runNameMode(Chooser chooser){ + private void runNameMode(){ if(nameData.isEmpty()){ return; } - if((nameData.getIgnoreNameList().size()>=nameData.getSize())&&mainConfig.isIgnorePastProperty()){ - + if((nameData.getIgnoreNameList().size()>=nameData.getSize())&&mainConfig.getPassSelectedResult()){ return; } anPaiBtn.setText("不玩了!"); - chooser.set(chosenNameLabel.textProperty(),chosenNameLabel.textProperty(),anPaiBtn,history,nameData,token,voiceConfig); - - chooser.run( - (short)(100-mainConfig.getSpeedProperty()) , - mainConfig.getCycleTimesProperty(), - mainConfig.isIgnorePastProperty(), - mainConfig.isEqualModeProperty(), - mainConfig.isVoicePlayProperty() - ); - + selector.run(); } - - private void runNumberMode(Chooser chooser){ + private void runNumberMode(){ try{ - int minNumber=Integer.parseInt(mainConfig.getMinNumberProperty()); - int maxNumber=Integer.parseInt(mainConfig.getMaxNumberProperty()); + int minNumber=Integer.parseInt(mainConfig.getMinNumber()); + int maxNumber=Integer.parseInt(mainConfig.getMaxNumber()); if(maxNumber-minNumber<=0){ return; } - if(nameData.getIgnoreNumberList().size()>=(maxNumber-minNumber+1) && mainConfig.isIgnorePastProperty()){ + if(nameData.getIgnoreNumberList().size()>=(maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){ return; } }catch (Exception e){ - new DialogMaker(rootPane).createMessageDialog("嗯哼?","倒是输入个有效的数字啊~"); return; } anPaiBtn.setText("不玩了!"); - chooser.set(chosenNameLabel.textProperty(),chosenNameLabel.textProperty(),anPaiBtn,history,nameData,token,voiceConfig); - - chooser.run( - Short.parseShort(mainConfig.getMaxNumberProperty()), - Short.parseShort(mainConfig.getMinNumberProperty()), - (short)(100-mainConfig.getSpeedProperty()) , - mainConfig.getCycleTimesProperty(), - mainConfig.isIgnorePastProperty(), - mainConfig.isEqualModeProperty(), - mainConfig.isVoicePlayProperty() - ); + selector.run(); } } diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/NumberSettingsPaneController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/NumberSettingsPaneController.java index 128b3b6..90c7e41 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/NumberSettingsPaneController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/NumberSettingsPaneController.java @@ -29,10 +29,10 @@ public class NumberSettingsPaneController extends VBox { public void bindProperties(MainConfig mainConfig){ - minValueField.textProperty().bindBidirectional(mainConfig.minNumberPropertyProperty()); + minValueField.textProperty().bindBidirectional(mainConfig.minNumberProperty()); minValueField.textProperty().addListener((observable, oldValue, newValue) -> nameData.clearNumberIgnoreList() ); - maxValueField.textProperty().bindBidirectional(mainConfig.maxNumberPropertyProperty()); + maxValueField.textProperty().bindBidirectional(mainConfig.maxNumberProperty()); maxValueField.textProperty().addListener((observable, oldValue, newValue) -> nameData.clearNumberIgnoreList() ); } diff --git a/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java b/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java index 8065f8f..20e164e 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java +++ b/Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java @@ -84,25 +84,25 @@ public class SettingsPaneController extends VBox { public void bindProperties(MainConfig mainConfig){ setMainConfig(mainConfig); - ignoreOnce.selectedProperty().bindBidirectional(mainConfig.ignorePastPropertyProperty()); - chooseOnce.setSelected(!mainConfig.isIgnorePastProperty()); + ignoreOnce.selectedProperty().bindBidirectional(mainConfig.passSelectedResultProperty()); + chooseOnce.setSelected(!mainConfig.getPassSelectedResult()); - randomTimes.selectedProperty().bindBidirectional(mainConfig.randomTimesPropertyProperty()); - fixedTimes.setSelected(!mainConfig.isRandomTimesProperty()); + randomTimes.selectedProperty().bindBidirectional(mainConfig.randomCountProperty()); + fixedTimes.setSelected(!mainConfig.getRandomCount()); - equalModeBtn.selectedProperty().bindBidirectional(mainConfig.equalModePropertyProperty()); + equalModeBtn.selectedProperty().bindBidirectional(mainConfig.equalModeProperty()); - newAlgoBtn.selectedProperty().bindBidirectional(mainConfig.newAlgoPropertyProperty()); + newAlgoBtn.selectedProperty().bindBidirectional(mainConfig.secureRandomProperty()); - voicePlayBtn.selectedProperty().bindBidirectional(mainConfig.voicePlayPropertyProperty()); + voicePlayBtn.selectedProperty().bindBidirectional(mainConfig.voicePlayProperty()); - cycleTimesBar.valueProperty().bindBidirectional(mainConfig.cycleTimesPropertyProperty()); + cycleTimesBar.valueProperty().bindBidirectional(mainConfig.maxTotalCountProperty()); - speedBar.valueProperty().bindBidirectional(mainConfig.speedPropertyProperty()); + speedBar.valueProperty().bindBidirectional(mainConfig.speedProperty()); showSayingBtn.selectedProperty().bindBidirectional(mainConfig.showSayingProperty()); - mainConfig.ignorePastPropertyProperty().addListener((observable, oldValue, isIgnorePast) -> { + mainConfig.passSelectedResultProperty().addListener((observable, oldValue, isIgnorePast) -> { if(!isIgnorePast) { //如果 忽略被点过的名字 被取消后就把机会均等模式的按钮给取消掉 @@ -145,7 +145,7 @@ public class SettingsPaneController extends VBox { @FXML void equalBtnAction(ActionEvent event) { - if(!mainConfig.isIgnorePastProperty()){ + if(!mainConfig.getPassSelectedResult()){ equalModeBtn.setSelected(false); new DialogMaker(rootPane).createMessageDialog("且慢","无法在“概率均分”的模式下使用,如需使用请在“人人有份”模式下启用。"); } diff --git a/Dogename/src/main/java/me/lensferno/dogename/data/History.java b/Dogename/src/main/java/me/lensferno/dogename/data/History.java index f4bbd18..8b96db9 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/data/History.java +++ b/Dogename/src/main/java/me/lensferno/dogename/data/History.java @@ -53,8 +53,7 @@ public class History { if(history.size()>2000) { history.clear(); } - history.add((history.size() + 1) +". "+name); - + history.add(String.format("%d. %s", (history.size() + 1), name)); writeHistory(); } diff --git a/Dogename/src/main/java/me/lensferno/dogename/data/NameData.java b/Dogename/src/main/java/me/lensferno/dogename/data/NameData.java index 05f7ae0..610b64d 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/data/NameData.java +++ b/Dogename/src/main/java/me/lensferno/dogename/data/NameData.java @@ -17,6 +17,9 @@ public class NameData { private List chooseList; private int listSize = 0; + + public static final int NAME_ONLY = 0; + public static final int NUMBER_ONLY = 1; HashSet ignoreNameList=new HashSet<>(); @@ -33,30 +36,37 @@ public class NameData { File nameIgnoreFile =new File("files"+File.separator+"IgnoredNameList.data"); File numbIgnoreFile =new File("files"+File.separator+"IgnoredNumberList.data"); - public void writeIgnoreList(String switchy){ - - if(!switchy.equals("not name")) { - try { - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(nameIgnoreFile)); - oos.writeObject(ignoreNameList); - oos.close(); - } catch (Exception e) { - e.printStackTrace(); - } + public void writeIgnoreList(int switchy){ + switch (switchy) { + case NAME_ONLY: + writeNameIgnoreList(); + case NUMBER_ONLY: + writeNumberIgnoreList(); + default: + writeNumberIgnoreList(); + writeNameIgnoreList(); } + } - if(!switchy.equals("not number")) { - try { - ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(numbIgnoreFile)); - oos.writeObject(ignoreNumberList); - oos.close(); - } catch (Exception e) { - e.printStackTrace(); - } + private void writeNameIgnoreList() { + try { + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(nameIgnoreFile)); + oos.writeObject(ignoreNameList); + oos.close(); + } catch (Exception e) { + e.printStackTrace(); } - } + private void writeNumberIgnoreList() { + try { + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(numbIgnoreFile)); + oos.writeObject(ignoreNumberList); + oos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } public void readIgnoreList(){ @@ -66,7 +76,7 @@ public class NameData { nameIgnoreFile.getParentFile().mkdirs(); nameIgnoreFile.createNewFile(); ignoreNameList= new HashSet<>(); - writeIgnoreList("not number"); + writeIgnoreList(NAME_ONLY); return; } @@ -76,10 +86,10 @@ public class NameData { }catch (EOFException e){ ignoreNameList=new HashSet<>(); log.warn("Past name list is empty."); - writeIgnoreList("not number"); + writeIgnoreList(NAME_ONLY); }catch (Exception e){ ignoreNameList=new HashSet<>(); - writeIgnoreList("not number"); + writeIgnoreList(NAME_ONLY); log.warn("Failed to load past name list:"+e.toString()); e.printStackTrace(); } @@ -90,7 +100,7 @@ public class NameData { numbIgnoreFile.getParentFile().mkdirs(); numbIgnoreFile.createNewFile(); ignoreNumberList= new HashSet<>(); - writeIgnoreList("not name"); + writeIgnoreList(NUMBER_ONLY); return; } @@ -100,11 +110,11 @@ public class NameData { }catch (EOFException e){ ignoreNumberList=new HashSet<>(); log.warn("Ignored number list is empty."); - writeIgnoreList("not name"); + writeIgnoreList(NUMBER_ONLY); }catch (Exception e){ ignoreNumberList=new HashSet<>(); log.warn("Failed to load ignored number list"); - writeIgnoreList("not name"); + writeIgnoreList(NUMBER_ONLY); e.printStackTrace(); } @@ -114,12 +124,12 @@ public class NameData { public void clearNameIgnoreList(){ ignoreNameList.clear(); - writeIgnoreList("not number"); + writeIgnoreList(NAME_ONLY); } public void clearNumberIgnoreList(){ ignoreNumberList.clear(); - writeIgnoreList("not name"); + writeIgnoreList(NUMBER_ONLY); } @@ -316,8 +326,8 @@ public class NameData { Random random =new Random(); //------------------------------------------------------ - public String randomGet(){ - if(newAlgo) + public String randomGet(boolean secureRandom){ + if(secureRandom) return nameList.get(secRandom.nextInt(nameList.size())); else return nameList.get(random.nextInt(nameList.size())); diff --git a/Dogename/src/main/java/me/lensferno/dogename/select/Selector.java b/Dogename/src/main/java/me/lensferno/dogename/select/Selector.java new file mode 100644 index 0000000..92a916c --- /dev/null +++ b/Dogename/src/main/java/me/lensferno/dogename/select/Selector.java @@ -0,0 +1,104 @@ +package me.lensferno.dogename.select; + +import javafx.animation.AnimationTimer; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.StringProperty; +import javafx.beans.value.ChangeListener; +import me.lensferno.dogename.configs.MainConfig; +import me.lensferno.dogename.configs.VoiceConfig; +import me.lensferno.dogename.data.History; +import me.lensferno.dogename.data.NameData; +import me.lensferno.dogename.select.core.Worker; +import me.lensferno.dogename.voice.TokenManager; +import me.lensferno.dogename.voice.VoicePlayer; + +public final class Selector { + + Processor processor = new Processor(); + + private VoicePlayer voicePlayer = null; + + public Selector() { + initialVoicePlayer(); + } + + public void initialVoicePlayer() { + TokenManager tokenManager = new TokenManager(); + VoiceConfig voiceConfig = new VoiceConfig(); + tokenManager.init(); + if (tokenManager.getTokenStatus() == TokenManager.TOKEN_OK) { + voicePlayer = new VoicePlayer(tokenManager.getToken(), voiceConfig); + } + } + + public void run() { + processor.setNumberRange(); + processor.updateNewValue(); + processor.stoppedIndicatorProperty().set(false); + + processor.start(); + } + + public void forceStop() { + processor.stopProcess(); + } + + public void initialVariable(MainConfig config, NameData nameData, History history, StringProperty... labelTexts) { + processor.initialVariable(config, voicePlayer, nameData, history, labelTexts); + } + + public boolean isWorkerRunning() { + return processor.isWorkerRunning(); + } + + public void addStoppedEventListener(ChangeListener listener) { + processor.stoppedIndicatorProperty().addListener(listener); + } + + // --------------------------------------------------- + static class Processor extends AnimationTimer { + + private Worker coreWorker; + + MainConfig config = null; + + protected void initialVariable(MainConfig config, VoicePlayer voicePlayer, NameData nameData, History history, StringProperty... labelTexts) { + coreWorker = new Worker(labelTexts, config, nameData, history, voicePlayer); + this.config = config; + } + + protected void updateNewValue() { + coreWorker.setSpeed(100 - config.getSpeed()); + coreWorker.setMaxTotalCount(config.getMaxTotalCount()); + } + + protected SimpleBooleanProperty stoppedIndicatorProperty() { + return coreWorker.stoppedIndicatorProperty(); + } + + @Override + public void handle(long time) { + coreWorker.displaySelectedResult(); + if (coreWorker.getStoppedIndicator()) { + this.stop(); + } + } + + public void stopProcess() { + coreWorker.setForceStop(true); + } + + public boolean isWorkerRunning() { + return !coreWorker.getStoppedIndicator(); + } + + public void setNumberRange() { + int minNumber = Integer.parseInt(config.getMinNumber()); + int maxNumber = Integer.parseInt(config.getMaxNumber()); + coreWorker.setNumberRange(minNumber, maxNumber); + } + + } + +} + diff --git a/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java b/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java new file mode 100644 index 0000000..6b68348 --- /dev/null +++ b/Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java @@ -0,0 +1,203 @@ +package me.lensferno.dogename.select.core; + +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.StringProperty; +import me.lensferno.dogename.configs.MainConfig; +import me.lensferno.dogename.data.History; +import me.lensferno.dogename.data.NameData; + +import me.lensferno.dogename.utils.Random; +import me.lensferno.dogename.voice.VoicePlayer; + +public final class Worker { + + private final Random randomNumber = new Random(); + + private final StringProperty[] labelTexts; + private final SimpleBooleanProperty stoppedIndicator = new SimpleBooleanProperty(true); + + private final MainConfig config; + + //挑选方法 + private final int selectMethod = MainConfig.METHOD_NAME; + private final NameData nameData; + + private final History history; + + private final VoicePlayer voicePlayer; + + private int speed = 0; + + //数值范围最大最小值 + private final int[] numberRange = new int[2]; + private final int MIN_NUMBER = 0; + private final int MAX_NUMBER = 1; + + //挑选次数和每一轮的挑选次数 + private int maxTotalCount = MainConfig.DEFAULT_MAX_TOTAL_COUNT; + private int maxCycleCount = 0; + + //已经挑选了多少次 + private int totalCount = 0; + private int cycleCount = 0; + + private boolean finalResult = true; + + private boolean forceStop = false; + + private String selectedResult; + private boolean continueSelecting = false; + + private int resultLabelId = 0; + + private final Counter counter = new Counter(); + + public Worker(StringProperty[] labelTexts, MainConfig config, NameData nameData, History history, VoicePlayer voicePlayer) { + this.labelTexts = labelTexts; + this.config = config; + this.nameData = nameData; + this.history = history; + this.voicePlayer = voicePlayer; + } + + private void select() { + // try的位置待定(前面or后面) + // 延时用,用于调整挑选速度 + try { + Thread.sleep(speed); + } catch (Exception e) { + e.printStackTrace(); + } + + stoppedIndicator.set(false); + + // 若 计数已达最大总数(挑选次数)或强制停止,进入停止分支, + if (totalCount >= maxTotalCount || forceStop) { + // 任意三种情况就停止:已忽略名单中没有该名字;不考虑忽略名单;强制停止 + // 如果不是这三种情况就选到符合其中一种情况为止 + // 由于后两者在安排进程中不可能改变,因此继续挑选后只有选中的结果不在忽略名单中才会结束挑选 + // 若忽略名单已满(名单中数量=总的名单数量),点击“安排一下”时会提示已满,无法再进行挑选,因此在运行时总有 忽略名单<总名单,总有名字不在忽略列表中,不会死循环,放心吧 + boolean resultIgnored = (config.getChooseMethod() == MainConfig.METHOD_NAME) ? (nameData.getIgnoreNameList().contains(selectedResult)) : (nameData.getIgnoreNumberList().contains(selectedResult)); + if (!resultIgnored || !config.getPassSelectedResult() || forceStop) { + this.stopSelect(); + finalResult = true; + return; + } else { + continueSelecting = true; + } + } + + counter.count(); + resultLabelId = counter.getNewResultLabelId(); + finalResult = false; + + this.selectedResult = this.pick(config.getChooseMethod()); + } + + public void stopSelect() { + if (config.getPassSelectedResult()) { + nameData.getIgnoreNameList().add(selectedResult); + } + + if (config.getEqualMode()) { + nameData.writeIgnoreList(NameData.NAME_ONLY); + } + + history.addHistory(selectedResult); + + if (config.getVoicePlay() && voicePlayer != null) { + voicePlayer.playVoice(selectedResult); + } + + counter.resetCounter(); + stoppedIndicator.set(true); + forceStop = false; + System.gc(); + } + + private String pick(int selectMethod) { + switch (selectMethod) { + case MainConfig.METHOD_NAME: + return nameData.randomGet(config.getSecureRandom()); + case MainConfig.METHOD_NUMBER: + randomNumber.setUseSecureRandom(config.getSecureRandom()); + return String.valueOf(randomNumber.getRandomNumber(numberRange[MIN_NUMBER], numberRange[MAX_NUMBER])); + default: + return "(´・ω・`)?"; + } + } + + public void displaySelectedResult() { + this.select(); + + if (!finalResult) { + labelTexts[resultLabelId].set(selectedResult); + } else { + for (StringProperty labelText : labelTexts) { + if (labelText.get().contains("→")) { + labelText.set(labelText.get().replace("→", "").replace("←", "")); + } + } + labelTexts[resultLabelId].set(String.format("→%s←", selectedResult)); + } + } + + // 这里有个内部类Counter计数菌 + final class Counter { + + private int newResultLabelId = 0; + + protected void count() { + newResultLabelId = resultLabelId; + + // 每一轮的计数到达最大时并且不强制继续这轮挑选时,重置这一轮的挑选,并指派新的显示label + // 否则这一轮的计数直接+1 + if (cycleCount >= maxCycleCount && !continueSelecting) { + cycleCount = 0; + maxCycleCount = randomNumber.getRandomNumber(1, maxTotalCount - totalCount); + newResultLabelId = randomNumber.getRandomNumber(labelTexts.length - 1); + } else { + cycleCount++; + } + + totalCount++; + } + + protected void resetCounter() { + totalCount = 0; + cycleCount = 0; + maxCycleCount = 0; + + continueSelecting = false; + } + + protected int getNewResultLabelId() { + return newResultLabelId; + } + } + + public boolean getStoppedIndicator() { + return stoppedIndicator.get(); + } + + public SimpleBooleanProperty stoppedIndicatorProperty() { + return stoppedIndicator; + } + + public void setNumberRange(int minNumber, int maxNumber) { + this.numberRange[MIN_NUMBER] = minNumber; + this.numberRange[MAX_NUMBER] = maxNumber; + } + + public void setForceStop(boolean forceStop) { + this.forceStop = forceStop; + } + + public void setMaxTotalCount(int maxTotalCount) { + this.maxTotalCount = maxTotalCount; + } + + public void setSpeed(int speed) { + this.speed = speed; + } +} diff --git a/Dogename/src/main/java/me/lensferno/dogename/utils/HtmlRequseter.java b/Dogename/src/main/java/me/lensferno/dogename/utils/HtmlRequseter.java index eb52641..d0e9431 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/utils/HtmlRequseter.java +++ b/Dogename/src/main/java/me/lensferno/dogename/utils/HtmlRequseter.java @@ -17,6 +17,10 @@ public class HtmlRequseter { static Logger log = LogManager.getLogger(); + public static String getHtml(String address) { + return getHtml(address, false); + } + public static String getHtml(String address,boolean output) { StringBuilder sb = new StringBuilder(); @@ -29,10 +33,6 @@ public class HtmlRequseter { conn.setRequestProperty("Accept-Encoding", "gzip,deflate"); conn.connect(); - System.out.println("--------------------------------------------------------------------"); - System.out.println("[INFO]Getting:"+conn.getURL()); - System.out.println("[INFO]Content compress type:"+conn.getContentEncoding()); - InputStream is = conn.getInputStream(); String connEncoding=conn.getContentEncoding(); diff --git a/Dogename/src/main/java/me/lensferno/dogename/utils/Random.java b/Dogename/src/main/java/me/lensferno/dogename/utils/Random.java new file mode 100644 index 0000000..44bab69 --- /dev/null +++ b/Dogename/src/main/java/me/lensferno/dogename/utils/Random.java @@ -0,0 +1,32 @@ +package me.lensferno.dogename.utils; + +import java.security.SecureRandom; + +public final class Random { + + private final java.util.Random random = new java.util.Random(); + private final java.util.Random secRandom = new SecureRandom(); + + private boolean useSecureRandom = true; + + public void setUseSecureRandom(boolean useSecureRandom) { + this.useSecureRandom = useSecureRandom; + } + + public int getRandomNumber (int minNumber, int maxNumber) { + if (useSecureRandom){ + return minNumber + secRandom.nextInt(maxNumber - minNumber + 1); + } else { + return minNumber + random.nextInt(maxNumber - minNumber + 1); + } + } + + public int getRandomNumber (int maxNumber) { + if (useSecureRandom){ + return secRandom.nextInt(maxNumber + 1); + } else { + return random.nextInt(maxNumber + 1); + } + } + +} diff --git a/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java b/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java index 56ae2cf..921acfa 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java +++ b/Dogename/src/main/java/me/lensferno/dogename/voice/TokenManager.java @@ -14,12 +14,14 @@ public class TokenManager { Logger log= LogManager.getLogger(); public static final String separator=File.separator; - - String cachedVoicePath="caches\\voice\\"; - final private int TOKEN_NULL = -2; - final private int TOKEN_EXPIRED = -1; - final private int TOKEN_OK = 0; + private final int TOKEN_NULL = -2; + private final int TOKEN_EXPIRED = -1; + + public static final int TOKEN_OK = 0; + public static final int TOKEN_BAD = 1; + + private int tokenStatus = TOKEN_OK; final String API_KEY="dIHCtamVdD0ERO1yyFir2iI4"; final String SEC_KEY="HmpBQY3gG4PyZ0cmudnCbMeoMcMejuuW"; @@ -28,36 +30,33 @@ public class TokenManager { File tokenFile=new File("API_voice.token"); - private Token token=null; - - String tokenStatus="ok"; + private Token token = null; private void updateTokenStatus(int statusCode){ switch(statusCode){ - case TOKEN_OK: - tokenStatus="ok"; - break; - - case TOKEN_EXPIRED: - if(netAvailable()){ refreshToken();} + case TOKEN_OK: + tokenStatus = TOKEN_OK; + break; - if(checkTokenAvailable()!=0){ tokenStatus="not ok";} - break; - - case TOKEN_NULL: - if(netAvailable()){ refreshToken();} + case TOKEN_EXPIRED: - if(checkTokenAvailable()!=0){ tokenStatus="not ok";} - break; - - default : - tokenStatus="not ok"; + case TOKEN_NULL: + if(netAvailable()){ + refreshToken(); + } + + if(checkTokenAvailable()!=0){ + tokenStatus = TOKEN_BAD; + } + break; + + default : + tokenStatus = TOKEN_BAD; break; } } private void refreshToken(){ - fetchToken(); writeToken(); } @@ -78,13 +77,13 @@ public class TokenManager { updateTokenStatus(checkTokenAvailable()); }else { - tokenStatus="not ok"; + tokenStatus = TOKEN_BAD; } } } - public String getTokenStatus() { + public int getTokenStatus() { return tokenStatus; } diff --git a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java index 6871076..cfb0f32 100644 --- a/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java +++ b/Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java @@ -1,6 +1,7 @@ package me.lensferno.dogename.voice; import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader; +import me.lensferno.dogename.configs.VoiceConfig; import me.lensferno.dogename.utils.FileProcessor; import okhttp3.*; import org.apache.logging.log4j.LogManager; @@ -23,12 +24,13 @@ public class VoicePlayer { Logger log= LogManager.getLogger("VoicePlayerLogger"); private final String VOICE_API="https://tsn.baidu.com/text2audio"; + private VoiceConfig voiceConfig = null; Token token; - public VoicePlayer(Token token){ - this.token=token; - + public VoicePlayer(Token token, VoiceConfig voiceConfig){ + this.token = token; + this.voiceConfig = voiceConfig; } String cachedVoicePath="caches"+separator+"voice"+separator; @@ -42,10 +44,14 @@ public class VoicePlayer { .build(); - public void playVoice(String name,String speaker,String intonation,String speed) { + public void playVoice(String name) { + String speaker = voiceConfig.getSpeaker(); + String intonation = String.valueOf(voiceConfig.getIntonation()); + String speed = String.valueOf(voiceConfig.getSpeed()); String cachedVoiceName; cachedVoiceName = name + "_" + speaker+ "_" + speed +"_"+intonation; + cachedVoiceName = String.format("%s_%s_%s_%s", name, speaker, speed,intonation); File cachedVoice = new File(cachedVoicePath + cachedVoiceName + ".mp3"); diff --git a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml index 60f439c..0b132bc 100644 --- a/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml +++ b/Dogename/src/main/resources/me/lensferno/dogename/FXMLs/MainInterface.fxml @@ -1,28 +1,18 @@ - - - - - - - - - - -