This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from CSC207-2022F-UofT/features/DataAccess
Data Access
- Loading branch information
Showing
48 changed files
with
1,426 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
# AWS User-specific | ||
.idea/**/aws.xml | ||
|
||
# Mac-specfic stuff | ||
# Mac Specific | ||
.DS_Store | ||
|
||
# Generated files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/mg105/data_control/access/MoveDataAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
183
src/main/java/com/mg105/data_control/access/PartyDataAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
} | ||
} |
Oops, something went wrong.