diff --git a/android/src/com/mygdx/game/AndroidInterfaceClass.java b/android/src/com/mygdx/game/AndroidInterfaceClass.java index 4326af241783ade797d7b01dea4063f892c582ca..b68ffe5b47512ec81894e53ad81e76a6b4fee422 100644 --- a/android/src/com/mygdx/game/AndroidInterfaceClass.java +++ b/android/src/com/mygdx/game/AndroidInterfaceClass.java @@ -6,19 +6,24 @@ import android.util.Log; import androidx.annotation.NonNull; +import com.google.android.gms.tasks.Task; +import com.google.android.gms.tasks.TaskCompletionSource; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; +import java.util.ArrayList; +import java.util.List; + public class AndroidInterfaceClass implements FireBaseInterface { FirebaseDatabase database; DatabaseReference myRef; public AndroidInterfaceClass() { database = FirebaseDatabase.getInstance(); - myRef = database.getReference("message"); + //myRef = database.getReference("highScores"); } @Override @@ -28,37 +33,89 @@ public class AndroidInterfaceClass implements FireBaseInterface { @Override public void FirstFirebaseTest() { - if (myRef != null) { + /* if (myRef != null) { myRef.setValue("hello world"); } else { System.out.println("dbRef was not set. Could not write to database."); - } + }*/ } @Override - public void SetOnValueChangedListener() { + public void SetOnValueChangedListener(String target) { + System.out.println("Yes I am running"); + myRef = database.getReference(target); + // Read from the database myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { // This method is called once with the initial value and again // whenever data at this location is updated. - String value = dataSnapshot.getValue(String.class); - Log.d(TAG, "Value is: " + value); + List<Integer> values = new ArrayList<>(); + for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { + Integer value = childSnapshot.getValue(Integer.class); + values.add(value); + } + Log.d(TAG, "Values are: " + values); + System.out.println("Values are: " + values); } @Override public void onCancelled(@NonNull DatabaseError error) { // Failed to read value Log.w(TAG, "Failed to read value.", error.toException()); + System.out.println("FAT Error"); } }); + } + + + // Pushes a highscore into the highscore list in the firebase realtime database + @Override + public void SetValueInDb(String target, Integer value) { + DatabaseReference targetRef = database.getReference(target); + // Read current maximum key from the database + targetRef.orderByKey().limitToLast(1).addListenerForSingleValueEvent(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot dataSnapshot) { + long newKey = 1; // Default key if there are no keys yet + for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { + // Get the current maximum key and increment it + newKey = Long.parseLong(childSnapshot.getKey()) + 1; + } + // Set the new value with the incremented key + targetRef.child(String.valueOf(newKey)).setValue(value); + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + System.out.println(error); + } + }); } @Override - public void SetValueInDb(String target, String value) { - myRef = database.getReference(target); - myRef.setValue(value); + public void getDataFromDatabase(String target, OnDataLoadedListener onDataLoadedListener) { + DatabaseReference targetRef = database.getReference(target); + + targetRef.addListenerForSingleValueEvent(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot dataSnapshot) { + List<Integer> values = new ArrayList<>(); + for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) { + Integer value = childSnapshot.getValue(Integer.class); + values.add(value); + } + onDataLoadedListener.onDataLoaded(values); + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + onDataLoadedListener.onError(error.toException()); + } + }); } + + } diff --git a/core/src/com/mygdx/game/CoreInterfaceClass.java b/core/src/com/mygdx/game/CoreInterfaceClass.java index 6e355b16a207326e7a8d2946016064acd1091689..4a66a1f59d790b33028b21bcd5ef5c59f5d668e8 100644 --- a/core/src/com/mygdx/game/CoreInterfaceClass.java +++ b/core/src/com/mygdx/game/CoreInterfaceClass.java @@ -1,5 +1,8 @@ package com.mygdx.game; + +import java.util.List; + public class CoreInterfaceClass implements FireBaseInterface { @Override @@ -13,12 +16,19 @@ public class CoreInterfaceClass implements FireBaseInterface { } @Override - public void SetOnValueChangedListener() { + public void SetOnValueChangedListener(String target) { + + } + + @Override + public void SetValueInDb(String target, Integer value) { } @Override - public void SetValueInDb(String target, String value) { + public void getDataFromDatabase(String target, OnDataLoadedListener onDataLoadedListener) { } + + } diff --git a/core/src/com/mygdx/game/FireBaseInterface.java b/core/src/com/mygdx/game/FireBaseInterface.java index e0b17ad35ef72f5f1829a78105dbd197dd60b355..a2d03cd7057b90712ee9c1b850435e94a9525e98 100644 --- a/core/src/com/mygdx/game/FireBaseInterface.java +++ b/core/src/com/mygdx/game/FireBaseInterface.java @@ -1,8 +1,17 @@ package com.mygdx.game; +import java.util.List; + public interface FireBaseInterface { public void SomeFunction(); public void FirstFirebaseTest(); - public void SetOnValueChangedListener(); - public void SetValueInDb(String target, String value); + public void SetOnValueChangedListener(String target); + public void SetValueInDb(String target, Integer value); + + void getDataFromDatabase(String target, OnDataLoadedListener onDataLoadedListener); + + interface OnDataLoadedListener { + void onDataLoaded(List<Integer> values); + void onError(Exception exception); + } } diff --git a/core/src/com/mygdx/game/FirebaseManager.java b/core/src/com/mygdx/game/FirebaseManager.java new file mode 100644 index 0000000000000000000000000000000000000000..47c493942097d7fdb6d5f55aae397301f35f2750 --- /dev/null +++ b/core/src/com/mygdx/game/FirebaseManager.java @@ -0,0 +1,34 @@ +package com.mygdx.game; + +// This class is a singleton that manages the Firebase interface and ensures that there is only one +//instance of the interface across the entire application. +public class FirebaseManager { + private static FirebaseManager instance; + private FireBaseInterface firebaseInterface; + + private FirebaseManager(FireBaseInterface firebaseInterface) { + this.firebaseInterface = firebaseInterface; + } + + // Initializes the FirebaseManager singleton. + public static void initialize(FireBaseInterface firebaseInterface) { + if (instance == null) { + instance = new FirebaseManager(firebaseInterface); + } + } + + // Returns the single instance of the FirebaseManager class. + public static FirebaseManager getInstance() { + if (instance == null) { + throw new IllegalStateException("FirebaseManager is not initialized"); + } + return instance; + } + + // This method returns the FirebaseInterface instance + //which can be used to access the methods to interact with Firebase services. + public FireBaseInterface getFirebaseInterface() { + return firebaseInterface; + } +} + diff --git a/core/src/com/mygdx/game/HeroesVsMonsters.java b/core/src/com/mygdx/game/HeroesVsMonsters.java index fc431bd4bea1c3ae964c4fb86511d6884926018d..d5cfde9de9a548d76ff56eb9573a0a554df36abd 100644 --- a/core/src/com/mygdx/game/HeroesVsMonsters.java +++ b/core/src/com/mygdx/game/HeroesVsMonsters.java @@ -17,6 +17,8 @@ public class HeroesVsMonsters extends ApplicationAdapter { //FireBaseInterface FBIC; public HeroesVsMonsters(FireBaseInterface FBIC) { + FirebaseManager.initialize(FBIC); + //this.FBIC = FBIC; } diff --git a/core/src/com/mygdx/game/ds/Board.java b/core/src/com/mygdx/game/ds/Board.java index db7dc4970b06eec17c7a5447d07388ff05234170..a64bc2a10bacdbf6c896bbbc9e0a5be70a11e221 100644 --- a/core/src/com/mygdx/game/ds/Board.java +++ b/core/src/com/mygdx/game/ds/Board.java @@ -27,6 +27,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Array; +import com.mygdx.game.FireBaseInterface; +import com.mygdx.game.FirebaseManager; import com.mygdx.game.MoneySystem; import com.mygdx.game.components.AttackComponent; import com.mygdx.game.components.HeroComponent; @@ -41,6 +43,8 @@ import com.mygdx.game.types.HeroType; import com.mygdx.game.ds.buttons.DisplayHeroButton; import com.mygdx.game.types.MonsterType; +import java.util.List; + public class Board extends Actor { private final int screenHeight = Gdx.graphics.getHeight(); private final int screenWidth = Gdx.graphics.getWidth(); @@ -79,10 +83,15 @@ public class Board extends Actor { private int rightPaneWidth; private final Engine engine; private final MoneySystem moneySystem = new MoneySystem(8000); + private FireBaseInterface firebaseInterface; + private List<Integer> data; + + public Board(int rows, int cols, Engine engine) { + this.firebaseInterface = FirebaseManager.getInstance().getFirebaseInterface(); this.engine = engine; this.rows = rows; this.cols = cols; @@ -132,6 +141,27 @@ public class Board extends Actor { this.batch.end(); } + // Fetches the highscore list from the firebase realtime database and stores the + //values in data; + public void fetchData(String target) { + firebaseInterface.getDataFromDatabase(target, new FireBaseInterface.OnDataLoadedListener() { + @Override + public void onDataLoaded(List<Integer> values) { + + data = values; + System.out.println("Here is the data:"); + System.out.println(data); + // Do something with the data + } + + @Override + public void onError(Exception exception) { + System.out.println(exception); + } + }); + } + + public int getRows() { return rows; } @@ -225,6 +255,11 @@ public class Board extends Actor { System.out.println("Cell clicked: row " + row + ", col " + col); moneySystem.removeMoney(450); + + firebaseInterface.SetValueInDb("highScores", 12); + + fetchData("highScores"); + } public void drawHeroes() { diff --git a/desktop/src/com/mygdx/game/DesktopInterfaceClass.java b/desktop/src/com/mygdx/game/DesktopInterfaceClass.java index 06c3c736eda422c88e7a2c2698621b353b3a2ee9..f0d653bca23f9bbe003460442c7cf736f181d40c 100644 --- a/desktop/src/com/mygdx/game/DesktopInterfaceClass.java +++ b/desktop/src/com/mygdx/game/DesktopInterfaceClass.java @@ -1,5 +1,9 @@ package com.mygdx.game; +import com.google.android.gms.tasks.Task; + +import java.util.List; + public class DesktopInterfaceClass implements FireBaseInterface { @Override public void SomeFunction() { @@ -12,12 +16,19 @@ public class DesktopInterfaceClass implements FireBaseInterface { } @Override - public void SetOnValueChangedListener() { + public void SetOnValueChangedListener(String target) { + + } + + @Override + public void SetValueInDb(String target, Integer value) { } @Override - public void SetValueInDb(String target, String value) { + public void getDataFromDatabase(String target, OnDataLoadedListener onDataLoadedListener) { } + + } diff --git a/files/settings.json b/files/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..03ce0cd78ffb547d34d5db315c17503394f36f1d --- /dev/null +++ b/files/settings.json @@ -0,0 +1 @@ +{AUDIO:100,SFX:100} \ No newline at end of file