From d345263b818bed2c77810d3c68d6589c3e76992d Mon Sep 17 00:00:00 2001 From: bigJIU <1572204178@qq.com> Date: Mon, 3 Apr 2023 14:03:25 +0800 Subject: [PATCH] Accelerate Try --- newMain.py | 218 ++++++++ templates/Main.html | 1199 ++++++++++++++++++++++++++++++++++++++++++- templates/data.js | 15 +- 3 files changed, 1404 insertions(+), 28 deletions(-) create mode 100644 newMain.py diff --git a/newMain.py b/newMain.py new file mode 100644 index 0000000..6cde2cb --- /dev/null +++ b/newMain.py @@ -0,0 +1,218 @@ +import json +import os +import struct +import uuid + +from IDManager import idManager + +from flask import Flask, render_template, request, redirect, url_for, session +import logging + +# 设置logging模块 +logging.basicConfig(filename='log.txt', level=logging.DEBUG) +# 将print输出重定向到logging模块 +print = logging.getLogger().info + +app = Flask(__name__, static_folder='') +idm = idManager() +app.secret_key = 'asdfasdfawefaewvaf' +replayDataPath = "reps/" +evalDataPath = "evals/" + +questionarePath = "data/questionare.csv" +annotationPath = "data/annotation.csv" +annotationPath2 = "data/annotation2.csv" + + +# id=idm.getId(request.remote_addr) +def getId(): + if 'name' not in session: + session['name'] = str(uuid.uuid4()) + return session['name'] + + +@app.route('/') +def gameinit(): + index = getId() + cid = idm.iniId(index) + return redirect(url_for('Main.html', id=index)) + + +@app.route('/') +def gamewelcome(index): + return render_template('Main.html', id=index) + + +@app.route('/question') +def gamequestion(): + return render_template('GameQuestion.html') + + +@app.route('/result', methods=['POST', 'GET']) +def gamepreplay(): + if request.method == 'POST': + result = request.form + # ip = request.remote_addr + ip = getId() + cid = idm.iniId(ip) + # Save the result to questionare + idm.write_csv(questionarePath, + [cid, + result.get("playeds"), + result.get("playedp"), + result.get("gamestyle"), + result.get("frequency"), + result.get("age") + result.get("myAge"), + result.get("gender") + result.get("myGender"), + ""]) + idm.setControl(cid, result.get("control")) + print(result.get("gamestyle")) + return redirect(url_for('gametutorial', id=cid)) + # debug use: + # return redirect(url_for('gameanno2', id=cid)) + + +@app.route('/gametutorial/') +def gametutorial(id): + return render_template('GameTutorial.html', tutorial=idm.addTutorial(id), next=idm.hasNextTutorial(id), + maxT=idm.tutorialMax, + control=idm.getControl(id)) + + +@app.route('/again') +def gamepreplayAgain(): + return redirect(url_for('gameplay', id=getId())) + + +@app.route('/gametutorial//data') +def gametutorialdata(id): + return redirect(url_for('gameplay', id=id)) + + +@app.route('/gameplay/') +def gameplay(id): + gamelevels = idm.getLevels(id) + return render_template('GamePlay.html', gamelevels=gamelevels, control=idm.getControl(id), levelNum=2, + jump="/annotation") + + +@app.route('/gameplay//data', methods=['POST']) +def getJSONData(id): + if request.method == 'POST': + print("POST Game") + resultList = list(request.form)[0].split(",") + saveFile(replayDataPath, id + resultList[0][:-2], resultList[1:]) + return "return!" + + +@app.route('/annotation') +def gamepreanno(): + return redirect(url_for('gameanno', id=getId())) + + +@app.route('/annotation/') +def gameanno(id): + if (id != "radioresult"): + print("anno " + id) + gamelevels = idm.getRecent(id) + + level1 = "lvl" + str(gamelevels[0]) + level2 = "lvl" + str(gamelevels[1]) + return render_template('GameAnnotation.html', level1=level1, level2=level2) + + +@app.route('/annotation/radioresult', methods=['POST']) +def getRadioData(): + ip = getId() + + if request.method == 'POST': + print("POST Eval") + result = request.form + ipRecent = idm.getRecent(ip) + idm.write_csv(annotationPath, [ip, ipRecent[0], ipRecent[1], result["fun"]]) + + if idm.getTimes(ip): + return redirect(url_for("gameplay2", id=ip)) + else: + idm.addTimes(ip) + return redirect(url_for("gameplay", id=ip)) + + +@app.route('/gameplay2') +def gamepreplay2(): + return redirect(url_for('gameplay2', id=getId())) + + +@app.route('/gameplay2/') +def gameplay2(id): + gamelevels = idm.getTypeLevels(id) + return render_template('GamePlay.html', gamelevels=gamelevels, control=idm.getControl(id), levelNum=3, + jump="/annotation2") + + +@app.route('/gameplay2//data', methods=['POST']) +def getJSONData2(id): + if request.method == 'POST': + print("POST Game") + resultList = list(request.form)[0].split(",") + saveFile(replayDataPath, id + resultList[0][:-2], resultList[1:]) + return "return!" + + +@app.route('/annotation2') +def gamepreanno2(): + return redirect(url_for('gameanno2', id=getId())) + + +@app.route('/annotation2/') +def gameanno2(id): + if id != "result": + print("anno " + id) + gamelevels = idm.getRecent(id) + # gamelevels = idm.getTypeLevels(id) + level1 = gamelevels[0] + level2 = gamelevels[1] + level3 = gamelevels[2] + return render_template('GameAnnotation2.html', level1=level1, level2=level2, level3=level3) + else: + print(id) + + +@app.route('/annotation2//result', methods=['POST']) +def gameannoresult2(id): + if request.method == 'POST': + print("result: " + id) + + resultList = list(request.form)[0].split(",") + levelList = idm.getRecent(getId()) + + idm.write_csv(annotationPath2, + [getId(), resultList[0], resultList[1], resultList[2], levelList[0], levelList[1], + levelList[2], + ""]) + + if idm.getTimes(id): + return redirect(url_for("over", id=id)) + else: + idm.addTimes(id) + return redirect(url_for("gameplay2", id=id)) + + +@app.route("/gameover") +def over(): + finish = idm.getTimes(getId()) + + return render_template("GameOver.html", finish=1, stage=1) + + +def saveFile(path, filename, content): + cp = list(map(int, content)) + 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)) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=80, debug=False) + # app.run() diff --git a/templates/Main.html b/templates/Main.html index 7ac6198..6e1456e 100644 --- a/templates/Main.html +++ b/templates/Main.html @@ -1,22 +1,1193 @@ + + + + + - - Title - - - - + + + + + + + + + AI and Games + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+ +
+
+ +
+ +
+

