OSDN Git Service

feat: Now software version check is available.
[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 = 0)
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]: " + "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                         btnImport.setEnabled(true);
143                         btnImport.setText("<html><body>楽曲<br>絞り込み</body></html>");
144                 };
145                 getWholeDataFuture.thenAcceptAsync(list -> LimitedLog.println(this.getClass() + ":[INFO]: Scraping data size:" + list.size()), es);
146                 getFromJsonFuture.thenAcceptAsync(list -> LimitedLog.println(this.getClass() + ":[INFO]: Currently database size:" + list.size()), es);
147                 CompletableFuture<Void> updatedFuture = getWholeDataFuture.thenAcceptBothAsync(getFromJsonFuture, updateConsumer, es);
148                 updatedFuture.thenRunAsync(setEnabled, es);
149                 LimitedLog.println(this.getClass() + ":[DEBUG]: " + "Version:" + CheckVersion.getVersion());
150                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
151                 setBounds(100, 100, 640, 360);
152                 contentPane = new JPanel();
153                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
154                 setContentPane(contentPane);
155                 contentPane.setLayout(new BorderLayout(0, 0));
156
157                 panelNorth = new JPanel();
158                 contentPane.add(panelNorth, BorderLayout.NORTH);
159                 panelNorth.setLayout(new FormLayout(new ColumnSpec[] {
160                                 ColumnSpec.decode("max(302dlu;default)"),
161                                 FormSpecs.RELATED_GAP_COLSPEC,
162                                 ColumnSpec.decode("40px"),},
163                         new RowSpec[] {
164                                 RowSpec.decode("20px"),}));
165
166                 labelTitle = new JLabel("デレステ課題曲セレクター");
167                 labelTitle.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 16));
168                 panelNorth.add(labelTitle, "1, 1, center, top");
169
170                 labelVersion = new JLabel(CheckVersion.getVersion());
171                 labelVersion.setFont(new Font("SansSerif", Font.BOLD, 12));
172                 panelNorth.add(labelVersion, "3, 1, right, top");
173
174                 panelWest = new JPanel();
175                 contentPane.add(panelWest, BorderLayout.WEST);
176                 panelWest.setLayout(new FormLayout(new ColumnSpec[] {
177                                 FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
178                                 ColumnSpec.decode("112px:grow"),},
179                         new RowSpec[] {
180                                 FormSpecs.LINE_GAP_ROWSPEC,
181                                 RowSpec.decode("19px"),
182                                 FormSpecs.RELATED_GAP_ROWSPEC,
183                                 RowSpec.decode("max(12dlu;default)"),
184                                 FormSpecs.RELATED_GAP_ROWSPEC,
185                                 RowSpec.decode("max(12dlu;default)"),
186                                 FormSpecs.RELATED_GAP_ROWSPEC,
187                                 RowSpec.decode("12dlu"),
188                                 FormSpecs.RELATED_GAP_ROWSPEC,
189                                 RowSpec.decode("max(12dlu;default)"),
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("max(52dlu;default)"),}));
196
197                 labelDifficulty = new JLabel("難易度選択");
198                 labelDifficulty.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
199                 panelWest.add(labelDifficulty, "2, 2, center, default");
200
201                 comboDifficultySelect = new JComboBox();
202                 comboDifficultySelect.setFont(new Font("Dialog", Font.BOLD, 12));
203                 comboDifficultySelect.setModel(new DefaultComboBoxModel(new String[] {"指定なし", "DEBUT", "REGULAR", "PRO", "MASTER", "MASTER+", "ⓁMASTER+", "LIGHT", "TRICK", "PIANO", "FORTE", "WITCH"}));
204                 panelWest.add(comboDifficultySelect, "2, 4, fill, default");
205
206                                 comboAttribute = new JComboBox();
207                                 comboAttribute.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
208                                 comboAttribute.setModel(new DefaultComboBoxModel(new String[] {"指定なし", "全タイプ", "キュート", "クール", "パッション"}));
209                                 panelWest.add(comboAttribute, "2, 6, fill, default");
210
211                                 labelLevel = new JLabel("楽曲Lv");
212                                 labelLevel.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
213                                 panelWest.add(labelLevel, "2, 8, center, default");
214
215                                 spinnerLevel = new JSpinner();
216                                 panelWest.add(spinnerLevel, "2, 10");
217
218                                 checkLessLv = new JCheckBox("指定Lv以下");
219                                 checkLessLv.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
220                                 panelWest.add(checkLessLv, "2, 12");
221
222                                 checkMoreLv = new JCheckBox("指定Lv以上");
223                                 checkMoreLv.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
224                                 panelWest.add(checkMoreLv, "2, 14");
225
226                                 labelLvCaution = new JLabel("<html><body>※以下以上両方にチェックをつけることで指定レベルのみ絞り込むことができます</body></html>");
227                                 labelLvCaution.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
228                                 panelWest.add(labelLvCaution, "2, 16, fill, fill");
229
230                 panelEast = new JPanel();
231                 contentPane.add(panelEast, BorderLayout.EAST);
232                 panelEast.setLayout(new FormLayout(new ColumnSpec[] {
233                                 ColumnSpec.decode("98px"),},
234                         new RowSpec[] {
235                                 RowSpec.decode("26px"),
236                                 FormSpecs.RELATED_GAP_ROWSPEC,
237                                 RowSpec.decode("max(36dlu;default)"),
238                                 FormSpecs.RELATED_GAP_ROWSPEC,
239                                 FormSpecs.DEFAULT_ROWSPEC,
240                                 FormSpecs.RELATED_GAP_ROWSPEC,
241                                 RowSpec.decode("max(30dlu;default)"),
242                                 FormSpecs.RELATED_GAP_ROWSPEC,
243                                 RowSpec.decode("max(15dlu;default)"),
244                                 FormSpecs.RELATED_GAP_ROWSPEC,
245                                 RowSpec.decode("max(11dlu;default)"),
246                                 FormSpecs.RELATED_GAP_ROWSPEC,
247                                 FormSpecs.DEFAULT_ROWSPEC,
248                                 FormSpecs.RELATED_GAP_ROWSPEC,
249                                 FormSpecs.DEFAULT_ROWSPEC,
250                                 FormSpecs.RELATED_GAP_ROWSPEC,
251                                 FormSpecs.DEFAULT_ROWSPEC,}));
252
253                 btnImport = new JButton("<html><body>データベース<br>更新中…</body></html>");
254                 btnImport.setEnabled(false);
255                 btnImport.addActionListener(new ActionListener() {
256                         public void actionPerformed(ActionEvent e) {
257                                 ArrayList<Song> fromJson = Scraping.getFromJson();
258                                         ArrayList<Song> specificlevelList = Scraping.getSpecificLevelSongs(fromJson, (Integer)spinnerLevel.getValue(), checkLessLv.isSelected(), checkMoreLv.isSelected());
259                                         ArrayList<Song> specificDifficultyList = Scraping.getSpecificDifficultySongs(specificlevelList, comboDifficultySelect.getSelectedItem().toString());
260                                         ArrayList<Song> specificAttributeList = Scraping.getSpecificAttributeSongs(specificDifficultyList, comboAttribute.getSelectedItem().toString());
261                                         if(!selectedSongsList.isEmpty())
262                                         selectedSongsList.clear();
263                                 selectedSongsList.addAll(specificAttributeList);
264                                 LimitedLog.println(this.getClass() + ":[INFO]: " +"Songs are selected.We are Ready to go.");
265                                 JOptionPane.showMessageDialog(null, "絞り込み完了!「開始」をクリックすることで選曲できます!");
266                         }
267                 });
268                 btnImport.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
269                 panelEast.add(btnImport, "1, 3, fill, fill");
270
271                 btnStart = new JButton("開始!");
272                 btnStart.addActionListener(new ActionListener() {
273                         public void actionPerformed(ActionEvent e) {
274                                 Random random = new Random(System.currentTimeMillis());
275                                 String[] tmp = new String[property.getSongLimit()];
276                                 integratorArray = new String[property.getSongLimit()];
277                                 for(int i = 0; i < property.getSongLimit(); i++) {
278                                         int randomInt = random.nextInt(selectedSongsList.size());
279                                         tmp[i] = (i + 1) + "曲目: " + selectedSongsList.get(randomInt).getAttribute() + " [" + selectedSongsList.get(randomInt).getDifficulty() + "]「" + selectedSongsList.get(randomInt).getName() + "」!(Lv:" + selectedSongsList.get(randomInt).getLevel() + ")\n\n";
280                                         integratorArray[i] = selectedSongsList.get(randomInt).getName() + "(Lv" + selectedSongsList.get(randomInt).getLevel() + ")\n";
281                                 }
282                                 String paneString = "";
283                                 for (int i = 0; i < tmp.length; i++) {
284                                         paneString = paneString + tmp[i];
285                                 }
286                                 paneString = paneString + "この" + tmp.length + "曲をプレイしましょう!!!";
287                                 textArea.setText(paneString);
288                                 integratorBool = true;
289                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "show up completed.");
290                         }
291                 });
292                 btnStart.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
293                 panelEast.add(btnStart, "1, 7, fill, fill");
294
295                                 btnTwitterIntegration = new JButton("Twitter連携");
296                                 btnTwitterIntegration.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 11));
297                                 btnTwitterIntegration.addActionListener(new ActionListener() {
298                                         public void actionPerformed(ActionEvent e) {
299                                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Twitter Integration requested.Verify permission status.");
300                                                 boolean authorizationStatus = TwitterIntegration.authorization();
301                                                 LimitedLog.println(this.getClass() + ":[INFO]: " + "Permission Verifying completed.\nStatus: " + authorizationStatus);
302                                                 LimitedLog.print(this.getClass() + ":[INFO]: " + "Construction status message...");
303                                                 String updatedStatus = "デレステ課題曲セレクターで\n";
304                                                 int lengthLimit = updatedStatus.length();
305                                                 boolean isBroken = false;
306                                                 if(!integratorBool) {
307                                                         JOptionPane.showMessageDialog(null, "ちひろ「まだプレイを始めていないみたいですね」");
308                                                         LimitedLog.println();
309                                                         return;
310                                                 }
311                                                 for(int i = 0; i < integratorArray.length; i++) {
312                                                         updatedStatus = updatedStatus + integratorArray[i];
313                                                         lengthLimit += integratorArray[i].length();
314                                                         if(lengthLimit > 69) {
315                                                                 isBroken = true;
316                                                                 break;
317                                                         }
318                                                 }
319                                                 if(isBroken) {
320                                                         updatedStatus = updatedStatus + "…その他数曲\nをプレイしました!\n#DelesteRandomSelector #デレステ ";
321                                                 } else {
322                                                         updatedStatus = updatedStatus + "をプレイしました!\n#DelesteRandomSelector #デレステ ";
323                                                 }
324                                                 LimitedLog.println("completed.\n" + updatedStatus);
325                                                 lengthLimit = updatedStatus.length();
326                                                 if(authorizationStatus) {
327                                                         int option = JOptionPane.showConfirmDialog(null, "Twitterへ以下の内容を投稿します。よろしいですか?\n\n" + updatedStatus + "\n\n文字数:" + lengthLimit);
328                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "User selected " + option);
329                                                         switch(option) {
330                                                                 case JOptionPane.OK_OPTION:
331                                                                         TwitterIntegration.PostTwitter(updatedStatus);
332                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "Success to update the status.");
333                                                                         JOptionPane.showMessageDialog(null, "投稿が完了しました。");
334                                                                         break;
335                                                                 case JOptionPane.NO_OPTION:
336                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "There is no will to post.");
337                                                                         break;
338                                                                 case JOptionPane.CANCEL_OPTION:
339                                                                         LimitedLog.println(this.getClass() + ":[INFO]: " + "The Operation was canceled by user.");
340                                                                         break;
341                                                                 default:
342                                                                         break;
343                                                         }
344                                                 } else {
345                                                         LimitedLog.println(this.getClass() + ":[WARN]: " + "seems to reject the permission.it should need try again.");
346                                                 }
347                                         }
348                                 });
349                                 panelEast.add(btnTwitterIntegration, "1, 11");
350
351                                                                 btnExit = new JButton("終了");
352                                                                 btnExit.addActionListener(new ActionListener() {
353                                                                         public void actionPerformed(ActionEvent e) {
354                                                                                 LimitedLog.println(this.getClass() + ":[INFO]: " +"Requested Exit by Button");
355                                                                                 if(updatedFuture.isDone()) {
356                                                                                         System.exit(0);
357                                                                                 } else {
358                                                                                         JOptionPane.showMessageDialog(null, "非同期処理が完了していません。少し時間が経ってからやり直してください。");
359                                                                                 }
360                                                                         }
361                                                                 });
362                                                                 btnExit.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
363                                                                 panelEast.add(btnExit, "1, 13");
364
365                 panelCentre = new JPanel();
366                 contentPane.add(panelCentre, BorderLayout.CENTER);
367                 panelCentre.setLayout(new BorderLayout(0, 0));
368
369                 textArea = new JTextArea();
370                 textArea.setText("楽曲選択の手順\r\n1.難易度、属性、レベルを選択する\r\n2.「楽曲取り込み」ボタンを押す!\r\n3.「開始」ボタンを押す!\r\n4.選択された楽曲がここに表示されます!\r\n現在設定されている楽曲選択の最大数:" + property.getSongLimit());
371                 textArea.setEditable(false);
372
373                 scrollPane = new JScrollPane(textArea);
374                 panelCentre.add(scrollPane, BorderLayout.CENTER);
375                 if(isFirst)
376                         setEnabled.run();
377         }
378
379 }