MarioWeb/main.py

113 lines
3.1 KiB
Python
Raw Normal View History

2022-11-03 15:16:45 +00:00
import json
2022-10-26 03:49:14 +00:00
import os
2022-11-08 09:22:21 +00:00
import struct
2022-11-03 14:07:08 +00:00
2022-11-03 12:03:04 +00:00
from IDManager import idManager
2022-10-26 03:49:14 +00:00
2022-10-20 14:48:14 +00:00
from flask import Flask, render_template, request, redirect, url_for
2022-10-21 09:46:57 +00:00
app = Flask(__name__, static_folder='')
2022-11-03 12:03:04 +00:00
idm = idManager()
2022-10-21 09:46:57 +00:00
2022-10-26 03:49:14 +00:00
replayDataPath = "reps/"
evalDataPath = "evals/"
2022-10-20 14:48:14 +00:00
2022-11-03 12:03:04 +00:00
questionarePath = "data/questionare.csv"
2022-11-03 14:07:08 +00:00
annotationPath = "data/annotation.csv"
2022-11-03 09:07:12 +00:00
@app.route('/annotation')
def gamepreanno():
return redirect(url_for('gameanno', id=request.remote_addr))
@app.route('/result', methods=['POST', 'GET'])
def gamepreplay():
if request.method == 'POST':
result = request.form
ip = request.remote_addr
# Save the result to questionare
2022-11-03 14:52:38 +00:00
idm.write_csv(questionarePath,
[ip, result.get("gamestyle"), result.get("frequency"), result.get("age"), result.get("gender"),
""])
2022-11-09 14:09:00 +00:00
idm.setControl(ip, result.get("control"))
2022-11-03 09:07:12 +00:00
print(result.get("gamestyle"))
2022-11-10 13:54:44 +00:00
return redirect(url_for('gametutorial', id=ip))
2022-11-03 09:07:12 +00:00
2022-11-08 09:22:21 +00:00
2022-11-03 14:52:38 +00:00
@app.route('/again')
def gamepreplayAgain():
return redirect(url_for('gameplay', id=request.remote_addr))
2022-11-03 09:07:12 +00:00
2022-11-08 09:22:21 +00:00
2022-11-03 09:07:12 +00:00
@app.route('/')
def gamequestion():
return render_template('GameQuestion.html')
2022-11-10 13:54:44 +00:00
@app.route('/gametutorial/<id>')
def gametutorial(id):
return render_template('GameTutorial.html', tutorial=idm.addTutorial(id), next=idm.hasNextTutorial(id))
@app.route('/gametutorial/<id>/data')
def gametutorialdata(id):
return redirect(url_for('gameplay', id=id))
2022-11-03 09:07:12 +00:00
@app.route('/gameplay/<id>')
def gameplay(id):
2022-11-03 12:03:04 +00:00
gamelevels = idm.getLevels(id)
2022-11-09 14:09:00 +00:00
return render_template('GamePlay.html', gamelevels=gamelevels, control = idm.getControl(id))
2022-10-21 09:46:57 +00:00
2022-11-03 09:07:12 +00:00
@app.route('/gameplay/<id>/data', methods=['POST'])
def getJSONData(id):
2022-10-24 14:17:55 +00:00
if request.method == 'POST':
2022-10-26 03:49:14 +00:00
print("POST Game")
2022-11-03 15:16:45 +00:00
resultList = list(request.form)[0].split(",")
2022-11-10 11:32:50 +00:00
print(resultList)
saveFile(replayDataPath, id + resultList[0][:-2], resultList[1:])
2022-11-10 12:12:06 +00:00
return "return!"
2022-10-24 14:17:55 +00:00
2022-11-03 09:07:12 +00:00
2022-11-09 14:09:00 +00:00
@app.route('/annotation/<id>')
def gameanno(id):
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)
2022-11-03 15:56:04 +00:00
@app.route('/annotation/radioresult', methods=['POST'])
2022-11-03 14:52:38 +00:00
def getRadioData():
2022-11-10 12:12:06 +00:00
ip = request.remote_addr
2022-10-21 09:46:57 +00:00
if request.method == 'POST':
2022-10-26 03:49:14 +00:00
print("POST Eval")
2022-11-03 14:07:08 +00:00
result = request.form
2022-11-03 14:52:38 +00:00
print(result)
ipRecent = idm.getRecent(ip)
2022-11-08 09:22:21 +00:00
idm.write_csv(annotationPath, [ip, ipRecent[0], ipRecent[1], result["fun"]])
2022-11-10 12:12:06 +00:00
idm.addTimes(ip)
2022-11-03 09:07:12 +00:00
# saveFile(evalDataPath,"gameanno",request.json[0]+request.json[1]+request.json[2])
2022-11-10 12:12:06 +00:00
finish = idm.getTimes(ip)
return render_template("GameOver.html",finish = finish)
2022-10-26 03:49:14 +00:00
2022-11-03 09:07:12 +00:00
2022-11-09 14:09:00 +00:00
2022-11-03 09:07:12 +00:00
def saveFile(path, filename, content):
2022-11-08 09:22:21 +00:00
cp = list(map(int, content))
2022-10-26 03:49:14 +00:00
file_dir = os.path.join(os.getcwd(), path)
2022-11-08 09:22:21 +00:00
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))
2022-11-03 14:07:08 +00:00
2022-11-03 09:07:12 +00:00
2022-10-20 14:48:14 +00:00
if __name__ == '__main__':
2022-10-21 09:46:57 +00:00
app.debug = True
app.run()
app.run(debug=True)