Purpose of the Experiment

+
    +
  • + This Experiment is designed to study the notion of "fun" elicited while playing Super + Mario + Bros levels. + +
+
+

Procedure of the Experiment

+
    +
  • This experiment has three phases. +
      +
    1. First you must answer a questionnaire.
    2. +
    3. Then you will complete a short game tutorial, play 2 levels of the game and answer a + simple question. You will play another 2 levels.
    4. +
    5. Finally you will play 3 levels and answer a simple question.
    6. +
    7. You can also replay the second phase after finishing the whole process.
    8. +
    +
  • In total, the experiment takes approximately 15 minutes to complete.
  • +
  • Have Fun! +
+ +

+ + +
+
+
+
+ + + + +
+
+
+ +
+

+

Copyright © Jialin Liu

+ +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+

+ Questionnaire +

+
+
+ +

+

1. Have you played Super Mario Bros before?

+ + + +

+ +

+

2. Have you ever played platform games like Super Mario Bros?

+ + + +

+ +

+

3. Which following selection represents you the best in Super Mario Bros?

+ + + + +

+ + +

+

4. How often do you play games?

+ + + + + +

+ +

+

5. What is your age?

+ + + +

+ +

+

6. What is your gender?

+ + + + + +

+ +

+

7. Please choose your control for later games.

+ + + +

+ + + +
+

+
+ +
+ +
+
+ +
+ + + + + + + +
+
+
+ +
+

+

Copyright © Jialin Liu

+ +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ + +
+
+ +

Super Mario Bros Tutorial

+

Before proceeding to the main part of the experiment please familiarize yourself with the game.
+ You may proceed to the next phase of this survey once you are ready!

+ + Click ME to show the instruction page again. + +

If the game is not responding to your keyboard, please click on the game screen.

+
+ + + + +
+ +
+
+
Loading
+
+ +
+ +
+ + + + +
+
+ + + + +
+
+ + + +
+ +
+ +
+ + + +
+ + + +
+ + + + +
+
+
+ +
+

+

Copyright © Jialin Liu

+ +
+ +
+
+
+ + + + + + + + + + + + + + + - \ No newline at end of file diff --git a/templates/data.js b/templates/data.js index 14f5c6d..f302de2 100644 --- a/templates/data.js +++ b/templates/data.js @@ -11,17 +11,7 @@ function getValue(){ } } } -// function PostToServer(data) { -// var httpRequest = new XMLHttpRequest();//第一步:建立所需的对象 -// httpRequest.open("POST",""); //调用AddDataToServer -// httpRequest.setRequestHeader("Content-Type", "application/json"); //设置请求头信息 -// httpRequest.onreadystatechange = function () { -// if (httpRequest.readyState == 4 && httpRequest.status == 200) { -// //alert('添加成功'); -// } -// } -// httpRequest.send(JSON.stringify(data)); //设置为发送给服务器数据 -// } + function PostToServer(url,data) { if(url == null){ $.post(window.location.href, data); @@ -59,9 +49,6 @@ function PlayLevel(level,control){ alert(obj[i].value); } } - // var radio = document.getElementsByName("gameRadio"); - // var addressID = $("input[name='sex']:checked").val(); - // alert(addressID) }