OSDN Git Service

Merge release/v3.303.106
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / data / Talk.java
1 /*
2  * dialogs in game
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.data;
9
10 import jp.sourceforge.jindolf.corelib.TalkType;
11
12 /**
13  * プレイヤーの発言。
14  */
15 public class Talk implements Topic{
16
17     private final Period homePeriod;
18     private final TalkType talkType;
19     private final Avatar avatar;
20     private final int talkNo;
21     private final String messageID;
22     private final int hour;
23     private final int minute;
24     private final CharSequence dialog;
25     private final int charNum;
26     private int count = -1;
27
28
29     /**
30      * Talkの生成。
31      * @param homePeriod 発言元Period
32      * @param talkType 発言種別
33      * @param avatar Avatar
34      * @param talkNo 公開発言番号。公開発言でないなら0以下の値を指定。
35      * @param messageID メッセージID
36      * @param hour 発言時
37      * @param minute 発言分
38      * @param dialog 会話データ
39      */
40     public Talk(Period homePeriod,
41                  TalkType talkType,
42                  Avatar avatar,
43                  int talkNo,
44                  String messageID,
45                  int hour, int minute,
46                  CharSequence dialog ){
47         if(    homePeriod == null
48             || talkType   == null
49             || avatar     == null
50             || messageID  == null
51             || dialog     == null ) throw new NullPointerException();
52         if(hour   < 0 || 23 < hour  ) throw new IllegalArgumentException();
53         if(minute < 0 || 59 < minute) throw new IllegalArgumentException();
54         if(talkType != TalkType.PUBLIC){
55             if(0 < talkNo) throw new IllegalArgumentException();
56         }
57
58         this.homePeriod = homePeriod;
59         this.talkType = talkType;
60         this.avatar = avatar;
61         this.talkNo = talkNo;
62         this.messageID = messageID;
63         this.hour = hour;
64         this.minute = minute;
65         this.dialog = dialog;
66
67         this.charNum = this.dialog.length();
68
69         return;
70     }
71
72
73     /**
74      * 会話種別から色名への変換を行う。
75      * @param type 会話種別
76      * @return 色名
77      */
78     public static String encodeColorName(TalkType type){
79         String result;
80
81         switch(type){
82         case PUBLIC:   result = "白"; break;
83         case PRIVATE:  result = "灰"; break;
84         case WOLFONLY: result = "赤"; break;
85         case GRAVE:    result = "青"; break;
86         default:
87             assert false;
88             return null;
89         }
90
91         return result;
92     }
93
94     /**
95      * 発言が交わされたPeriodを返す。
96      * @return Period
97      */
98     public Period getPeriod(){
99         return this.homePeriod;
100     }
101
102     /**
103      * 発言種別を得る。
104      * @return 種別
105      */
106     public TalkType getTalkType(){
107         return this.talkType;
108     }
109
110     /**
111      * 墓下発言か否か判定する。
112      * @return 墓下発言ならtrue
113      */
114     public boolean isGrave(){
115         return this.talkType == TalkType.GRAVE;
116     }
117
118     /**
119      * 発言種別ごとにその日(Period)の累積発言回数を返す。
120      * 1から始まる。
121      * @return 累積発言回数。
122      */
123     public int getTalkCount(){
124         return this.count;
125     }
126
127     /**
128      * 発言文字数を返す。
129      * 改行(\n)は1文字。
130      * @return 文字数
131      */
132     public int getTotalChars(){
133         return this.charNum;
134     }
135
136     /**
137      * 発言元Avatarを得る。
138      * @return 発言元Avatar
139      */
140     public Avatar getAvatar(){
141         return this.avatar;
142     }
143
144     /**
145      * 公開発言番号を取得する。
146      * 公開発言番号が割り振られてなければ0以下の値を返す。
147      * @return 公開発言番号
148      */
149     public int getTalkNo(){
150         return this.talkNo;
151     }
152
153     /**
154      * 公開発言番号の有無を返す。
155      * @return 公開発言番号が割り当てられているならtrueを返す。
156      */
157     public boolean hasTalkNo(){
158         if(0 < this.talkNo) return true;
159         return false;
160     }
161
162     /**
163      * メッセージIDを取得する。
164      * @return メッセージID
165      */
166     public String getMessageID(){
167         return this.messageID;
168     }
169
170     /**
171      * メッセージIDからエポック秒(ms)に変換する。
172      * @return GMT 1970-01-01 00:00:00 からのエポック秒(ms)
173      */
174     public long getTimeFromID(){
175         String epoch = this.messageID.replace("mes", "");
176         long result = Long.parseLong(epoch) * 1000;
177         return result;
178     }
179
180     /**
181      * 発言時を取得する。
182      * @return 発言時
183      */
184     public int getHour(){
185         return this.hour;
186     }
187
188     /**
189      * 発言分を取得する。
190      * @return 発言分
191      */
192     public int getMinute(){
193         return this.minute;
194     }
195
196     /**
197      * 会話データを取得する。
198      * @return 会話データ
199      */
200     public CharSequence getDialog(){
201         return this.dialog;
202     }
203
204     /**
205      * 発言種別ごとの発言回数を設定する。
206      * @param count 発言回数
207      */
208     public void setCount(int count){
209         this.count = count;
210         return;
211     }
212
213     /**
214      * この会話を識別するためのアンカー文字列を生成する。
215      * 例えば「3d09:56」など。
216      * @return アンカー文字列
217      */
218     public String getAnchorNotation(){
219         int day = this.homePeriod.getDay();
220
221         String hstr = "0"+this.hour;
222         hstr = hstr.substring(hstr.length() - 2);
223         String mstr = "0"+this.minute;
224         mstr = mstr.substring(mstr.length() - 2);
225
226         return day + "d" + hstr + ":" + mstr;
227     }
228
229     /**
230      * この会話を識別するためのG国用アンカー文字列を発言番号から生成する。
231      * 例えば「{@literal >>172}」など。
232      * @return アンカー文字列。発言番号がなければ空文字列。
233      */
234     public String getAnchorNotation_G(){
235         if( ! hasTalkNo() ) return "";
236         return ">>" + this.talkNo;
237     }
238
239     /**
240      * {@inheritDoc}
241      * 会話のString表現を返す。
242      * 実体参照やHTMLタグも含まれる。
243      * @return 会話のString表現
244      */
245     @Override
246     public String toString(){
247         StringBuilder result = new StringBuilder();
248
249         result.append(this.avatar.getFullName());
250
251         if     (this.talkType == TalkType.PUBLIC)   result.append(" says ");
252         else if(this.talkType == TalkType.PRIVATE)  result.append(" think ");
253         else if(this.talkType == TalkType.WOLFONLY) result.append(" howl ");
254         else if(this.talkType == TalkType.GRAVE)    result.append(" groan ");
255
256         result.append(this.dialog);
257
258         return result.toString();
259     }
260
261 }