Skip to content
Snippets Groups Projects
Commit dabf2f71 authored by Kristoffer Krogh Wetterhus's avatar Kristoffer Krogh Wetterhus
Browse files

Created class and functions to update the database from .txt file

parent a79e5e0e
No related branches found
No related tags found
2 merge requests!22Resolve "Create game model",!18Draft: Resolve "Create game model"
......@@ -5,7 +5,9 @@ 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 com.wordbattle.game.model.Category;
import com.wordbattle.game.model.LobbyModel;
import com.wordbattle.game.model.Word;
import com.wordbattle.game.network.FirebaseInterface;
public class AndroidInterfaceClass implements FirebaseInterface {
......@@ -45,6 +47,31 @@ public class AndroidInterfaceClass implements FirebaseInterface {
});
}
@Override
public void createNewCategory(Category category) {
DatabaseReference categoryRef = database.getReference("categories").child(category.getName());
categoryRef.setValue(category)
.addOnSuccessListener(aVoid -> {
System.out.println("Category " + category.getName() + " created successfully.");
})
.addOnFailureListener(e -> {
System.err.println("Error creating category " + category.getName() + ": " + e.getMessage());
});
}
@Override
public void addWordToCategory(String categoryName, Word word) {
DatabaseReference wordsRef = database.getReference("categories").child(categoryName).child("words");
wordsRef.child(word.getWord()).setValue(word)
.addOnSuccessListener(aVoid -> {
// Handle success
System.out.println("Word " + word.getWord() + " added to category " + categoryName + " successfully.");
})
.addOnFailureListener(e -> {
// Handle failure
System.err.println("Error adding word " + word.getWord() + " to category " + categoryName + ": " + e.getMessage());
});
}
@Override
public void joinLobby(String pin, String playerNickname) {
// Reference to the specific lobby by its pin
DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
......
File moved
......@@ -2,10 +2,9 @@ package com.wordbattle.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.wordbattle.game.model.CreateWords;
import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.states.MainMenuState;
import com.wordbattle.game.states.StateManager;
......@@ -26,6 +25,9 @@ public class WordBattle extends ApplicationAdapter {
_FBIC.SomeFunction();
batch = new SpriteBatch();
stateManager = new StateManager();
// Make changes in assets/words.txt, and uncomment the two lines beneath to update database
// CreateWords createWords = new CreateWords(_FBIC);
// createWords.updateDB();
ScreenUtils.clear(1, 0, 0, 1);
stateManager.setState(new MainMenuState(stateManager, _FBIC)); // Set the main menu as the initial state
}
......
package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.wordbattle.game.model.LobbyModel;
import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.states.LobbyState;
import com.wordbattle.game.view.LobbyView;
......
package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Null;
import com.wordbattle.game.model.CreateWords;
import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.network.FirebaseManager;
import com.wordbattle.game.states.CreateGameState;
import com.wordbattle.game.states.JoinGameState;
import com.wordbattle.game.states.HowToPlayState;
import com.wordbattle.game.states.MainMenuState;
import com.wordbattle.game.states.StartState;
import com.wordbattle.game.states.StartingGameState;
import com.wordbattle.game.states.StateManager;
import com.wordbattle.game.states.WaitForHostState;
import com.wordbattle.game.view.MainMenuView;
import java.util.logging.Logger;
public class MainMenuController {
private MainMenuState state;
private MainMenuView mainMenuView;
......@@ -55,7 +47,6 @@ public class MainMenuController {
if (joinGameButtonBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("Join Game Button Pressed");
state.getStateManager().setState(new WaitForHostState(state.getStateManager()));
}
if (newGameButtonBounds.contains(touchPos.x, touchPos.y)) {
System.out.println("New Game Button Pressed");
......
package com.wordbattle.game.model;
import com.badlogic.gdx.Gdx;
import com.wordbattle.game.model.Category;
import com.wordbattle.game.model.Word;
import com.wordbattle.game.network.FirebaseInterface;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CreateWords {
Map<String, Category> categories;
FirebaseInterface _FBIC;
public CreateWords(FirebaseInterface _FBIC) {
this.categories = new HashMap<>();
this._FBIC = _FBIC;
}
public static void main(String[] args) {
Map<String, Category> categories = new HashMap<>();
public void updateDB() {
try {
String wrds = Gdx.files.internal("words.txt").readString();
try (BufferedReader br = new BufferedReader(new FileReader("/Users/kkw/OneDrive – NTNU/NTNU/Progark/progarkproject/core/src/com/wordbattle/game/model/words.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] lines = wrds.split("\n");
for (String line : lines) {
String[] parts = line.split(":");
String categoryName = parts[0];
String[] wordsArray = parts[1].split(",");
@SuppressWarnings("NewApi") Category category = categories.getOrDefault(categoryName, new Category(categoryName));
categories.put(categoryName, category);
for (String wordStr : wordsArray) {
Word word = new Word(wordStr, category);
category.addWord(word);
System.out.println("Category: " + categoryName);
Category category = new Category(categoryName);
_FBIC.createNewCategory(category);
for (String word : wordsArray) {
System.out.println("Word: " + word);
Word newWord = new Word(word);
_FBIC.addWordToCategory(category.getName(), newWord);
}
}
// Print categories and words
for (Category category : categories.values()) {
System.out.println(" ");
System.out.println("Category: " + category.getName());
List<Word> words = category.getWords();
for (Word word : words) {
System.out.println(word.getWord());
}
}
} catch (IOException e) {
catch (Exception e) {
e.printStackTrace();
}
}
}
}}
......@@ -8,20 +8,15 @@ import java.util.Random;
public class Word {
private final String word;
private final Category category;
public Word(String word, Category category) {
public Word(String word) {
this.word = word;
this.category = category;
}
public String getWord() {
return word;
}
public Category getCategory() {
return category;
}
public List<String> getLetters(int amount) {
List<String> letters = new ArrayList<>();
......
package com.wordbattle.game.model;
// Denne skal slettes
public class WordTest {
public static void main(String[] args) {
// Create an instance of Word
Category category = new Category("Test");
Word word = new Word("lemon", category);
Word word = new Word("lemon");
// Test the getWord method
System.out.println("Word: " + word.getWord());
......
package com.wordbattle.game.network;
import com.wordbattle.game.model.Category;
import com.wordbattle.game.model.Word;
import java.util.List;
public interface FirebaseInterface {
public void SomeFunction();
......@@ -7,6 +12,11 @@ public interface FirebaseInterface {
public void createNewLobby(String hostNickname, String pin);
void createNewCategory(Category category);
void addWordToCategory(String categoryName, Word word);
public void joinLobby(String pin, String playerNickname);
......
package com.wordbattle.game.network;
import com.wordbattle.game.model.Category;
import com.wordbattle.game.model.Word;
public class FirebaseManager implements FirebaseInterface {
......@@ -14,6 +17,16 @@ public class FirebaseManager implements FirebaseInterface {
}
@Override
public void createNewCategory(Category category) {
}
@Override
public void addWordToCategory(String categoryName, Word word) {
}
@Override
public void joinLobby(String pin, String playerNickname) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment