Skip to content

Commit

Permalink
#20 It is now possible to import gameplay features. Added new functio…
Browse files Browse the repository at this point in the history
…n importGeneral with which it is possible to easily import things that only require the .txt file. This function is currently used by import gameplay and engine feature.
  • Loading branch information
LMH01 committed Mar 16, 2021
1 parent a0c342a commit 0b952ce
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.github.lmh01.mgt2mt.MadGamesTycoon2ModTool;
import com.github.lmh01.mgt2mt.util.*;
import com.github.lmh01.mgt2mt.util.interfaces.Exporter;
import com.github.lmh01.mgt2mt.util.interfaces.FreeId;
import com.github.lmh01.mgt2mt.util.interfaces.Importer;
import com.github.lmh01.mgt2mt.windows.WindowMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -447,34 +449,15 @@ public static boolean exportEngineFeature(String engineFeatureName){

public static String importEngineFeature(String importFolderPath) throws IOException{
AnalyzeExistingEngineFeatures.analyzeEngineFeatures();
File fileToImport = new File(importFolderPath + "\\engineFeature.txt");
Map<String, String> map = Utils.parseDataFile(fileToImport).get(0);
map.put("ID", Integer.toString(AnalyzeExistingEngineFeatures.getFreeEngineFeatureId()));
boolean CanBeImported = false;
for(String string : SharingManager.ENGINE_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS){
if(string.equals(map.get("MGT2MT VERSION"))){
CanBeImported = true;
}
}
if(!CanBeImported){
return "Engine feature [" + map.get("NAME EN") + "] could not be imported:\nThe engine feature is not with the current mod tool version compatible\nEngine feature was exported in version: " + map.get("MGT2MT VERSION");
}
for(Map<String, String> existingEngineFeatures : AnalyzeExistingEngineFeatures.engineFeatures){
for(Map.Entry<String, String> entry : existingEngineFeatures.entrySet()){
if(entry.getValue().equals(map.get("NAME EN"))){
LOGGER.info("Engine feature already exists - The engine feature name is already taken");
return "false";
}
}
}
boolean addFeature = Summaries.showEngineFeatureMessage(map);
if(addFeature){
EditEngineFeaturesFile.addEngineFeature(map);
ChangeLog.addLogEntry(32, map.get("NAME EN"));
JOptionPane.showMessageDialog(null, "Engine feature [" + map.get("NAME EN") + "] has been added successfully");
WindowMain.checkActionAvailability();
}
return "true";
String returnValue = SharingManager.importGeneral("engineFeature.txt",
"Engine feature",
importFolderPath,
SharingManager.ENGINE_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS,
(map) -> EditEngineFeaturesFile.addEngineFeature(map),
() -> AnalyzeExistingEngineFeatures.getFreeEngineFeatureId(),
30,
(map) -> Summaries.showEngineFeatureMessage(map));
return returnValue;
}

/**
Expand Down Expand Up @@ -506,8 +489,8 @@ public static boolean exportGameplayFeature(String gameplayFeatureName){
bw.write("[GRAPHIC]" + map.get("GRAPHIC") + System.getProperty("line.separator"));
bw.write("[SOUND]" + map.get("SOUND") + System.getProperty("line.separator"));
bw.write("[TECH]" + map.get("TECH") + System.getProperty("line.separator"));
bw.write("[BAD]" + map.get("BAD") + System.getProperty("line.separator"));
bw.write("[GOOD]" + map.get("GOOD") + System.getProperty("line.separator"));
bw.write("[BAD]" + getGenreNames(map.get("BAD")) + System.getProperty("line.separator"));
bw.write("[GOOD]" + getGenreNames(map.get("GOOD")) + System.getProperty("line.separator"));
bw.close();
ChangeLog.addLogEntry(29, map.get("NAME EN"));
return true;
Expand All @@ -519,7 +502,16 @@ public static boolean exportGameplayFeature(String gameplayFeatureName){
}

public static String importGameplayFeature(String importFolderPath) throws IOException{
return null;
AnalyzeExistingGameplayFeatures.analyzeGameplayFeatures();
String returnValue = SharingManager.importGeneral("gameplayFeature.txt",
"Gameplay feature",
importFolderPath,
SharingManager.GAMEPLAY_FEATURE_IMPORT_COMPATIBLE_MOD_TOOL_VERSIONS,
EditGameplayFeaturesFile::addGameplayFeature,
AnalyzeExistingGameplayFeatures::getFreeGameplayFeatureId,
30,
Summaries::showGameplayFeatureMessage);
return returnValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void addGameplayFeature(){
JComboBox comboBoxFeatureType = new JComboBox();
comboBoxFeatureType.setToolTipText("Select what type your gameplay feature should be");
comboBoxFeatureType.setModel(new DefaultComboBoxModel<>(new String[]{"Graphic", "Sound", "Physics", "Gameplay", "Controls", "Multiplayer"}));
comboBoxFeatureType.setSelectedItem("Multiplayer");
comboBoxFeatureType.setSelectedItem("Graphic");
panelType.add(labelSelectType);
panelType.add(comboBoxFeatureType);

Expand Down Expand Up @@ -249,50 +249,9 @@ public static void addGameplayFeature(){
newGameplayFeature.put("TECH", spinnerTech.getValue().toString());
newGameplayFeature.put("GOOD", Utils.transformArrayListToString(goodGenreIds[0]));
newGameplayFeature.put("BAD", Utils.transformArrayListToString(badGenreIds[0]));
StringBuilder badGenresFeatures = new StringBuilder();
boolean firstBadFeature = true;
if(newGameplayFeature.get("BAD").equals("")){
badGenresFeatures.append("None");
}else{
for(Integer integer : badGenreIds[0]){
if(!firstBadFeature){
badGenresFeatures.append(", ");
}else{
firstBadFeature = false;
}
badGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(integer));
}
}
StringBuilder goodGenresFeatures = new StringBuilder();
boolean firstGoodFeature = true;
if(newGameplayFeature.get("GOOD").equals("")){
goodGenresFeatures.append("None");
}else{
for(Integer integer : goodGenreIds[0]){
if(!firstGoodFeature){
goodGenresFeatures.append(", ");
}else{
firstGoodFeature = false;
}
goodGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(integer));
}
}
String messageBody = "Your gameplay feature is ready:\n\n" +
"Name: " + newGameplayFeature.get("NAME EN") + "\n" +
"Description: " + newGameplayFeature.get("DESC EN") + "\n" +
"Unlock date: " + newGameplayFeature.get("DATE") + "\n" +
"Type: " + getGameplayFeatureNameByTypeId(Integer.parseInt(newGameplayFeature.get("TYP"))) + "\n" +
"Research point cost: " + newGameplayFeature.get("RES POINTS") + "\n" +
"Research cost " + newGameplayFeature.get("PRICE") + "\n" +
"Development cost: " + newGameplayFeature.get("DEV COSTS") + "\n" +
"\n*Bad genres*\n\n" + badGenresFeatures.toString() + "\n" +
"\n*Good genres*\n\n" + goodGenresFeatures.toString() + "\n" +
"\n*Points*\n\n" +
"Gameplay: " + newGameplayFeature.get("GAMEPLAY") + "\n" +
"Graphic: " + newGameplayFeature.get("GRAPHIC") + "\n" +
"Sound: " + newGameplayFeature.get("SOUND") + "\n" +
"Tech: " + newGameplayFeature.get("TECH") + "\n";
if(JOptionPane.showConfirmDialog(null, messageBody, "Add gameplay feature?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){

boolean addFeature = Summaries.showGameplayFeatureMessage(newGameplayFeature);
if(addFeature) {
EditGameplayFeaturesFile.addGameplayFeature(newGameplayFeature);
JOptionPane.showMessageDialog(null, "Gameplay feature: [" + newGameplayFeature.get("NAME EN") + "] has been added successfully!", "Gameplay feature added", JOptionPane.INFORMATION_MESSAGE);
break;
Expand Down
56 changes: 53 additions & 3 deletions src/main/java/com/github/lmh01/mgt2mt/util/SharingManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.github.lmh01.mgt2mt.util;

import com.github.lmh01.mgt2mt.data_stream.AnalyzeExistingEngineFeatures;
import com.github.lmh01.mgt2mt.data_stream.ChangeLog;
import com.github.lmh01.mgt2mt.data_stream.SharingHandler;
import com.github.lmh01.mgt2mt.util.interfaces.FreeId;
import com.github.lmh01.mgt2mt.util.interfaces.Importer;
import com.github.lmh01.mgt2mt.util.interfaces.ReturnValue;
import com.github.lmh01.mgt2mt.util.interfaces.Summary;
import com.github.lmh01.mgt2mt.windows.WindowMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -11,6 +17,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

public class SharingManager {
//This class contains functions with which it is easy to export/import things
Expand All @@ -25,15 +32,15 @@ public class SharingManager {
*
* @param fileName This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt
* @param importName The name that is written is some JOptionPanes. Eg. genre, publisher, theme
* @param exportFunction The function that exports the files
* @param importFunction The function that imports the files
* @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file
*/
public static void importThings(String fileName, String importName, ReturnValue exportFunction, String[] compatibleModToolVersions){
public static void importThings(String fileName, String importName, ReturnValue importFunction, String[] compatibleModToolVersions){
try {
ArrayList<String> importFolders = getImportFolderPath(fileName);
try{
for(String importFolder : importFolders){
analyzeReturnValue(importName, exportFunction.getReturnValue(importFolder), compatibleModToolVersions);
analyzeReturnValue(importName, importFunction.getReturnValue(importFolder), compatibleModToolVersions);
}
}catch(NullPointerException ignored){

Expand All @@ -44,6 +51,49 @@ public static void importThings(String fileName, String importName, ReturnValue
}
}

/**
* Loads the contents of import file into a map and imports feature with this map
* @param importFile This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt
* @param importName The name that is written is some JOptionPanes. Eg. Engine feature, Gameplay feature
* @param importFolderPath The folder where the importFile is located.
* @param compatibleModToolVersions A array containing the compatible mod tool versions for the import file
* @param importFunction The function that edits the file
* @param freeId The function that returns the free id
* @param changelogId The id that should be used when the changelog file is being edited
* @param summary The summary function that should be used
* @return
*/
public static String importGeneral(String importFile, String importName, String importFolderPath, String[] compatibleModToolVersions, Importer importFunction, FreeId freeId, int changelogId, Summary summary) throws IOException{
File fileToImport = new File(importFolderPath + "\\" + importFile);
Map<String, String> map = Utils.parseDataFile(fileToImport).get(0);
map.put("ID", Integer.toString(freeId.getFreeId()));
boolean CanBeImported = false;
for(String string : compatibleModToolVersions){
if(string.equals(map.get("MGT2MT VERSION"))){
CanBeImported = true;
}
}
if(!CanBeImported){
return importName + " [" + map.get("NAME EN") + "] could not be imported:\n" + importName + " is not with the current mod tool version compatible\n" + importName + " was exported in version: " + map.get("MGT2MT VERSION");
}
for(Map<String, String> existingEngineFeatures : AnalyzeExistingEngineFeatures.engineFeatures){
for(Map.Entry<String, String> entry : existingEngineFeatures.entrySet()){
if(entry.getValue().equals(map.get("NAME EN"))){
LOGGER.info(importName + " already exists - " + importName + " name is already taken");
return "false";
}
}
}
boolean addFeature = summary.showSummary(map);
if(addFeature){
importFunction.importer(map);
JOptionPane.showMessageDialog(null, importName + " [" + map.get("NAME EN") + "] has been added successfully");
ChangeLog.addLogEntry(changelogId, map.get("NAME EN"));
WindowMain.checkActionAvailability();
}
return "true";
}

/**
* This function will prompt the user to choose a folder where the files to import are located
* @param fileName This is the file the tool will search for in the folder. Eg. genre.txt or publisher.txt
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/github/lmh01/mgt2mt/util/Summaries.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.github.lmh01.mgt2mt.util;

import com.github.lmh01.mgt2mt.data_stream.AnalyzeExistingGenres;
import com.github.lmh01.mgt2mt.data_stream.EditGameplayFeaturesFile;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Map;

public class Summaries {
Expand Down Expand Up @@ -32,4 +36,71 @@ public static boolean showEngineFeatureMessage(Map<String, String> map){
return false;
}
}

public static boolean showGameplayFeatureMessage(Map<String, String> map){
if(!map.get("BAD").matches(".*\\d.*")){
ArrayList<String> badGenreNames = Utils.getEntriesFromString(map.get("BAD"));
ArrayList<String> goodGenreNames = Utils.getEntriesFromString(map.get("GOOD"));
ArrayList<Integer> badGenreIds = new ArrayList<>();
ArrayList<Integer> goodGenreIds = new ArrayList<>();
for(String string : badGenreNames){
badGenreIds.add(AnalyzeExistingGenres.getGenreIdByName(string));
}
for(String string : goodGenreNames){
goodGenreIds.add(AnalyzeExistingGenres.getGenreIdByName(string));
}
map.remove("BAD");
map.remove("GOOD");
map.put("BAD", Utils.transformArrayListToString(badGenreIds));
map.put("GOOD", Utils.transformArrayListToString(goodGenreIds));
}
StringBuilder badGenresFeatures = new StringBuilder();
boolean firstBadFeature = true;
if(map.get("BAD").equals("")){
badGenresFeatures.append("None");
}else{
for(String string : Utils.getEntriesFromString(map.get("BAD"))){
if(!firstBadFeature){
badGenresFeatures.append(", ");
}else{
firstBadFeature = false;
}
badGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(Integer.parseInt(string)));
}
}
StringBuilder goodGenresFeatures = new StringBuilder();
boolean firstGoodFeature = true;
if(map.get("GOOD").equals("")){
goodGenresFeatures.append("None");
}else{
for(String string : Utils.getEntriesFromString(map.get("GOOD"))){
if(!firstGoodFeature){
goodGenresFeatures.append(", ");
}else{
firstGoodFeature = false;
}
goodGenresFeatures.append(AnalyzeExistingGenres.getGenreNameById(Integer.parseInt(string)));
}
}
String messageBody = "Your gameplay feature is ready:\n\n" +
"Name: " + map.get("NAME EN") + "\n" +
"Description: " + map.get("DESC EN") + "\n" +
"Unlock date: " + map.get("DATE") + "\n" +
"Type: " + GameplayFeatureHelper.getGameplayFeatureNameByTypeId(Integer.parseInt(map.get("TYP"))) + "\n" +
"Research point cost: " + map.get("RES POINTS") + "\n" +
"Research cost " + map.get("PRICE") + "\n" +
"Development cost: " + map.get("DEV COSTS") + "\n" +
"\n*Bad genres*\n\n" + badGenresFeatures.toString() + "\n" +
"\n*Good genres*\n\n" + goodGenresFeatures.toString() + "\n" +
"\n*Points*\n\n" +
"Gameplay: " + map.get("GAMEPLAY") + "\n" +
"Graphic: " + map.get("GRAPHIC") + "\n" +
"Sound: " + map.get("SOUND") + "\n" +
"Tech: " + map.get("TECH") + "\n";
if(JOptionPane.showConfirmDialog(null, messageBody, "Add gameplay feature?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
return true;
}else{
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.lmh01.mgt2mt.util.interfaces;

@FunctionalInterface
public interface FreeId {
int getFreeId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.lmh01.mgt2mt.util.interfaces;

import java.io.IOException;
import java.util.Map;

@FunctionalInterface
public interface Importer {
void importer(Map<String, String> map) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.lmh01.mgt2mt.util.interfaces;

import java.util.Map;

public interface Summary {
boolean showSummary(Map<String, String> map);
}

0 comments on commit 0b952ce

Please sign in to comment.