Replay over

This commit is contained in:
bigJIU 2022-10-21 17:46:57 +08:00
parent 8c37071d49
commit fcfdf8e844
9 changed files with 116 additions and 87 deletions

36
.idea/workspace.xml generated
View File

@ -2,15 +2,13 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="c32bfd4e-6189-47f3-9d6c-c1825d3d7e5f" name="Changes" comment=""> <list default="true" id="c32bfd4e-6189-47f3-9d6c-c1825d3d7e5f" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.idea/MarioWeb.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/artifacts/Mario_AI_Interface_jar.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/libraries/Mario_AI_Interface.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Flask/main.py" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/Mario-AI-Interface.jar.js" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface.jar.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/play.html" beforeDir="false" afterPath="$PROJECT_DIR$/play.html" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/Play.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/Play.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioGame.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioGame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/helper/Assets.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/helper/Assets.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/templates/play.html" beforeDir="false" afterPath="$PROJECT_DIR$/templates/play.html" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -28,16 +26,16 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"WebServerToolWindowFactoryState": "false", &quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
"last_opened_file_path": "C:/Users/duaho/Projects/website materials/MarioWeb", &quot;last_opened_file_path&quot;: &quot;C:/Users/duaho/Projects/website materials/MarioWeb/Mario-AI-Interface&quot;,
"nodejs_package_manager_path": "npm" &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;
} }
}]]></component> }</component>
<component name="RunManager" selected="Flask server.Flask (main.py)"> <component name="RunManager" selected="Flask server.Flask (main.py)">
<configuration name="MarioWeb" type="PythonConfigurationType" factoryName="Python"> <configuration name="MarioWeb" type="PythonConfigurationType" factoryName="Python">
<module name="MarioWeb" /> <module name="MarioWeb" />
@ -91,6 +89,8 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1666269904844</updated> <updated>1666269904844</updated>
<workItem from="1666269906002" duration="7106000" /> <workItem from="1666269906002" duration="7106000" />
<workItem from="1666331056069" duration="7259000" />
<workItem from="1666340478319" duration="84000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -109,6 +109,6 @@
</option> </option>
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/MarioWeb$Flask__main_py_.coverage" NAME="Flask (main.py) Coverage Results" MODIFIED="1666277231897" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" /> <SUITE FILE_PATH="coverage/MarioWeb$Flask__main_py_.coverage" NAME="Flask (main.py) Coverage Results" MODIFIED="1666345159743" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
</component> </component>
</project> </project>

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -3,9 +3,12 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import agents.HumanAgent;
import agents.ReplayAgent;
import engine.core.MarioAgentEvent; import engine.core.MarioAgentEvent;
import engine.core.MarioGame; import engine.core.MarioGame;
import engine.core.MarioResult; import engine.core.MarioResult;
import engine.helper.Replay;
public class Play { public class Play {
public static String getLevel(String filepath) { public static String getLevel(String filepath) {
@ -16,46 +19,43 @@ public class Play {
} }
return content; return content;
} }
public static byte[] serializeAgentEvents(ArrayList<MarioAgentEvent> events) {
byte[] content = new byte[events.size()];
for (int i = 0; i < events.size(); i++) {
boolean[] action = events.get(i).getActions();
// System.out.println(action);
content[i] = serializeAction(action);
}
return content;
}
public static byte serializeAction(boolean[] action) {
byte res = 0;
for (int i = 0; i < 5; i++) {
if (action[i])
res += 1 << i;
}
return res;
}
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String groupID = args[0]; String groupID = args[0];
String levelName = args[1]; String levelName = args[1];
String participantID = args[2]; // MarioGame game = new MarioGame();
//MarioGame game = new MarioGame(); //
//// String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web
//// String repPath = String.format("/files/tmp.rep"); // For web
//
// String levelPath = String.format("./levels/group%s/%s.txt", groupID, levelName); // For local
// String repPath = String.format("./reps/%s_sav.rep", levelName); // For local
// MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 200, repPath);
String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web // MarioGame game2 = new MarioGame();
//String repPath = String.format("/files/reps/participant%s/%s.rep", participantID, levelName); // For web // MarioResult r2 = game2.playGame(getLevel(levelPath),200,repPath)
String repPath = String.format("/files/1.rep"); // For web
//String levelPath = String.format("./levels/group%s/%s.txt", groupID, levelName); // For web
//String repPath = String.format("./reps/participant%s/%s.rep", participantID, levelName); // For web
//MarioResult tmpResult = game.playGame(getLevel(levelPath), 200, repPath);
//return serializeAgentEvents(tmpResult.getAgentEvents());
} }
public static byte[] otherMain(String groupID, String levelName, String participantID){
MarioGame game = new MarioGame(); public static byte[] playGameMain(String groupID, String levelName){
String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web
//String repPath = String.format("/files/reps/participant%s/%s.rep", participantID, levelName); // For web String repPath = String.format("/files/%s_sav.rep", levelName); // For web
String repPath = String.format("/files/1.rep"); // For web
MarioResult tmpResult = game.playGame(getLevel(levelPath), 200, repPath); MarioGame game = new MarioGame();
return serializeAgentEvents(tmpResult.getAgentEvents()); MarioResult tmpResult = game.playGame(new HumanAgent(),getLevel(levelPath), 200, repPath);
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
}
public static void replayGameMain(String groupID, String levelName){
String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
// String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For local
// String repPath = String.format("./files/%s_sav.rep", levelName); // For local
MarioGame game = new MarioGame();
game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 200, repPath);
} }
} }

