Skip to content

Commit 06377e6

Browse files
committed
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.
1 parent 6b0978d commit 06377e6

File tree

7 files changed

+97
-52
lines changed

7 files changed

+97
-52
lines changed

build.gradle

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
1-
/*
2-
* This build file was generated by the Gradle 'init' task.
3-
*
4-
* This generated file contains a commented-out sample Java project to get you started.
5-
* For more details take a look at the Java Quickstart chapter in the Gradle
6-
* user guide available at https://docs.gradle.org/4.4.1/userguide/tutorial_java_projects.html
7-
*/
8-
9-
// Apply the java plugin to add support for Java
10-
//apply plugin: 'java'
11-
121
plugins {
132
id 'java'
143
id "org.sonarqube" version "4.2.1.3168"
154
id "com.diffplug.spotless" version "6.20.0"
5+
id 'test-report-aggregation'
166
}
177

18-
// In this section you declare where to find the dependencies of your project
198
repositories {
209
mavenCentral()
2110
}
2211

2312
sourceSets {
2413
main {
14+
java {
15+
srcDir 'src/main/java/'
16+
}
2517
resources {
2618
srcDir 'Images/'
2719
}
@@ -36,20 +28,26 @@ jar {
3628
}
3729
}
3830

39-
// In this section you declare the dependencies for your production and test code
4031
dependencies {
41-
// The production code uses the SLF4J logging API at compile time
42-
// compile 'org.slf4j:slf4j-api:1.7.25'
43-
44-
// Declare the dependency for your favourite test framework you want to use in your tests.
45-
// TestNG is also supported by the Gradle Test task. Just change the
46-
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
47-
// 'test.useTestNG()' to your build script.
48-
// testCompile 'junit:junit:4.12'
32+
testImplementation 'junit:junit:4.12'
33+
testImplementation 'org.mockito:mockito-core:5.4.0'
4934
}
5035

5136
spotless {
5237
java {
5338
googleJavaFormat()
5439
}
5540
}
41+
42+
test {
43+
// Discover and execute JUnit4-based tests
44+
useJUnit()
45+
}
46+
47+
reporting {
48+
reports {
49+
testAggregateTestReport(AggregateTestReport) {
50+
testType = TestSuiteType.UNIT_TEST
51+
}
52+
}
53+
}

src/main/java/DCourt/Control/Player.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import DCourt.Static.Constants;
1212
import DCourt.Tools.Buffer;
1313
import DCourt.Tools.Loader;
14+
import DCourt.Tools.FileLoader;
1415
import DCourt.Tools.Tools;
1516

1617
/* loaded from: DCourt.jar:DCourt/Control/Player.class */
@@ -109,7 +110,7 @@ public void startValues() {
109110
this.start.add(Constants.LEVEL, this.hero.getLevel());
110111
this.start.add(Constants.EXP, this.hero.getExp());
111112
this.start.add(Constants.FAME, this.hero.getFame());
112-
this.start.add("Marks", this.hero.getMoney());
113+
this.start.add(Constants.MONEY, this.hero.getMoney());
113114
}
114115

115116
public boolean loadHero(String tname, String tpass) {
@@ -125,36 +126,31 @@ public boolean loadHero(String tname, String tpass) {
125126
.append("|")
126127
.append(this.sessionID)));
127128
this.sessionID = alterSessionID(this.sessionID);
128-
/* ADD REST API HERE */
129-
/*
130-
if (!readFindValues(Loader.cgiBuffer(Loader.FINDHERO, msg))) {
129+
if (!readFindValues(FileLoader.cgiBuffer(Loader.FINDHERO, msg))) {
131130
return false;
132131
}
133-
*/
134132
this.hero = null;
135-
String STATIC_HERO =
136-
"{itHero|Static|150|100|100|50|0|300|"
137-
+ "{~|values|{=|place|fields}}|"
138-
+ "{~|pack|{#|Marks|100}}|"
139-
+ "{~|gear|{itArms|Hatchet|4|0|-1|right}}|"
140-
+ "}";
141-
/* ADD REST API HERE */
142133
return readHeroValues(
143-
new Buffer(STATIC_HERO)); // //Loader.cgiBuffer(Loader.READHERO, this.name));
134+
FileLoader.cgiBuffer(Loader.READHERO, this.name)
135+
);
144136
}
145137

146138
boolean readHeroValues(Buffer buf) {
139+
System.out.println("== readHeroValues == 1/");
147140
err = 1;
148141
if (buf == null || buf.isError()) {
149142
return false;
150143
}
151144
err = 0;
145+
System.out.println("== readHeroValues == 2/ > " + buf.toString());
152146
this.hero = (itHero) Item.factory(buf);
153147
if (this.hero == null) {
154148
return true;
155149
}
150+
System.out.println("== readHeroValues == 3/");
156151
this.hero.update(this.name, this.powers);
157152
startValues();
153+
System.out.println("== readHeroValues == 4/");
158154
return true;
159155
}
160156

