OSDN Git Service

implement execution tag parser.
[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.osdn.jindolf.parser.content.DecodedContent;
16 import jp.sourceforge.jindolf.corelib.EventFamily;
17 import jp.sourceforge.jindolf.corelib.GameRole;
18 import jp.sourceforge.jindolf.corelib.SysEventType;
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     /** for playerList and onStage. */
37     private final List<Player> playerList = new LinkedList<>();
38     /** for execution. */
39     private final List<Nominated> nominatedList = new LinkedList<>();
40
41
42     /**
43      * コンストラクタ。
44      */
45     public SysEvent(){
46         super();
47         return;
48     }
49
50     /**
51      * イベントファミリを取得する。
52      * @return イベントファミリ
53      */
54     public EventFamily getEventFamily(){
55         return this.eventFamily;
56     }
57
58     /**
59      * イベントファミリを設定する。
60      * @param eventFamily イベントファミリ
61      * @throws NullPointerException 引数がnull
62      */
63     public void setEventFamily(EventFamily eventFamily)
64             throws NullPointerException{
65         this.eventFamily = eventFamily;
66         return;
67     }
68
69     /**
70      * イベント種別を取得する。
71      * @return イベント種別
72      */
73     public SysEventType getSysEventType(){
74         return this.sysEventType;
75     }
76
77     /**
78      * イベント種別を設定する。
79      * @param type イベント種別
80      * @throws NullPointerException 引数がnull
81      */
82     public void setSysEventType(SysEventType type)
83             throws NullPointerException{
84         if(type == null) throw new NullPointerException();
85         this.sysEventType = type;
86         return;
87     }
88
89     /**
90      * イベントメッセージを取得する。
91      * @return イベントメッセージ
92      */
93     public DecodedContent getContent(){
94         return this.content;
95     }
96
97     /**
98      * イベントメッセージを設定する。
99      * @param content イベントメッセージ
100      * @throws NullPointerException 引数がnull
101      */
102     public void setContent(DecodedContent content)
103             throws NullPointerException{
104         if(content == null) throw new NullPointerException();
105         this.content = content;
106         return;
107     }
108
109     /**
110      * Avatarリストを取得する。
111      * @return Avatarリスト
112      */
113     public List<Avatar> getAvatarList(){
114         List<Avatar> result = Collections.unmodifiableList(this.avatarList);
115         return result;
116     }
117
118     /**
119      * Roleリストを取得する。
120      * @return Roleリスト
121      */
122     public List<GameRole> getRoleList(){
123         List<GameRole> result = Collections.unmodifiableList(this.roleList);
124         return result;
125     }
126
127     /**
128      * Integerリストを取得する。
129      * @return Integerリスト
130      */
131     public List<Integer> getIntegerList(){
132         List<Integer> result = Collections.unmodifiableList(this.integerList);
133         return result;
134     }
135
136     /**
137      * CharSequenceリストを取得する。
138      * @return CharSequenceリスト
139      */
140     public List<CharSequence> getCharSequenceList(){
141         List<CharSequence> result =
142                 Collections.unmodifiableList(this.charseqList);
143         return result;
144     }
145
146     /**
147      * Playerリストを返す。
148      *
149      * @return Playerリスト
150      */
151     public List<Player> getPlayerList(){
152         List<Player> result =
153                 Collections.unmodifiableList(this.playerList);
154         return result;
155     }
156
157     /**
158      * Nominatedリストを返す。
159      *
160      * @return Nominatedリスト
161      */
162     public List<Nominated> getNominatedList(){
163         List<Nominated> result =
164                 Collections.unmodifiableList(this.nominatedList);
165         return result;
166     }
167
168     /**
169      * Avatar一覧を追加する。
170      * @param list Avatar一覧
171      */
172     public void addAvatarList(List<Avatar> list){
173         this.avatarList.addAll(list);
174         return;
175     }
176
177     /**
178      * 役職一覧を追加する。
179      * @param list 役職一覧
180      */
181     public void addRoleList(List<GameRole> list){
182         this.roleList.addAll(list);
183         return;
184     }
185
186     /**
187      * 数値一覧を追加する。
188      * @param list 数値一覧
189      */
190     public void addIntegerList(List<Integer> list){
191         this.integerList.addAll(list);
192         return;
193     }
194
195     /**
196      * 文字列一覧を追加する。
197      * @param list 文字列一覧
198      */
199     public void addCharSequenceList(List<CharSequence> list){
200         this.charseqList.addAll(list);
201         return;
202     }
203
204     /**
205      * Player一覧を追加する。
206      *
207      * @param list Player一覧
208      */
209     public void addPlayerList(List<Player> list){
210         this.playerList.addAll(list);
211         return;
212     }
213
214     /**
215      * Nominated一覧を追加する。
216      *
217      * @param list Nominated一覧
218      */
219     public void addNominatedList(List<Nominated> list){
220         this.nominatedList.addAll(list);
221         return;
222     }
223
224     /**
225      * システムイベントを解析し、処刑されたAvatarを返す。
226      * G国運用中の時点で、処刑者が出るのはCOUNTINGとEXECUTIONのみ。
227      * @return 処刑されたAvatar。いなければnull
228      */
229     public Avatar getExecutedAvatar(){
230         Avatar result = null;
231
232         int avatarNum;
233         Avatar lastAvatar;
234
235         switch(this.sysEventType){
236         case COUNTING:
237             if(this.avatarList.isEmpty()) return null;
238             avatarNum = this.avatarList.size();
239             if(avatarNum % 2 != 0){
240                 lastAvatar = this.avatarList.get(avatarNum - 1);
241                 result = lastAvatar;
242             }
243             break;
244         case EXECUTION:
245             if( ! this.avatarList.isEmpty()){
246                 result = this.avatarList.get(0);
247             }
248             break;
249         case COUNTING2:
250             // NOTHING
251             break;
252         default:
253             break;
254         }
255
256         return result;
257     }
258
259     /**
260      * 投票に参加したAvatarの集合を返す。
261      * G国運用中の時点で、投票者が出るのはCOUNTINGとCOUNTING2のみ。
262      * @param set 結果格納先。nullなら自動的に確保される。
263      * @return 投票に参加したAvatarのSet
264      */
265     public Set<Avatar> getVoterSet(Set<Avatar> set){
266         Set<Avatar> result;
267         if(set == null) result = new HashSet<>();
268         else            result = set;
269
270         if(    this.sysEventType != SysEventType.COUNTING
271             && this.sysEventType != SysEventType.COUNTING2 ){
272             return result;
273         }
274
275         int size = this.avatarList.size();
276         assert size >= 2;
277         int limit = size - 1;
278         if(size % 2 != 0) limit--;
279
280         for(int idx = 0; idx <= limit; idx += 2){
281             Avatar avatar = this.avatarList.get(idx);
282             result.add(avatar);
283         }
284
285         return result;
286     }
287
288 }