OSDN Git Service

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