MarioWeb/main.py

185 lines
5.3 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-12-09 13:41:03 +00:00
annotationPath2 = "data/annotation2.csv"
2022-11-03 09:07:12 +00:00
2022-11-24 06:49:02 +00:00
@app.route('/')
2022-12-09 13:41:03 +00:00
def gamewelcome():
return render_template('GameWelcome.html')
@app.route('/question')
2022-11-24 06:49:02 +00:00
def gamequestion():
return render_template('GameQuestion.html')
2022-11-03 09:07:12 +00:00
@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,
2022-12-09 13:41:03 +00:00
[ip,result.get("playeds"),result.get("playedp"), result.get("gamestyle"), result.get("frequency"), result.get("age"), result.get("gender"),
2022-11-03 14:52:38 +00:00
""])
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-12-09 13:41:03 +00:00
return redirect(url_for('gametutorial', id=ip))
# debug use:
2022-12-09 13:41:03 +00:00
#return redirect(url_for('gameplay2', id=ip))
2022-11-03 09:07:12 +00:00
2022-11-08 09:22:21 +00:00
2022-11-24 06:49:02 +00:00
@app.route('/gametutorial/<id>')
def gametutorial(id):
2022-12-09 13:41:03 +00:00
return render_template('GameTutorial.html', tutorial=idm.addTutorial(id), next=idm.hasNextTutorial(id),control=idm.getControl(id))
2022-11-24 06:49:02 +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-10 13:54:44 +00:00
@app.route('/gametutorial/<id>/data')
def gametutorialdata(id):
return redirect(url_for('gameplay', id=id))
2022-11-03 09:07:12 +00:00
2022-11-24 06:49:02 +00:00
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)
return render_template('GamePlay.html', gamelevels=gamelevels, control=idm.getControl(id), levelNum=2,
jump="/annotation")
2022-11-24 06:49:02 +00:00
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-24 06:49:02 +00:00
@app.route('/annotation')
def gamepreanno():
return redirect(url_for('gameanno', id=request.remote_addr))
2022-11-09 14:09:00 +00:00
@app.route('/annotation/<id>')
def gameanno(id):
if (id != "radioresult"):
2022-11-24 06:49:02 +00:00
print("anno " + id)
gamelevels = idm.getRecent(id)
2022-12-09 13:41:03 +00:00
2022-11-24 06:49:02 +00:00
level1 = "lvl" + str(gamelevels[0])
level2 = "lvl" + str(gamelevels[1])
return render_template('GameAnnotation.html', level1=level1, level2=level2)
2022-11-09 14:09:00 +00:00
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
2022-10-26 03:49:14 +00:00
2022-12-09 13:41:03 +00:00
return redirect(url_for('over', stage=1))
2022-11-03 09:07:12 +00:00
2022-12-09 13:41:03 +00:00
@app.route('/gameplay2')
def gamepreplay2():
return redirect(url_for('gameplay2', id=request.remote_addr))
2022-11-09 14:09:00 +00:00
2022-11-24 07:44:32 +00:00
@app.route('/gameplay2/<id>')
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/<id>/data', methods=['POST'])
def getJSONData2(id):
if request.method == 'POST':
print("POST Game")
resultList = list(request.form)[0].split(",")
print(resultList)
saveFile(replayDataPath, id + resultList[0][:-2], resultList[1:])
return "return!"
2022-11-24 06:49:02 +00:00
@app.route('/annotation2')
def gamepreanno2():
2022-12-09 13:41:03 +00:00
return redirect(url_for('gameanno2', id=request.remote_addr))
2022-11-24 06:49:02 +00:00
@app.route('/annotation2/<id>')
def gameanno2(id):
if id != "result":
print("anno " + id)
2022-12-09 13:41:03 +00:00
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/<id>/result', methods=['POST'])
def gameannoresult2(id):
2022-11-24 06:49:02 +00:00
if request.method == 'POST':
print("result! " + id)
resultList = list(request.form)[0].split(",")
2022-12-09 13:41:03 +00:00
levelList = idm.getRecent(request.remote_addr)
print(resultList)
2022-11-24 06:49:02 +00:00
2022-12-09 13:41:03 +00:00
idm.write_csv(annotationPath2,
[request.remote_addr, resultList[0], resultList[1], resultList[2], levelList[0], levelList[1], levelList[2],
""])
return redirect(url_for('over', stage=2))
@app.route("/gameover/<stage>")
def over(stage):
finish = idm.getTimes(request.remote_addr)
print("finish %d",finish)
if finish:
idm.setTimes(request.remote_addr)
else:
idm.addTimes(request.remote_addr)
return render_template("GameOver.html", finish=finish, stage=stage)
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__':
app.run(host='0.0.0.0', port=80, debug=False)
# app.run()