task assignment

This commit is contained in:
2025-05-06 22:35:09 +08:00
parent 1982894b79
commit 44c1b3cf9b
8 changed files with 17862 additions and 0 deletions

146
task_assignment/app.py Normal file
View File

@ -0,0 +1,146 @@
from flask import Flask, render_template, request, redirect, url_for
import random
from json import loads,dumps
from pathlib import Path
from os import remove
from enum import Enum
class Status(Enum):
NoConfig=0
Assign=1
Assigned=2
app = Flask(__name__)
app.secret_key = "supersecretkey"
# 全局存储任务配置和分配结果
# task_config = {}
# user_intentions = {}
# totTas=0
# totInt=0
# assignment_result = {}
# status = Status.NoConfig
cfgFile=Path("config.json")
if cfgFile.exists():
cfg:dict=loads(cfgFile.read_text())
else:
cfg=dict()
task_config:dict=cfg.get("task_config",{})
user_intentions:dict = cfg.get("user_intentions",{})
assignment_result:dict = cfg.get("assignment_result",{})
totTas = sum(task_config.values())
totInt = sum(map(len,user_intentions.values()))
status=Status(cfg.get("status",0))
def save():
cfg={
"task_config":task_config,
"user_intentions":user_intentions,
"status":status.value
}
cfgFile.write_text(dumps(cfg))
@app.route("/config", methods=["GET", "POST"])
def config():
global task_config,user_intentions,totTas,status
if request.method == "POST":
# 从表单获取任务和人数配置
tasks = request.form.getlist("task")
counts = request.form.getlist("count")
task_config = {task: int(count) for task, count in zip(tasks, counts)}
user_intentions = {task: [] for task in tasks}
totTas=sum(task_config.values())
status = Status.Assign
save()
return redirect(url_for("user_page"))
if status==Status.NoConfig:
return render_template("config.html")
elif status==Status.Assign:
task_status = []
for task, max_count in task_config.items():
current_count = len(user_intentions[task])
status_color = "red" if current_count > max_count else "green" if current_count < max_count else "white"
task_status.append({
"task": task,
"max_count": max_count,
"current_count": current_count,
"status_color": status_color,
"users": user_intentions[task]
})
return render_template("config_view.html", task_status=task_status)
else:
return redirect(url_for("result"))
@app.route("/")
def index():
if status == Status.Assigned:
return redirect(url_for("result"))
elif status == Status.Assign:
return redirect(url_for("user_page"))
else:
return error("未配置","任务信息未配置,请联系管理员")
def error(title,content):
return render_template("error.html", title=title, content=content)
@app.route("/user", methods=["GET", "POST"])
def user_page():
global user_intentions,totInt
if status != Status.Assign:
return redirect(url_for("index"))
if request.method == "POST":
# 用户提交意向
name = request.form["name"]
intention = request.form["intention"]
user_intentions[intention].append(name)
totInt+=1
save()
if totInt==totTas:
assign()
return redirect(url_for("result"))
return render_template("user.html", tasks=task_config,init=list(task_config.values())[0])
# @app.route("/assign", methods=["GET"])
def assign():
global assignment_result,status
# 任务总人数计算
task_slots = {task: [] for task in task_config}
# 分配意向
unassigned_users = []
notFull = {}
for task,maxcnt in task_config.items():
cnt=len(user_intentions[task])
if cnt>maxcnt:
random.shuffle(user_intentions[task])
unassigned_users.extend(user_intentions[task][:(maxcnt-cnt)])
task_slots[task]=user_intentions[task][(maxcnt-cnt):]
else:
task_slots[task]=user_intentions[task]
if cnt<maxcnt: notFull.update({task:maxcnt-cnt})
offset=0
for task,need in notFull.items():
task_slots[task].extend(unassigned_users[offset:(need+offset)])
offset+=need
assignment_result = task_slots
status=Status.Assigned
remove(cfgFile)
@app.route("/result", methods=["GET"])
def result():
if status==Status.NoConfig:
return error("无法查看结果","分配尚未开始,请联系管理员。")
elif status==Status.Assign:
return error("无法查看结果","分配尚未结束,请等待所有人选择后查看。")
return render_template("result.html", assignment_result=assignment_result)
if __name__ == "__main__":
app.run(debug=True)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,126 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>配置任务</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
color: #333;
margin-top: 20px;
}
form {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
#tasks div {
margin-bottom: 10px;
display: flex;
align-items: center;
gap: 10px;
}
label {
font-weight: bold;
flex: 1;
}
input[type="text"], input[type="number"] {
flex: 2;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
button {
display: inline-block;
padding: 10px 20px;
margin: 10px 5px 0;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
color: #fff;
}
button[type="button"] {
background-color: #28a745;
}
button.delete {
background-color: #dc3545;
}
button.clear {
background-color: #ffc107;
}
button[type="submit"] {
background-color: #007bff;
}
button:hover {
opacity: 0.9;
}
</style>
</head>
<body>
<h1>配置任务</h1>
<form method="POST">
<div id="tasks">
<div>
<label>任务:</label>
<input type="text" name="task" required>
<label>人数:</label>
<input type="number" name="count" min="1" required>
<button type="button" class="delete" onclick="deleteTask(this)">删除</button>
</div>
</div>
<div style="text-align: center;">
<button type="button" onclick="addTask()">添加任务</button>
<button type="button" class="clear" onclick="clearTasks()">清空任务</button>
<button type="submit">提交</button>
</div>
</form>
<script>
// 添加新任务行
function addTask() {
const tasksDiv = document.getElementById("tasks");
const newTaskDiv = document.createElement("div");
newTaskDiv.innerHTML = `
<label>任务:</label>
<input type="text" name="task" required>
<label>人数:</label>
<input type="number" name="count" min="1" required>
<button type="button" class="delete" onclick="deleteTask(this)">删除</button>
`;
tasksDiv.appendChild(newTaskDiv);
}
// 删除单行任务
function deleteTask(button) {
const taskDiv = button.parentElement;
taskDiv.remove();
}
// 清空所有任务
function clearTasks() {
const tasksDiv = document.getElementById("tasks");
tasksDiv.innerHTML = `
<div>
<label>任务:</label>
<input type="text" name="task" required>
<label>人数:</label>
<input type="number" name="count" min="1" required>
<button type="button" class="delete" onclick="deleteTask(this)">删除</button>
</div>
`;
}
</script>
</body>
</html>

View File

@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>任务意向与人数配置</title>
<style>
table {
width: 80%;
margin: 20px auto;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
text-align: center;
padding: 10px;
}
th {
background-color: #f4f4f4;
}
.red {
background-color: #ffcccc;
}
.green {
background-color: #ccffcc;
}
</style>
</head>
<body>
<h1 style="text-align: center;">任务意向与人数配置</h1>
<table>
<thead>
<tr>
<th>任务</th>
<th>最大人数</th>
<th>当前人数</th>
<th>意向人列表</th>
</tr>
</thead>
<tbody>
{% for task in task_status %}
<tr class="{{ task.status_color }}">
<td>{{ task.task }}</td>
<td>{{ task.max_count }}</td>
<td>{{ task.current_count }}</td>
<td>
{% for user in task.users %}
{{ user }}{% if not loop.last %}, {% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<style>
body {
background-color: #f8f9fa;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.modal-dialog {
max-width: 600px;
margin: auto;
border-radius: 15px;
overflow: hidden;
}
.modal-content {
border-radius: 15px;
padding: 20px;
background-color: #ffffff;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.modal-header {
background-color: #dc3545;
color: white;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
text-align: center;
}
.modal-title {
margin: 0 auto;
font-size: 1.5rem;
font-weight: bold;
}
.modal-body {
padding: 20px 30px;
font-size: 1.25rem;
color: #495057;
text-align: center;
line-height: 1.8;
}
</style>
</head>
<body>
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h5 class="modal-title w-100">{{ title }}</h5>
</div>
<!-- Modal Body -->
<div class="modal-body">
{{ content }}
</div>
</div>
</div>
<script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
</body>
</html>

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分配结果</title>
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center mb-4">任务分配结果</h1>
<div class="card shadow p-4">
<table class="table table-bordered table-striped">
<thead class="table-dark">
<tr>
<th scope="col">任务</th>
<th scope="col">分配人员</th>
</tr>
</thead>
<tbody>
{% for task, users in assignment_result.items() %}
<tr>
<td>{{ task }}</td>
<td>{{ users | join(', ') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
</body>
</html>

View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户意向</title>
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center mb-4">提交任务意向</h1>
<form method="POST" class="card shadow p-4">
<div class="mb-3">
<label for="name" class="form-label">姓名</label>
<input type="text" name="name" id="name" class="form-control" placeholder="请输入您的姓名" required>
</div>
<div class="mb-3">
<label for="intention" class="form-label">任务意向</label>
<select name="intention" id="intention" class="form-select" required onchange="updateTaskInfo()">
{% for task, count in tasks.items() %}
<option value="{{ task }}" data-max="{{ count }}">{{ task }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<p id="task-info" class="text-muted">任务人数:{{ init }}</p>
</div>
<button type="submit" class="btn btn-primary w-100">提交</button>
</form>
</div>
<script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
<script>
// 动态更新任务信息
function updateTaskInfo() {
const taskSelect = document.getElementById("intention");
const selectedOption = taskSelect.options[taskSelect.selectedIndex];
const maxCount = selectedOption.getAttribute("data-max");
const taskInfo = document.getElementById("task-info");
if (maxCount) {
taskInfo.textContent = `任务人数:${maxCount}`;
} else {
taskInfo.textContent = "请选择一个任务以查看最多人数。";
}
}
</script>
</body>
</html>