Skip to content

Commit

Permalink
Replace CGI by local file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
murlock committed Jul 28, 2023
1 parent 6b0978d commit 06377e6
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
40 changes: 19 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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/'
}
Expand All @@ -36,20 +28,26 @@ 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 {
java {
googleJavaFormat()
}
}

test {
// Discover and execute JUnit4-based tests
useJUnit()
}

reporting {
reports {
testAggregateTestReport(AggregateTestReport) {
testType = TestSuiteType.UNIT_TEST
}
}
}
37 changes: 14 additions & 23 deletions src/main/java/DCourt/Control/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -209,7 +200,7 @@ public boolean saveHero() {
}

public void saveScore() {
Loader.cgi(
FileLoader.cgi(
Loader.SAVESCORE,
String.valueOf(
String.valueOf(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/DCourt/Items/List/itHero.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/DCourt/Screens/Command/arCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -327,5 +328,6 @@ void createHero() {
if (isGuild()) {
hero.fixStatTrait(Constants.GUILD);
}
System.out.println("createHero: " + hero.toString());
}
}
7 changes: 1 addition & 6 deletions src/main/java/DCourt/Screens/Command/arEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/DCourt/Screens/Utility/arPeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/DCourt/Tools/FileLoader.java
Original file line number Diff line number Diff line change
@@ -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";
}
}

0 comments on commit 06377e6

Please sign in to comment.