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

feat: added start game/listen for start game

parent 0a86d962
No related branches found
No related tags found
2 merge requests!22Resolve "Create game model",!18Draft: Resolve "Create game model"
......@@ -85,7 +85,7 @@ public class AndroidInterfaceClass implements FirebaseInterface {
@Override
public void fetchPlayers(String pin, PlayerListUpdateCallback callback) {
DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
lobbyRef.addListenerForSingleValueEvent(new ValueEventListener() {
lobbyRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
LobbyModel lobby = dataSnapshot.getValue(LobbyModel.class);
......@@ -100,4 +100,36 @@ public class AndroidInterfaceClass implements FirebaseInterface {
}
});
}
public void startGame(String pin) {
DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
lobbyRef.child("gameStarted").setValue(true)
.addOnSuccessListener(aVoid -> {
System.out.println("The game has started for lobby with pin: " + pin);
})
.addOnFailureListener(e -> {
System.err.println("Failed to start game for lobby: " + pin + ", error: " + e.getMessage());
});
}
public void listenForGameStart(String pin, GameStartCallback callback) {
DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
lobbyRef.child("gameStarted").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Boolean gameStarted = dataSnapshot.getValue(Boolean.class);
if (gameStarted != null && gameStarted) {
callback.onGameStarted();
lobbyRef.child("gameStarted").removeEventListener(this);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Handle the error
}
});
}
}
......@@ -2,6 +2,7 @@ package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.wordbattle.game.model.LobbyModel;
import com.wordbattle.game.network.FirebaseInterface;
......@@ -45,8 +46,17 @@ public class LobbyController {
public void handleInput() {
Rectangle startGame = lobbyView.getStartGameButtonBounds();
if (Gdx.input.justTouched()) {
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
if (startGame.contains(touchPos.x, touchPos.y)) {
_FBIC.startGame(pin);
}
}
}
......
......@@ -36,6 +36,7 @@ public class WaitForHostController {
this._FBIC = _FBIC;
this.waitForHostView = new WaitForHostView(state.getCam(), _FBIC, nickname, pin);
fetchPlayers();
listenForStart();
}
......@@ -54,7 +55,12 @@ public class WaitForHostController {
});
}
int mouseYCoordinate;
private void listenForStart() {
_FBIC.listenForGameStart(pin, () -> {
// state.getStateManager().setState(new GameState(state.getStateManager(), _FBIC, pin));
});
}
private void handleInput(){
......
......@@ -8,6 +8,7 @@ public class LobbyModel {
private String hostNickname;
private String pin;
private List<String> playerNicknames;
private boolean gameStarted;
// Constructor for initializing a new Lobby with the host's nickname and PIN
......@@ -20,6 +21,7 @@ public class LobbyModel {
this.pin = pin;
this.playerNicknames = new ArrayList<>(); // Initialize the list of player nicknames
this.playerNicknames.add(hostNickname);
gameStarted = false;
}
......
......@@ -13,6 +13,14 @@ public interface FirebaseInterface {
void fetchPlayers(String pin, PlayerListUpdateCallback callback);
void startGame(String pin);
void listenForGameStart(String pin, GameStartCallback callback);
interface GameStartCallback {
void onGameStarted();
}
// Callback interface
interface PlayerListUpdateCallback {
void onPlayerListUpdated(List<String> playerNames);
......
......@@ -24,6 +24,13 @@ public class FirebaseManager implements FirebaseInterface {
}
@Override
public void startGame(String pin){
}
@Override
public void listenForGameStart(String pin, GameStartCallback callback) {
}
}
......@@ -127,7 +127,7 @@ public class LobbyView {
return startGameButton;
}
public Rectangle getCreateGameBounds() {
public Rectangle getStartGameButtonBounds() {
return startTheGameBounds;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment