OSDN Git Service

抜けたthisを補充
[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: assert false; return null;
87         }
88
89         return result;
90     }
91
92     /**
93      * 発言が交わされたPeriodを返す。
94      * @return Period
95      */
96     public Period getPeriod(){
97         return this.homePeriod;
98     }
99
100     /**
101      * 発言種別を得る。
102      * @return 種別
103      */
104     public TalkType getTalkType(){
105         return this.talkType;
106     }
107
108     /**
109      * 墓下発言か否か判定する。
110      * @return 墓下発言ならtrue
111      */
112     public boolean isGrave(){
113         return this.talkType == TalkType.GRAVE;
114     }
115
116     /**
117      * 発言種別ごとにその日(Period)の累積発言回数を返す。
118      * 1から始まる。
119      * @return 累積発言回数。
120      */
121     public int getTalkCount(){
122         return this.count;
123     }
124
125     /**
126      * 発言文字数を返す。
127      * 改行(\n)は1文字。
128      * @return 文字数
129      */
130     public int getTotalChars(){
131         return this.charNum;
132     }
133
134     /**
135      * 発言元Avatarを得る。
136      * @return 発言元Avatar
137      */
138     public Avatar getAvatar(){
139         return this.avatar;
140     }
141
142     /**
143      * 公開発言番号を取得する。
144      * 公開発言番号が割り振られてなければ0以下の値を返す。
145      * @return 公開発言番号
146      */
147     public int getTalkNo(){
148         return this.talkNo;
149     }
150
151     /**
152      * 公開発言番号の有無を返す。
153      * @return 公開発言番号が割り当てられているならtrueを返す。
154      */
155     public boolean hasTalkNo(){
156         if(0 < this.talkNo) return true;
157         return false;
158     }
159
160     /**
161      * メッセージIDを取得する。
162      * @return メッセージID
163      */
164     public String getMessageID(){
165         return this.messageID;
166     }
167
168     /**
169      * メッセージIDからエポック秒(ms)に変換する。
170      * @return GMT 1970-01-01 00:00:00 からのエポック秒(ms)
171      */
172     public long getTimeFromID(){
173         String epoch = this.messageID.replace("mes", "");
174         long result = Long.parseLong(epoch) * 1000;
175         return result;
176     }
177
178     /**
179      * 発言時を取得する。
180      * @return 発言時
181      */
182     public int getHour(){
183         return this.hour;
184     }
185
186     /**
187      * 発言分を取得する。
188      * @return 発言分
189      */
190     public int getMinute(){
191         return this.minute;
192     }
193
194     /**
195      * 会話データを取得する。
196      * @return 会話データ
197      */
198     public CharSequence getDialog(){
199         return this.dialog;
200     }
201
202     /**
203      * 発言種別ごとの発言回数を設定する。
204      * @param count 発言回数
205      */
206     public void setCount(int count){
207         this.count = count;
208         return;
209     }
210
211     /**
212      * この会話を識別するためのアンカー文字列を生成する。
213      * 例えば「3d09:56」など。
214      * @return アンカー文字列
215      */
216     public String getAnchorNotation(){
217         int day = this.homePeriod.getDay();
218
219         String hstr = "0"+this.hour;
220         hstr = hstr.substring(hstr.length() - 2);
221         String mstr = "0"+this.minute;
222         mstr = mstr.substring(mstr.length() - 2);
223
224         return day + "d" + hstr + ":" + mstr;
225     }
226
227     /**
228      * この会話を識別するためのG国用アンカー文字列を発言番号から生成する。
229      * 例えば「{@literal >>172}」など。
230      * @return アンカー文字列。発言番号がなければ空文字列。
231      */
232     public String getAnchorNotation_G(){
233         if( ! hasTalkNo() ) return "";
234         return ">>" + this.talkNo;
235     }
236
237     /**
238      * {@inheritDoc}
239      * 会話のString表現を返す。
240      * 実体参照やHTMLタグも含まれる。
241      * @return 会話のString表現
242      */
243     @Override
244     public String toString(){
245         StringBuilder result = new StringBuilder();
246
247         result.append(this.avatar.getFullName());
248
249         if     (this.talkType == TalkType.PUBLIC)   result.append(" says ");
250         else if(this.talkType == TalkType.PRIVATE)  result.append(" think ");
251         else if(this.talkType == TalkType.WOLFONLY) result.append(" howl ");
252         else if(this.talkType == TalkType.GRAVE)    result.append(" groan ");
253
254         result.append(this.dialog);
255
256         return result.toString();
257     }
258
259 }