• R/O
  • SSH
  • HTTPS

chaplet: 提交


Commit MetaInfo

修訂30 (tree)
時間2007-06-26 22:27:54
作者koe

Log Message

サーバの状態の保存と読み取り機能を、N◎VAプラグイン上で実装

Change Summary

差異

--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/CardManager.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/CardManager.java (revision 30)
@@ -10,7 +10,7 @@
1010 import java.io.IOException;
1111 import java.util.ArrayList;
1212 import java.util.Iterator;
13-import java.util.Properties;
13+import java.util.Map;
1414
1515 /**
1616 * CardManager
@@ -32,10 +32,10 @@
3232 * 全てのカードの状態をプロパティにセットする。
3333 * @param properties プロパティ
3434 */
35- public void setProperties(Properties properties){
35+ public void setProperties(Map<String, Object> map){
3636 for (int i = 0, n = cardList.size(); i < n; i++) {
3737 StringBuffer element = cardList.get(i);
38- properties.setProperty("nova.card." + i, element.toString());
38+ map.put("card." + 1, element.toString());
3939 }
4040 }
4141
@@ -45,26 +45,22 @@
4545 * 計算する。
4646 * @param properties プロパティ
4747 */
48- public void getProperties(Properties properties){
48+ public void getProperties(Map<String, Object> map){
4949 cardList.clear();
5050 numAvailable = 0;
5151 numJoker = 0;
5252 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+ }
6864 numDeck = cardList.size() / 4;
6965 if(cardList.size() % 4 > 0){
7066 numJoker = cardList.get(cardList.size() - 1).length();
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/HandManager.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/HandManager.java (revision 30)
@@ -6,6 +6,7 @@
66 package jp.sf.chaplet.nova.server;
77
88 import java.util.HashMap;
9+import java.util.Map;
910
1011 /**
1112 * HandManager
@@ -25,6 +26,62 @@
2526 // } catch (NumberFormatException e) {}
2627 // }
2728 }
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+ }
2885
2986 public int addHandOf(String sessionId){
3087 Hand hand = handMap.get(sessionId);
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/NovaServerPlugin.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/NovaServerPlugin.java (revision 30)
@@ -90,6 +90,7 @@
9090 addAction("nova:givecard", actions[11]);
9191 addAction("nova:exchange", actions[12]);
9292 addAction("nova:init", actions[13]);
93+ addAction("nova:status", actions[0]);
9394
9495 NovaConfig config = (NovaConfig) StorageUtils.load(NovaConfig.NAME, NovaConfig.class);
9596 tarotManager.setConfig(config);
@@ -115,10 +116,20 @@
115116 */
116117 @Override
117118 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);
121122 }
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+ }
122133
123134 /**
124135 * @see jp.sf.chaplet.core.server.ServerPlugin#sendPluginStatus()
@@ -136,6 +147,7 @@
136147 */
137148 public void sendPluginStatus(ClientProxy proxy) {
138149 Hand hand = getHandManager().getHandOf(proxy.getSessionId());
150+ // 手札の再読込
139151 String[] cards = hand.getHand(0);
140152 String[] resp = new String[cards.length + 1];
141153 resp[0] = "ok/nova:feed";
@@ -143,6 +155,11 @@
143155 resp[i + 1] = cards[i];
144156 }
145157 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+ }
146163 }
147164
148165 /**
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/TarotManager.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/server/TarotManager.java (revision 30)
@@ -6,22 +6,15 @@
66 package jp.sf.chaplet.nova.server;
77
88 import java.io.BufferedReader;
9-import java.io.BufferedWriter;
10-import java.io.File;
11-import java.io.FileInputStream;
12-import java.io.FileOutputStream;
139 import java.io.IOException;
1410 import java.io.InputStream;
1511 import java.io.InputStreamReader;
16-import java.io.OutputStreamWriter;
17-import java.util.ArrayList;
1812 import java.util.HashMap;
1913 import java.util.HashSet;
2014 import java.util.Iterator;
15+import java.util.Map;
2116 import java.util.Set;
2217
23-import jp.sf.chaplet.ConfigLoader;
24-import jp.sf.chaplet.Configuration;
2518 import jp.sf.chaplet.nova.NovaConfig;
2619
2720 /**
@@ -34,8 +27,9 @@
3427 private static final String DEFAULT_FILE = "tarot.txt";
3528 private String fileName = DEFAULT_FILE;
3629
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>();
3933
4034 private NovaConfig config;
4135
@@ -45,63 +39,51 @@
4539 // fileName = tarotFile;
4640 // }
4741 }
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+ }
4856
4957 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);
6062 }
61- }
62-
63- public void init() throws IOException{
64- unuseSet.clear();
6563 if(!config.isHirukoEnabled()){
66- unuseSet.add(String.valueOf(-1));
64+ reminderSet.remove(-1);
6765 }
6866 if(!config.isArashiEnabled()){
69- unuseSet.add(String.valueOf(-7));
67+ reminderSet.remove(-7);
7068 }
7169 if(!config.isKagemushaEnabled()){
72- unuseSet.add(String.valueOf(-9));
70+ reminderSet.remove(-9);
7371 }
7472 if(!config.isAyakashiEnabled()){
75- unuseSet.add(String.valueOf(-18));
73+ reminderSet.remove(-18);
7674 }
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();
9581 String line = null;
9682 try {
9783 while ((line = reader.readLine()) != null) {
9884 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);
10587 }
10688 }
10789 } catch (IOException e) {
@@ -109,38 +91,26 @@
10991 } finally {
11092 reader.close();
11193 }
112- tarotList.add(0, "-");
94+ reset();
11395 }
11496
11597 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();
123103 for(int i = 0; i < nextIndex - 1; i++){
124104 iterator.next();
125105 }
126106 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;
131112 }
132113
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-
144114 public void setFileName(String tarotFile) {
145115 this.fileName = tarotFile;
146116 }
@@ -150,23 +120,15 @@
150120 }
151121
152122 int count(){
153- return tarotList.size() - 1;
123+ return tarotMap.size();
154124 }
155125
156126 public String getSceneCard(){
157- String cardNo = (String) tarotList.get(0);
158- if(cardNo.equals("-")){
127+ if(current == null){
159128 return "";
160129 }
161130
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;
170132 }
171133
172134 public NovaConfig getConfig() {
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/NovaConfig.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/NovaConfig.java (revision 30)
@@ -10,7 +10,7 @@
1010 * @version $Id$
1111 */
1212 public class NovaConfig {
13- public static final String NAME = "novaconf";
13+ public static final String NAME = "novaconf.xml";
1414
1515 private int numJoker = 2;
1616
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/ui/CardListRenderer.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/ui/CardListRenderer.java (revision 30)
@@ -25,12 +25,12 @@
2525 BorderFactory.createEmptyBorder(3, 3, 3, 3),
2626 BorderFactory.createCompoundBorder(
2727 BorderFactory.createLineBorder(Color.BLACK, 3),
28- BorderFactory.createEmptyBorder(3, 3, 3, 3)));
28+ BorderFactory.createEmptyBorder(15, 5, 15, 5)));
2929 private Border selectedBorder = BorderFactory.createCompoundBorder(
3030 BorderFactory.createLineBorder(SystemColor.textHighlight, 3),
3131 BorderFactory.createCompoundBorder(
3232 BorderFactory.createLineBorder(Color.BLACK, 3),
33- BorderFactory.createEmptyBorder(3, 3, 3, 3)));
33+ BorderFactory.createEmptyBorder(15, 5, 15, 5)));
3434
3535 public CardListRenderer(){
3636 icons = new ImageIcon[4];
--- chaplet/trunk/src/nova/jp/sf/chaplet/nova/ui/NovaView.java (revision 29)
+++ chaplet/trunk/src/nova/jp/sf/chaplet/nova/ui/NovaView.java (revision 30)
@@ -702,6 +702,9 @@
702702 getToPlotButton().setEnabled(ok);
703703 getPlotToPileButton().setEnabled(ok);
704704 getAddCharaButton().setEnabled(ok);
705+ getRemoveCharacterButton().setEnabled(ok);
706+ getToReactionButton().setEnabled(ok);
707+// getInitButton().setEnabled(ok);
705708 }
706709
707710 /**
--- chaplet/trunk/src/core/jp/sf/chaplet/core/server/ChatServer.java (revision 29)
+++ chaplet/trunk/src/core/jp/sf/chaplet/core/server/ChatServer.java (revision 30)
@@ -389,9 +389,8 @@
389389
390390 /**
391391 * サーバ設定ファイルに現在の状態を書き込む。
392- * @param fileName サーバ設定ファイル
393392 */
394- public void save(String fileName){
393+ public void save(){
395394 HashMap<String, HashMap<String, Object>> map = new HashMap<String, HashMap<String,Object>>();
396395 HashMap<String, Object> coreMap = new HashMap<String, Object>();
397396 coreMap.put("users", userManager.getAllUsers());
@@ -409,10 +408,9 @@
409408
410409 /**
411410 * サーバ設定ファイルから前の状態を読み込む。
412- * @param fileName サーバ設定ファイル
413411 */
414412 @SuppressWarnings("unchecked")
415- public void load(String fileName){
413+ public void load(){
416414 HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) StorageUtils.load(StorageUtils.SERVER, HashMap.class);
417415 HashMap<String, Object> coreMap = map.get("core");
418416 userManager.setAllUsers((User[])coreMap.get("users"));
--- chaplet/trunk/src/core/jp/sf/chaplet/core/ui/MainWindow.java (revision 29)
+++ chaplet/trunk/src/core/jp/sf/chaplet/core/ui/MainWindow.java (revision 30)
@@ -893,6 +893,9 @@
893893
894894 public void setGM(boolean gm) {
895895 getAdminMenu().setEnabled(gm);
896+ for(ClientPlugin plugin : pluginMap.values()){
897+ plugin.getPluginView().setGM(gm);
898+ }
896899 }
897900
898901 /**
@@ -1088,6 +1091,11 @@
10881091 if (loadServerItem == null) {
10891092 loadServerItem = new JMenuItem();
10901093 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+ });
10911099 }
10921100 return loadServerItem;
10931101 }
@@ -1101,6 +1109,11 @@
11011109 if (saveServerItem == null) {
11021110 saveServerItem = new JMenuItem();
11031111 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+ });
11041117 }
11051118 return saveServerItem;
11061119 }
--- chaplet/trunk/src/core/jp/sf/chaplet/core/client/ChatClient.java (revision 29)
+++ chaplet/trunk/src/core/jp/sf/chaplet/core/client/ChatClient.java (revision 30)
@@ -284,10 +284,11 @@
284284 return (JPanel) view.getContentPane();
285285 }
286286
287- public void loadServerSetting(final String fileName) {
287+ @Action
288+ public void loadServerSetting() {
288289 Thread thread = new Thread(new Runnable() {
289290 public void run() {
290- server.load(fileName);
291+ server.load();
291292 SwingUtilities.invokeLater(new Runnable() {
292293 public void run() {
293294 JOptionPane.showMessageDialog(view, "ロードに成功しました。",
@@ -299,10 +300,11 @@
299300 thread.start();
300301 }
301302
302- public void saveServerSetting(final String fileName) {
303+ @Action
304+ public void saveServerSetting() {
303305 Thread thread = new Thread(new Runnable() {
304306 public void run() {
305- server.save(fileName);
307+ server.save();
306308 SwingUtilities.invokeLater(new Runnable() {
307309 public void run() {
308310 SwingUtilities.invokeLater(new Runnable() {
Show on old repository browser