Newer
Older
Skillsemi2023_WEB_Otaki_Nemoto / flask / testapp / views.py
@daiga nemoto daiga nemoto on 14 Feb 2024 1 KB Merge branch 'webserve'
from flask import render_template, request
from testapp import app
from collections import Counter

# ユーザーごとの点数を格納する辞書
user_score = {}

# 各データの保存
data = []

# playerの数
players = 50


@app.route("/form", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("testapp/index.html")
    if request.method == "POST":

        # POSTデータを受け取る
        data.append(request.get_data(as_text=True))  # バイナリデータではなくテキストとして取得する
        # 全員分のデータが集まった時
        if len(data) >= players:
            # リスト内の要素の出現回数をカウントする
            counter = Counter(data)
            # 最も多く出現する要素を取得する
            most_common_element = counter.most_common(1)[0][0]
            return most_common_element

        # ユーザーごとのデータを取得または作成
        if request.form.get("username"):
            username = request.form.get("username")
            user_data[username] = []

        # クライアントから送信されたデータを取得
        data = request.form.get("data")
        # ユーザーごとのデータに追加
        user_data[username].append(data)
        data_count += 1
        # 受け取ったデータを印刷する
        print("Received data:", user_data[username])

        # 必要な処理を行う(例:データベースへの書き込みなど)
        return "Data received successfully"