Skip to content

Commit

Permalink
[log] Improve log
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Apr 5, 2023
1 parent 27f8e0f commit acdd1e9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 46 deletions.
2 changes: 1 addition & 1 deletion leekscript
Submodule leekscript updated 51 files
+2 −2 .github/workflows/build.yml
+13 −55 src/main/java/leekscript/AILog.java
+5 −0 src/main/java/leekscript/common/AccessLevel.java
+4 −0 src/main/java/leekscript/common/ArrayType.java
+4 −0 src/main/java/leekscript/common/CompoundType.java
+1 −0 src/main/java/leekscript/common/Error.java
+4 −0 src/main/java/leekscript/common/MapType.java
+7 −0 src/main/java/leekscript/common/Type.java
+2 −2 src/main/java/leekscript/compiler/JavaWriter.java
+4 −4 src/main/java/leekscript/compiler/LeekScript.java
+7 −2 src/main/java/leekscript/compiler/WordCompiler.java
+1 −1 src/main/java/leekscript/compiler/bloc/AnonymousFunctionBlock.java
+12 −1 src/main/java/leekscript/compiler/bloc/ClassMethodBlock.java
+1 −1 src/main/java/leekscript/compiler/bloc/ConditionalBloc.java
+5 −0 src/main/java/leekscript/compiler/bloc/ForeachBlock.java
+5 −0 src/main/java/leekscript/compiler/bloc/ForeachKeyBlock.java
+1 −1 src/main/java/leekscript/compiler/bloc/FunctionBlock.java
+0 −1 src/main/java/leekscript/compiler/bloc/MainLeekBlock.java
+1 −1 src/main/java/leekscript/compiler/expression/Expression.java
+3 −0 src/main/java/leekscript/compiler/expression/LeekArrayAccess.java
+7 −15 src/main/java/leekscript/compiler/expression/LeekExpression.java
+30 −12 src/main/java/leekscript/compiler/expression/LeekFunctionCall.java
+28 −15 src/main/java/leekscript/compiler/expression/LeekObjectAccess.java
+4 −0 src/main/java/leekscript/compiler/expression/LeekString.java
+28 −39 src/main/java/leekscript/compiler/expression/LeekVariable.java
+189 −102 src/main/java/leekscript/compiler/instruction/ClassDeclarationInstruction.java
+3 −3 src/main/java/leekscript/compiler/instruction/LeekVariableDeclarationInstruction.java
+43 −16 src/main/java/leekscript/compiler/resolver/NativeFileSystem.java
+731 −81 src/main/java/leekscript/runner/AI.java
+105 −0 src/main/java/leekscript/runner/BasicAILog.java
+9 −0 src/main/java/leekscript/runner/Final.java
+4 −1 src/main/java/leekscript/runner/LeekFunctions.java
+20 −0 src/main/java/leekscript/runner/LeekOperations.java
+23 −24 src/main/java/leekscript/runner/LeekValueManager.java
+16 −6 src/main/java/leekscript/runner/Wrapper.java
+5 −2 src/main/java/leekscript/runner/classes/StandardClass.java
+16 −17 src/main/java/leekscript/runner/values/ArrayLeekValue.java
+18 −10 src/main/java/leekscript/runner/values/Box.java
+75 −41 src/main/java/leekscript/runner/values/ClassLeekValue.java
+1 −1 src/main/java/leekscript/runner/values/FunctionLeekValue.java
+3 −1 src/main/java/leekscript/runner/values/LeekValue.java
+35 −25 src/main/java/leekscript/runner/values/LegacyArrayLeekValue.java
+7 −2 src/main/java/leekscript/runner/values/MapLeekValue.java
+11 −11 src/main/java/leekscript/runner/values/ObjectLeekValue.java
+4 −1 src/test/java/test/TestArray.java
+17 −1 src/test/java/test/TestCommon.java
+9 −8 src/test/java/test/TestMain.java
+1 −0 src/test/java/test/TestMap.java
+87 −36 src/test/java/test/TestObject.java
+28 −0 src/test/java/test/TestOperations.java
+23 −14 src/test/java/test/TestReference.java
21 changes: 11 additions & 10 deletions src/main/java/com/leekwars/generator/classes/ChipClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
import leekscript.runner.values.ArrayLeekValue;
import leekscript.runner.values.FunctionLeekValue;
import leekscript.runner.values.LegacyArrayLeekValue;
import leekscript.AILog;
import leekscript.common.Error;

