11package com .dinosaur .dinosaurexploder .utils ;
22
3+ import com .dinosaur .dinosaurexploder .exception .LockedShipException ;
34import com .dinosaur .dinosaurexploder .exception .LockedWeaponException ;
45import com .dinosaur .dinosaurexploder .model .HighScore ;
6+ import com .dinosaur .dinosaurexploder .model .TotalCoins ;
7+
58import java .io .FileInputStream ;
69import java .io .IOException ;
710import java .io .ObjectInputStream ;
@@ -14,11 +17,18 @@ public class WeaponUnlockChecker {
1417 1 , 0 ,
1518 2 , 50 ,
1619 3 , 100 );
20+
21+ private static final Map <Integer , Integer > coinMap = Map .of ( // key: weaponNumber, value: lower limit total coins
22+ 1 , 0 ,
23+ 2 , 5 ,
24+ 3 , 10 );
25+
1726 private HighScore highScore = new HighScore ();
27+ private TotalCoins totalCoins = new TotalCoins ();
1828
1929 public int check (int weaponNumber ) {
2030 highScore = getHighScore ();
21- checkScore (weaponNumber );
31+ checkScoreAndCoins (weaponNumber );
2232 return weaponNumber ;
2333 }
2434
@@ -31,11 +41,44 @@ public HighScore getHighScore() {
3141 }
3242 }
3343
34- private void checkScore (int weaponNumber ) {
35- int lowerLimit = scoreMap .getOrDefault (weaponNumber , 0 );
36- if (lowerLimit <= highScore .getHigh ())
44+ public TotalCoins getTotalCoins () {
45+ try (FileInputStream file = new FileInputStream ("totalCoins.ser" );
46+ ObjectInputStream in = new ObjectInputStream (file )) {
47+ return (TotalCoins ) in .readObject ();
48+ } catch (IOException | ClassNotFoundException e ) {
49+ return new TotalCoins ();
50+ }
51+ }
52+
53+ // private void checkScore(int weaponNumber) {
54+ // int lowerLimit = scoreMap.getOrDefault(weaponNumber, 0);
55+ // if (lowerLimit <= highScore.getHigh())
56+ // return;
57+ // throw new
58+ // LockedWeaponException(languageManager.getTranslation("weapon_locked") + "\n"
59+ // +
60+ // languageManager.getTranslation("unlock_highScore").replace("##",
61+ // String.valueOf(lowerLimit)));
62+ // }
63+
64+ private void checkScoreAndCoins (int weaponNumber ) {
65+ int lowerScoreLimit = scoreMap .getOrDefault (weaponNumber , 0 );
66+ int lowerCoinLimit = coinMap .getOrDefault (weaponNumber , 0 );
67+
68+ if (lowerScoreLimit <= highScore .getHigh () && lowerCoinLimit <= totalCoins .getTotal ())
3769 return ;
38- throw new LockedWeaponException (languageManager .getTranslation ("weapon_locked" ) + "\n " +
39- languageManager .getTranslation ("unlock_highScore" ).replace ("##" , String .valueOf (lowerLimit )));
70+ else if (lowerScoreLimit > highScore .getHigh () && lowerCoinLimit <= totalCoins .getTotal ()) {
71+ throw new LockedWeaponException (languageManager .getTranslation ("weapon_locked" ) + "\n " +
72+ languageManager .getTranslation ("unlock_highScore" ).replace ("##" , String .valueOf (lowerScoreLimit )));
73+ } else if (lowerScoreLimit <= highScore .getHigh () && lowerCoinLimit > totalCoins .getTotal ()) {
74+ throw new LockedWeaponException (languageManager .getTranslation ("weapon_locked" ) + "\n " +
75+ languageManager .getTranslation ("unlock_totalCoins" ).replace ("##" , String .valueOf (lowerCoinLimit )));
76+ } else {
77+ throw new LockedWeaponException (languageManager .getTranslation ("weapon_locked" ) + "\n "
78+ + languageManager .getTranslation ("unlock_highScore" ).replace ("##" , String .valueOf (lowerScoreLimit ))
79+ + "\n " +
80+ languageManager .getTranslation ("unlock_totalCoins" ).replace ("##" , String .valueOf (lowerCoinLimit )));
81+ }
82+
4083 }
4184}
0 commit comments