@@ -191,15 +187,10 @@ public boolean saveHero() {
191187
* name|sessionID|...
192188
*/
193189
Buffer buf =
194-
Loader.cgiBuffer(
195-
Loader.SAVEHERO,
196-
String.valueOf(
197-
String.valueOf(
198-
new StringBuffer(String.valueOf(String.valueOf(this.name)))
199-
.append("|")
200-
.append(this.sessionID)
201-
.append("\n")
202-
.append(this.hero.toString()))));
190+
FileLoader.cgiBuffer(
191+
Loader.SAVEHERO, this.hero.toString());
192+
System.out.println("SAVEHERO " + buf);
193+
System.out.println(this.hero.toString());
203194
if (!buf.isError()) {
204195
return true;
205196
}
@@ -209,7 +200,7 @@ public boolean saveHero() {
209200
}
210201

211202
public void saveScore() {
212-
Loader.cgi(
203+
FileLoader.cgi(
213204
Loader.SAVESCORE,
214205
String.valueOf(
215206
String.valueOf(

src/main/java/DCourt/Items/List/itHero.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public static Item factory(Buffer buf) {
6868
if ((!buf.match("itHero") && !buf.match("itAgent")) || !buf.split()) {
6969
return null;
7070
}
71+
System.out.println("itHero.factory");
7172
itHero who = new itHero(buf.token());
7273
who.loadAttributes(buf);
7374
who.loadBody(buf);

src/main/java/DCourt/Screens/Command/arCreate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ boolean isGuild() {
9393
@Override // DCourt.Screens.Screen
9494
public void update(Graphics g) {
9595
getPic(1).show(getBuild() == 0);
96-
update(g);
96+
super.update(g);
9797
}
9898

9999
@Override // DCourt.Screens.Screen
@@ -155,6 +155,7 @@ public synchronized boolean action(Event e, Object o) {
155155
if (this.traits[ix2].getState() && getBuild() < 0) {
156156
this.traits[ix2].setState(false);
157157
}
158+
break;
158159
}
159160
}
160161
if (e.target == getPic(0)) {
@@ -327,5 +328,6 @@ void createHero() {
327328
if (isGuild()) {
328329
hero.fixStatTrait(Constants.GUILD);
329330
}
331+
System.out.println("createHero: " + hero.toString());
330332
}
331333
}

src/main/java/DCourt/Screens/Command/arEntry.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,36 +185,31 @@ public void keyReleased(KeyEvent e) {}
185185
public void keyTyped(KeyEvent e) {}
186186

187187
Screen EnterGame() {
188+
System.out.println("EnterGame");
188189
int a = 0;
189-
System.out.println("EnterGame: " + a);
190190
a += 1;
191191
String name = scoreString(this.nameTXF.getText());
192192
String pass = scoreString(this.passTXF.getText());
193-
System.out.println("EnterGame: " + a);
194193
a += 1;
195194
Player player = Tools.getPlayer();
196-
System.out.println("EnterGame: " + a);
197195
a += 1;
198196
if (!player.loadHero(name, pass)) {
199197
System.out.println("error loadHero EnterGame: " + a);
200198
a += 1;
201199
return player.errorScreen(this);
202200
}
203-
System.out.println("EnterGame: " + a);
204201
a += 1;
205202
if (player.getHero() == null) {
206203
System.out.println("error getHero EnterGame: " + a);
207204
a += 1;
208205
return new arCreate(player);
209206
}
210-
System.out.println("EnterGame: " + a);
211207
a += 1;
212208
if (player.isDead()) {
213209
System.out.println("Dead EnterGame: " + a);
214210
a += 1;
215211
return new arNotice(this, GameStrings.heroHasDied);
216212
}
217-
System.out.println("EnterGame: " + a);
218213
a += 1;
219214
PlaceTable lot = Tools.getPlaceTable();
220215
lot.select(player.getPlace());

src/main/java/DCourt/Screens/Utility/arPeer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import DCourt.Static.Constants;
1212
import DCourt.Tools.Buffer;
1313
import DCourt.Tools.Loader;
14+
import DCourt.Tools.FileLoader;
1415
import DCourt.Tools.MadLib;
1516
import DCourt.Tools.Tools;
1617
import java.awt.Button;
@@ -188,7 +189,7 @@ void LoadVision(String who) {
188189
if (this.spend == 2) {
189190
h.subPack(SEEKGEM, 3);
190191
}
191-
Buffer buf = Loader.cgiBuffer(Loader.READHERO, this.pname);
192+
Buffer buf = FileLoader.cgiBuffer(Loader.READHERO, this.pname);
192193
if (buf == null || buf.isEmpty() || buf.isError()) {
193194
setMessage(
194195
String.valueOf(
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package DCourt.Tools;
2+
3+
import DCourt.Items.Item;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
9+
/* loaded from: DCourt.jar:DCourt/Tools/Loader.class */
10+
public class FileLoader extends Loader {
11+
static final String HEROFILE = "hero.txt";
12+
13+
public static Buffer cgiBuffer(String action, String data) {
14+
return new Buffer(cgi(action, data));
15+
}
16+
17+
public static Item cgiItem(String action, String data) {
18+
return Item.factory(cgi(action, data));
19+
}
20+
21+
public static String cgi(String action, String data) {
22+
switch (action) {
23+
case FINDHERO:
24+
return "2023-07-27|0||";
25+
case SAVEHERO:
26+
try {
27+
FileOutputStream output = new FileOutputStream(HEROFILE);
28+
byte[] strToBytes = data.getBytes();
29+
output.write(strToBytes);
30+
output.close();
31+
return "";
32+
} catch (IOException e) {
33+
return "Error: " + e.getMessage();
34+
}
35+
case READHERO:
36+
File f = new File(HEROFILE);
37+
if (!f.exists()) {
38+
return "";
39+
}
40+
try {
41+
FileInputStream input = new FileInputStream(HEROFILE);
42+
byte[] Bytes = new byte[32768];
43+
input.read(Bytes);
44+
input.close();
45+
String s = new String(Bytes, "US-ASCII");
46+
return s;
47+
} catch (IOException e) {
48+
System.out.println(e.getMessage());
49+
return "Error: " + e.getMessage();
50+
} catch (Exception e) {
51+
System.out.println(e.getMessage());
52+
return "Error: " + e.getMessage();
53+
}
54+
}
55+
return "Error: " + action + " not implemented";
56+
}
57+
}

0 commit comments

Comments
 (0)