public class ChipClass {

// ---- Fonctions Chip ----

public static Object getCurrentCooldown(EntityAI ai, long chip_id) throws LeekRunException {
((EntityAI) ai).addSystemLog(FarmerLog.WARNING, Error.DEPRECATED_FUNCTION, new String[] { "getCurrentCooldown", "getCooldown" });
((EntityAI) ai).addSystemLog(AILog.WARNING, Error.DEPRECATED_FUNCTION, new String[] { "getCurrentCooldown", "getCooldown" });
return null;
}

public static Object getCurrentCooldown(EntityAI ai, long chip_id, long v) throws LeekRunException {
((EntityAI) ai).addSystemLog(FarmerLog.WARNING, Error.DEPRECATED_FUNCTION, new String[] { "getCurrentCooldown", "getCooldown" });
((EntityAI) ai).addSystemLog(AILog.WARNING, Error.DEPRECATED_FUNCTION, new String[] { "getCurrentCooldown", "getCooldown" });
return null;
}

Expand Down Expand Up @@ -67,9 +68,9 @@ public static long useChip(EntityAI ai, long chip_id, long leek_id) throws LeekR
if (chip == null) {
Chip ct = Chips.getChip((int) chip_id);
if (ct == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(chip_id) });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(chip_id) });
} else {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(chip_id), ct.getName() });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(chip_id), ct.getName() });
}
}
if (target != null && chip != null && !target.isDead()) {
Expand All @@ -92,9 +93,9 @@ public static long useChipOnCell(EntityAI ai, long chip_id, long cell_id) throws
if (template == null) {
Chip ct = Chips.getChip((int) chip_id);
if (ct == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(chip_id) });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(chip_id) });
} else {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(chip_id), ct.getName() });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(chip_id), ct.getName() });
}
}
if (target != null && template != null) {
Expand Down Expand Up @@ -259,9 +260,9 @@ public static long summon(EntityAI ai, long chip, long cell, FunctionLeekValue s
if (template == null) {
Chip ct = Chips.getChip((int) chip);
if (ct == null)
ai.addSystemLog(LeekLog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(ai.integer(chip)) });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(ai.integer(chip)) });
else
ai.addSystemLog(LeekLog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(ai.integer(chip)), ct.getName() });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(ai.integer(chip)), ct.getName() });
return -1;
}

Expand Down Expand Up @@ -291,9 +292,9 @@ public static long resurrect(EntityAI ai, long entity, long cell) throws LeekRun
Chip ct = Chips.getChip(FightConstants.CHIP_RESURRECTION.getIntValue());

if (ct == null)
ai.addSystemLog(LeekLog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(FightConstants.CHIP_RESURRECTION) });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EXISTS, new String[] { String.valueOf(FightConstants.CHIP_RESURRECTION) });
else
ai.addSystemLog(LeekLog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(FightConstants.CHIP_RESURRECTION), ct.getName() });
ai.addSystemLog(AILog.WARNING, FarmerLog.CHIP_NOT_EQUIPPED, new String[] { String.valueOf(FightConstants.CHIP_RESURRECTION), ct.getName() });
return -1;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/leekwars/generator/classes/EntityClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.leekwars.generator.fight.entity.EntityAI;
import com.leekwars.generator.leek.FarmerLog;

import leekscript.AILog;
import leekscript.runner.LeekRunException;
import leekscript.runner.values.ArrayLeekValue;
import leekscript.runner.values.LegacyArrayLeekValue;
Expand Down Expand Up @@ -356,7 +357,7 @@ public static boolean setWeapon(EntityAI ai, long weapon_id) throws LeekRunExcep
// Check if it is a valid weapon
var wt = Weapons.getWeapon((int) weapon_id);
if (wt == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.WEAPON_NOT_EXISTS, new String[] { String.valueOf(weapon_id) });
ai.addSystemLog(AILog.WARNING, FarmerLog.WEAPON_NOT_EXISTS, new String[] { String.valueOf(weapon_id) });
return false;
}

Expand All @@ -368,7 +369,7 @@ public static boolean setWeapon(EntityAI ai, long weapon_id) throws LeekRunExcep
}
}
if (w == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.WEAPON_NOT_EQUIPPED, new String[] { String.valueOf(weapon_id), wt.getName() });
ai.addSystemLog(AILog.WARNING, FarmerLog.WEAPON_NOT_EQUIPPED, new String[] { String.valueOf(weapon_id), wt.getName() });
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/leekwars/generator/classes/FieldClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.leekwars.generator.maps.Cell;
import com.leekwars.generator.maps.Pathfinding;

