View Javadoc

1   package net.sourceforge.simplegamenet.dice;
2   
3   import net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel;
4   import net.sourceforge.simplegamenet.specs.to.GameSettings;
5   import net.sourceforge.simplegamenet.specs.to.PlayerSettings;
6   import net.sourceforge.simplegamenet.specs.tools.StandardPlayerSettings;
7   
8   public class DiceSettings extends GameSettings implements DiceSettingOptions {
9   
10      private int[] amountValues = {8, 8};
11      // [0] participantsAmount
12      // [1] connectionsAmount
13  
14      private int[] settingValues = {FIRST_PLAYER, MIN_5, 35, 25, 30, 40, 50};
15      // [0] bonusDeval
16      // [1] rightDeval
17      // [2] bonusScore
18      // [3] fullHouseScore
19      // [4] smallStreetScore
20      // [5] bigStreetScore
21      // [6] fiveOAKScore
22  
23      public DiceSettings() {
24      }
25  
26      public DiceSettings(int[] amountValues, int[] settingValues) {
27          this.amountValues = amountValues;
28          this.settingValues = settingValues;
29      }
30  
31      public GameSettingsPanel createGameSettingsPanel() {
32          DiceSettingsPanel diceSettingsPanel = new DiceSettingsPanel();
33          for (int i = 0; i < amountValues.length; i++) {
34              switch (i) {
35                  case 0:
36                      diceSettingsPanel
37                              .setMaximumParticipantsComboBoxSelectedIndex(amountValues[i] - 2);
38                      break;
39                  case 1:
40                      diceSettingsPanel
41                              .setMaximumConnectionsComboBoxSelectedIndex(amountValues[i] - 2);
42                      break;
43              }
44          }
45          for (int i = 0; i < settingValues.length; i++) {
46              if (i == 0 || i == 1) {
47                  if (settingValues[i] == FIRST_PLAYER) {
48                      diceSettingsPanel.setSettingComboBoxesSelectedIndex((i + 1), 0);
49                  } else if (settingValues[i] == MIN_5) {
50                      diceSettingsPanel.setSettingComboBoxesSelectedIndex((i + 1), 1);
51                  } else if (settingValues[i] == MIN_10) {
52                      diceSettingsPanel.setSettingComboBoxesSelectedIndex((i + 1), 2);
53                  } else if (settingValues[i] == NONE) {
54                      diceSettingsPanel.setSettingComboBoxesSelectedIndex((i + 1), 3);
55                  }
56              } else {
57                  diceSettingsPanel
58                          .setSettingComboBoxesSelectedIndex((i + 1), settingValues[i] - 1);
59              }
60          }
61          return diceSettingsPanel;
62      }
63  
64      public GameSettings createChangedGameSettings(GameSettingsPanel gameSettingsPanel) {
65          DiceSettingsPanel diceSettingsPanel = (DiceSettingsPanel) gameSettingsPanel;
66          int[] changedAmountValues = new int[amountValues.length];
67          int[] changedSettingValues = new int[settingValues.length];
68          changedAmountValues[0] =
69                  (diceSettingsPanel.getMaximumParticipantsComboBoxSelectedItem()).intValue();
70          changedAmountValues[1] =
71                  (diceSettingsPanel.getMaximumConnectionsComboBoxSelectedItem()).intValue();
72          for (int i = 0; i < diceSettingsPanel.getSettingComboBoxesLength(); i++) {
73              if (i == 1) {
74                  if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 0) {
75                      changedSettingValues[0] = FIRST_PLAYER;
76                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 1) {
77                      changedSettingValues[0] = MIN_5;
78                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 2) {
79                      changedSettingValues[0] = MIN_10;
80                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 3) {
81                      changedSettingValues[0] = NONE;
82                  }
83              } else if (i == 2) {
84                  if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 0) {
85                      changedSettingValues[1] = FIRST_PLAYER;
86                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 1) {
87                      changedSettingValues[1] = MIN_5;
88                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 2) {
89                      changedSettingValues[1] = MIN_10;
90                  } else if (diceSettingsPanel.getSettingComboBoxesSelectedIndex(i) == 3) {
91                      changedSettingValues[1] = NONE;
92                  }
93              }
94              changedSettingValues[2] = Integer.parseInt(
95                      diceSettingsPanel.getSettingComboBoxesScoresSelectedItem(3));
96              changedSettingValues[3] = Integer.parseInt(
97                      diceSettingsPanel.getSettingComboBoxesScoresSelectedItem(4));
98              changedSettingValues[4] = Integer.parseInt(
99                      diceSettingsPanel.getSettingComboBoxesScoresSelectedItem(5));
100             changedSettingValues[5] = Integer.parseInt(
101                     diceSettingsPanel.getSettingComboBoxesScoresSelectedItem(6));
102             changedSettingValues[6] = Integer.parseInt(
103                     diceSettingsPanel.getSettingComboBoxesScoresSelectedItem(7));
104         }
105         return new DiceSettings(changedAmountValues, changedSettingValues);
106     }
107 
108     public int[] getAmountValues() {
109         int[] returnAmountValues = new int[amountValues.length];
110         System.arraycopy(amountValues, 0, returnAmountValues, 0, amountValues.length);
111         return returnAmountValues;
112     }
113 
114     public int[] getSettingValues() {
115         int[] returnSettingValues = new int[settingValues.length];
116         System.arraycopy(settingValues, 0, returnSettingValues, 0, settingValues.length);
117         return returnSettingValues;
118     }
119 
120     public boolean isChangeGameSettingsAllowed(GameSettings newGameSettings) {
121         DiceSettings diceSettings = (DiceSettings) newGameSettings;
122         if (engine.getPlayerSettingsMap().getConnectionsAmount()
123                 > diceSettings.amountValues[1]
124                 || engine.getPlayerSettingsMap().getParticipantsAmount()
125                 > diceSettings.amountValues[0]) {
126             return false;
127         } else {
128             return true;
129         }
130     }
131 
132     public boolean isCreateDefaultPlayerSettingsAllowed(int playerType) {
133         if (engine.getPlayerSettingsMap().getConnectionsAmount() >= amountValues[1]) {
134             return false;
135         }
136         return true;
137     }
138 
139     public PlayerSettings createDefaultPlayerSettings(Integer playerID, int playerType,
140                                                       String nickname) {
141         int playingState = engine.isGamePlaying() ? PlayerSettings.OBSERVING
142                 : PlayerSettings.PARTICIPATING;
143         if (engine.getPlayerSettingsMap().getParticipantsAmount() >= amountValues[0]) {
144             playingState = PlayerSettings.OBSERVING;
145         }
146         return new StandardPlayerSettings(engine, playerID, playerType, nickname,
147                 playingState);
148     }
149 
150     public boolean isChangePlayerSettingsAllowed(PlayerSettings currentPlayerSettings,
151                                                  PlayerSettings changedPlayerSettings) {
152         if (currentPlayerSettings.getPlayingState() == PlayerSettings.OBSERVING
153                 && changedPlayerSettings.getPlayingState() == PlayerSettings.PARTICIPATING
154                 && engine.getPlayerSettingsMap().getParticipantsAmount() >= amountValues[0]) {
155             return false;
156         } else {
157             return true;
158         }
159     }
160 
161     public boolean isStartGameAllowed() {
162         if (engine.getPlayerSettingsMap().getParticipantsAmount() < 1) {
163             return false;
164         }
165         return true;
166     }
167 
168 }