View File

@ -94,8 +94,8 @@ public class MarioGame {
* @param timer number of ticks for that level to be played. Setting timer to anything &lt;=0 will make the time infinite * @param timer number of ticks for that level to be played. Setting timer to anything &lt;=0 will make the time infinite
* @return statistics about the current game * @return statistics about the current game
*/ */
public MarioResult playGame(String level, int timer, String resultPath) { public MarioResult playGame(MarioAgent gameAgent,String level, int timer, String resultPath) {
return this.runGame(new HumanAgent(), level, timer, 0, true, 30, 2, resultPath); return this.runGame(gameAgent, level, timer, 0, true, 30, 2, resultPath);
} }
/** /**

View File

@ -42,7 +42,8 @@ public class Assets {
} }
private static Image getImage(GraphicsConfiguration gc, String imageName) throws IOException { private static Image getImage(GraphicsConfiguration gc, String imageName) throws IOException {
// File file = new File(System.getProperty("user.dir") + "/img/" + imageName); // Local test //FIXME web/local
//File file = new File(System.getProperty("user.dir") + "/img/" + imageName); // Local test
File file = new File("/app/img/" + imageName); // For web File file = new File("/app/img/" + imageName); // For web
BufferedImage source = ImageIO.read(file); BufferedImage source = ImageIO.read(file);
Image image = gc.createCompatibleImage(source.getWidth(), source.getHeight(), Transparency.BITMASK); Image image = gc.createCompatibleImage(source.getWidth(), source.getHeight(), Transparency.BITMASK);

Binary file not shown.

29
main.py
View File

@ -1,22 +1,29 @@
from flask import Flask, render_template, request, redirect, url_for from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__,static_folder='') app = Flask(__name__, static_folder='')
bufferJson = ['']
@app.route('/') @app.route('/')
def index(): def index():
py2htmlstr = 'py2html test str' py2htmlstr = 'py2html test str'
return render_template('play.html',py2htmlstr = py2htmlstr) return render_template('play.html', py2htmlstr=py2htmlstr)
@app.route('/dataPage')
def dataPage(data):
return data
@app.route('/',methods = ['POST']) @app.route('/datapage')
def datapage():
return bufferJson
@app.route('/', methods=['POST'])
def getData(): def getData():
if request.method == 'POST': if request.method == 'POST':
print("POST") print("POST")
print(request.json) print(request.json)
return redirect(url_for('dataPage',data = request.json)) return redirect(url_for('datapage'))
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.debug = True
app.run()
app.run(debug=True)

View File

@ -4,18 +4,11 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>CheerpJ test</title> <title>CheerpJ test</title>
<script src="https://cjrtnc.leaningtech.com/2.3/loader.js"></script> <script src="https://cjrtnc.leaningtech.com/2.3/loader.js"></script>
<script>
window.onload = function () {
</head> }
<body> function PostToServer(data) {
</body>
<br />{{ py2htmlstr }}
<script>
function showPreloadProgress(loadedFiles, totalFiles)
{
console.log("Percentage loaded "+(loadedFiles*100/totalFiles));
}
//上传测井数据表到服务器数据库
function PostToServer(data) {
var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象 var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象
httpRequest.open("POST",""); //调用AddDataToServer httpRequest.open("POST",""); //调用AddDataToServer
@ -31,24 +24,52 @@
//httpRequest.send((""+data)); //httpRequest.send((""+data));
} }
var cheerpjListener = {preloadProgress:showPreloadProgress};
cheerpjInit({listener:cheerpjListener}); </script>
//cheerpjInit(); </head>
<body>
</body>
<br />{{ py2htmlstr }}
<script>
cheerpjInit();
cheerpjCreateDisplay(800,600); cheerpjCreateDisplay(800,600);
//cheerpjRunMain("Play", "/app/Mario-AI-Interface.jar", "0", "f_l", "0"); cheerpjRunMain("Play", "/app/Mario-AI-Interface.jar", "0", "f_l");
console.log("done"); console.log("done");
cheerpjRunJar("/app/Mario-AI-Interface.jar"); //cheerpjRunJar("/app/Mario-AI-Interface.jar");
var returnVal = cjCall("Play", "otherMain", "0", "f_l", "0");// var returnVal = cjCall("Play", "playGameMain", "0", "f_l");
returnVal.then(function(e) {
returnVal.then(function(){
console.log("the return val is ready"); console.log("the return val is ready");
console.log(returnVal.value); console.log(returnVal.value);
PostToServer(returnVal.value); PostToServer(returnVal.value);
});
}).catch(e => console.log("Critical failure: " + e.message)); //.then(function(){console.log(" over2 ");});
//.catch((e) => {});
//console.log(returnVal); //console.log(returnVal);
// cheerpjRunJar("/app/Mario-AI-Interface.jar"); // cheerpjRunJar("/app/Mario-AI-Interface.jar");
// var actionSequenceInBytes = cjCall("MarioProxy", "play", "0", "f_l", "0"); // var actionSequenceInBytes = cjCall("MarioProxy", "play", "0", "f_l", "0");
</script> </script>
<form>
<input type="button" id="replayButton" value="Replay">
</form>
<script>
var button = document.getElementById("replayButton");
button.onclick = function(){
//alert("WTF?")
cjCall("Play", "replayGameMain", "0", "f_l");
};
</script>
</html> </html>