OSDN Git Service

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