OSDN Git Service

ダイヤモンド演算子対応
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / data / SysEvent.java
1 /*
2  * system event in game
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.data;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.LinkedList;
13 import java.util.List;
14 import java.util.Set;
15 import jp.sourceforge.jindolf.corelib.EventFamily;
16 import jp.sourceforge.jindolf.corelib.GameRole;
17 import jp.sourceforge.jindolf.corelib.SysEventType;
18 import jp.sourceforge.jindolf.parser.DecodedContent;
19
20 /**
21  * 人狼BBSシステムが生成する各種メッセージ。
22  * Topicの具体化。
23  */
24 public class SysEvent implements Topic{
25     // TODO 狼の襲撃先表示は Talk か SysEvent どちらにしよう...
26
27     private EventFamily eventFamily;
28     private SysEventType sysEventType;
29     private DecodedContent content;
30
31     private final List<Avatar>   avatarList  = new LinkedList<>();
32     private final List<GameRole> roleList    = new LinkedList<>();
33     private final List<Integer>  integerList = new LinkedList<>();
34     private final List<CharSequence>  charseqList =
35             new LinkedList<>();
36
37     /**
38      * コンストラクタ。
39      */
40     public SysEvent(){
41         super();
42         return;
43     }
44
45     /**
46      * イベントファミリを取得する。
47      * @return イベントファミリ
48      */
49     public EventFamily getEventFamily(){
50         return this.eventFamily;
51     }
52
53     /**
54      * イベントファミリを設定する。
55      * @param eventFamily イベントファミリ
56      * @throws NullPointerException 引数がnull
57      */
58     public void setEventFamily(EventFamily eventFamily)
59             throws NullPointerException{
60         this.eventFamily = eventFamily;
61         return;
62     }
63
64     /**
65      * イベント種別を取得する。
66      * @return イベント種別
67      */
68     public SysEventType getSysEventType(){
69         return this.sysEventType;
70     }
71
72     /**
73      * イベント種別を設定する。
74      * @param type イベント種別
75      * @throws NullPointerException 引数がnull
76      */
77     public void setSysEventType(SysEventType type)
78             throws NullPointerException{
79         if(type == null) throw new NullPointerException();
80         this.sysEventType = type;
81         return;
82     }
83
84     /**
85      * イベントメッセージを取得する。
86      * @return イベントメッセージ
87      */
88     public DecodedContent getContent(){
89         return this.content;
90     }
91
92     /**
93      * イベントメッセージを設定する。
94      * @param content イベントメッセージ
95      * @throws NullPointerException 引数がnull
96      */
97     public void setContent(DecodedContent content)
98             throws NullPointerException{
99         if(content == null) throw new NullPointerException();
100         this.content = content;
101         return;
102     }
103
104     /**
105      * Avatarリストを取得する。
106      * @return Avatarリスト
107      */
108     public List<Avatar> getAvatarList(){
109         List<Avatar> result = Collections.unmodifiableList(this.avatarList);
110         return result;
111     }
112
113     /**
114      * Roleリストを取得する。
115      * @return Roleリスト
116      */
117     public List<GameRole> getRoleList(){
118         List<GameRole> result = Collections.unmodifiableList(this.roleList);
119         return result;
120     }
121
122     /**
123      * Integerリストを取得する。
124      * @return Integerリスト
125      */
126     public List<Integer> getIntegerList(){
127         List<Integer> result = Collections.unmodifiableList(this.integerList);
128         return result;
129     }
130
131     /**
132      * CharSequenceリストを取得する。
133      * @return CharSequenceリスト
134      */
135     public List<CharSequence> getCharSequenceList(){
136         List<CharSequence> result =
137                 Collections.unmodifiableList(this.charseqList);
138         return result;
139     }
140
141     /**
142      * Avatar一覧を追加する。
143      * @param list Avatar一覧
144      */
145     public void addAvatarList(List<Avatar> list){
146         this.avatarList.addAll(list);
147         return;
148     }
149
150     /**
151      * 役職一覧を追加する。
152      * @param list 役職一覧
153      */
154     public void addRoleList(List<GameRole> list){
155         this.roleList.addAll(list);
156         return;
157     }
158
159     /**
160      * 数値一覧を追加する。
161      * @param list 数値一覧
162      */
163     public void addIntegerList(List<Integer> list){
164         this.integerList.addAll(list);
165         return;
166     }
167
168     /**
169      * 文字列一覧を追加する。
170      * @param list 文字列一覧
171      */
172     public void addCharSequenceList(List<CharSequence> list){
173         this.charseqList.addAll(list);
174         return;
175     }
176
177     /**
178      * システムイベントを解析し、処刑されたAvatarを返す。
179      * G国運用中の時点で、処刑者が出るのはCOUNTINGとEXECUTIONのみ。
180      * @return 処刑されたAvatar。いなければnull
181      */
182     public Avatar getExecutedAvatar(){
183         Avatar result = null;
184
185         int avatarNum;
186         Avatar lastAvatar;
187
188         switch(this.sysEventType){
189         case COUNTING:
190             if(this.avatarList.isEmpty()) return null;
191             avatarNum = this.avatarList.size();
192             if(avatarNum % 2 != 0){
193                 lastAvatar = this.avatarList.get(avatarNum - 1);
194                 result = lastAvatar;
195             }
196             break;
197         case EXECUTION:
198             if(this.avatarList.isEmpty()) return null;
199             avatarNum = this.avatarList.size();
200             List<Integer> intList = getIntegerList();
201             int intNum = intList.size();
202             assert intNum > 0;
203             if(avatarNum != intNum || intList.get(intNum - 1) <= 0){
204                 lastAvatar = this.avatarList.get(avatarNum - 1);
205                 result = lastAvatar;
206             }
207             break;
208         case COUNTING2:
209             // NOTHING
210             break;
211         default:
212             break;
213         }
214
215         return result;
216     }
217
218     /**
219      * 投票に参加したAvatarの集合を返す。
220      * G国運用中の時点で、投票者が出るのはCOUNTINGとCOUNTING2のみ。
221      * @param set 結果格納先。nullなら自動的に確保される。
222      * @return 投票に参加したAvatarのSet
223      */
224     public Set<Avatar> getVoterSet(Set<Avatar> set){
225         Set<Avatar> result;
226         if(set == null) result = new HashSet<>();
227         else            result = set;
228
229         if(   this.sysEventType != SysEventType.COUNTING
230            && this.sysEventType != SysEventType.COUNTING2 ){
231             return result;
232         }
233
234         int size = this.avatarList.size();
235         assert size >= 2;
236         int limit = size - 1;
237         if(size % 2 != 0) limit--;
238
239         for(int idx = 0; idx <= limit; idx += 2){
240             Avatar avatar = this.avatarList.get(idx);
241             result.add(avatar);
242         }
243
244         return result;
245     }
246
247 }