修改data部分(改了,但没有完全改

main
lensfrex 3 years ago
parent e889b75605
commit e7a2d1f343
No known key found for this signature in database
GPG Key ID: E10FF75B77E1924D
  1. 31
      Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java
  2. 16
      Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java
  3. 44
      Dogename/src/main/java/me/lensferno/dogename/controllers/NameManagerPaneController.java
  4. 12
      Dogename/src/main/java/me/lensferno/dogename/controllers/NumberSettingsPaneController.java
  5. 12
      Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java
  6. 373
      Dogename/src/main/java/me/lensferno/dogename/data/Data.java
  7. 361
      Dogename/src/main/java/me/lensferno/dogename/data/NameData.java
  8. 10
      Dogename/src/main/java/me/lensferno/dogename/select/Selector.java
  9. 20
      Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java

@ -18,7 +18,7 @@ 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.data.Data;
import me.lensferno.dogename.ocr.Ocr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -26,7 +26,6 @@ import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Random;
public final class MainInterfaceController {
@ -75,9 +74,6 @@ public final class MainInterfaceController {
public MainInterfaceController(){
history.loadHistory();
nameData.readIgnoreList();
this.ignoreNameList=nameData.getIgnoreNameList();
this.ignoreNumberList=nameData.getIgnoreNumberList();
}
MainConfig mainConfig;
@ -116,7 +112,7 @@ public final class MainInterfaceController {
new DialogMaker(rootPane).createMessageDialog("(・。・)","安排中......\n为保证运行的稳定,此时还不能进行该操作哦。");
return;
}
NameManagerPaneController nameManagerPaneController =new NameManagerPaneController(nameData,rootPane,ocrTool);
NameManagerPaneController nameManagerPaneController =new NameManagerPaneController(data,rootPane,ocrTool);
new DialogMaker(rootPane).createDialogWithOneBtn("名单管理",nameManagerPaneController);
}
@ -127,7 +123,7 @@ public final class MainInterfaceController {
new DialogMaker(rootPane).createMessageDialog("(・。・)","安排中......\n为保证运行的稳定,此时还不能进行该操作哦。");
return;
}
NumberSettingsPaneController numberSettingsPaneController =new NumberSettingsPaneController(nameData);
NumberSettingsPaneController numberSettingsPaneController =new NumberSettingsPaneController(data);
numberSettingsPaneController.bindProperties(mainConfig);
new DialogMaker(rootPane).createDialogWithOneBtn("调整数字",numberSettingsPaneController);
@ -152,7 +148,7 @@ public final class MainInterfaceController {
miniStage.initStyle(StageStyle.UNDECORATED);
MiniPaneController miniPaneController=loader.getController();
miniPaneController.setBase(nameData, mainConfig, selector);
miniPaneController.setBase(data, mainConfig, selector);
Stage currentStage=(Stage)anPaiBtn.getScene().getWindow();
miniPaneController.setForwStage(currentStage);
@ -178,7 +174,7 @@ public final class MainInterfaceController {
settingsPaneController.setRootPane(rootPane);
settingsPaneController.setNameData(nameData);
settingsPaneController.setData(data);
new DialogMaker(rootPane).createDialogWithOneBtn("更多设置",settingsPaneController);
}
@ -193,16 +189,13 @@ public final class MainInterfaceController {
Random random=new Random();
HashSet<String> ignoreNameList;
HashSet<String> ignoreNumberList;
NameData nameData=new NameData();
Data data =new Data();
Selector selector =new Selector();
public void init(){
selector.initialVariable(mainConfig, nameData, history, upperLabel.textProperty(), downLabel.textProperty());
selector.initialVariable(mainConfig, data, history, upperLabel.textProperty(), downLabel.textProperty());
selector.addStoppedEventListener((observableValue, oldValue, stop)->{
if (stop) {
anPaiBtn.setText("安排一下");
@ -241,15 +234,15 @@ public final class MainInterfaceController {
private void runNameMode(){
if(nameData.isEmpty()){
if(data.isEmpty()){
new DialogMaker(rootPane).createMessageDialog("哦霍~","现在名单还是空的捏~请前往名单管理添加名字 或 使用数字挑选法。");
return;
}
if((nameData.getIgnoreNameList().size() >= nameData.getSize()) && mainConfig.getPassSelectedResult()){
if(data.compareNameIgnoreList() && mainConfig.getPassSelectedResult()){
if(mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部名字都被点完啦!\n要把名字的忽略列表重置吗?", e ->nameData.clearNameIgnoreList());
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部名字都被点完啦!\n要把名字的忽略列表重置吗?", e -> data.clearNameIgnoreList());
}else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部名字都被点完啦!\n请多添加几个名字 或 点击“机会均等”的“重置”按钮。");
}
@ -273,9 +266,9 @@ public final class MainInterfaceController {
return;
}
if(nameData.getIgnoreNumberList().size() >= (maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){
if(data.getNumberIgnoreListSize() >= (maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){
if(mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部数字都被点完啦!\n要把数字的忽略列表重置吗?", e ->nameData.clearNumberIgnoreList());
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部数字都被点完啦!\n要把数字的忽略列表重置吗?", e -> data.clearNumberIgnoreList());
}else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部数字都被点完啦!\n请扩大数字范围 或 点击“机会均等”的“重置”按钮。");
}

@ -11,11 +11,9 @@ import javafx.scene.input.TouchEvent;
import javafx.stage.Stage;
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.NameData;
import me.lensferno.dogename.voice.Token;
import me.lensferno.dogename.data.Data;
import java.util.Random;
@ -41,7 +39,7 @@ public class MiniPaneController {
}
private Random random=new Random();
private NameData nameData;
private Data data;
Stage currentStage;
Scene currentScene;
@ -54,8 +52,8 @@ public class MiniPaneController {
this.currentStage = currentStage;
}
public void setBase(NameData nameData, MainConfig mainConfig, Selector selector){
this.nameData = nameData;
public void setBase(Data data, MainConfig mainConfig, Selector selector){
this.data = data;
this.mainConfig=mainConfig;
this.selector = selector;
}
@ -114,11 +112,11 @@ public class MiniPaneController {
private void runNameMode(){
if(nameData.isEmpty()){
if(data.isEmpty()){
return;
}
if((nameData.getIgnoreNameList().size()>=nameData.getSize())&&mainConfig.getPassSelectedResult()){
if(data.compareNameIgnoreList()&&mainConfig.getPassSelectedResult()){
return;
}
@ -138,7 +136,7 @@ public class MiniPaneController {
return;
}
if(nameData.getIgnoreNumberList().size()>=(maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){
if(data.getNumberIgnoreListSize()>=(maxNumber-minNumber+1) && mainConfig.getPassSelectedResult()){
return;
}

@ -13,7 +13,7 @@ import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.data.NameData;
import me.lensferno.dogename.data.Data;
import me.lensferno.dogename.ocr.Ocr;
import me.lensferno.dogename.utils.Clipboard;
@ -22,7 +22,7 @@ import java.util.logging.Logger;
public class NameManagerPaneController extends VBox {
NameData nameData;
Data data;
Pane rootPane;
Ocr ocrTool;
@ -30,7 +30,7 @@ public class NameManagerPaneController extends VBox {
public static final ObservableList<String> shownNameList = FXCollections.observableArrayList();
public NameManagerPaneController(NameData nameData, Pane rootPane, Ocr ocrTool){
public NameManagerPaneController(Data data, Pane rootPane, Ocr ocrTool){
FXMLLoader loader=new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/NameManagerPane.fxml"));
loader.setRoot(this);
loader.setController(this);
@ -39,10 +39,10 @@ public class NameManagerPaneController extends VBox {
}catch(Exception e){
e.printStackTrace();
}
this.nameData=nameData;
this.data=data;
this.rootPane=rootPane;
shownNameList.setAll(nameData.getNameList());
shownNameList.setAll(data.getNameList());
this.nameList.setItems(shownNameList);
this.ocrTool=ocrTool;
@ -73,10 +73,10 @@ public class NameManagerPaneController extends VBox {
e -> {
String deletedName = nameList.getSelectionModel().getSelectedItems().get(0);
nameData.delete(deletedName);
data.delete(deletedName);
shownNameList.remove(deletedName);
nameData.saveToFile();
data.saveToFile();
System.gc();
});
@ -89,19 +89,19 @@ public class NameManagerPaneController extends VBox {
"真的要删掉所有名字吗?该操作无法撤销,除非您已经备份了名单。",
e -> {
//delete all name
nameData.deleteAll();
shownNameList.setAll(nameData.getNameList());
nameData.saveToFile();
data.deleteAllName();
shownNameList.setAll(data.getNameList());
data.saveToFile();
});
}
@FXML
void makeMass(ActionEvent event) {
nameData.makeMass();
data.makeMass();
shownNameList.clear();
shownNameList.setAll(nameData.getNameList());
nameData.saveToFile();
shownNameList.setAll(data.getNameList());
data.saveToFile();
}
@FXML
@ -110,7 +110,7 @@ public class NameManagerPaneController extends VBox {
fileChooser.setInitialFileName("nameList.txt");
fileChooser.setTitle("想保存到哪?");
File file = fileChooser.showSaveDialog(rootPane.getScene().getWindow());
nameData.exportNameList(file);
data.exportNameList(file);
System.gc();
}
@ -125,14 +125,14 @@ public class NameManagerPaneController extends VBox {
fileChooser.setTitle("告诉我在哪?");
File file = fileChooser.showOpenDialog(rootPane.getScene().getWindow());
nameData.importNameList(file);
data.importNameList(file);
shownNameList.clear();
shownNameList.setAll(nameData.getNameList());
shownNameList.setAll(data.getNameList());
nameData.clearNameIgnoreList();
nameData.clearNumberIgnoreList();
data.clearNameIgnoreList();
data.clearNumberIgnoreList();
nameData.saveToFile();
data.saveToFile();
System.gc();
});
@ -146,12 +146,12 @@ public class NameManagerPaneController extends VBox {
return;
}
nameData.add(inputName.getText());
data.add(inputName.getText());
shownNameList.clear();
shownNameList.setAll(nameData.getNameList());
shownNameList.setAll(data.getNameList());
nameData.saveToFile();
data.saveToFile();
inputName.clear();
System.gc();
}

@ -5,11 +5,11 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.data.NameData;
import me.lensferno.dogename.data.Data;
public class NumberSettingsPaneController extends VBox {
NameData nameData;
public NumberSettingsPaneController(NameData nameData){
Data data;
public NumberSettingsPaneController(Data data){
FXMLLoader loader=new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/NumberSettingPane.fxml"));
loader.setRoot(this);
loader.setController(this);
@ -18,7 +18,7 @@ public class NumberSettingsPaneController extends VBox {
}catch(Exception e){
e.printStackTrace();
}
this.nameData=nameData;
this.data=data;
}
@FXML
@ -30,10 +30,10 @@ public class NumberSettingsPaneController extends VBox {
public void bindProperties(MainConfig mainConfig){
minValueField.textProperty().bindBidirectional(mainConfig.minNumberProperty());
minValueField.textProperty().addListener((observable, oldValue, newValue) -> nameData.clearNumberIgnoreList() );
minValueField.textProperty().addListener((observable, oldValue, newValue) -> data.clearNumberIgnoreList() );
maxValueField.textProperty().bindBidirectional(mainConfig.maxNumberProperty());
maxValueField.textProperty().addListener((observable, oldValue, newValue) -> nameData.clearNumberIgnoreList() );
maxValueField.textProperty().addListener((observable, oldValue, newValue) -> data.clearNumberIgnoreList() );
}
}

@ -12,7 +12,7 @@ import javafx.scene.layout.VBox;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.data.NameData;
import me.lensferno.dogename.data.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -54,7 +54,7 @@ public class SettingsPaneController extends VBox {
Pane rootPane;
NameData nameData;
Data data;
Logger log = LogManager.getLogger("SettingsPaneControllerLogger");
@ -138,8 +138,8 @@ public class SettingsPaneController extends VBox {
@FXML
void clearIgnoreList(ActionEvent event) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("真的吗?","真的要重置吗?",(e)->{
nameData.clearNumberIgnoreList();
nameData.clearNameIgnoreList();
data.clearNumberIgnoreList();
data.clearNameIgnoreList();
});
}
@ -151,7 +151,7 @@ public class SettingsPaneController extends VBox {
}
}
public void setNameData(NameData nameData) {
this.nameData = nameData;
public void setData(Data data) {
this.data = data;
}
}

@ -0,0 +1,373 @@
package me.lensferno.dogename.data;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.*;
public class Data {
Logger log = LogManager.getLogger("dataLogger");
public static final int IGNORELIST_NAME_ONLY = 0;
public static final int IGNORELIST_NUMBER_ONLY = 1;
public static final int IGNORELIST_ALL = 2;
private List<String> nameList;
private IgnoreList ignoreList = new IgnoreList();
File dataFile;
SecureRandom secRandom = new SecureRandom();
public List<String> getNameList() {
return nameList;
}
public void exportNameList(File path) {
if (path != null) {
try {
FileOutputStream oos = new FileOutputStream(path);
oos.write(new Gson().toJson(nameList).getBytes(StandardCharsets.UTF_8));
oos.close();
log.info("Exported list to:" + path.getPath());
} catch (Exception e) {
log.warn("error in export namelist: " + e.toString());
e.printStackTrace();
}
}
}
public void importNameList(File path) {
if (path != null) {
try {
FileInputStream fis = new FileInputStream(path);
String temp;
BufferedReader bis = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
while ((temp = bis.readLine()) != null) {
sb.append(temp);
sb.append("\n");
}
nameList = new Gson().fromJson(sb.toString(), List.class);
log.info("Imported list from:" + path.getPath());
} catch (Exception e) {
log.warn("error in import namelist:" + e.toString());
e.printStackTrace();
}
}
}
public void makeMass() {
HashSet<Integer> alreadyList = new HashSet<>();
List<String> tempList = new LinkedList<>();
int i;
Random random = new Random();
while (tempList.size() < nameList.size()) {
i = random.nextInt(nameList.size());
while (alreadyList.contains(i))
i = random.nextInt(nameList.size());
tempList.add(nameList.get(i));
alreadyList.add(i);
}
nameList.clear();
nameList.addAll(tempList);
}
public Data() {
if (System.getProperty("os.name").toLowerCase().contains("window"))
dataFile = new File("files\\Namelist.data");
else
dataFile = new File("files/Namelist.data");
File oldDataFile = new File("D:\\dogename\\files\\data");
try {
if (oldDataFile.exists()) {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldDataFile));
this.nameList = (ArrayList) ois.readObject();
ois.close();
oldDataFile.delete();
saveToFile();
return;
}
if (!dataFile.exists()) {
dataFile.getParentFile().mkdirs();
dataFile.createNewFile();
nameList = new ArrayList<>();
saveToFile();
return;
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(dataFile));
this.nameList = (ArrayList) ois.readObject();
log.info(nameList.size() + " names loaded.");
} catch (EOFException EOFe) {
nameList = new ArrayList<>();
log.warn("Data file is empty.");
saveToFile();
} catch (Exception e) {
nameList = new ArrayList<>();
saveToFile();
log.warn("Failed to load data file.");
e.printStackTrace();
}
ignoreList.readIgnoreList();
}
public void add(String text) {
String[] splitedText;
if (text.contains("\n") && text.contains("\r")) {
// for windows
splitedText = text.split("\r\n");
nameList.addAll(Arrays.asList(splitedText));
} else if (text.contains("\n")) {
// for linux
splitedText = text.split("\n");
nameList.addAll(Arrays.asList(splitedText));
} else if (text.contains("\r")) {
// for macos
splitedText = text.split("\r");
nameList.addAll(Arrays.asList(splitedText));
} else {
nameList.add(text);
}
System.gc();
}
public boolean compareNameIgnoreList() {
return ignoreList.getNameIgnoreListSize() >= nameList.size();
}
public void delete(String name) {
if (nameList.isEmpty())
return;
nameList.remove(name);
ignoreList.removeName(name);
System.gc();
}
public boolean isEmpty() {
return nameList.isEmpty();
}
Random random = new Random();
public String randomGet(boolean secureRandom) {
if (secureRandom)
return nameList.get(secRandom.nextInt(nameList.size()));
else
return nameList.get(random.nextInt(nameList.size()));
}
public void saveToFile() {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(dataFile));
oos.writeObject(nameList);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteAllName() {
nameList.clear();
ignoreList.clearNameIgnoreList();
}
public boolean checkNameIgnored(String name) {
return ignoreList.checkNameExists(name);
}
public boolean checkNumberIgnored(String number) {
return ignoreList.checkNumberExists(number);
}
public void addNameToIgnoreList(String name) {
ignoreList.addName(name);
}
public void addNumberToIgnoreList(String number) {
ignoreList.addNumber(number);
}
public void writeIgnoreList(int switchy) {
ignoreList.writeIgnoreList(switchy);
}
public void clearNameIgnoreList() {
ignoreList.clearNameIgnoreList();
}
public void clearNumberIgnoreList() {
ignoreList.clearNumberIgnoreList();
}
public int getNumberIgnoreListSize() {
return ignoreList.getNumberIgnoreListSize();
}
class IgnoreList {
private HashSet<String> ignoreNameList = new HashSet<>();
private HashSet<String> ignoreNumberList = new HashSet<>();
private final File nameIgnoreFile = new File("files" + File.separator + "IgnoredNameList.data");
private final File numbIgnoreFile = new File("files" + File.separator + "IgnoredNumberList.data");
public void writeIgnoreList(int switchy) {
switch (switchy) {
case IGNORELIST_NAME_ONLY:
writeNameIgnoreList();
case IGNORELIST_NUMBER_ONLY:
writeNumberIgnoreList();
default:
writeNumberIgnoreList();
writeNameIgnoreList();
}
}
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() {
readNameIgnoreList();
readNumberIgnoreList();
log.info("There are " + ignoreNameList.size() + " names and " + ignoreNumberList.size() + " numbers ignored.");
}
private void readNameIgnoreList() {
try {
if (!nameIgnoreFile.exists()) {
nameIgnoreFile.getParentFile().mkdirs();
nameIgnoreFile.createNewFile();
ignoreNameList = new HashSet<>();
writeIgnoreList(IGNORELIST_NAME_ONLY);
return;
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(nameIgnoreFile));
this.ignoreNameList = (HashSet) ois.readObject();
} catch (EOFException e) {
ignoreNameList = new HashSet<>();
writeIgnoreList(IGNORELIST_NAME_ONLY);
} catch (Exception e) {
ignoreNameList = new HashSet<>();
writeIgnoreList(IGNORELIST_NAME_ONLY);
e.printStackTrace();
}
}
private void readNumberIgnoreList() {
try {
if (!numbIgnoreFile.exists()) {
numbIgnoreFile.getParentFile().mkdirs();
numbIgnoreFile.createNewFile();
ignoreNumberList = new HashSet<>();
writeIgnoreList(IGNORELIST_NUMBER_ONLY);
return;
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(numbIgnoreFile));
this.ignoreNumberList = (HashSet) ois.readObject();
} catch (EOFException e) {
ignoreNumberList = new HashSet<>();
log.warn("Ignored number list is empty.");
writeIgnoreList(IGNORELIST_NUMBER_ONLY);
} catch (Exception e) {
ignoreNumberList = new HashSet<>();
log.warn("Failed to load ignored number list");
writeIgnoreList(IGNORELIST_NUMBER_ONLY);
e.printStackTrace();
}
}
protected void clearNameIgnoreList() {
ignoreNameList.clear();
writeIgnoreList(IGNORELIST_NAME_ONLY);
}
protected void clearNumberIgnoreList() {
ignoreNumberList.clear();
writeIgnoreList(IGNORELIST_NUMBER_ONLY);
}
protected int getNameIgnoreListSize() {
return ignoreNameList.size();
}
public int getNumberIgnoreListSize() {
return ignoreNumberList.size();
}
protected void removeName(String name) {
ignoreNameList.remove(name);
}
protected void addName(String name) {
ignoreNameList.add(name);
}
protected void addNumber(String number) {
ignoreNumberList.add(number);
}
protected boolean checkNameExists(String name) {
return ignoreNameList.contains(name);
}
protected boolean checkNumberExists(String number) {
return ignoreNumberList.contains(number);
}
}
}

@ -1,361 +0,0 @@
package me.lensferno.dogename.data;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.*;
public class NameData {
Logger log = LogManager.getLogger("NameDataLogger");
private List<String> nameList;
private List<String> chooseList;
private int listSize = 0;
public static final int NAME_ONLY = 0;
public static final int NUMBER_ONLY = 1;
HashSet<String> ignoreNameList=new HashSet<>();
HashSet<String> ignoreNumberList=new HashSet<>();
File dataFile ;//=new File("namelist.data");
boolean newAlgo=true;
SecureRandom secRandom =new SecureRandom();
//不做注释了,自己慢慢看。:)
File nameIgnoreFile =new File("files"+File.separator+"IgnoredNameList.data");
File numbIgnoreFile =new File("files"+File.separator+"IgnoredNumberList.data");
public void writeIgnoreList(int switchy){
switch (switchy) {
case NAME_ONLY:
writeNameIgnoreList();
case NUMBER_ONLY:
writeNumberIgnoreList();
default:
writeNumberIgnoreList();
writeNameIgnoreList();
}
}
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(){
try{
if(!nameIgnoreFile.exists()){
nameIgnoreFile.getParentFile().mkdirs();
nameIgnoreFile.createNewFile();
ignoreNameList= new HashSet<>();
writeIgnoreList(NAME_ONLY);
return;
}
ObjectInputStream ois =new ObjectInputStream(new FileInputStream(nameIgnoreFile));
this.ignoreNameList=(HashSet)ois.readObject();
}catch (EOFException e){
ignoreNameList=new HashSet<>();
log.warn("Past name list is empty.");
writeIgnoreList(NAME_ONLY);
}catch (Exception e){
ignoreNameList=new HashSet<>();
writeIgnoreList(NAME_ONLY);
log.warn("Failed to load past name list:"+e.toString());
e.printStackTrace();
}
try{
if(!numbIgnoreFile.exists()){
numbIgnoreFile.getParentFile().mkdirs();
numbIgnoreFile.createNewFile();
ignoreNumberList= new HashSet<>();
writeIgnoreList(NUMBER_ONLY);
return;
}
ObjectInputStream ois =new ObjectInputStream(new FileInputStream(numbIgnoreFile));
this.ignoreNumberList=(HashSet)ois.readObject();
}catch (EOFException e){
ignoreNumberList=new HashSet<>();
log.warn("Ignored number list is empty.");
writeIgnoreList(NUMBER_ONLY);
}catch (Exception e){
ignoreNumberList=new HashSet<>();
log.warn("Failed to load ignored number list");
writeIgnoreList(NUMBER_ONLY);
e.printStackTrace();
}
log.info("There are "+ignoreNameList.size()+" names and "+ignoreNumberList.size()+" numbers ignored.");
}
public void clearNameIgnoreList(){
ignoreNameList.clear();
writeIgnoreList(NAME_ONLY);
}
public void clearNumberIgnoreList(){
ignoreNumberList.clear();
writeIgnoreList(NUMBER_ONLY);
}
public HashSet<String> getIgnoreNameList() {
return ignoreNameList;
}
public void setIgnoreNameList(HashSet<String> ignoreNameList) {
this.ignoreNameList = ignoreNameList;
}
public HashSet<String> getIgnoreNumberList() {
return ignoreNumberList;
}
public void setIgnoreNumberList(HashSet<String> ignoreNumberList) {
this.ignoreNumberList = ignoreNumberList;
}
public List<String> getNameList() {
return nameList;
}
public void exportNameList(File path) {
if(path!=null) {
try{
FileOutputStream oos =new FileOutputStream(path);
oos.write(new Gson().toJson(nameList).getBytes(StandardCharsets.UTF_8));
oos.close();
log.info("Exported list to:"+path.getPath());
}catch (Exception e){log.warn("error in export namelist: "+e.toString());e.printStackTrace();}
}
}
public void importNameList(File path) {
if(path!=null) {
try{
FileInputStream fis =new FileInputStream(path);
String temp;
BufferedReader bis=new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));
StringBuilder sb=new StringBuilder();
while ((temp = bis.readLine()) != null) {
sb.append(temp);
sb.append("\n");
}
nameList=new Gson().fromJson(sb.toString(),List.class);
log.info("Imported list from:"+path.getPath());
}catch (Exception e){log.warn("error in import namelist:"+e.toString());e.printStackTrace();}
}
}
public void makeMass() {
HashSet<Integer> alreadyList = new HashSet<>();
List<String> tempList = new LinkedList<>();
int i;
Random random = new Random();
while (tempList.size() < nameList.size()) {
i = random.nextInt(nameList.size());
while (alreadyList.contains(i))
i = random.nextInt(nameList.size());
tempList.add(nameList.get(i));
alreadyList.add(i);
}
nameList.clear();
nameList.addAll(tempList);
}
//------------------------------------------------------
public void setNewAlgo(boolean newAlgo) {
this.newAlgo=newAlgo;
if(newAlgo)
System.out.println("[INFO]Use SecureRandom");
else
System.out.println("[INFO]Not use SecureRandom");
}
//------------------------------------------------------
public NameData(){
if(System.getProperty("os.name").toLowerCase().contains("window"))
dataFile=new File("files\\Namelist.data");
else
dataFile=new File("files/Namelist.data");
File oldDataFile=new File("D:\\dogename\\files\\data");
try{
if(oldDataFile.exists()) {
ObjectInputStream ois =new ObjectInputStream(new FileInputStream(oldDataFile));
this.nameList=(ArrayList)ois.readObject();
listSize=nameList.size();
this.chooseList=new ArrayList<>(nameList);
ois.close();
oldDataFile.delete();
saveToFile();
return;
}
if(!dataFile.exists()){
dataFile.getParentFile().mkdirs();
dataFile.createNewFile();
nameList= new ArrayList<>();
saveToFile();
return;
}
ObjectInputStream ois =new ObjectInputStream(new FileInputStream(dataFile));
this.nameList=(ArrayList)ois.readObject();
log.info(nameList.size()+" names loaded.");
listSize=nameList.size();
this.chooseList=new ArrayList<>(nameList);
}catch (EOFException EOFe){
nameList=new ArrayList<>();
chooseList=new ArrayList<>();
log.warn("Data file is empty.");
saveToFile();
}catch (Exception e){
nameList=new ArrayList<>();
chooseList=new ArrayList<>();
saveToFile();
log.warn("Failed to load data file.");
e.printStackTrace();
}
}
//------------------------------------------------------
public void add(String text){
String[] splitedText;
if(text.contains("\n")&&text.contains("\r")){//-----------windows
splitedText=text.split("\r\n");
nameList.addAll(Arrays.asList(splitedText));
}else if(text.contains("\n")){//--------------------------linux,unix
splitedText=text.split("\n");
nameList.addAll(Arrays.asList(splitedText));
}else if(text.contains("\r")){//--------------------------macos
splitedText=text.split("\r");
nameList.addAll(Arrays.asList(splitedText));
}else {
nameList.add(text);
listSize=nameList.size();
}
chooseList=new ArrayList<>(nameList);
System.gc();
}
//------------------------------------------------------
public String get(int i){
if(i<listSize-1)
return null;
else
return nameList.get(i);
}
//------------------------------------------------------
public int getSize(){
return nameList.size();
}
//------------------------------------------------------
public void delete(String name){
if(nameList.isEmpty())
return;
nameList.remove(name);
ignoreNameList.remove(name);
chooseList=new ArrayList<>(nameList);
listSize=nameList.size();
System.gc();
}
//------------------------------------------------------
public boolean isEmpty(){
return nameList.isEmpty();
}
Random random =new Random();
//------------------------------------------------------
public String randomGet(boolean secureRandom){
if(secureRandom)
return nameList.get(secRandom.nextInt(nameList.size()));
else
return nameList.get(random.nextInt(nameList.size()));
}
//------------------------------------------------------
public String[] getAll(){
return nameList.toArray(new String[0]);
}
//------------------------------------------------------
public void saveToFile(){
try{
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream(dataFile));
oos.writeObject(nameList);
oos.close();
}catch (Exception e){
e.printStackTrace();
}
}
//------------------------------------------------------
public void deleteAll(){
nameList.clear();
chooseList.clear();
}
//------------------------------------------------------
}

@ -7,7 +7,7 @@ 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.data.Data;
import me.lensferno.dogename.select.core.Worker;
import me.lensferno.dogename.voice.TokenManager;
import me.lensferno.dogename.voice.VoicePlayer;
@ -43,8 +43,8 @@ public final class Selector {
processor.stopProcess();
}
public void initialVariable(MainConfig config, NameData nameData, History history, StringProperty... labelTexts) {
processor.initialVariable(config, voicePlayer, nameData, history, labelTexts);
public void initialVariable(MainConfig config, Data data, History history, StringProperty... labelTexts) {
processor.initialVariable(config, voicePlayer, data, history, labelTexts);
}
public boolean isWorkerRunning() {
@ -62,8 +62,8 @@ public final class Selector {
MainConfig config = null;
protected void initialVariable(MainConfig config, VoicePlayer voicePlayer, NameData nameData, History history, StringProperty... labelTexts) {
coreWorker = new Worker(labelTexts, config, nameData, history, voicePlayer);
protected void initialVariable(MainConfig config, VoicePlayer voicePlayer, Data data, History history, StringProperty... labelTexts) {
coreWorker = new Worker(labelTexts, config, data, history, voicePlayer);
this.config = config;
}

@ -4,7 +4,7 @@ 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.data.Data;
import me.lensferno.dogename.utils.Random;
import me.lensferno.dogename.voice.VoicePlayer;
@ -20,7 +20,7 @@ public final class Worker {
//挑选方法
private final int selectMethod = MainConfig.METHOD_NAME;
private final NameData nameData;
private final Data data;
private final History history;
@ -52,10 +52,10 @@ public final class Worker {
private final Counter counter = new Counter();
public Worker(StringProperty[] labelTexts, MainConfig config, NameData nameData, History history, VoicePlayer voicePlayer) {
public Worker(StringProperty[] labelTexts, MainConfig config, Data data, History history, VoicePlayer voicePlayer) {
this.labelTexts = labelTexts;
this.config = config;
this.nameData = nameData;
this.data = data;
this.history = history;
this.voicePlayer = voicePlayer;
}
@ -77,7 +77,7 @@ public final class Worker {
// 如果不是这三种情况就选到符合其中一种情况为止
// 由于后两者在安排进程中不可能改变,因此继续挑选后只有选中的结果不在忽略名单中才会结束挑选
// 若忽略名单已满(名单中数量=总的名单数量),点击“安排一下”时会提示已满,无法再进行挑选,因此在运行时总有 忽略名单<总名单,总有名字不在忽略列表中,不会死循环,放心吧
boolean resultIgnored = (config.getChooseMethod() == MainConfig.METHOD_NAME) ? (nameData.getIgnoreNameList().contains(selectedResult)) : (nameData.getIgnoreNumberList().contains(selectedResult));
boolean resultIgnored = (config.getChooseMethod() == MainConfig.METHOD_NAME) ? (data.checkNameIgnored(selectedResult)) : (data.checkNumberIgnored(selectedResult));
if (!resultIgnored || !config.getPassSelectedResult() || forceStop) {
this.stopSelect();
finalResult = true;
@ -96,11 +96,15 @@ public final class Worker {
public void stopSelect() {
if (config.getPassSelectedResult()) {
nameData.getIgnoreNameList().add(selectedResult);
if (config.nameChooseProperty().getValue()) {
data.addNameToIgnoreList(selectedResult);
} else {
data.addNumberToIgnoreList(selectedResult);
}
}
if (config.getEqualMode()) {
nameData.writeIgnoreList(NameData.NAME_ONLY);
data.writeIgnoreList(Data.IGNORELIST_ALL);
}
history.addHistory(selectedResult);
@ -118,7 +122,7 @@ public final class Worker {
private String pick(int selectMethod) {
switch (selectMethod) {
case MainConfig.METHOD_NAME:
return nameData.randomGet(config.getSecureRandom());
return data.randomGet(config.getSecureRandom());
case MainConfig.METHOD_NUMBER:
randomNumber.setUseSecureRandom(config.getSecureRandom());
return String.valueOf(randomNumber.getRandomNumber(numberRange[MIN_NUMBER], numberRange[MAX_NUMBER]));

Loading…
Cancel
Save