001    package jagafa.stats;
002    
003    import jagafa.JassRound;
004    import jagafa.object.Card;
005    import jagafa.object.CardList;
006    import jagafa.object.GoneCardHeap;
007    import jagafa.object.Player;
008    import jagafa.rule.RuleSet;
009    
010    import javax.swing.JComponent;
011    
012    public class PlayerStats {
013    
014            private static final int COLORS = 4;
015    
016            private CardList bockList_[];
017    
018            private CardList trumpfList_;
019    
020            private CardList colorList_[];
021    
022            private CardList validList_;
023    
024            private JassRound round_;
025    
026            private Player player_;
027    
028            private GoneCardHeap goneHeap_;
029    
030            public PlayerStats(JassRound round, Player player) {
031                    this.round_ = round;
032                    this.player_ = player;
033                    this.goneHeap_ = round.getGoneHeap();
034                    this.update();
035            }
036            
037            public PlayerStats(RuleSet rules, Player player) {
038                    this.round_ = new JassRound(rules);
039                    this.goneHeap_ = round_.getGoneHeap();
040                    this.player_ = player;
041                    this.update();
042            }
043    
044            /**
045             * Update the stats
046             */
047            public synchronized void update() {
048                    if (this.round_ != null) {
049                            this.createColorList();
050                            if (this.round_.getRules()==null) {
051                                    return;
052                            }
053                            
054                            
055                            this.createBockList();
056                            this.createValidList();
057                            this.createTrumpfList();
058                    }
059            }
060    
061            private void createTrumpfList() {
062                    CardList cards = player_.getHand();
063                    this.trumpfList_ = new CardList();
064    
065                    for (int i = 0; i < cards.size(); i++) {
066                            Card c = cards.get(i);
067                            if (c.getColor() == this.round_.getRules().specialColor()) {
068                                    this.trumpfList_.add(c);
069                            }
070                    }
071    
072            }
073    
074            private void createValidList() {
075                    CardList cards = player_.getHand();
076                    this.validList_ = new CardList();
077    
078                    for (int i = 0; i < cards.size(); i++) {
079                            Card c = cards.get(i);
080                            if (this.round_.isValidPlay(c)) {
081                                    this.validList_.add(c);
082                            }
083                    }
084    
085            }
086    
087            /**
088             * Create lists with each containing the cards of a color of the active Players hand
089             */
090            private void createColorList() {
091                    CardList cards = player_.getHand();
092                    this.colorList_ = new CardList[COLORS];
093    
094                    // All Cards of Color i
095                    for (int i = 0; i < COLORS; i++) {
096                            this.colorList_[i] = new CardList();
097                            CardList colorC = CardTool.getAllOfColor(cards, i);
098                            this.colorList_[i].addAll(colorC);
099                    }
100            }
101    
102            /**
103             * Create lists with Bocks sorted by its level and color
104             */
105            private void createBockList() {
106                    if (round_.getRules().specialColor() != -1) {
107                            String trumpfe = "Anzahl Trümpfe: ";
108                            CardList cardsL = player_.getHand();
109                            this.trumpfList_ = CardTool.getAllOfColor(cardsL, round_.getRules()
110                                            .specialColor());
111                    }
112    
113                    bockList_ = new CardList[9];
114                    CardList cardsL = player_.getHand();
115                    for (int j = 0; j < 9; j++) {
116                            bockList_[j] = new CardList();
117                            for (int i = 0; i < cardsL.size(); i++) {
118                                    if (CardTool.isBock(j, cardsL.get(i), this.goneHeap_, round_
119                                                    .getRules())) {
120                                            bockList_[j].add(cardsL.get(i));
121                                    }
122                            }
123                    }
124            }
125    
126            /**
127             * @return Returns the bockList array where the array has the form arr[level]
128             */
129            public CardList[] getBockList() {
130                    return this.bockList_;
131            }
132    
133            /**
134             * @return Returns the colorList in the form CardList[color].
135             */
136            public CardList[] getColorList() {
137                    return this.colorList_;
138            }
139    
140            /**
141             * @return Returns the trumpfList
142             */
143            public CardList getTrumpfList() {
144                    return this.trumpfList_;
145            }
146    
147            public int countColorCards(int color) {
148                    return this.colorList_[color].size();
149            }
150    
151            /**
152             * @return
153             */
154            public CardList getValidList() {
155                    return this.validList_;
156            }
157    
158            public void setRound(JassRound round) {
159                    this.round_ = round;
160            }
161    
162            public JComponent getView() {
163                    return null;
164            }
165            
166            public Player getPlayer() {
167                    return this.player_;
168            }
169            
170            public void setPlayer(Player p) {
171                    this.player_ = p;
172            }
173    
174    }