Skip to content
Snippets Groups Projects
Commit 59685862 authored by Sixten Müller's avatar Sixten Müller
Browse files

feat: working fetch word from DB and display in screen. Still missing visuals

parent 79c94be1
Branches
No related tags found
2 merge requests!22Resolve "Create game model",!18Draft: Resolve "Create game model"
...@@ -12,6 +12,10 @@ import com.wordbattle.game.model.LobbyModel; ...@@ -12,6 +12,10 @@ import com.wordbattle.game.model.LobbyModel;
import com.wordbattle.game.model.Word; import com.wordbattle.game.model.Word;
import com.wordbattle.game.network.FirebaseInterface; import com.wordbattle.game.network.FirebaseInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class AndroidInterfaceClass implements FirebaseInterface { public class AndroidInterfaceClass implements FirebaseInterface {
FirebaseDatabase database; FirebaseDatabase database;
...@@ -168,5 +172,36 @@ public class AndroidInterfaceClass implements FirebaseInterface { ...@@ -168,5 +172,36 @@ public class AndroidInterfaceClass implements FirebaseInterface {
// Should return seed, will be done later // Should return seed, will be done later
} }
public void fetchWord(String pin, String category, WordFetchCallback callback) {
DatabaseReference wordsRef = database.getReference("categories").child(category).child("words");
wordsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> words = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
words.add(snapshot.getKey()); // Assuming the words are stored as keys
}
if (!words.isEmpty()) {
long seed = generateSeedFromPin(pin);
Collections.shuffle(words, new Random(seed));
String selectedWord = words.get(0);
callback.onWordFetched(selectedWord);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
callback.onError(databaseError.getMessage());
}
});
}
private long generateSeedFromPin(String pin) {
return Long.parseLong(pin);
}
} }
...@@ -12,6 +12,7 @@ public class GamePlayController { ...@@ -12,6 +12,7 @@ public class GamePlayController {
private final GamePlayView gamePlayView; private final GamePlayView gamePlayView;
private FirebaseInterface _FBIC; private FirebaseInterface _FBIC;
private String pin; private String pin;
private String word;
...@@ -21,12 +22,30 @@ public class GamePlayController { ...@@ -21,12 +22,30 @@ public class GamePlayController {
this.pin = pin; this.pin = pin;
this.gamePlayView = new GamePlayView(state.getCam(), _FBIC, pin); // Assuming you provide a getter for the camera in BaseState this.gamePlayView = new GamePlayView(state.getCam(), _FBIC, pin); // Assuming you provide a getter for the camera in BaseState
// Fetch the word using the PIN and the category
fetchWord();
} }
public void handleInput() { public void handleInput() {
} }
public void fetchWord() {
_FBIC.fetchWord(pin, "Animals", new FirebaseInterface.WordFetchCallback() {
@Override
public void onWordFetched(String word) {
gamePlayView.setCurrentWord(word.toUpperCase());
System.out.println("Set word to " + word);
}
@Override
public void onError(String error) {
// Handle the error here
System.err.println("Error fetching word: " + error);
}
});
}
public void update(float dt) { public void update(float dt) {
handleInput(); handleInput();
} }
......
...@@ -26,7 +26,7 @@ public class LetterPool { ...@@ -26,7 +26,7 @@ public class LetterPool {
// Add extra random letters to the pool // Add extra random letters to the pool
for (int i = 0; i < extraLettersCount; i++) { for (int i = 0; i < extraLettersCount; i++) {
char randomChar = (char) ('A' + randomGenerator.nextInt(26)); char randomChar = (char) ('A' + randomGenerator.nextInt(26));
letters.add(randomChar); letters.add(Character.toUpperCase(randomChar));
} }
// Shuffle the pool // Shuffle the pool
......
...@@ -22,6 +22,8 @@ public interface FirebaseInterface { ...@@ -22,6 +22,8 @@ public interface FirebaseInterface {
long getSeed(); long getSeed();
void fetchWord(String pin, String category, WordFetchCallback callback);
interface GameStartCallback { interface GameStartCallback {
void onGameStarted(); void onGameStarted();
} }
...@@ -33,6 +35,11 @@ public interface FirebaseInterface { ...@@ -33,6 +35,11 @@ public interface FirebaseInterface {
void onError(String error); void onError(String error);
} }
interface WordFetchCallback {
void onWordFetched(String word);
void onError(String error);
}
void createNewCategory(Category category); void createNewCategory(Category category);
void addWordToCategory(String categoryName, Word word); void addWordToCategory(String categoryName, Word word);
......
...@@ -49,5 +49,11 @@ public class FirebaseManager implements FirebaseInterface { ...@@ -49,5 +49,11 @@ public class FirebaseManager implements FirebaseInterface {
public long getSeed() { public long getSeed() {
return 0; return 0;
} }
@Override
public void fetchWord(String pin, String category, WordFetchCallback callback) {
}
} }
...@@ -45,9 +45,7 @@ public class GamePlayView { ...@@ -45,9 +45,7 @@ public class GamePlayView {
stage = new Stage(new ScreenViewport(cam)); stage = new Stage(new ScreenViewport(cam));
Gdx.input.setInputProcessor(stage); Gdx.input.setInputProcessor(stage);
currentWord = "HEI"; // This is just a test
letterPool = new LetterPool(currentWord, 5, pin); // Or however you decide the number of extra letters
backgroundTexture = new Texture("bg2.png"); backgroundTexture = new Texture("bg2.png");
...@@ -68,13 +66,20 @@ public class GamePlayView { ...@@ -68,13 +66,20 @@ public class GamePlayView {
} }
public void setCurrentWord(String currentWord) {
this.currentWord = currentWord;
letterPool = new LetterPool(currentWord, 5, pin); // Or however you decide the number of extra letters
}
public void render(SpriteBatch sb) { public void render(SpriteBatch sb) {
if (letterPool != null) {
ArrayList<Character> letters = letterPool.getLetters(); ArrayList<Character> letters = letterPool.getLetters();
System.out.println(letters);
float letterSpacing = 100; float letterSpacing = 100;
float totalWidthLetter = ((((float) letters.size() * letterSpacing)) / 2); float totalWidthLetter = ((((float) letters.size() * letterSpacing)) / 2);
float letterStartX = ((cam.viewportWidth - totalWidthLetter) - letterSpacing) / 2; float letterStartX = ((cam.viewportWidth - totalWidthLetter) - 2*letterSpacing) / 2;
float letterStartY = cam.viewportHeight / 2; float letterStartY = cam.viewportHeight / 2;
float offset = 100; float offset = 100;
sb.setProjectionMatrix(cam.combined); sb.setProjectionMatrix(cam.combined);
...@@ -92,6 +97,7 @@ public class GamePlayView { ...@@ -92,6 +97,7 @@ public class GamePlayView {
} else { } else {
// For odd indices, use the range 300 to 800 // For odd indices, use the range 300 to 800
y = letterStartY - 400;// Alternate within the range y = letterStartY - 400;// Alternate within the range
x -= letterSpacing;
} }
...@@ -105,9 +111,9 @@ public class GamePlayView { ...@@ -105,9 +111,9 @@ public class GamePlayView {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.WHITE); // Set the color for the boxes shapeRenderer.setColor(Color.WHITE); // Set the color for the boxes
float boxWidth = 150; // Set the width for the boxes float boxWidth = 120; // Set the width for the boxes
float boxHeight = 150; // Set the height for the boxes float boxHeight = 120; // Set the height for the boxes
float spacing = 30; // Space between boxes float spacing = 25; // Space between boxes
float totalWidth = currentWord.length() * (boxWidth + spacing) - spacing; float totalWidth = currentWord.length() * (boxWidth + spacing) - spacing;
float startX = (cam.viewportWidth - totalWidth) / 2; // Center the boxes float startX = (cam.viewportWidth - totalWidth) / 2; // Center the boxes
float yPosition = cam.viewportHeight / 2; // Y position for the boxes float yPosition = cam.viewportHeight / 2; // Y position for the boxes
...@@ -119,6 +125,8 @@ public class GamePlayView { ...@@ -119,6 +125,8 @@ public class GamePlayView {
shapeRenderer.end(); shapeRenderer.end();
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment