From 06377e699df7567bebd4ad1e4856ab86bc0dd1c9 Mon Sep 17 00:00:00 2001 From: Michael Bonfils Date: Fri, 28 Jul 2023 09:13:34 +0200 Subject: [PATCH] Replace CGI by local file It is now possible to create hero and load hero. Other call than find, read and write hero are not implemented. Only one hero at time. --- build.gradle | 40 +++++++------ src/main/java/DCourt/Control/Player.java | 37 +++++------- src/main/java/DCourt/Items/List/itHero.java | 1 + .../java/DCourt/Screens/Command/arCreate.java | 4 +- .../java/DCourt/Screens/Command/arEntry.java | 7 +-- .../java/DCourt/Screens/Utility/arPeer.java | 3 +- src/main/java/DCourt/Tools/FileLoader.java | 57 +++++++++++++++++++ 7 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 src/main/java/DCourt/Tools/FileLoader.java diff --git a/build.gradle b/build.gradle index 56d7c37..79975bb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,27 +1,19 @@ -/* - * This build file was generated by the Gradle 'init' task. - * - * This generated file contains a commented-out sample Java project to get you started. - * For more details take a look at the Java Quickstart chapter in the Gradle - * user guide available at https://docs.gradle.org/4.4.1/userguide/tutorial_java_projects.html - */ - -// Apply the java plugin to add support for Java -//apply plugin: 'java' - plugins { id 'java' id "org.sonarqube" version "4.2.1.3168" id "com.diffplug.spotless" version "6.20.0" + id 'test-report-aggregation' } -// In this section you declare where to find the dependencies of your project repositories { mavenCentral() } sourceSets { main { + java { + srcDir 'src/main/java/' + } resources { srcDir 'Images/' } @@ -36,16 +28,9 @@ jar { } } -// In this section you declare the dependencies for your production and test code dependencies { - // The production code uses the SLF4J logging API at compile time - // compile 'org.slf4j:slf4j-api:1.7.25' - - // Declare the dependency for your favourite test framework you want to use in your tests. - // TestNG is also supported by the Gradle Test task. Just change the - // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add - // 'test.useTestNG()' to your build script. - // testCompile 'junit:junit:4.12' + testImplementation 'junit:junit:4.12' + testImplementation 'org.mockito:mockito-core:5.4.0' } spotless { @@ -53,3 +38,16 @@ spotless { googleJavaFormat() } } + +test { + // Discover and execute JUnit4-based tests + useJUnit() +} + +reporting { + reports { + testAggregateTestReport(AggregateTestReport) { + testType = TestSuiteType.UNIT_TEST + } + } +} diff --git a/src/main/java/DCourt/Control/Player.java b/src/main/java/DCourt/Control/Player.java index 2d105c1..f733d9d 100644 --- a/src/main/java/DCourt/Control/Player.java +++ b/src/main/java/DCourt/Control/Player.java @@ -11,6 +11,7 @@ import DCourt.Static.Constants; import DCourt.Tools.Buffer; import DCourt.Tools.Loader; +import DCourt.Tools.FileLoader; import DCourt.Tools.Tools; /* loaded from: DCourt.jar:DCourt/Control/Player.class */ @@ -109,7 +110,7 @@ public void startValues() { this.start.add(Constants.LEVEL, this.hero.getLevel()); this.start.add(Constants.EXP, this.hero.getExp()); this.start.add(Constants.FAME, this.hero.getFame()); - this.start.add("Marks", this.hero.getMoney()); + this.start.add(Constants.MONEY, this.hero.getMoney()); } public boolean loadHero(String tname, String tpass) { @@ -125,36 +126,31 @@ public boolean loadHero(String tname, String tpass) { .append("|") .append(this.sessionID))); this.sessionID = alterSessionID(this.sessionID); - /* ADD REST API HERE */ - /* - if (!readFindValues(Loader.cgiBuffer(Loader.FINDHERO, msg))) { + if (!readFindValues(FileLoader.cgiBuffer(Loader.FINDHERO, msg))) { return false; } - */ this.hero = null; - String STATIC_HERO = - "{itHero|Static|150|100|100|50|0|300|" - + "{~|values|{=|place|fields}}|" - + "{~|pack|{#|Marks|100}}|" - + "{~|gear|{itArms|Hatchet|4|0|-1|right}}|" - + "}"; - /* ADD REST API HERE */ return readHeroValues( - new Buffer(STATIC_HERO)); // //Loader.cgiBuffer(Loader.READHERO, this.name)); + FileLoader.cgiBuffer(Loader.READHERO, this.name) + ); } boolean readHeroValues(Buffer buf) { + System.out.println("== readHeroValues == 1/"); err = 1; if (buf == null || buf.isError()) { return false; } err = 0; + System.out.println("== readHeroValues == 2/ > " + buf.toString()); this.hero = (itHero) Item.factory(buf); if (this.hero == null) { return true; } + System.out.println("== readHeroValues == 3/"); this.hero.update(this.name, this.powers); startValues(); + System.out.println("== readHeroValues == 4/"); return true; } @@ -191,15 +187,10 @@ public boolean saveHero() { * name|sessionID|... */ Buffer buf = - Loader.cgiBuffer( - Loader.SAVEHERO, - String.valueOf( - String.valueOf( - new StringBuffer(String.valueOf(String.valueOf(this.name))) - .append("|") - .append(this.sessionID) - .append("\n") - .append(this.hero.toString())))); + FileLoader.cgiBuffer( + Loader.SAVEHERO, this.hero.toString()); + System.out.println("SAVEHERO " + buf); + System.out.println(this.hero.toString()); if (!buf.isError()) { return true; } @@ -209,7 +200,7 @@ public boolean saveHero() { } public void saveScore() { - Loader.cgi( + FileLoader.cgi( Loader.SAVESCORE, String.valueOf( String.valueOf( diff --git a/src/main/java/DCourt/Items/List/itHero.java b/src/main/java/DCourt/Items/List/itHero.java index b4844da..12c458a 100644 --- a/src/main/java/DCourt/Items/List/itHero.java +++ b/src/main/java/DCourt/Items/List/itHero.java @@ -68,6 +68,7 @@ public static Item factory(Buffer buf) { if ((!buf.match("itHero") && !buf.match("itAgent")) || !buf.split()) { return null; } + System.out.println("itHero.factory"); itHero who = new itHero(buf.token()); who.loadAttributes(buf); who.loadBody(buf); diff --git a/src/main/java/DCourt/Screens/Command/arCreate.java b/src/main/java/DCourt/Screens/Command/arCreate.java index bd5e721..0b2cced 100644 --- a/src/main/java/DCourt/Screens/Command/arCreate.java +++ b/src/main/java/DCourt/Screens/Command/arCreate.java @@ -93,7 +93,7 @@ boolean isGuild() { @Override // DCourt.Screens.Screen public void update(Graphics g) { getPic(1).show(getBuild() == 0); - update(g); + super.update(g); } @Override // DCourt.Screens.Screen @@ -155,6 +155,7 @@ public synchronized boolean action(Event e, Object o) { if (this.traits[ix2].getState() && getBuild() < 0) { this.traits[ix2].setState(false); } + break; } } if (e.target == getPic(0)) { @@ -327,5 +328,6 @@ void createHero() { if (isGuild()) { hero.fixStatTrait(Constants.GUILD); } + System.out.println("createHero: " + hero.toString()); } } diff --git a/src/main/java/DCourt/Screens/Command/arEntry.java b/src/main/java/DCourt/Screens/Command/arEntry.java index a4941c2..0e40d32 100644 --- a/src/main/java/DCourt/Screens/Command/arEntry.java +++ b/src/main/java/DCourt/Screens/Command/arEntry.java @@ -185,36 +185,31 @@ public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} Screen EnterGame() { + System.out.println("EnterGame"); int a = 0; - System.out.println("EnterGame: " + a); a += 1; String name = scoreString(this.nameTXF.getText()); String pass = scoreString(this.passTXF.getText()); - System.out.println("EnterGame: " + a); a += 1; Player player = Tools.getPlayer(); - System.out.println("EnterGame: " + a); a += 1; if (!player.loadHero(name, pass)) { System.out.println("error loadHero EnterGame: " + a); a += 1; return player.errorScreen(this); } - System.out.println("EnterGame: " + a); a += 1; if (player.getHero() == null) { System.out.println("error getHero EnterGame: " + a); a += 1; return new arCreate(player); } - System.out.println("EnterGame: " + a); a += 1; if (player.isDead()) { System.out.println("Dead EnterGame: " + a); a += 1; return new arNotice(this, GameStrings.heroHasDied); } - System.out.println("EnterGame: " + a); a += 1; PlaceTable lot = Tools.getPlaceTable(); lot.select(player.getPlace()); diff --git a/src/main/java/DCourt/Screens/Utility/arPeer.java b/src/main/java/DCourt/Screens/Utility/arPeer.java index 23bc4df..d04954b 100644 --- a/src/main/java/DCourt/Screens/Utility/arPeer.java +++ b/src/main/java/DCourt/Screens/Utility/arPeer.java @@ -11,6 +11,7 @@ import DCourt.Static.Constants; import DCourt.Tools.Buffer; import DCourt.Tools.Loader; +import DCourt.Tools.FileLoader; import DCourt.Tools.MadLib; import DCourt.Tools.Tools; import java.awt.Button; @@ -188,7 +189,7 @@ void LoadVision(String who) { if (this.spend == 2) { h.subPack(SEEKGEM, 3); } - Buffer buf = Loader.cgiBuffer(Loader.READHERO, this.pname); + Buffer buf = FileLoader.cgiBuffer(Loader.READHERO, this.pname); if (buf == null || buf.isEmpty() || buf.isError()) { setMessage( String.valueOf( diff --git a/src/main/java/DCourt/Tools/FileLoader.java b/src/main/java/DCourt/Tools/FileLoader.java new file mode 100644 index 0000000..4abf10f --- /dev/null +++ b/src/main/java/DCourt/Tools/FileLoader.java @@ -0,0 +1,57 @@ +package DCourt.Tools; + +import DCourt.Items.Item; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/* loaded from: DCourt.jar:DCourt/Tools/Loader.class */ +public class FileLoader extends Loader { + static final String HEROFILE = "hero.txt"; + + public static Buffer cgiBuffer(String action, String data) { + return new Buffer(cgi(action, data)); + } + + public static Item cgiItem(String action, String data) { + return Item.factory(cgi(action, data)); + } + + public static String cgi(String action, String data) { + switch (action) { + case FINDHERO: + return "2023-07-27|0||"; + case SAVEHERO: + try { + FileOutputStream output = new FileOutputStream(HEROFILE); + byte[] strToBytes = data.getBytes(); + output.write(strToBytes); + output.close(); + return ""; + } catch (IOException e) { + return "Error: " + e.getMessage(); + } + case READHERO: + File f = new File(HEROFILE); + if (!f.exists()) { + return ""; + } + try { + FileInputStream input = new FileInputStream(HEROFILE); + byte[] Bytes = new byte[32768]; + input.read(Bytes); + input.close(); + String s = new String(Bytes, "US-ASCII"); + return s; + } catch (IOException e) { + System.out.println(e.getMessage()); + return "Error: " + e.getMessage(); + } catch (Exception e) { + System.out.println(e.getMessage()); + return "Error: " + e.getMessage(); + } + } + return "Error: " + action + " not implemented"; + } +}