From fc15b98a514ee242b45e971d0f9b65fc4a5bafaf Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Sat, 5 Oct 2024 16:40:29 +0800 Subject: [PATCH 01/17] Initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f0fb02 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# double_work + From 8490a23cbb131e595b448a1cff5b5637bdcc5f52 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:22:34 +0800 Subject: [PATCH 02/17] ADD file via upload --- classManager.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 classManager.js diff --git a/classManager.js b/classManager.js new file mode 100644 index 0000000..1a3ef65 --- /dev/null +++ b/classManager.js @@ -0,0 +1,48 @@ +const fs = require('fs'); +const path = require('path'); +const xlsx = require('xlsx'); +const pool = require('../db'); + +const classManager = { + addClass: (req, res) => { + const filePath = req.file.path; + + // 读取并解析Excel文件 + const workbook = xlsx.readFile(filePath); + const sheetName = workbook.SheetNames[0]; + const worksheet = workbook.Sheets[sheetName]; + + const students = xlsx.utils.sheet_to_json(worksheet); + + // 定义所需的字段 + const requiredFields = [ 'student_id', 'student_name']; + let hasError = false; + + students.forEach(student => { + // 数据验证 + const missingFields = requiredFields.filter(field =>!(field in student)); + if (missingFields.length > 0) { + console.error(`Student data is missing fields: ${missingFields.join(', ')}`); + hasError = true; + return; + } + + // 将学生数据插入数据库 + const query = 'INSERT INTO students (student_id, student_name, score, call_count) VALUES (?,?, 0, 0)'; + pool.query(query, [student.student_id, student.student_name], (error) => { + if (error) { + console.error(`Error inserting student: ${student.name}`, error); + hasError = true; + } + }); + }); + + if (hasError) { + res.status(500).json({ message: 'Error adding class. Some students were not added successfully.' }); + } else { + res.status(200).json({ message: 'Class added successfully!' }); + } + } +}; + +module.exports = classManager; From b905d93ba45a58ab707de3c9c260e425eb514a47 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:25:08 +0800 Subject: [PATCH 03/17] ADD file via upload --- randomSelect.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 randomSelect.js diff --git a/randomSelect.js b/randomSelect.js new file mode 100644 index 0000000..ffda649 --- /dev/null +++ b/randomSelect.js @@ -0,0 +1,45 @@ +const pool = require('../db'); + +async function selectStudent(req, res) { + try { + const connection = await pool.getConnection(); + + // 从数据库查询学生 + const [students] = await connection.query('SELECT * FROM students'); + + // 计算权重的总和(使用 softmax 权重计算) + const totalWeight = students.reduce((acc, student) => acc + Math.exp(-student.score), 0); + + // 将每个学生的权重归一化 + const weightedStudents = students.map(student => ({ + student, + weight: Math.exp(-student.score) / totalWeight, + })); + + // 生成随机数 + const random = Math.random(); // 介于 0 和 1 之间 + let sum = 0; // 用于累加权重 + let selectedStudent = null; + + // 遍历加权后的学生,累加权重,并判断随机数落在哪个学生的区间 + for (let i = 0; i < weightedStudents.length; i++) { + sum += weightedStudents[i].weight; // 累加当前学生的权重 + if (random <= sum) { // 如果随机数小于或等于当前的累积权重 + selectedStudent = weightedStudents[i].student; // 选中该学生 + break; // 找到后立即退出循环 + } + } + + if (selectedStudent) { + res.send({ student: selectedStudent }); + } else { + res.status(404).send({ message: '无学生数据' }); + } + + connection.release(); + } catch (error) { + res.status(500).send({ error: '随机选择学生失败' }); + } +} + +module.exports = { selectStudent }; From f163d70d782eee48875bf1219f5a88eb171f3163 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:25:19 +0800 Subject: [PATCH 04/17] ADD file via upload --- ranking.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ranking.js diff --git a/ranking.js b/ranking.js new file mode 100644 index 0000000..1b2a6b5 --- /dev/null +++ b/ranking.js @@ -0,0 +1,14 @@ +const pool = require('../db'); + +async function getRanking(req, res) { + try { + const connection = await pool.getConnection(); + const [ranking] = await connection.query('SELECT student_name, student_id, score, call_count FROM students ORDER BY score DESC'); + connection.release(); + res.send({ ranking }); + } catch (error) { + res.status(500).send({ error: '无法获取排名' }); + } +} + +module.exports = { getRanking }; From 74ab09815fc569353121e612e9f29afe295cb497 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:25:27 +0800 Subject: [PATCH 05/17] ADD file via upload --- scoreManager.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 scoreManager.js diff --git a/scoreManager.js b/scoreManager.js new file mode 100644 index 0000000..5aebd1c --- /dev/null +++ b/scoreManager.js @@ -0,0 +1,22 @@ +const pool = require('../db'); + +async function updateScore(req, res) { + const { student_id, points } = req.body; //假设前端发送的数据是这两部分 + + try { + const connection = await pool.getConnection(); + const [result] = await connection.query('UPDATE students SET score = score + ?, call_count = call_count + 1 WHERE student_id = ?', [points, student_id]); + + if (result.affectedRows > 0) { + res.send({ message: '积分已更新' }); + } else { + res.status(404).send({ message: '学生不存在' }); + } + + connection.release(); + } catch (error) { + res.status(500).send({ error: '更新积分失败' }); + } +} + +module.exports = { updateScore }; From 815cdd3052caa5071c7fdaa7541d5695595b659d Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:28:03 +0800 Subject: [PATCH 06/17] ADD file via upload --- app.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..0d25f71 --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +const express = require('express'); +const app = express(); +const router = require('./router'); +const bodyParser = require('body-parser'); + +// 中间件 +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +// 使用路由 +app.use('/', router); + +// 监听端口 +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); +}); From 44ff1dbee6d835292f9593488cbefee488ae0e52 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:28:35 +0800 Subject: [PATCH 07/17] ADD file via upload --- db.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 db.js diff --git a/db.js b/db.js new file mode 100644 index 0000000..d7b62d8 --- /dev/null +++ b/db.js @@ -0,0 +1,24 @@ +const mysql = require('mysql2/promise'); + +// 创建连接池 +const pool = mysql.createPool({ + host: 'localhost', // 数据库地址 + user: 'root', // 数据库用户名 + password: '20041025', // 数据库密码 + database: 'class_k' ,// 使用的数据库 + waitForConnections: true, + connectionLimit: 10, + queueLimit: 0 +}); + +// 测试数据库连接 +pool.getConnection((err, connection) => { + if (err) { + console.error('Error connecting to the database:', err); + } else { + console.log('Database connected successfully!'); + connection.release(); // 释放连接 + } +}); + +module.exports = pool; From e5c6bd7da0d2330eaeeaf8095c0afe2b98d3efe6 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Sun, 6 Oct 2024 11:28:44 +0800 Subject: [PATCH 08/17] ADD file via upload --- router.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 router.js diff --git a/router.js b/router.js new file mode 100644 index 0000000..7ee77e8 --- /dev/null +++ b/router.js @@ -0,0 +1,24 @@ +const express = require('express'); +const router = express.Router(); +const multer = require('multer'); +const randomSelect = require('./services/randomSelect'); +const ranking = require('./services/ranking'); +const classManager = require('./services/classManager'); +const scoreManager = require('./services/scoreManager'); + +// 文件上传配置 +const upload = multer({ dest: 'uploads/' }); + +// 路由 - 添加班级 +router.post('/upload', upload.single('file'), classManager.addClass); + +// 路由 - 随机点名 +router.get('/random-call', randomSelect.selectStudent); + +// 路由 - 获取排名 +router.get('/ranking', ranking.getRanking); + +// 路由 - 更新分数 +router.post('/update-score', scoreManager.updateScore); + +module.exports = router; From a4377fb08006d080685490e37bad8b00fb72ecb6 Mon Sep 17 00:00:00 2001 From: pjmw9izve <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:17:48 +0800 Subject: [PATCH 09/17] ADD file via upload --- services | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 services diff --git a/services b/services new file mode 100644 index 0000000..e69de29 From 65d3c761e4d37760bf04f61f288b5868efeac87c Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:17:57 +0800 Subject: [PATCH 10/17] Delete 'services' --- services | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 services diff --git a/services b/services deleted file mode 100644 index e69de29..0000000 From 7065c8be8f1c1aa010028d4af1dcd8730f7fe618 Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:18:45 +0800 Subject: [PATCH 11/17] Delete 'app.js' --- app.js | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 app.js diff --git a/app.js b/app.js deleted file mode 100644 index 0d25f71..0000000 --- a/app.js +++ /dev/null @@ -1,17 +0,0 @@ -const express = require('express'); -const app = express(); -const router = require('./router'); -const bodyParser = require('body-parser'); - -// 中间件 -app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ extended: true })); - -// 使用路由 -app.use('/', router); - -// 监听端口 -const PORT = process.env.PORT || 3000; -app.listen(PORT, () => { - console.log(`Server running on port ${PORT}`); -}); From 8c4ec5d2ba6f7dd717943302dcd4380a9e1d235b Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:18:50 +0800 Subject: [PATCH 12/17] Delete 'classManager.js' --- classManager.js | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 classManager.js diff --git a/classManager.js b/classManager.js deleted file mode 100644 index 1a3ef65..0000000 --- a/classManager.js +++ /dev/null @@ -1,48 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const xlsx = require('xlsx'); -const pool = require('../db'); - -const classManager = { - addClass: (req, res) => { - const filePath = req.file.path; - - // 读取并解析Excel文件 - const workbook = xlsx.readFile(filePath); - const sheetName = workbook.SheetNames[0]; - const worksheet = workbook.Sheets[sheetName]; - - const students = xlsx.utils.sheet_to_json(worksheet); - - // 定义所需的字段 - const requiredFields = [ 'student_id', 'student_name']; - let hasError = false; - - students.forEach(student => { - // 数据验证 - const missingFields = requiredFields.filter(field =>!(field in student)); - if (missingFields.length > 0) { - console.error(`Student data is missing fields: ${missingFields.join(', ')}`); - hasError = true; - return; - } - - // 将学生数据插入数据库 - const query = 'INSERT INTO students (student_id, student_name, score, call_count) VALUES (?,?, 0, 0)'; - pool.query(query, [student.student_id, student.student_name], (error) => { - if (error) { - console.error(`Error inserting student: ${student.name}`, error); - hasError = true; - } - }); - }); - - if (hasError) { - res.status(500).json({ message: 'Error adding class. Some students were not added successfully.' }); - } else { - res.status(200).json({ message: 'Class added successfully!' }); - } - } -}; - -module.exports = classManager; From f87e1297cfc4fd66eb6c8ded929548bb7d01f228 Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:18:56 +0800 Subject: [PATCH 13/17] Delete 'db.js' --- db.js | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 db.js diff --git a/db.js b/db.js deleted file mode 100644 index d7b62d8..0000000 --- a/db.js +++ /dev/null @@ -1,24 +0,0 @@ -const mysql = require('mysql2/promise'); - -// 创建连接池 -const pool = mysql.createPool({ - host: 'localhost', // 数据库地址 - user: 'root', // 数据库用户名 - password: '20041025', // 数据库密码 - database: 'class_k' ,// 使用的数据库 - waitForConnections: true, - connectionLimit: 10, - queueLimit: 0 -}); - -// 测试数据库连接 -pool.getConnection((err, connection) => { - if (err) { - console.error('Error connecting to the database:', err); - } else { - console.log('Database connected successfully!'); - connection.release(); // 释放连接 - } -}); - -module.exports = pool; From 2a099505737df66bb517b39b1e35e1832669c738 Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:19:01 +0800 Subject: [PATCH 14/17] Delete 'randomSelect.js' --- randomSelect.js | 45 --------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 randomSelect.js diff --git a/randomSelect.js b/randomSelect.js deleted file mode 100644 index ffda649..0000000 --- a/randomSelect.js +++ /dev/null @@ -1,45 +0,0 @@ -const pool = require('../db'); - -async function selectStudent(req, res) { - try { - const connection = await pool.getConnection(); - - // 从数据库查询学生 - const [students] = await connection.query('SELECT * FROM students'); - - // 计算权重的总和(使用 softmax 权重计算) - const totalWeight = students.reduce((acc, student) => acc + Math.exp(-student.score), 0); - - // 将每个学生的权重归一化 - const weightedStudents = students.map(student => ({ - student, - weight: Math.exp(-student.score) / totalWeight, - })); - - // 生成随机数 - const random = Math.random(); // 介于 0 和 1 之间 - let sum = 0; // 用于累加权重 - let selectedStudent = null; - - // 遍历加权后的学生,累加权重,并判断随机数落在哪个学生的区间 - for (let i = 0; i < weightedStudents.length; i++) { - sum += weightedStudents[i].weight; // 累加当前学生的权重 - if (random <= sum) { // 如果随机数小于或等于当前的累积权重 - selectedStudent = weightedStudents[i].student; // 选中该学生 - break; // 找到后立即退出循环 - } - } - - if (selectedStudent) { - res.send({ student: selectedStudent }); - } else { - res.status(404).send({ message: '无学生数据' }); - } - - connection.release(); - } catch (error) { - res.status(500).send({ error: '随机选择学生失败' }); - } -} - -module.exports = { selectStudent }; From c373e4b62cdfc41c038fd97c24d7e12ac7aacfdc Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:19:06 +0800 Subject: [PATCH 15/17] Delete 'ranking.js' --- ranking.js | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 ranking.js diff --git a/ranking.js b/ranking.js deleted file mode 100644 index 1b2a6b5..0000000 --- a/ranking.js +++ /dev/null @@ -1,14 +0,0 @@ -const pool = require('../db'); - -async function getRanking(req, res) { - try { - const connection = await pool.getConnection(); - const [ranking] = await connection.query('SELECT student_name, student_id, score, call_count FROM students ORDER BY score DESC'); - connection.release(); - res.send({ ranking }); - } catch (error) { - res.status(500).send({ error: '无法获取排名' }); - } -} - -module.exports = { getRanking }; From c6e56bae5a9b7bdc841fab33e75dac894d901678 Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:19:10 +0800 Subject: [PATCH 16/17] Delete 'router.js' --- router.js | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 router.js diff --git a/router.js b/router.js deleted file mode 100644 index 7ee77e8..0000000 --- a/router.js +++ /dev/null @@ -1,24 +0,0 @@ -const express = require('express'); -const router = express.Router(); -const multer = require('multer'); -const randomSelect = require('./services/randomSelect'); -const ranking = require('./services/ranking'); -const classManager = require('./services/classManager'); -const scoreManager = require('./services/scoreManager'); - -// 文件上传配置 -const upload = multer({ dest: 'uploads/' }); - -// 路由 - 添加班级 -router.post('/upload', upload.single('file'), classManager.addClass); - -// 路由 - 随机点名 -router.get('/random-call', randomSelect.selectStudent); - -// 路由 - 获取排名 -router.get('/ranking', ranking.getRanking); - -// 路由 - 更新分数 -router.post('/update-score', scoreManager.updateScore); - -module.exports = router; From d278752fa822849aa74168f813818e48a8af9432 Mon Sep 17 00:00:00 2001 From: luoyonghuang <2308014474@qq.com> Date: Wed, 9 Oct 2024 11:19:16 +0800 Subject: [PATCH 17/17] Delete 'scoreManager.js' --- scoreManager.js | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 scoreManager.js diff --git a/scoreManager.js b/scoreManager.js deleted file mode 100644 index 5aebd1c..0000000 --- a/scoreManager.js +++ /dev/null @@ -1,22 +0,0 @@ -const pool = require('../db'); - -async function updateScore(req, res) { - const { student_id, points } = req.body; //假设前端发送的数据是这两部分 - - try { - const connection = await pool.getConnection(); - const [result] = await connection.query('UPDATE students SET score = score + ?, call_count = call_count + 1 WHERE student_id = ?', [points, student_id]); - - if (result.affectedRows > 0) { - res.send({ message: '积分已更新' }); - } else { - res.status(404).send({ message: '学生不存在' }); - } - - connection.release(); - } catch (error) { - res.status(500).send({ error: '更新积分失败' }); - } -} - -module.exports = { updateScore };