Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #69 from CSC207-2022F-UofT/features/DataAccess
Browse files Browse the repository at this point in the history
Data Access
  • Loading branch information
siddharthgowda authored Dec 2, 2022
2 parents aafa649 + a89a031 commit f615e1a
Show file tree
Hide file tree
Showing 48 changed files with 1,426 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# AWS User-specific
.idea/**/aws.xml

# Mac-specfic stuff
# Mac Specific
.DS_Store

# Generated files
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dependencies {
implementation 'junit:junit:4.13.1'
compileOnly 'org.jetbrains:annotations:23.0.0'
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')

implementation 'com.opencsv:opencsv:5.7.1'
}

java {
Expand All @@ -28,7 +30,7 @@ test {

javafx {
version = '17'
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.base', 'javafx.graphics', 'javafx.media' ]
modules = ['javafx.controls', 'javafx.fxml', 'javafx.base', 'javafx.graphics', 'javafx.media']
}

application {
Expand Down
45 changes: 20 additions & 25 deletions src/main/java/com/mg105/Application.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.mg105;

import com.mg105.controllers.TutorialTextController;
import com.mg105.data_control.access.MoveDataAccess;
import com.mg105.data_control.access.PartyDataAccess;
import com.mg105.data_control.creator.MoveDataCreator;
import com.mg105.data_control.creator.PartyDataCreator;
import com.mg105.entities.*;
import com.mg105.interface_adapters.InputInterpreter;
import com.mg105.interface_adapters.MapGeneratorInterpreter;
Expand All @@ -9,19 +13,18 @@
import com.mg105.interface_adapters.inventory.InventoryController;
import com.mg105.interface_adapters.inventory.InventoryPresenter;
import com.mg105.use_cases.CharacterMover;
import com.mg105.use_cases.Inventory.InventoryInteractor;
import com.mg105.use_cases.MapGenerator;
import com.mg105.use_cases.RoomGetter;
import com.mg105.use_cases.RoomUpdater;
import com.mg105.use_cases.inventory.InventoryInteractor;
import com.mg105.use_cases.set_up.data_system_creator.CreateDataStorage;
import com.mg105.use_cases.set_up.data_system_creator.DataStorageSystemCreator;
import com.mg105.use_cases.set_up.state_setter.GameStateSetter;
import com.mg105.use_cases.set_up.state_setter.PartyCreator;
import com.mg105.user_interface.*;
import com.mg105.utils.TutorialTexts;
import com.mg105.user_interface.inventory.InventoryDisplay;
import com.mg105.utils.PartyConstants;
import javafx.animation.AnimationTimer;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,27 +44,19 @@ public class Application extends javafx.application.Application {
public void start(Stage primaryStage) {

// Set up the initial use cases
Inventory inventory = new Inventory();

BattleCharacter a = new BattleCharacter(30, PartyConstants.ALL_PARTY_MEMBER_NAMES[0], 4, 5,
false, new Move(-3, 0, "Slow swing", false),
new Move(0, -1, "Nullify", false));

BattleCharacter b = new BattleCharacter(20, PartyConstants.ALL_PARTY_MEMBER_NAMES[1], 6, 8,
false, new Move(-4, 0, "Strong swing", false),
new Move(3, 0, "Weak heal", true));

BattleCharacter c = new BattleCharacter(25, PartyConstants.ALL_PARTY_MEMBER_NAMES[2], 3, 6,
false, new Move(6, 0, "Strong heal", true),
new Move(2, 2, "Reinforce", true));

BattleCharacter d = new BattleCharacter(15, PartyConstants.ALL_PARTY_MEMBER_NAMES[3], 9, 10,
false, new Move(-5, 0, "Surprise attack", false),
new Move(-2, -2, "Sabotage", false));
GameState state = new GameState(inventory, new WalkingCharacter(new Point(1, 1)));

BattleCharacter[] party = {a, b, c, d};
Inventory inventory = new Inventory();
// Setting up database
CreateDataStorage[] databaseCreators = {new MoveDataCreator(), new PartyDataCreator()};
DataStorageSystemCreator databaseCreator = new DataStorageSystemCreator(databaseCreators);
databaseCreator.create();

GameState state = new GameState(inventory, party, new WalkingCharacter(new Point(1, 1)));
// Setting the values from the database in game state
PartyCreator[] partyCreator = {new PartyCreator(new PartyDataAccess(new MoveDataAccess()))};
GameStateSetter setter = new GameStateSetter(partyCreator);
setter.setState(state);

// InventoryDisplay set up
InventoryPresenter inventoryPresenter = new InventoryPresenter();
Expand Down Expand Up @@ -96,7 +91,7 @@ false, new Move(-5, 0, "Surprise attack", false),

RoomUpdater roomUpdater = new RoomUpdater();
roomUpdater.addObserver(mapDrawer);

CharacterMover characterMover = new CharacterMover(state, roomUpdater);
InputInterpreter inputInterpreter = new InputInterpreter(characterMover, sceneController, textChanger);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public String bottomText() {
/**
* Go to the next tutorial phase
*/
public void nextPhase() { this.tutorial.nextPhase(); }
public void nextPhase() {
this.tutorial.nextPhase();
}

/**
* Make text start changing
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/mg105/data_control/access/MoveDataAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.mg105.data_control.access;

import com.mg105.outputds.MoveDetails;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import org.jetbrains.annotations.NotNull;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;

import static com.mg105.utils.DataAccessConstants.*;

/**
* This is a class mean to interact and get information about moves from a "database"
* <p>
* This class is really only here so PartyDataAccess does not violate Single Responsibility Principle and to
* help satisfy open and closed principle in PartyDataAccess
*/

public class MoveDataAccess {


/**
* Returns an object that represents the details of the move desired
*
* @param name the name of move to return the details of
* @return an object that represents the details of the move desired
* @throws NoSuchElementException iff the move's details are not found (move does not exist)
* @see MoveDetails
*/

public @NotNull MoveDetails getMoveDetails(String name) throws NoSuchElementException {

try {
CSVParser parser = new CSVParserBuilder().withSeparator(',').build();
CSVReader reader = new CSVReaderBuilder(new FileReader(MOVE_DATA_PATH)).withCSVParser(parser).build();
List<String[]> partyStats = reader.readAll();

for (String[] moveDetails : partyStats) {

if (moveDetails[0].equals(name)) {
int healthChange = Integer.parseInt(moveDetails[1]);
int damageChange = Integer.parseInt(moveDetails[2]);
boolean isFriendly = moveDetails[3].equals(IS_TRUE);
reader.close();

return new MoveDetails(name, healthChange, damageChange, isFriendly);
}

}

reader.close();

} catch (IOException e) {
System.out.println("Could not find file");
e.printStackTrace();
} catch (CsvException e) {
e.printStackTrace();
}

throw new NoSuchElementException("No move of this name exists");

}

}
183 changes: 183 additions & 0 deletions src/main/java/com/mg105/data_control/access/PartyDataAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.mg105.data_control.access;

import com.mg105.outputds.BattleCharacterDetails;
import com.mg105.outputds.MoveDetails;
import com.mg105.use_cases.save.PartyDataInterface;
import com.mg105.utils.PartyConstants;
import com.mg105.utils.StatConstants;
import com.opencsv.*;
import com.opencsv.exceptions.CsvException;
import org.jetbrains.annotations.NotNull;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import static com.mg105.utils.DataAccessConstants.PARTY_DATA_PATH;

public class PartyDataAccess implements PartyDataInterface {

private final static int NUMBER_OF_MOVES_PER_CHARACTER = 2;
private final MoveDataAccess moveDataAccess;

public static void main(String[] args){
PartyDataAccess p = new PartyDataAccess(new MoveDataAccess());

System.out.println(p.getPartyBattleDetails()[0].getName());
}


public PartyDataAccess(MoveDataAccess moveDataAccess) {
// Could define a new moveDataAccess here but it is passed in to follow dependency inversion
this.moveDataAccess = moveDataAccess;
}


/**
* Updates the given stat of the given party member to the value provided
* <p>
* Precondition(s): name is the name of some party member and stat is the name of a valid stat
*
* @param name name of the character to change the stat of
* @param stat the name of the stat to change
* @param value the value to change that stat to
*/

@Override
public void changeCharacterStat(@NotNull String name, @NotNull String stat, int value) {
try {
CSVParser parser = new CSVParserBuilder().withSeparator(',').build();
CSVReader reader = new CSVReaderBuilder(new FileReader(PARTY_DATA_PATH)).withCSVParser(parser).build();
List<String[]> partyStats = reader.readAll();

// Setting which value to change in the file by reading

for (String[] memberStats : partyStats) {
if (memberStats[0].equals(name)) {
int i = getColumnNumber(stat);
System.out.println(i);
if (i == -1) {
// Occurs when an invalid stat argument is passed
reader.close();
return;
}

if (memberStats[i].equals(String.valueOf(value))) {
// value of stat to change is the same as the "New" value
reader.close();
return;
}
memberStats[i] = String.valueOf(value);
break;

}
}

reader.close();

// Changing that value in the file
CSVWriter writer = new CSVWriter(new FileWriter(PARTY_DATA_PATH), ',',
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);

// Writing the entire file might be expensive but this works
writer.writeAll(partyStats);
writer.flush();
writer.close();

} catch (IOException e) {
System.out.println("Could not find file");
e.printStackTrace();
} catch (CsvException e) {
e.printStackTrace();
}

}

/**
* Returns an array of objects that each represent the battle attributes of a single party member
*
* @return an array of objects that each represent the battle attributes of a single party member
* @see BattleCharacterDetails
*/

@Override
public @NotNull BattleCharacterDetails[] getPartyBattleDetails() {

BattleCharacterDetails[] res = new BattleCharacterDetails[PartyConstants.ALL_PARTY_MEMBER_NAMES.length];

try {
CSVParser parser = new CSVParserBuilder().withSeparator(',').build();
CSVReader reader = new CSVReaderBuilder(new FileReader(PARTY_DATA_PATH)).withCSVParser(parser).build();
List<String[]> partyStats = reader.readAll();

// Starts at one since first line is just the variable names (same reason for + 1)

for (int i = 1; i < PartyConstants.ALL_PARTY_MEMBER_NAMES.length + 1; i++) {
String[] memberAttributes = partyStats.get(i);
String name = memberAttributes[0];
int maxHp = Integer.parseInt(memberAttributes[1]);
int dmg = Integer.parseInt(memberAttributes[2]);
int speed = Integer.parseInt(memberAttributes[3]);
MoveDetails[] moveDetails = moveDetails(memberAttributes);
// All party members are always not opponents so that why false is hard coded
res[i - 1] = new BattleCharacterDetails(name, maxHp, dmg, speed, false, moveDetails);
}

reader.close();

} catch (IOException e) {
System.out.println("Could not find file");
e.printStackTrace();
} catch (CsvException e) {
e.printStackTrace();
}

return res;

}

private @NotNull MoveDetails[] moveDetails(String[] memberAttributes) {

return getMoveDetails(getMoveNames(memberAttributes));

}

private @NotNull String[] getMoveNames(String[] memberAttributes) {
String[] names = new String[NUMBER_OF_MOVES_PER_CHARACTER];

System.arraycopy(memberAttributes, memberAttributes.length - 2, names,
0, NUMBER_OF_MOVES_PER_CHARACTER);

return names;

}

private @NotNull MoveDetails[] getMoveDetails(String[] moveNames) {

// moveNames should have the same length as NUMBER_OF_MOVES_PER_CHARACTER

MoveDetails[] details = new MoveDetails[NUMBER_OF_MOVES_PER_CHARACTER];

for (int i = 0; i < NUMBER_OF_MOVES_PER_CHARACTER; i++) {

details[i] = moveDataAccess.getMoveDetails(moveNames[i]);

}

return details;

}

private int getColumnNumber(String rowName) {
return switch (rowName) {
case StatConstants.MAX_HP -> 1;
case StatConstants.DAMAGE -> 2;
case StatConstants.SPEED -> 3;
default -> -1;
};

}
}
Loading

0 comments on commit f615e1a

Please sign in to comment.