OSDN Git Service

chore: increment version to v1.3.1
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / main / DelesteRandomSelector.java
1 package com.ranfa.main;
2
3 import java.awt.BorderLayout;
4 import java.awt.EventQueue;
5 import java.awt.Font;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.util.ArrayList;
9 import java.util.Random;
10 import java.util.concurrent.CompletableFuture;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13 import java.util.function.BiConsumer;
14
15 import javax.swing.DefaultComboBoxModel;
16 import javax.swing.JButton;
17 import javax.swing.JCheckBox;
18 import javax.swing.JComboBox;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21 import javax.swing.JOptionPane;
22 import javax.swing.JPanel;
23 import javax.swing.JScrollPane;
24 import javax.swing.JSpinner;
25 import javax.swing.JTextArea;
26 import javax.swing.border.EmptyBorder;
27
28 import com.jgoodies.forms.layout.ColumnSpec;
29 import com.jgoodies.forms.layout.FormLayout;
30 import com.jgoodies.forms.layout.FormSpecs;
31 import com.jgoodies.forms.layout.RowSpec;
32 import com.ranfa.lib.CheckVersion;
33 import com.ranfa.lib.LimitedLog;
34 import com.ranfa.lib.Scraping;
35 import com.ranfa.lib.SettingJSONProperty;
36 import com.ranfa.lib.Settings;
37 import com.ranfa.lib.Song;
38 import com.ranfa.lib.TwitterIntegration;
39 import com.ranfa.lib.Version;
40
41 @Version(major = 1, minor = 3, patch = 1)
42 public class DelesteRandomSelector extends JFrame {
43
44         private static ArrayList<Song> selectedSongsList = new ArrayList<Song>();
45
46         private JPanel contentPane;
47         private JPanel panelNorth;
48         private JPanel panelWest;
49         private JLabel labelVersion;
50         private JLabel labelTitle;
51         private JLabel labelDifficulty;
52         private JLabel labelLevel;
53         private JSpinner spinnerLevel;
54         private JCheckBox checkMoreLv;
55         private JCheckBox checkLessLv;
56         private JPanel panelEast;
57         private JPanel panelCentre;
58         private JButton btnImport;
59         private JButton btnStart;
60         private JButton btnExit;
61         private JComboBox comboDifficultySelect;
62         private JLabel labelLvCaution;
63         private JComboBox comboAttribute;
64         private SettingJSONProperty property = new SettingJSONProperty();
65         private JButton btnTwitterIntegration;
66         private String[] integratorArray;
67         private boolean integratorBool = false;
68         private JTextArea textArea;
69         private JScrollPane scrollPane;
70
71         /**
72          * Launch the application.
73          */
74         public static void main(String[] args) {
75                 EventQueue.invokeLater(new Runnable() {
76                         public void run() {
77                                 try {
78                                         DelesteRandomSelector frame = new DelesteRandomSelector();
79                                         frame.setVisible(true);
80                                 } catch (Exception e) {
81                                         e.printStackTrace();
82                                 }
83                         }
84                 });
85         }
86
87         /**
88          * log file prefix:
89          *  this.getClass() + ":[LEVEL]: " +
90          */
91
92         /**
93          * Create the frame.
94          */
95         public DelesteRandomSelector() {
96                 boolean isFirst = !Scraping.databaseExists();
97                 if(isFirst) {
98                         JOptionPane.showMessageDialog(this, "楽曲データベースが見つかりませんでした。自動的に作成されます…\n注意:初回起動ではなく、かつ、Jarファイルと同じ階層に\"database.json\"というファイルが存在するにも関わらず\nこのポップアップが出た場合、開発者までご一報ください。\nGithub URL: https://github.com/hizumiaoba/DelesteRandomSelector/issues");
99                         if(!Scraping.writeToJson(Scraping.getWholeData())) {
100                                 JOptionPane.showMessageDialog(this, "Exception:NullPointerException\\nCannot Keep up! Please re-download this Application!");
101                                 throw new NullPointerException("FATAL: cannot continue!");
102                         }
103                 }
104                 ExecutorService es = Executors.newWorkStealingPool();
105                 CompletableFuture<ArrayList<Song>> getFromJsonFuture = CompletableFuture.supplyAsync(() -> Scraping.getFromJson(), es);
106                 CompletableFuture<ArrayList<Song>> getWholeDataFuture = CompletableFuture.supplyAsync(() -> Scraping.getWholeData(), es);
107                 if(!Settings.fileExists() && !Settings.writeDownJSON()) {
108                         JOptionPane.showMessageDialog(this, "Exception:NullPointerException\nCannot Keep up! Please re-download this Application!");
109                         throw new NullPointerException("FATAL: cannot continue!");
110                 }
111                 LimitedLog.println(this.getClass() + ":[DEBUG]: " + "Loading Settings...");
112                 property.setCheckLibraryUpdates(Settings.needToCheckLibraryUpdates());
113                 property.setCheckVersion(Settings.needToCheckVersion());
114                 property.setWindowWidth(Settings.getWindowWidth());
115                 property.setWindowHeight(Settings.getWindowHeight());
116                 property.setSongLimit(Settings.getSongsLimit());
117                 property.setSaveScoreLog(Settings.saveScoreLog());
118                 property.setOutputDebugSentences(Settings.outputDebugSentences());
119                 LimitedLog.println(this.getClass() + ":[DEBUG]: " + "Loading Settings done."
120                                 + "\nVersion Check: " + property.isCheckVersion()
121                                 + "\nLibrary Update Check: " + property.isCheckLibraryUpdates()
122                                 + "\nWindow Width: " + property.getWindowWidth()
123                                 + "\nWindow Height: " + property.getWindowHeight()
124                                 + "\nSong Limit: " + property.getSongLimit()
125                                 + "\nSaveScoreLog: " + property.isSaveScoreLog()
126                                 + "\nOutputDebugSentences: " + property.isOutputDebugSentences());
127                 if(property.isCheckVersion())
128                         CheckVersion.needToBeUpdated();
129                 BiConsumer<ArrayList<Song>, ArrayList<Song>> updateConsumer = (list1, list2) -> {
130                         LimitedLog.println(this.getClass() + ":[INFO]: " + "Checking database updates...");
131                         if(list1.size() > list2.size()) {
132                                 long time = System.currentTimeMillis();
133                                 LimitedLog.println(this.getClass() + ":[INFO]: " + (list1.size() - list2.size()) + " Update detected.");
134                                 Scraping.writeToJson(list1);
135                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Update completed in " + (System.currentTimeMillis() - time) + "ms");
136                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Updated database size: " + list1.size());
137                         } else {
138                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "database is up-to-date.");
139                         }
140                 };
141                 Runnable setEnabled = () -> {
142                         try {
143                                 Thread.sleep(1000);
144                         } catch (InterruptedException e1) {
145                                 // TODO 自動生成された catch ブロック
146                                 e1.printStackTrace();
147                         }
148                         btnImport.setEnabled(true);
149                         btnImport.setText("<html><body>楽曲<br>絞り込み</body></html>");
150                 };
151                 getWholeDataFuture.thenAcceptAsync(list -> LimitedLog.println(this.getClass() + ":[INFO]: Scraping data size:" + list.size()), es);
152                 getFromJsonFuture.thenAcceptAsync(list -> LimitedLog.println(this.getClass() + ":[INFO]: Currently database size:" + list.size()), es);
153                 if(property.isCheckLibraryUpdates()) {
154                         CompletableFuture<Void> updatedFuture = getWholeDataFuture.thenAcceptBothAsync(getFromJsonFuture, updateConsumer, es);
155                         updatedFuture.thenRunAsync(setEnabled, es);
156                 }
157                 LimitedLog.println(this.getClass() + ":[DEBUG]: " + "Version:" + CheckVersion.getVersion());
158                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159                 setBounds(100, 100, property.getWindowWidth(), property.getWindowHeight());
160                 contentPane = new JPanel();
161                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
162                 setContentPane(contentPane);
163                 contentPane.setLayout(new BorderLayout(0, 0));
164
165                 panelNorth = new JPanel();
166                 contentPane.add(panelNorth, BorderLayout.NORTH);
167                 panelNorth.setLayout(new FormLayout(new ColumnSpec[] {
168                                 ColumnSpec.decode("max(302dlu;default)"),
169                                 FormSpecs.RELATED_GAP_COLSPEC,
170                                 ColumnSpec.decode("40px"),},
171                         new RowSpec[] {
172                                 RowSpec.decode("20px"),}));
173
174                 labelTitle = new JLabel("デレステ課題曲セレクター");
175                 labelTitle.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 16));
176                 panelNorth.add(labelTitle, "1, 1, center, top");
177
178                 labelVersion = new JLabel(CheckVersion.getVersion());
179                 labelVersion.setFont(new Font("SansSerif", Font.BOLD, 12));
180                 panelNorth.add(labelVersion, "3, 1, right, top");
181
182                 panelWest = new JPanel();
183                 contentPane.add(panelWest, BorderLayout.WEST);
184                 panelWest.setLayout(new FormLayout(new ColumnSpec[] {
185                                 FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
186                                 ColumnSpec.decode("112px:grow"),},
187                         new RowSpec[] {
188                                 FormSpecs.LINE_GAP_ROWSPEC,
189                                 RowSpec.decode("19px"),
190                                 FormSpecs.RELATED_GAP_ROWSPEC,
191                                 RowSpec.decode("max(12dlu;default)"),
192                                 FormSpecs.RELATED_GAP_ROWSPEC,
193                                 RowSpec.decode("max(12dlu;default)"),
194                                 FormSpecs.RELATED_GAP_ROWSPEC,
195                                 RowSpec.decode("12dlu"),
196                                 FormSpecs.RELATED_GAP_ROWSPEC,
197                                 RowSpec.decode("max(12dlu;default)"),
198                                 FormSpecs.RELATED_GAP_ROWSPEC,
199                                 RowSpec.decode("max(12dlu;default)"),
200                                 FormSpecs.RELATED_GAP_ROWSPEC,
201                                 RowSpec.decode("max(12dlu;default)"),
202                                 FormSpecs.RELATED_GAP_ROWSPEC,
203                                 RowSpec.decode("max(52dlu;default)"),}));
204
205                 labelDifficulty = new JLabel("難易度選択");
206                 labelDifficulty.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
207                 panelWest.add(labelDifficulty, "2, 2, center, default");
208
209                 comboDifficultySelect = new JComboBox();
210                 comboDifficultySelect.setFont(new Font("Dialog", Font.BOLD, 12));
211                 comboDifficultySelect.setModel(new DefaultComboBoxModel(new String[] {"指定なし", "DEBUT", "REGULAR", "PRO", "MASTER", "MASTER+", "ⓁMASTER+", "LIGHT", "TRICK", "PIANO", "FORTE", "WITCH"}));
212                 panelWest.add(comboDifficultySelect, "2, 4, fill, default");
213
214                                 comboAttribute = new JComboBox();
215                                 comboAttribute.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
216                                 comboAttribute.setModel(new DefaultComboBoxModel(new String[] {"指定なし", "全タイプ", "キュート", "クール", "パッション"}));
217                                 panelWest.add(comboAttribute, "2, 6, fill, default");
218
219                                 labelLevel = new JLabel("楽曲Lv");
220                                 labelLevel.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
221                                 panelWest.add(labelLevel, "2, 8, center, default");
222
223                                 spinnerLevel = new JSpinner();
224                                 panelWest.add(spinnerLevel, "2, 10");
225
226                                 checkLessLv = new JCheckBox("指定Lv以下");
227                                 checkLessLv.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
228                                 panelWest.add(checkLessLv, "2, 12");
229
230                                 checkMoreLv = new JCheckBox("指定Lv以上");
231                                 checkMoreLv.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
232                                 panelWest.add(checkMoreLv, "2, 14");
233
234                                 labelLvCaution = new JLabel("<html><body>※以下以上両方にチェックをつけることで指定レベルのみ絞り込むことができます</body></html>");
235                                 labelLvCaution.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
236                                 panelWest.add(labelLvCaution, "2, 16, fill, fill");
237
238                 panelEast = new JPanel();
239                 contentPane.add(panelEast, BorderLayout.EAST);
240                 panelEast.setLayout(new FormLayout(new ColumnSpec[] {
241                                 ColumnSpec.decode("98px"),},
242                         new RowSpec[] {
243                                 RowSpec.decode("26px"),
244                                 FormSpecs.RELATED_GAP_ROWSPEC,
245                                 RowSpec.decode("max(36dlu;default)"),
246                                 FormSpecs.RELATED_GAP_ROWSPEC,
247                                 FormSpecs.DEFAULT_ROWSPEC,
248                                 FormSpecs.RELATED_GAP_ROWSPEC,
249                                 RowSpec.decode("max(30dlu;default)"),
250                                 FormSpecs.RELATED_GAP_ROWSPEC,
251                                 RowSpec.decode("max(15dlu;default)"),
252                                 FormSpecs.RELATED_GAP_ROWSPEC,
253                                 RowSpec.decode("max(11dlu;default)"),
254                                 FormSpecs.RELATED_GAP_ROWSPEC,
255                                 FormSpecs.DEFAULT_ROWSPEC,
256                                 FormSpecs.RELATED_GAP_ROWSPEC,
257                                 FormSpecs.DEFAULT_ROWSPEC,
258                                 FormSpecs.RELATED_GAP_ROWSPEC,
259                                 FormSpecs.DEFAULT_ROWSPEC,}));
260
261                 btnImport = new JButton("<html><body>データベース<br>更新中…</body></html>");
262                 btnImport.setEnabled(false);
263                 btnImport.addActionListener(new ActionListener() {
264                         public void actionPerformed(ActionEvent e) {
265                                 ArrayList<Song> fromJson = Scraping.getFromJson();
266                                         ArrayList<Song> specificlevelList = Scraping.getSpecificLevelSongs(fromJson, (Integer)spinnerLevel.getValue(), checkLessLv.isSelected(), checkMoreLv.isSelected());
267                                         ArrayList<Song> specificDifficultyList = Scraping.getSpecificDifficultySongs(specificlevelList, comboDifficultySelect.getSelectedItem().toString());
268                                         ArrayList<Song> specificAttributeList = Scraping.getSpecificAttributeSongs(specificDifficultyList, comboAttribute.getSelectedItem().toString());
269                                         if(!selectedSongsList.isEmpty())
270                                         selectedSongsList.clear();
271                                 selectedSongsList.addAll(specificAttributeList);
272                                 LimitedLog.println(this.getClass() + ":[INFO]: " +"Songs are selected.We are Ready to go.");
273                                 JOptionPane.showMessageDialog(null, "絞り込み完了!「開始」をクリックすることで選曲できます!");
274                         }
275                 });
276                 btnImport.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
277                 panelEast.add(btnImport, "1, 3, fill, fill");
278
279                 btnStart = new JButton("開始!");
280                 btnStart.addActionListener(new ActionListener() {
281                         public void actionPerformed(ActionEvent e) {
282                                 Random random = new Random(System.currentTimeMillis());
283                                 String[] tmp = new String[property.getSongLimit()];
284                                 integratorArray = new String[property.getSongLimit()];
285                                 for(int i = 0; i < property.getSongLimit(); i++) {
286                                         int randomInt = random.nextInt(selectedSongsList.size());
287                                         tmp[i] = (i + 1) + "曲目: " + selectedSongsList.get(randomInt).getAttribute() + " [" + selectedSongsList.get(randomInt).getDifficulty() + "]「" + selectedSongsList.get(randomInt).getName() + "」!(Lv:" + selectedSongsList.get(randomInt).getLevel() + ")\n\n";
288                                         integratorArray[i] = selectedSongsList.get(randomInt).getName() + "(Lv" + selectedSongsList.get(randomInt).getLevel() + ")\n";
289                                 }
290                                 String paneString = "";
291                                 for (int i = 0; i < tmp.length; i++) {
292                                         paneString = paneString + tmp[i];
293                                 }
294                                 paneString = paneString + "この" + tmp.length + "曲をプレイしましょう!!!";
295                                 textArea.setText(paneString);
296                                 integratorBool = true;
297                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "show up completed.");
298                         }
299                 });
300                 btnStart.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
301                 panelEast.add(btnStart, "1, 7, fill, fill");
302
303                                 btnTwitterIntegration = new JButton("Twitter連携");
304                                 btnTwitterIntegration.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 11));
305                                 btnTwitterIntegration.addActionListener(new ActionListener() {
306                                         public void actionPerformed(ActionEvent e) {
307                                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Twitter Integration requested.Verify permission status.");
308                                                 boolean authorizationStatus = TwitterIntegration.authorization();
309                                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Permission Verifying completed.\nStatus: " + authorizationStatus);
310                                                 LimitedLog.print(this.getClass() + ":[INFO]: " + "Construction status message...");
311                                                 String updatedStatus = "デレステ課題曲セレクターで\n";
312                                                 int lengthLimit = updatedStatus.length();
313                                                 boolean isBroken = false;
314                                                 if(!integratorBool) {
315                                                         JOptionPane.showMessageDialog(null, "ちひろ「まだプレイを始めていないみたいですね」");
316                                                         LimitedLog.println();
317                                                         return;
318                                                 }
319                                                 for(int i = 0; i < integratorArray.length; i++) {
320                                                         updatedStatus = updatedStatus + integratorArray[i];
321                                                         lengthLimit += integratorArray[i].length();
322                                                         if(lengthLimit > 69) {
323                                                                 isBroken = true;
324                                                                 break;
325                                                         }
326                                                 }
327                                                 if(isBroken) {
328                                                         updatedStatus = updatedStatus + "…その他数曲\nをプレイしました!\n#DelesteRandomSelector #デレステ ";
329                                                 } else {
330                                                         updatedStatus = updatedStatus + "をプレイしました!\n#DelesteRandomSelector #デレステ ";
331                                                 }
332                                                 LimitedLog.println("completed.\n" + updatedStatus);
333                                                 lengthLimit = updatedStatus.length();
334                                                 if(authorizationStatus) {
335                                                         int option = JOptionPane.showConfirmDialog(null, "Twitterへ以下の内容を投稿します。よろしいですか?\n\n" + updatedStatus + "\n\n文字数:" + lengthLimit);
336                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "User selected " + option);
337                                                         switch(option) {
338                                                                 case JOptionPane.OK_OPTION:
339                                                                         TwitterIntegration.PostTwitter(updatedStatus);
340                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "Success to update the status.");
341                                                                         JOptionPane.showMessageDialog(null, "投稿が完了しました。");
342                                                                         break;
343                                                                 case JOptionPane.NO_OPTION:
344                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "There is no will to post.");
345                                                                         break;
346                                                                 case JOptionPane.CANCEL_OPTION:
347                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "The Operation was canceled by user.");
348                                                                         break;
349                                                                 default:
350                                                                         break;
351                                                         }
352                                                 } else {
353                                                         LimitedLog.println(this.getClass() + ":[WARN]: " + "seems to reject the permission.it should need try again.");
354                                                 }
355                                         }
356                                 });
357                                 panelEast.add(btnTwitterIntegration, "1, 11");
358
359                                                                 btnExit = new JButton("終了");
360                                                                 btnExit.addActionListener(new ActionListener() {
361                                                                         public void actionPerformed(ActionEvent e) {
362                                                                                 LimitedLog.println(this.getClass() + ":[INFO]: " +"Requested Exit by Button");
363                                                                                 System.exit(0);
364                                                                         }
365                                                                 });
366                                                                 btnExit.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
367                                                                 panelEast.add(btnExit, "1, 13");
368
369                 panelCentre = new JPanel();
370                 contentPane.add(panelCentre, BorderLayout.CENTER);
371                 panelCentre.setLayout(new BorderLayout(0, 0));
372
373                 textArea = new JTextArea();
374                 textArea.setText("楽曲選択の手順\r\n1.難易度、属性、レベルを選択する\r\n2.「楽曲取り込み」ボタンを押す!\r\n3.「開始」ボタンを押す!\r\n4.選択された楽曲がここに表示されます!\r\n現在設定されている楽曲選択の最大数:" + property.getSongLimit());
375                 textArea.setEditable(false);
376
377                 scrollPane = new JScrollPane(textArea);
378                 panelCentre.add(scrollPane, BorderLayout.CENTER);
379                 if(isFirst || !property.isCheckLibraryUpdates())
380                         setEnabled.run();
381         }
382
383 }