サーバの状態の保存と読み取り機能を、N◎VAプラグイン上で実装
@@ -10,7 +10,7 @@ | ||
10 | 10 | import java.io.IOException; |
11 | 11 | import java.util.ArrayList; |
12 | 12 | import java.util.Iterator; |
13 | -import java.util.Properties; | |
13 | +import java.util.Map; | |
14 | 14 | |
15 | 15 | /** |
16 | 16 | * CardManager |
@@ -32,10 +32,10 @@ | ||
32 | 32 | * 全てのカードの状態をプロパティにセットする。 |
33 | 33 | * @param properties プロパティ |
34 | 34 | */ |
35 | - public void setProperties(Properties properties){ | |
35 | + public void setProperties(Map<String, Object> map){ | |
36 | 36 | for (int i = 0, n = cardList.size(); i < n; i++) { |
37 | 37 | StringBuffer element = cardList.get(i); |
38 | - properties.setProperty("nova.card." + i, element.toString()); | |
38 | + map.put("card." + 1, element.toString()); | |
39 | 39 | } |
40 | 40 | } |
41 | 41 |
@@ -45,26 +45,22 @@ | ||
45 | 45 | * 計算する。 |
46 | 46 | * @param properties プロパティ |
47 | 47 | */ |
48 | - public void getProperties(Properties properties){ | |
48 | + public void getProperties(Map<String, Object> map){ | |
49 | 49 | cardList.clear(); |
50 | 50 | numAvailable = 0; |
51 | 51 | numJoker = 0; |
52 | 52 | numDeck = 0; |
53 | - int i = 0; | |
54 | - while(true){ | |
55 | - String element = properties.getProperty("nova.card." + i); | |
56 | - if(element != null){ | |
57 | - cardList.add(new StringBuffer(element)); | |
58 | - for(int j = 0; j < element.length(); j++){ | |
59 | - if(element.charAt(j) == '0'){ | |
60 | - numAvailable++; | |
61 | - } | |
62 | - } | |
63 | - i++; | |
64 | - } else { | |
65 | - break; | |
66 | - } | |
67 | - } | |
53 | + for (String key : map.keySet()) { | |
54 | + if(key.startsWith("card.")){ | |
55 | + String element = (String) map.get(key); | |
56 | + cardList.add(new StringBuffer(element)); | |
57 | + for(int j = 0; j < element.length(); j++){ | |
58 | + if(element.charAt(j) == '0'){ | |
59 | + numAvailable++; | |
60 | + } | |
61 | + } | |
62 | + } | |
63 | + } | |
68 | 64 | numDeck = cardList.size() / 4; |
69 | 65 | if(cardList.size() % 4 > 0){ |
70 | 66 | numJoker = cardList.get(cardList.size() - 1).length(); |
@@ -6,6 +6,7 @@ | ||
6 | 6 | package jp.sf.chaplet.nova.server; |
7 | 7 | |
8 | 8 | import java.util.HashMap; |
9 | +import java.util.Map; | |
9 | 10 | |
10 | 11 | /** |
11 | 12 | * HandManager |
@@ -25,6 +26,62 @@ | ||
25 | 26 | // } catch (NumberFormatException e) {} |
26 | 27 | // } |
27 | 28 | } |
29 | + | |
30 | + public void getProperties(Map<String, Object> map){ | |
31 | + for (String sessionId : handMap.keySet()) { | |
32 | + Hand hand = handMap.get(sessionId); | |
33 | + { | |
34 | + String[] cards = hand.getHand(0); | |
35 | + StringBuilder sb = new StringBuilder(); | |
36 | + for (int j = 0; j < cards.length; j++){ | |
37 | + sb.append(cards[j]); | |
38 | + sb.append(','); | |
39 | + } | |
40 | + if(sb.length() > 0){ | |
41 | + sb.deleteCharAt(sb.length() - 1); | |
42 | + } | |
43 | + map.put("hand." + sessionId + ".0", sb.toString()); | |
44 | + } | |
45 | + int[] plotNo = hand.getPlotNoList(); | |
46 | + for (int i = 0; i < plotNo.length; i++){ | |
47 | + String[] cards = hand.getHand(plotNo[i]); | |
48 | + StringBuilder sb = new StringBuilder(); | |
49 | + for (int j = 0; j < cards.length; j++){ | |
50 | + sb.append(cards[j]); | |
51 | + sb.append(','); | |
52 | + } | |
53 | + if(sb.length() > 0){ | |
54 | + sb.deleteCharAt(sb.length() - 1); | |
55 | + } | |
56 | + map.put("hand." + sessionId + "." + (i+1), sb.toString()); | |
57 | + } | |
58 | + } | |
59 | + } | |
60 | + | |
61 | + public void setProperties(Map<String, Object> map){ | |
62 | + handMap.clear(); | |
63 | + for (String key : map.keySet()) { | |
64 | + if(key.startsWith("hand.")){ | |
65 | + String[] values = key.split("\\."); | |
66 | + String sessionId = values[1]; | |
67 | + int plotNo = Integer.parseInt(values[2]); | |
68 | + Hand hand = handMap.get(sessionId); | |
69 | + if(hand == null){ | |
70 | + hand = new Hand(); | |
71 | + handMap.put(sessionId, hand); | |
72 | + hand.setName(sessionId); | |
73 | + } | |
74 | + String[] cards = ((String)map.get(key)).split("\\,"); | |
75 | + if(plotNo != 0){ | |
76 | + // プロット | |
77 | + plotNo = hand.addHand(); | |
78 | + } | |
79 | + for (String card : cards) { | |
80 | + hand.addCard(plotNo, card); | |
81 | + } | |
82 | + } | |
83 | + } | |
84 | + } | |
28 | 85 | |
29 | 86 | public int addHandOf(String sessionId){ |
30 | 87 | Hand hand = handMap.get(sessionId); |
@@ -90,6 +90,7 @@ | ||
90 | 90 | addAction("nova:givecard", actions[11]); |
91 | 91 | addAction("nova:exchange", actions[12]); |
92 | 92 | addAction("nova:init", actions[13]); |
93 | + addAction("nova:status", actions[0]); | |
93 | 94 | |
94 | 95 | NovaConfig config = (NovaConfig) StorageUtils.load(NovaConfig.NAME, NovaConfig.class); |
95 | 96 | tarotManager.setConfig(config); |
@@ -115,10 +116,20 @@ | ||
115 | 116 | */ |
116 | 117 | @Override |
117 | 118 | public void save(Map<String, Object> map) { |
118 | - map.put("card", cardManager); | |
119 | - map.put("tarot", tarotManager); | |
120 | - map.put("hand", handManager); | |
119 | + cardManager.getProperties(map); | |
120 | + tarotManager.getProperties(map); | |
121 | + handManager.getProperties(map); | |
121 | 122 | } |
123 | + | |
124 | + /** | |
125 | + * @see jp.sf.chaplet.core.server.ServerPlugin#load(java.util.Map) | |
126 | + */ | |
127 | + @Override | |
128 | + public void load(Map<String, Object> map) { | |
129 | + cardManager.setProperties(map); | |
130 | + tarotManager.setProperties(map); | |
131 | + handManager.setProperties(map); | |
132 | + } | |
122 | 133 | |
123 | 134 | /** |
124 | 135 | * @see jp.sf.chaplet.core.server.ServerPlugin#sendPluginStatus() |
@@ -136,6 +147,7 @@ | ||
136 | 147 | */ |
137 | 148 | public void sendPluginStatus(ClientProxy proxy) { |
138 | 149 | Hand hand = getHandManager().getHandOf(proxy.getSessionId()); |
150 | + // 手札の再読込 | |
139 | 151 | String[] cards = hand.getHand(0); |
140 | 152 | String[] resp = new String[cards.length + 1]; |
141 | 153 | resp[0] = "ok/nova:feed"; |
@@ -143,6 +155,11 @@ | ||
143 | 155 | resp[i + 1] = cards[i]; |
144 | 156 | } |
145 | 157 | proxy.sendMessage(resp); |
158 | + try { | |
159 | + getAction("nova:status").execute(new String[]{"nova:status"}, proxy); | |
160 | + } catch (IOException e) { | |
161 | + getLogger().log(Level.SEVERE, "scene", e); | |
162 | + } | |
146 | 163 | } |
147 | 164 | |
148 | 165 | /** |
@@ -6,22 +6,15 @@ | ||
6 | 6 | package jp.sf.chaplet.nova.server; |
7 | 7 | |
8 | 8 | import java.io.BufferedReader; |
9 | -import java.io.BufferedWriter; | |
10 | -import java.io.File; | |
11 | -import java.io.FileInputStream; | |
12 | -import java.io.FileOutputStream; | |
13 | 9 | import java.io.IOException; |
14 | 10 | import java.io.InputStream; |
15 | 11 | import java.io.InputStreamReader; |
16 | -import java.io.OutputStreamWriter; | |
17 | -import java.util.ArrayList; | |
18 | 12 | import java.util.HashMap; |
19 | 13 | import java.util.HashSet; |
20 | 14 | import java.util.Iterator; |
15 | +import java.util.Map; | |
21 | 16 | import java.util.Set; |
22 | 17 | |
23 | -import jp.sf.chaplet.ConfigLoader; | |
24 | -import jp.sf.chaplet.Configuration; | |
25 | 18 | import jp.sf.chaplet.nova.NovaConfig; |
26 | 19 | |
27 | 20 | /** |
@@ -34,8 +27,9 @@ | ||
34 | 27 | private static final String DEFAULT_FILE = "tarot.txt"; |
35 | 28 | private String fileName = DEFAULT_FILE; |
36 | 29 | |
37 | - private ArrayList<String> tarotList = new ArrayList<String>(); | |
38 | - private Set<String> unuseSet = new HashSet<String>(); | |
30 | + private String current; | |
31 | + private Map<Integer, String> tarotMap = new HashMap<Integer, String>(); | |
32 | + private Set<Integer> reminderSet = new HashSet<Integer>(); | |
39 | 33 | |
40 | 34 | private NovaConfig config; |
41 | 35 |
@@ -45,63 +39,51 @@ | ||
45 | 39 | // fileName = tarotFile; |
46 | 40 | // } |
47 | 41 | } |
42 | + | |
43 | + public void getProperties(Map<String, Object> map){ | |
44 | + map.put("tarot.reminder", reminderSet); | |
45 | + map.put("tarot.current", current); | |
46 | + } | |
47 | + | |
48 | + @SuppressWarnings("unchecked") | |
49 | + public void setProperties(Map<String, Object> map){ | |
50 | + if(map.get("tarot.reminder") != null){ | |
51 | + reminderSet = (Set<Integer>) map.get("tarot.reminder"); | |
52 | + } | |
53 | + current = (String) map.get("tarot.current"); | |
54 | + | |
55 | + } | |
48 | 56 | |
49 | 57 | public void reset(){ |
50 | - tarotList.set(0, "-"); | |
51 | - for (int i = 1, n = tarotList.size(); i < n;i++) { | |
52 | - String line = tarotList.get(i); | |
53 | - String[] tarot = line.split(","); | |
54 | - String number = tarot[0]; | |
55 | - if(unuseSet.contains(number)){ | |
56 | - tarotList.set(i, tarot[0] + "," + tarot[1] + ",1"); | |
57 | - } else { | |
58 | - tarotList.set(i, tarot[0] + "," + tarot[1] + ",0"); | |
59 | - } | |
58 | + current = null; | |
59 | + reminderSet.clear(); | |
60 | + for (int i : tarotMap.keySet()){ | |
61 | + reminderSet.add(i); | |
60 | 62 | } |
61 | - } | |
62 | - | |
63 | - public void init() throws IOException{ | |
64 | - unuseSet.clear(); | |
65 | 63 | if(!config.isHirukoEnabled()){ |
66 | - unuseSet.add(String.valueOf(-1)); | |
64 | + reminderSet.remove(-1); | |
67 | 65 | } |
68 | 66 | if(!config.isArashiEnabled()){ |
69 | - unuseSet.add(String.valueOf(-7)); | |
67 | + reminderSet.remove(-7); | |
70 | 68 | } |
71 | 69 | if(!config.isKagemushaEnabled()){ |
72 | - unuseSet.add(String.valueOf(-9)); | |
70 | + reminderSet.remove(-9); | |
73 | 71 | } |
74 | 72 | if(!config.isAyakashiEnabled()){ |
75 | - unuseSet.add(String.valueOf(-18)); | |
73 | + reminderSet.remove(-18); | |
76 | 74 | } |
77 | - File file = new File(fileName); | |
78 | - Configuration conf = ConfigLoader.getInstance().load(); | |
79 | - if(!file.exists()){ | |
80 | - fileName = DEFAULT_FILE; | |
81 | - file.createNewFile(); | |
82 | - InputStream in = getClass().getResourceAsStream("/nova/" + DEFAULT_FILE); | |
83 | - BufferedReader reader = new BufferedReader(new InputStreamReader(in, conf.getEncoding())); | |
84 | - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), conf.getEncoding())); | |
85 | - String line = null; | |
86 | - while((line = reader.readLine()) != null){ | |
87 | - writer.write(line); | |
88 | - writer.newLine(); | |
89 | - } | |
90 | - writer.close(); | |
91 | - reader.close(); | |
92 | - } | |
93 | - BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), conf.getEncoding())); | |
94 | - tarotList.clear(); | |
75 | + } | |
76 | + | |
77 | + public void init() throws IOException{ | |
78 | + InputStream in = getClass().getResourceAsStream("/nova/" + DEFAULT_FILE); | |
79 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in, "Windows-31J")); | |
80 | + tarotMap.clear(); | |
95 | 81 | String line = null; |
96 | 82 | try { |
97 | 83 | while ((line = reader.readLine()) != null) { |
98 | 84 | if(line.length() > 0){ |
99 | - String number = line.split(",")[0]; | |
100 | - if(unuseSet.contains(number)){ | |
101 | - tarotList.add(line + ",1"); | |
102 | - } else { | |
103 | - tarotList.add(line + ",0"); | |
104 | - } | |
85 | + String index = line.split(",")[0]; | |
86 | + tarotMap.put(Integer.parseInt(index), line); | |
105 | 87 | } |
106 | 88 | } |
107 | 89 | } catch (IOException e) { |
@@ -109,38 +91,26 @@ | ||
109 | 91 | } finally { |
110 | 92 | reader.close(); |
111 | 93 | } |
112 | - tarotList.add(0, "-"); | |
94 | + reset(); | |
113 | 95 | } |
114 | 96 | |
115 | 97 | public String next() throws IOException{ |
116 | - HashMap<Integer, String> map = getRestTarots(); | |
117 | - if(map.size() == 0){ | |
118 | - reset(); | |
119 | - map = getRestTarots(); | |
120 | - } | |
121 | - int nextIndex = (int) (Math.random() * (map.size())); | |
122 | - Iterator iterator = map.keySet().iterator(); | |
98 | + if(reminderSet.size() == 0){ | |
99 | + reset(); | |
100 | + } | |
101 | + int nextIndex = (int) (Math.random() * (reminderSet.size())); | |
102 | + Iterator iterator = reminderSet.iterator(); | |
123 | 103 | for(int i = 0; i < nextIndex - 1; i++){ |
124 | 104 | iterator.next(); |
125 | 105 | } |
126 | 106 | Integer key = (Integer) iterator.next(); |
127 | - String tarot = map.get(key); | |
128 | - tarotList.set(key.intValue(), tarot.substring(0, tarot.length() - 1) + "1"); | |
129 | - tarotList.set(0, tarot.split(",")[0]); | |
130 | - return tarot; | |
107 | + | |
108 | + current = tarotMap.get(key); | |
109 | + | |
110 | + reminderSet.remove(key); | |
111 | + return current; | |
131 | 112 | } |
132 | 113 | |
133 | - HashMap<Integer, String> getRestTarots() { | |
134 | - HashMap<Integer, String> map = new HashMap<Integer, String>(); | |
135 | - for(int i = 0, n = tarotList.size(); i < n; i++){ | |
136 | - String tarot = tarotList.get(i); | |
137 | - if(tarot.indexOf(',') >= 0 && tarot.endsWith("0")){ | |
138 | - map.put(new Integer(i), tarot); | |
139 | - } | |
140 | - } | |
141 | - return map; | |
142 | - } | |
143 | - | |
144 | 114 | public void setFileName(String tarotFile) { |
145 | 115 | this.fileName = tarotFile; |
146 | 116 | } |
@@ -150,23 +120,15 @@ | ||
150 | 120 | } |
151 | 121 | |
152 | 122 | int count(){ |
153 | - return tarotList.size() - 1; | |
123 | + return tarotMap.size(); | |
154 | 124 | } |
155 | 125 | |
156 | 126 | public String getSceneCard(){ |
157 | - String cardNo = (String) tarotList.get(0); | |
158 | - if(cardNo.equals("-")){ | |
127 | + if(current == null){ | |
159 | 128 | return ""; |
160 | 129 | } |
161 | 130 | |
162 | - for (int i = 1, n = tarotList.size(); i < n; i++) { | |
163 | - String card = tarotList.get(i); | |
164 | - String[] s = card.split(","); | |
165 | - if(s[0].equals(cardNo)){ | |
166 | - return card; | |
167 | - } | |
168 | - } | |
169 | - return ""; | |
131 | + return current; | |
170 | 132 | } |
171 | 133 | |
172 | 134 | public NovaConfig getConfig() { |
@@ -10,7 +10,7 @@ | ||
10 | 10 | * @version $Id$ |
11 | 11 | */ |
12 | 12 | public class NovaConfig { |
13 | - public static final String NAME = "novaconf"; | |
13 | + public static final String NAME = "novaconf.xml"; | |
14 | 14 | |
15 | 15 | private int numJoker = 2; |
16 | 16 |
@@ -25,12 +25,12 @@ | ||
25 | 25 | BorderFactory.createEmptyBorder(3, 3, 3, 3), |
26 | 26 | BorderFactory.createCompoundBorder( |
27 | 27 | BorderFactory.createLineBorder(Color.BLACK, 3), |
28 | - BorderFactory.createEmptyBorder(3, 3, 3, 3))); | |
28 | + BorderFactory.createEmptyBorder(15, 5, 15, 5))); | |
29 | 29 | private Border selectedBorder = BorderFactory.createCompoundBorder( |
30 | 30 | BorderFactory.createLineBorder(SystemColor.textHighlight, 3), |
31 | 31 | BorderFactory.createCompoundBorder( |
32 | 32 | BorderFactory.createLineBorder(Color.BLACK, 3), |
33 | - BorderFactory.createEmptyBorder(3, 3, 3, 3))); | |
33 | + BorderFactory.createEmptyBorder(15, 5, 15, 5))); | |
34 | 34 | |
35 | 35 | public CardListRenderer(){ |
36 | 36 | icons = new ImageIcon[4]; |
@@ -702,6 +702,9 @@ | ||
702 | 702 | getToPlotButton().setEnabled(ok); |
703 | 703 | getPlotToPileButton().setEnabled(ok); |
704 | 704 | getAddCharaButton().setEnabled(ok); |
705 | + getRemoveCharacterButton().setEnabled(ok); | |
706 | + getToReactionButton().setEnabled(ok); | |
707 | +// getInitButton().setEnabled(ok); | |
705 | 708 | } |
706 | 709 | |
707 | 710 | /** |
@@ -389,9 +389,8 @@ | ||
389 | 389 | |
390 | 390 | /** |
391 | 391 | * サーバ設定ファイルに現在の状態を書き込む。 |
392 | - * @param fileName サーバ設定ファイル | |
393 | 392 | */ |
394 | - public void save(String fileName){ | |
393 | + public void save(){ | |
395 | 394 | HashMap<String, HashMap<String, Object>> map = new HashMap<String, HashMap<String,Object>>(); |
396 | 395 | HashMap<String, Object> coreMap = new HashMap<String, Object>(); |
397 | 396 | coreMap.put("users", userManager.getAllUsers()); |
@@ -409,10 +408,9 @@ | ||
409 | 408 | |
410 | 409 | /** |
411 | 410 | * サーバ設定ファイルから前の状態を読み込む。 |
412 | - * @param fileName サーバ設定ファイル | |
413 | 411 | */ |
414 | 412 | @SuppressWarnings("unchecked") |
415 | - public void load(String fileName){ | |
413 | + public void load(){ | |
416 | 414 | HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) StorageUtils.load(StorageUtils.SERVER, HashMap.class); |
417 | 415 | HashMap<String, Object> coreMap = map.get("core"); |
418 | 416 | userManager.setAllUsers((User[])coreMap.get("users")); |
@@ -893,6 +893,9 @@ | ||
893 | 893 | |
894 | 894 | public void setGM(boolean gm) { |
895 | 895 | getAdminMenu().setEnabled(gm); |
896 | + for(ClientPlugin plugin : pluginMap.values()){ | |
897 | + plugin.getPluginView().setGM(gm); | |
898 | + } | |
896 | 899 | } |
897 | 900 | |
898 | 901 | /** |
@@ -1088,6 +1091,11 @@ | ||
1088 | 1091 | if (loadServerItem == null) { |
1089 | 1092 | loadServerItem = new JMenuItem(); |
1090 | 1093 | loadServerItem.setText("サーバの状態を読み込み"); |
1094 | + loadServerItem.addActionListener(new java.awt.event.ActionListener() { | |
1095 | + public void actionPerformed(java.awt.event.ActionEvent e) { | |
1096 | + actionMap.get("loadServerSetting").actionPerformed(e); | |
1097 | + } | |
1098 | + }); | |
1091 | 1099 | } |
1092 | 1100 | return loadServerItem; |
1093 | 1101 | } |
@@ -1101,6 +1109,11 @@ | ||
1101 | 1109 | if (saveServerItem == null) { |
1102 | 1110 | saveServerItem = new JMenuItem(); |
1103 | 1111 | saveServerItem.setText("サーバの状態を保存"); |
1112 | + saveServerItem.addActionListener(new java.awt.event.ActionListener() { | |
1113 | + public void actionPerformed(java.awt.event.ActionEvent e) { | |
1114 | + actionMap.get("saveServerSetting").actionPerformed(e); | |
1115 | + } | |
1116 | + }); | |
1104 | 1117 | } |
1105 | 1118 | return saveServerItem; |
1106 | 1119 | } |
@@ -284,10 +284,11 @@ | ||
284 | 284 | return (JPanel) view.getContentPane(); |
285 | 285 | } |
286 | 286 | |
287 | - public void loadServerSetting(final String fileName) { | |
287 | + @Action | |
288 | + public void loadServerSetting() { | |
288 | 289 | Thread thread = new Thread(new Runnable() { |
289 | 290 | public void run() { |
290 | - server.load(fileName); | |
291 | + server.load(); | |
291 | 292 | SwingUtilities.invokeLater(new Runnable() { |
292 | 293 | public void run() { |
293 | 294 | JOptionPane.showMessageDialog(view, "ロードに成功しました。", |
@@ -299,10 +300,11 @@ | ||
299 | 300 | thread.start(); |
300 | 301 | } |
301 | 302 | |
302 | - public void saveServerSetting(final String fileName) { | |
303 | + @Action | |
304 | + public void saveServerSetting() { | |
303 | 305 | Thread thread = new Thread(new Runnable() { |
304 | 306 | public void run() { |
305 | - server.save(fileName); | |
307 | + server.save(); | |
306 | 308 | SwingUtilities.invokeLater(new Runnable() { |
307 | 309 | public void run() { |
308 | 310 | SwingUtilities.invokeLater(new Runnable() { |