MarioWeb/main.py

233 lines
6.7 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
2023-03-08 07:50:05 +00:00
import uuid
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
2023-03-08 07:50:05 +00:00
from flask import Flask, render_template, request, redirect, url_for, session
2023-03-16 04:07:00 +00:00
import logging
# 设置logging模块
logging.basicConfig(filename='log.txt', level=logging.DEBUG)
# 将print输出重定向到logging模块
print = logging.getLogger().info
2022-10-20 14:48:14 +00:00
2022-10-21 09:46:57 +00:00
app = Flask(__name__, static_folder='')
2022-11-03 12:03:04 +00:00
idm = idManager()
2023-03-28 06:03:23 +00:00
app.secret_key = 'asdfasdfawefaewvaf'
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"
2023-04-03 06:53:14 +00:00
feedbackPath = "data/feedback.csv"
2022-11-03 09:07:12 +00:00
2023-04-03 06:53:14 +00:00
# id=idm.getId(request.remote_addr)
2023-03-08 07:50:05 +00:00
def getId():
if 'name' not in session:
session['name'] = str(uuid.uuid4())
return session['name']
2022-11-03 09:07:12 +00:00
2023-04-03 06:53:14 +00:00
2022-11-24 06:49:02 +00:00
@app.route('/')
2022-12-09 13:41:03 +00:00
def gamewelcome():
2023-03-28 06:03:23 +00:00
ip = getId()
2023-02-28 07:13:42 +00:00
# return redirect(url_for('gameplay', id=request.remote_addr))
2023-03-28 06:03:23 +00:00
return render_template('GameWelcome.html')
2023-02-28 07:13:42 +00:00
2023-03-28 06:39:20 +00:00
2022-12-09 13:41:03 +00:00
@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('/privacy')
def privacypage():
return render_template('Privacy.html')
2022-11-03 09:07:12 +00:00
@app.route('/result', methods=['POST', 'GET'])
def gamepreplay():
if request.method == 'POST':
result = request.form
2023-03-08 07:50:05 +00:00
# ip = request.remote_addr
ip = getId()
2023-03-01 13:05:56 +00:00
cid = idm.iniId(ip)
2022-11-03 09:07:12 +00:00
# Save the result to questionare
2022-11-03 14:52:38 +00:00
idm.write_csv(questionarePath,
2023-03-01 13:05:56 +00:00
[cid,
result.get("playeds"),
result.get("playedp"),
result.get("gamestyle"),
result.get("frequency"),
2023-04-03 06:53:14 +00:00
result.get("age") + result.get("myAge"),
result.get("gender") + result.get("myGender"),
2022-11-03 14:52:38 +00:00
""])
2023-03-01 13:05:56 +00:00
idm.setControl(cid, result.get("control"))
2022-11-03 09:07:12 +00:00
print(result.get("gamestyle"))
2023-03-07 11:26:21 +00:00
return redirect(url_for('gametutorial', id=cid))
# debug use:
2023-03-01 13:05:56 +00:00
# return redirect(url_for('gameanno2', id=cid))
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):
2023-04-03 06:53:14 +00:00
return render_template('GameTutorial.html', tutorial=idm.addTutorial(id), next=idm.hasNextTutorial(id),
maxT=idm.tutorialMax,
2023-02-28 07:13:42 +00:00
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():
2023-03-08 08:06:08 +00:00
return redirect(url_for('gameplay', id=getId()))
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,
2023-04-03 06:53:14 +00:00
times=idm.getTimes(id),
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(",")
2023-04-03 06:53:14 +00:00
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():
2023-03-08 08:06:08 +00:00
return redirect(url_for('gameanno', id=getId()))
2022-11-24 06:49:02 +00:00
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():
2023-03-08 07:55:30 +00:00
ip = getId()
2022-11-10 12:12:06 +00:00
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
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
2023-02-07 19:38:06 +00:00
if idm.getTimes(ip):
2023-02-28 07:13:42 +00:00
return redirect(url_for("gameplay2", id=ip))
2023-02-07 19:38:06 +00:00
else:
idm.addTimes(ip)
2023-02-28 07:13:42 +00:00
return redirect(url_for("gameplay", id=ip))
2022-11-03 09:07:12 +00:00
2022-12-09 13:41:03 +00:00
@app.route('/gameplay2')
def gamepreplay2():
2023-03-08 08:06:08 +00:00
return redirect(url_for('gameplay2', id=getId()))
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(",")
2023-04-03 06:53:14 +00:00
saveFile(replayDataPath, id+"_" + resultList[0][:-2], resultList[1:])
return "return!"
2022-11-24 06:49:02 +00:00
@app.route('/annotation2')
def gamepreanno2():
2023-03-08 08:06:08 +00:00
return redirect(url_for('gameanno2', id=getId()))
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)
2023-02-28 07:13:42 +00:00
# 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(",")
2023-03-08 07:55:30 +00:00
levelList = idm.getRecent(getId())
2022-12-09 13:41:03 +00:00
idm.write_csv(annotationPath2,
2023-03-08 07:55:30 +00:00
[getId(), resultList[0], resultList[1], resultList[2], levelList[0], levelList[1],
2023-02-28 07:13:42 +00:00
levelList[2],
2022-12-09 13:41:03 +00:00
""])
2023-02-07 19:38:06 +00:00
if idm.getTimes(id):
return redirect(url_for("over", id=id))
else:
idm.addTimes(id)
return redirect(url_for("gameplay2", id=id))
2023-04-04 01:27:51 +00:00
@app.route("/gameover",methods=['POST','GET'])
2023-02-07 19:38:06 +00:00
def over():
2023-03-08 07:55:30 +00:00
finish = idm.getTimes(getId())
2023-04-03 06:53:14 +00:00
if request.method == 'POST':
resultList = list(request.form)[0].split(",")
idm.write_csv(feedbackPath,
[getId(), resultList[0],
""])
2023-04-04 01:27:51 +00:00
return render_template("GameOver.html", finish=1, stage=1)
# @app.route('/feedback', methods=['POST'])
# def overa():
# if request.method == 'POST':
# resultList = list(request.form)[0].split(",")
# idm.write_csv(feedbackPath,
# [getId(), resultList[0],
# ""])
2023-04-03 06:53:14 +00:00
2023-04-04 01:27:51 +00:00
# return redirect(url_for("over", id=id))
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()