import leekscript.AILog;
import leekscript.runner.LeekRunException;
import leekscript.runner.values.ArrayLeekValue;
import leekscript.runner.values.GenericArrayLeekValue;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static LegacyArrayLeekValue getPath_v1_3(EntityAI ai, long c1, long c2, O
if (leeks_to_ignore instanceof GenericArrayLeekValue) {
ai.putCells(ignore, (GenericArrayLeekValue) leeks_to_ignore);
} else if (leeks_to_ignore instanceof Number) {
ai.getLogs().addLog(FarmerLog.WARNING,
ai.getLogs().addLog(AILog.WARNING,
"Attention, la fonction getPath(Cell start, Cell end, Leek leek_to_ignore) va disparaitre, il faut désormais utiliser un tableau de cellules à ignorer.");
Entity l = ai.getFight().getEntity(ai.integer(leeks_to_ignore));
if (l != null && l.getCell() != null) {
Expand Down Expand Up @@ -140,7 +141,7 @@ public static ArrayLeekValue getPath(EntityAI ai, long c1, long c2, Object leeks
if (leeks_to_ignore instanceof GenericArrayLeekValue) {
ai.putCells(ignore, (GenericArrayLeekValue) leeks_to_ignore);
} else if (leeks_to_ignore instanceof Number) {
ai.getLogs().addLog(FarmerLog.WARNING,
ai.getLogs().addLog(AILog.WARNING,
"Attention, la fonction getPath(Cell start, Cell end, Leek leek_to_ignore) va disparaitre, il faut désormais utiliser un tableau de cellules à ignorer.");
Entity l = ai.getFight().getEntity(ai.integer(leeks_to_ignore));
if (l != null && l.getCell() != null) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/leekwars/generator/classes/WeaponClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.leekwars.generator.maps.Cell;
import com.leekwars.generator.maps.Pathfinding;

import leekscript.AILog;
import leekscript.common.Error;
import leekscript.runner.LeekRunException;
import leekscript.runner.values.ArrayLeekValue;
Expand All @@ -23,7 +24,7 @@ public static long useWeapon(EntityAI ai, long leek_id) throws LeekRunException
Entity target = ai.getFight().getEntity(leek_id);
if (target != null && target != ai.getEntity() && !target.isDead()) {
if (ai.getEntity().getWeapon() == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.NO_WEAPON_EQUIPPED);
ai.addSystemLog(AILog.WARNING, FarmerLog.NO_WEAPON_EQUIPPED);
}
success = ai.getFight().useWeapon(ai.getEntity(), target.getCell());
}
Expand All @@ -39,7 +40,7 @@ public static long useWeaponOnCell(EntityAI ai, long cell_id) throws LeekRunExce
Cell target = ai.getFight().getMap().getCell((int) cell_id);
if (target != null && target != ai.getEntity().getCell()) {
if (ai.getEntity().getWeapon() == null) {
ai.addSystemLog(FarmerLog.WARNING, FarmerLog.NO_WEAPON_EQUIPPED);
ai.addSystemLog(AILog.WARNING, FarmerLog.NO_WEAPON_EQUIPPED);
}
success = ai.getFight().useWeapon(ai.getEntity(), target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class BulbAI extends EntityAI {
private EntityAI mOwnerAI;

public BulbAI(Entity entity, EntityAI owner_ai, FunctionLeekValue ai) {
super(entity, owner_ai.logs);
super(entity, owner_ai.getLogs());
valid = true;
mAIFunction = ai;
setFight(owner_ai.fight);
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/leekwars/generator/fight/entity/EntityAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import leekscript.runner.values.ArrayLeekValue;
import leekscript.runner.values.GenericArrayLeekValue;
import leekscript.runner.values.LegacyArrayLeekValue;
import leekscript.AILog;
import leekscript.common.Error;

public class EntityAI extends AI {
Expand Down Expand Up @@ -76,7 +77,6 @@ public GenericArrayLeekValue getArray(EntityAI ai) throws LeekRunException {
protected long mIACpuRunTime = 0;

protected String ai_name = "";
protected LeekLog logs;

protected final List<LeekMessage> mMessages = new ArrayList<LeekMessage>();
protected final List<String> mSays = new ArrayList<String>();
Expand All @@ -89,9 +89,8 @@ public EntityAI(int instructions, int version) {
}

public EntityAI(Entity entity, LeekLog logs) {
super(0, LeekScript.LATEST_VERSION);
super(0, LeekScript.LATEST_VERSION, logs);
setEntity(entity);
this.logs = logs;
}

public static AIFile resolve(Generator generator, EntityInfo entityInfo, Entity entity) {
Expand Down Expand Up @@ -207,16 +206,14 @@ public void addSystemLog(int type, Error error, String[] parameters, StackTraceE

public void addSystemLog(int type, int key, String[] parameters, StackTraceElement[] elements) {
opsNoCheck(AI.ERROR_LOG_COST);
if (type == FarmerLog.WARNING)
type = FarmerLog.SWARNING;
else if (type == FarmerLog.ERROR)
type = FarmerLog.SERROR;
else if (type == FarmerLog.STANDARD)
type = FarmerLog.SSTANDARD;

if (this != null) {
logs.addSystemLog(type, this.getErrorMessage(elements), key, parameters);
}
if (type == AILog.WARNING)
type = AILog.SWARNING;
else if (type == AILog.ERROR)
type = AILog.SERROR;
else if (type == AILog.STANDARD)
type = AILog.SSTANDARD;

logs.addSystemLog(type, this.getErrorMessage(elements), key, parameters);
}

public long getIARunTime() {
Expand All @@ -232,7 +229,7 @@ public boolean isValid() {
}

public LeekLog getLogs() {
return logs;
return (LeekLog) logs;
}

public void addMessage(LeekMessage leekMessage) {
Expand All @@ -248,7 +245,7 @@ public List<String> getSays() {

public void setFight(Fight fight) {
this.fight = fight;
logs.setLogs(fight.getActions());
((LeekLog) logs).setLogs(fight.getActions());
}

public void runTurn(int turn) {
Expand Down
45 changes: 42 additions & 3 deletions src/main/java/com/leekwars/generator/leek/FarmerLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.leekwars.generator.fight.entity.Entity;

import leekscript.AILog;
import leekscript.runner.AI;
import leekscript.runner.LeekRunException;

public class FarmerLog extends AILog {
public class FarmerLog {

private final static int MAX_LENGTH = 500000;

Expand Down Expand Up @@ -61,7 +63,40 @@ public void addAction(JSONArray action) {
mCurArray.add(action);
}

public void addSystemLog(Entity leek, int type, String error, int key, String[] parameters) {
public void addSystemLog(AI ai, int type, String error, int key, Object[] parameters) throws LeekRunException {
throw new RuntimeException("not implemented");
}

public void addSystemLog(AI ai, Entity leek, int type, String error, int key, Object[] parameters) throws LeekRunException {

if (!addSize(20)) {
return;
}

String[] parametersString = parameters != null ? new String[parameters.length] : null;

if (parameters != null) {
for (int p = 0; p < parameters.length; ++p) {
var parameterString = ai.string(parameters[p]);
if (!addSize(parameterString.length())) {
parametersString[p] = "[...]";
} else {
parametersString[p] = parameterString;
}
}
}
JSONArray obj = new JSONArray();
obj.add(leek.getFId());
obj.add(type);
obj.add(error);
obj.add(key);
if (parameters != null) {
obj.add(parametersString);
}
addAction(obj);
}

public void addSystemLogString(Entity leek, int type, String error, int key, String[] parameters) {
int paramSize = 0;
if (parameters != null) {
for (String p : parameters) {
Expand Down Expand Up @@ -142,7 +177,7 @@ public void addLog(Entity leek, int type, String message, int color) {
JSONArray obj = new JSONArray();
obj.add(leek.getFId());
obj.add(type);
obj.add(new String(message.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
obj.add(message);
if (color != 0) {
obj.add(color);
}
Expand Down Expand Up @@ -186,4 +221,8 @@ public void addPause(Entity leek) {
obj.add(PAUSE);
addAction(obj);
}

public boolean isFull() {
return mSize >= MAX_LENGTH;
}
}
23 changes: 13 additions & 10 deletions src/main/java/com/leekwars/generator/leek/LeekLog.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.leekwars.generator.leek;

import com.alibaba.fastjson.JSONArray;
import com.leekwars.generator.fight.action.Actions;
import com.leekwars.generator.fight.entity.Entity;
import leekscript.common.Error;

import leekscript.runner.AI;
import leekscript.runner.LeekRunException;
import leekscript.AILog;

public class LeekLog extends AILog {
Expand All @@ -15,13 +15,6 @@ public class LeekLog extends AILog {
public LeekLog(FarmerLog farmerLogs, Entity entity) {
this.farmerLogs = farmerLogs;
this.entity = entity;
stream = new AILog.Stream() {
@Override
public void write(JSONArray array) {
array.set(0, entity.getFId());
farmerLogs.addAction(array);
}
};
}

public void addLog(int warning, String string) {
Expand All @@ -42,7 +35,12 @@ public void addSystemLog(int type, Error error, String[] parameters) {
}

public void addSystemLog(int type, String trace, int key, String[] parameters) {
farmerLogs.addSystemLog(entity, type, trace, key, parameters);
farmerLogs.addSystemLogString(entity, type, trace, key, parameters);
}

@Override
public void addSystemLog(AI ai, int type, String trace, int key, Object[] parameters) throws LeekRunException {
farmerLogs.addSystemLog(ai, entity, type, trace, key, parameters);
}

public void setLogs(Actions actions) {
Expand All @@ -64,4 +62,9 @@ public void addCellText(int[] cells, String text, int color, int duration) {
public void addPause() {
farmerLogs.addPause(entity);
}

@Override
public boolean isFull() {
return farmerLogs.isFull();
}
}

0 comments on commit acdd1e9

Please sign in to comment.