Update JSON out
This commit is contained in:
parent
8d3b99ad69
commit
86292e77dc
1
.gitignore
vendored
1
.gitignore
vendored
@ -36,3 +36,4 @@ __pycache__/IDManager.cpython-39.pyc
|
||||
*.pyc
|
||||
questionare.csv
|
||||
log.txt
|
||||
*.json
|
||||
|
2
.idea/MarioWeb.iml
generated
2
.idea/MarioWeb.iml
generated
@ -5,7 +5,7 @@
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="VisMOO" jdkType="Python SDK" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="TemplatesService">
|
||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="VisMOO" project-jdk-type="Python SDK" />
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
</project>
|
Binary file not shown.
File diff suppressed because one or more lines are too long
@ -7,5 +7,6 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="top.jfunc.json.fastjson" level="project" />
|
||||
</component>
|
||||
</module>
|
Binary file not shown.
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
||||
|
||||
import agents.HumanAgent;
|
||||
import agents.ReplayAgent;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import engine.core.MarioAgentEvent;
|
||||
import engine.core.MarioGame;
|
||||
import engine.core.MarioResult;
|
||||
@ -24,8 +25,8 @@ public class Play {
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
//FIXME: Debug Use
|
||||
// game = new MarioGame();
|
||||
// playJavaGame();
|
||||
//game = new MarioGame();
|
||||
//System.out.println(playJavaGame());
|
||||
System.out.println("Java: Play Java Main Function Done");
|
||||
}
|
||||
public static boolean initialGame(){
|
||||
@ -37,7 +38,7 @@ public class Play {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static byte[] playJavaGame(){
|
||||
public static String playJavaGame(){
|
||||
|
||||
game.setLives(3);
|
||||
String levelPath = "./levels/group0/lvl1.lvl"; // For local
|
||||
@ -47,11 +48,14 @@ public class Play {
|
||||
MarioResult tmpResult = game.playGame(new HumanAgent(false),getLevel(levelPath), 10, repPath,30);
|
||||
//Replay
|
||||
//MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 30, repPath,30);
|
||||
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
|
||||
//return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
|
||||
String jsonString = Replay.serializeGameResult(tmpResult);
|
||||
|
||||
|
||||
return jsonString;//Replay.serializeAgentEvents(tmpResult.getAgentEvents());
|
||||
}
|
||||
|
||||
public static byte[] playGameMain(String levelName, int lives, boolean control,int time,int col){
|
||||
public static MarioResult playGameMain(String levelName, int lives, boolean control,int time,int col){
|
||||
|
||||
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
|
||||
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
|
||||
@ -61,10 +65,12 @@ public class Play {
|
||||
game.setLives(lives);
|
||||
MarioResult tmpResult = game.playGame(new HumanAgent(control),getLevel(levelPath), time, repPath,col);
|
||||
|
||||
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
|
||||
//return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
|
||||
//return Replay.serializeGameResult(tmpResult);
|
||||
return tmpResult;
|
||||
}
|
||||
|
||||
public static byte[] playGameMain(String levelName){
|
||||
public static MarioResult playGameMain(String levelName){
|
||||
return playGameMain(levelName, 5, false,30,16);
|
||||
}
|
||||
|
||||
@ -81,4 +87,15 @@ public class Play {
|
||||
public static void stopReplay(){
|
||||
game.stopGame();
|
||||
}
|
||||
|
||||
public static byte[] serializeActionFromJson(String jsonString){
|
||||
MarioResult marioResult= JSON.parseObject(jsonString,MarioResult.class);
|
||||
ArrayList<MarioAgentEvent> events = marioResult.getAgentEvents();
|
||||
byte[] content = new byte[events.size()];
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
boolean[] action = events.get(i).getActions();
|
||||
content[i] = Replay.serializeAction(action);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,14 @@ import java.util.ArrayList;
|
||||
import engine.helper.EventType;
|
||||
import engine.helper.GameStatus;
|
||||
import engine.helper.SpriteType;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
public class MarioResult {
|
||||
private MarioWorld world;
|
||||
private ArrayList<MarioEvent> gameEvents;
|
||||
private ArrayList<MarioAgentEvent> agentEvents;
|
||||
|
||||
public MarioResult(){}
|
||||
/**
|
||||
* Create a mario result object
|
||||
*
|
||||
@ -18,6 +20,7 @@ public class MarioResult {
|
||||
* @param gameEvents the events that happens in the playthrough of the game
|
||||
* @param agentEvents the events that happens in the playthrough of the game
|
||||
*/
|
||||
|
||||
public MarioResult(MarioWorld world, ArrayList<MarioEvent> gameEvents, ArrayList<MarioAgentEvent> agentEvents) {
|
||||
this.world = world;
|
||||
this.gameEvents = gameEvents;
|
||||
|
@ -1,7 +1,12 @@
|
||||
package engine.helper;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import agents.ReplayAgent;
|
||||
import engine.core.MarioAgentEvent;
|
||||
import engine.core.MarioResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@ -34,6 +39,11 @@ public class Replay {
|
||||
return content;
|
||||
}
|
||||
|
||||
public static String serializeGameResult(MarioResult marioResult) {
|
||||
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(marioResult);
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
public static void saveReplay(String filepath, ArrayList<MarioAgentEvent> events) {
|
||||
try {
|
||||
byte[] content = serializeAgentEvents(events);
|
||||
@ -61,4 +71,5 @@ public class Replay {
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
}
|
||||
|
48
main.py
48
main.py
@ -9,14 +9,15 @@ from flask import Flask, render_template, request, redirect, url_for, session
|
||||
import logging
|
||||
|
||||
# 设置logging模块
|
||||
logging.basicConfig(filename='log.txt', level=logging.DEBUG)
|
||||
# logging.basicConfig(filename='log.txt', level=logging.DEBUG)
|
||||
# 将print输出重定向到logging模块
|
||||
print = logging.getLogger().info
|
||||
# print = logging.getLogger().info
|
||||
|
||||
app = Flask(__name__, static_folder='')
|
||||
idm = idManager()
|
||||
app.secret_key = 'asdfasdfawefaewvaf'
|
||||
replayDataPath = "reps/"
|
||||
jsonDataPath = "jsons/"
|
||||
evalDataPath = "evals/"
|
||||
|
||||
questionarePath = "data/questionare.csv"
|
||||
@ -97,8 +98,9 @@ def gameplay(id):
|
||||
def getJSONData(id):
|
||||
if request.method == 'POST':
|
||||
print("POST Game")
|
||||
resultList = list(request.form)[0].split(",")
|
||||
saveFile(replayDataPath, id + "_" + resultList[0][:-2], resultList[1:])
|
||||
resultList = list(request.form)[0].split("@@@")
|
||||
saveJsonFile(jsonDataPath, id + "_" + resultList[0], resultList[1])
|
||||
saveRepFile(replayDataPath, id + "_" + resultList[0], resultList[1])
|
||||
return "return!"
|
||||
|
||||
|
||||
@ -151,8 +153,9 @@ def gameplay2(id):
|
||||
def getJSONData2(id):
|
||||
if request.method == 'POST':
|
||||
print("POST Game")
|
||||
resultList = list(request.form)[0].split(",")
|
||||
saveFile(replayDataPath, id+"_" + resultList[0][:-2], resultList[1:])
|
||||
resultList = list(request.form)[0].split("@@@")
|
||||
saveJsonFile(jsonDataPath, id + "_" + resultList[0], resultList[1])
|
||||
saveRepFile(replayDataPath, id + "_" + resultList[0], resultList[1])
|
||||
return "return!"
|
||||
|
||||
|
||||
@ -193,7 +196,7 @@ def gameannoresult2(id):
|
||||
return redirect(url_for("gameplay2", id=id))
|
||||
|
||||
|
||||
@app.route("/gameover",methods=['POST','GET'])
|
||||
@app.route("/gameover", methods=['POST', 'GET'])
|
||||
def over():
|
||||
finish = idm.getTimes(getId())
|
||||
if request.method == 'POST':
|
||||
@ -213,15 +216,40 @@ def over():
|
||||
# [getId(), resultList[0],
|
||||
# ""])
|
||||
|
||||
# return redirect(url_for("over", id=id))
|
||||
def saveFile(path, filename, content):
|
||||
cp = list(map(int, content))
|
||||
# return redirect(url_for("over", id=id))
|
||||
def saveRepFile(path, filename, content):
|
||||
o_dict = json.loads(content)
|
||||
action_dict = o_dict["elementData1"][1:]
|
||||
actionList = []
|
||||
for actions in action_dict:
|
||||
try:
|
||||
alist = actions["actions0"]
|
||||
actionsInput = [alist["0"], alist["1"], alist["2"], alist["3"], alist["4"], alist["5"], alist["6"]]
|
||||
actionList.append(serializeAction(actionsInput))
|
||||
except Exception: continue
|
||||
|
||||
cp = list(map(int, actionList))
|
||||
file_dir = os.path.join(os.getcwd(), path)
|
||||
file_path = os.path.join(file_dir, filename + ".rep")
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(b''.join(struct.pack('B', c) for c in cp))
|
||||
|
||||
|
||||
def serializeAction(actions):
|
||||
res = 0
|
||||
for i in range(5):
|
||||
if not actions[i]:
|
||||
tmp = 1 << i
|
||||
res += tmp
|
||||
return res
|
||||
|
||||
def saveJsonFile(path, filename, content):
|
||||
file_dir = os.path.join(os.getcwd(), path)
|
||||
file_path = os.path.join(file_dir, filename + ".json")
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=80, debug=False)
|
||||
# app.run()
|
||||
|
@ -332,18 +332,12 @@
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
cheerpjCreateDisplay(500, 500, divElement);
|
||||
cheerpjRunMain("Play", "/app/Mario-AI-Interface.jar", "0", "f_l");
|
||||
|
||||
GameLoad().then(function () {
|
||||
|
||||
$("#windowshow:visible").hide();
|
||||
{#console.log("Cheerpj Initialize Succeed!", control);#}
|
||||
|
||||
|
||||
|
||||
document.getElementById("loading").style.visibility = 'hidden';
|
||||
})
|
||||
|
||||
@ -370,9 +364,6 @@
|
||||
}else {
|
||||
$("#top:hidden").show();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
@ -25,14 +25,20 @@ function GameLoad(){
|
||||
function PlayLevel(level,control){
|
||||
console.log("HTML:Start PlayLevel cjCall")
|
||||
var returnVal = cjCall("Play", "playGameMain", level, 5, control,30,16);
|
||||
return returnVal.then(function(){
|
||||
return returnVal.then(function(){
|
||||
stringVal = toJSON(returnVal.value['agentEvents2'])
|
||||
console.log("the return val is ready");
|
||||
console.log(returnVal.value);
|
||||
PostToServer(window.location.href+"/data",level+returnVal.value);
|
||||
console.log(stringVal);
|
||||
PostToServer(window.location.href+"/data",level+"@@@"+stringVal);
|
||||
//Array.from()
|
||||
});
|
||||
}
|
||||
|
||||
function toJSON(returnVal)
|
||||
{
|
||||
return JSON.stringify(returnVal)
|
||||
}
|
||||
|
||||
function GameOver(){
|
||||
if(alert("Game Over!")){
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user