1 Star 1 Fork 0

ManiacRaTT/HospitalFrontDesk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
view_test.py 5.32 KB
一键复制 编辑 原始数据 按行查看 历史
ManiacRaTT 提交于 2023-06-21 16:45 . sql
import json
from datetime import datetime
from random import sample
import tornado.websocket
import ctrl_base
from database.Xmongodb import mongo, mongodb
from util.utils import generate_video_list
class test_index(ctrl_base.BaseHandler):
async def get(self):
current_type, current_username, current_user_id, find_0, find_0_0, find_0_1 = await self.get_base_config()
await self.render("test_index.html",find_0=find_0,current_type=current_type,
current_username=current_username, current_user_id=current_user_id,find_0_0=find_0_0,
find_0_1=find_0_1)
class test(ctrl_base.BaseHandler):
async def get(self):
current_type, current_username, current_user_id, find_0, find_0_0, find_0_1 = await self.get_base_config()
await self.render("test.html",find_0=find_0,current_type=current_type,
current_username=current_username, current_user_id=current_user_id,find_0_0=find_0_0,
find_0_1=find_0_1)
class test_websocket(tornado.websocket.WebSocketHandler):
testIn = 0
videoIn = 1
testSubmit = 2
users = []
users_video = {}
users_cache = {}
def createMessage(self, type, data):
message = {
"type": type,
"msg": data,
"create_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
return json.dumps(message)
def init_video_choice(self):
random_list = sample(self.users_video.get('{}'.format(self)), 2)
msg = {
"img_a": '/static/img/test_img/{}.png'.format(random_list[0]),
"img_b": '/static/img/test_img/{}.png'.format(random_list[1]),
"num_a": random_list[0],
"num_b": random_list[1]
}
self.users_cache.update(
{
'{}'.format(self):
{
'video_choice_A': random_list[0],
'video_choice_B': random_list[1],
'begin_time': datetime.now()
}
})
return msg
async def open(self, *args, **kwargs):
self.users.append(self)
user_video = generate_video_list()
self.users_video.update(
{
"{}".format(self):user_video
}
)
msg = self.init_video_choice()
await mongo.record.insert_one(
{
'user': str(self),
'all': user_video,
'create_time': datetime.now()
})
await self.write_message(self.createMessage(self.testIn,msg))
async def on_message(self, message):
message = json.loads(message)
if message.get('type') == 'choose':
video_label = message.get('ob')
self.users_video.get('{}'.format(self)).remove(int(video_label))
self.users_cache.get(str(self)).update(
{
'choose_time':(datetime.now()-self.users_cache.get(str(self)).get('begin_time')).microseconds/1000,
'choose_video':int(video_label)
})
self.users_cache.get(str(self)).pop('begin_time')
print(self.users_cache.get(str(self)))
print(len(self.users_video.get('{}'.format(self))))
video = '/static/video/test_video/{}.mp4'.format(int(video_label))
await self.write_message(self.createMessage(self.videoIn,video))
elif message.get('type') == 'submit':
await mongo.record.update_one(
{
'user': str(self)
},
{
"$push":
{
"history":
{
"round": message.get('round'),
"disgusted": message.get('disgusted'),
"amused": message.get('amused'),
"calm": message.get('calm'),
"anxious": message.get('anxious'),
"sad": message.get('sad'),
"choose_time": self.users_cache.get(str(self)).get('choose_time'),
"choose_video": self.users_cache.get(str(self)).get('choose_video'),
"video_choice_A": self.users_cache.get(str(self)).get('video_choice_A'),
"video_choice_B": self.users_cache.get(str(self)).get('video_choice_B')
}
}
})
if int(message.get('round')) < 40:
msg = self.init_video_choice()
await self.write_message(self.createMessage(self.testSubmit, msg))
else:
await self.write_message(self.createMessage(self.testSubmit, None))
def on_close(self):
record = mongodb.record.find_one(
{
'user': str(self),
})
if record.get('history') is None:
mongodb.record.delete_one(
{
'user': str(self),
})
del self.users_video[str(self)]
del self.users_cache[str(self)]
self.users.remove(self)
def check_origin(self, origin):
return True
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/maniacratt/HospitalFrontDesk.git
git@gitee.com:maniacratt/HospitalFrontDesk.git
maniacratt
HospitalFrontDesk
HospitalFrontDesk
master

搜索帮助