Skip to content
Snippets Groups Projects
Commit 9f668a3a authored by Xiemuqing Xiao's avatar Xiemuqing Xiao
Browse files

Merge branch...

Merge branch '73-as-a-user-i-want-ranking-can-be-update-by-match-so-that-i-can-see-the-ranking-for-real-time' into 'main'

Resolve "As a user, I want ranking can be update by match so that I can see the ranking for real time"

Closes #73

See merge request !57
parents 0633e0d4 b90aa373
No related branches found
No related tags found
1 merge request!57Resolve "As a user, I want ranking can be update by match so that I can see the ranking for real time"
Showing
with 96 additions and 43 deletions
......@@ -42,6 +42,11 @@ public class RankingController {
//rankingService.processCompletedMatches();
return rankingService.getRankingsBySportAndOrder(sport, order);
}
@PostMapping("/updatedata")
public void updateRankings() {
rankingService.processCompletedMatches();
}
......
......@@ -77,7 +77,7 @@ public class RankingRepository {
WHERE status = 'completed'
""";
return jdbcTemplate.query(sql, (rs, rowNum) -> new MatchResult(
rs.getLong("winnerId"),
rs.getLong("winner_id"),
rs.getString("sport")
));
......@@ -85,7 +85,7 @@ public class RankingRepository {
public List<RankingTable> findAllSortedByWins(String order) {
// 确保order参数的值是"asc"或"desc",如果不是则默认为"desc"
if (order == null || (!order.equalsIgnoreCase("asc") && !order.equalsIgnoreCase("desc"))) {
order = "asc"; // 默认降序
order = "desc"; // 默认降序
}
String sql = "SELECT * FROM ranking ORDER BY wins " + order.toUpperCase();
......@@ -99,7 +99,7 @@ public class RankingRepository {
sortOrder = "asc";
}
String sql = "SELECT user_id,username,'totalwins' AS sport, SUM(wins) AS wins FROM ranking GROUP BY user_id ORDER BY wins " + sortOrder;
String sql = "SELECT user_id,username,'all sports' AS sport, SUM(wins) AS wins FROM ranking GROUP BY user_id ORDER BY wins " + sortOrder;
return jdbcTemplate.query(sql, mapRowToTotalWinsDTO());
......@@ -123,7 +123,7 @@ public class RankingRepository {
TotalWinsDTO dto = new TotalWinsDTO();
dto.setUsername(rs.getString("username"));
dto.setUserId(rs.getLong("user_id"));
dto.setSport("totalwins"); // 聚合查询时 sport 固定为 "totalwins"
dto.setSport("all sports"); // 聚合查询时 sport 固定为 "allsports"
dto.setWins(rs.getInt("wins"));
return dto;
};
......
......@@ -35,6 +35,7 @@ public class RankingServiceImpl implements RankingService {
public void processCompletedMatches() {
List<RankingRepository.MatchResult> completedMatches = rankingRepository.findWinnersAndSportsFromCompletedMatches();
System.out.println(completedMatches);
// loop every match result
for (RankingRepository.MatchResult result : completedMatches) {
......
......@@ -9,34 +9,42 @@ INSERT INTO information (id, username, email, password, role)
VALUES (3, 'xzc', 'xzc@creditsafe.com', '$2a$16$R0aSGzbklUhpRfIMhocewephgUDMFOffLb7faSwwHusqHh81G026i', 'USER');
INSERT INTO information (id, username, email, password, role)
VALUES (4, 'xx', 'xx@creditsafe.com', '$2a$16$R0aSGzbklUhpRfIMhocewephgUDMFOffLb7faSwwHusqHh81G026i', 'USER');
# insert into match_item (sport, player_AId, player_BId, plan_Time, status, score_a, score_b, confirm_a, confirm_b, winner_id)
# values
# -- test data
# -- 1: Pending status
# ('Pools', 1, 2, '2024-11-25 14:30:00', 'pending', 0, 0, FALSE, FALSE, NULL),
# -- 2: Confirmed status, Player 3 wins
# ('Darts', 3, 4, '2024-11-26 16:00:00', 'confirmed', 3, 2, TRUE, TRUE, 3),
# -- 3: Completed status, Player 6 wins
# ('TableTennis', 1, 2, '2024-11-27 18:00:00', 'completed', 21, 18, TRUE, TRUE, 2),
# -- 4: Pending status
# ('Pools', 2, 1, '2024-11-28 20:00:00', 'pending', 0, 0, FALSE, FALSE, NULL),
# -- 5: Completed status, Player 9 wins
# ('Darts', 1, 2, '2024-11-29 15:30:00', 'completed', 300, 280, TRUE, TRUE, 2),
# -- 6: Confirmed status, Player 1 wins
# ('TableTennis', 1, 2, '2024-11-30 17:00:00', 'confirmed', 3, 1, TRUE, TRUE, 1),
# ('TableTennis', null, null, '2024-12-20 17:00:00', 'pending', 0, 0, false, false, null),
# ('TableTennis', null, null, '2024-12-21 17:00:00', 'pending', 0, 0, false, false, null),
# ('Pools', null, null, '2024-12-22 17:00:00', 'pending', 0, 0, false, false, null),
# ('Pools', null, null, '2024-12-23 17:00:00', 'pending', 0, 0, false, false, null),
# ('Pools', null, null, '2024-12-24 17:00:00', 'pending', 0, 0, false, false, null),
# ('Darts', null, null, '2024-12-25 17:00:00', 'pending', 0, 0, false, false, null),
# ('Darts', null, null, '2024-12-26 17:00:00', 'pending', 0, 0, false, false, null),
# ('Darts', null, null, '2024-12-27 17:00:00', 'pending', 0, 0, false, false, null);
insert into match_item (sport, player_AId, player_BId, plan_Time, status, score_a, score_b, confirm_a, confirm_b, winner_id)
values
-- test data
-- 1: Pending status
('Pools', 1, 2, '2024-11-25 14:30:00', 'pending', 0, 0, FALSE, FALSE, NULL),
-- 2: Confirmed status, Player 3 wins
('Darts', 3, 4, '2024-11-26 16:00:00', 'confirmed', 3, 2, TRUE, TRUE, 3),
-- 3: Completed status, Player 6 wins
('TableTennis', 1, 2, '2024-11-27 18:00:00', 'completed', 21, 18, TRUE, TRUE, 2),
-- 4: Pending status
('Pools', 2, 1, '2024-11-28 20:00:00', 'pending', 0, 0, FALSE, FALSE, NULL),
-- 5: Completed status, Player 9 wins
('Darts', 1, 2, '2024-11-29 15:30:00', 'completed', 300, 280, TRUE, TRUE, 2),
-- 6: Confirmed status, Player 1 wins
('TableTennis', 1, 2, '2024-11-30 17:00:00', 'confirmed', 3, 1, TRUE, TRUE, 1),
('TableTennis', null, null, '2024-12-20 17:00:00', 'pending', 0, 0, false, false, null),
('TableTennis', null, null, '2024-12-21 17:00:00', 'pending', 0, 0, false, false, null),
('Pools', null, null, '2024-12-22 17:00:00', 'pending', 0, 0, false, false, null),
('Pools', null, null, '2024-12-23 17:00:00', 'pending', 0, 0, false, false, null),
('Pools', null, null, '2024-12-24 17:00:00', 'pending', 0, 0, false, false, null),
('Darts', null, null, '2024-12-25 17:00:00', 'pending', 0, 0, false, false, null),
('Darts', null, null, '2024-12-26 17:00:00', 'pending', 0, 0, false, false, null),
('Darts', null, null, '2024-12-27 17:00:00', 'pending', 0, 0, false, false, null);
INSERT INTO ranking (user_id,username,sport, wins) VALUES
(1, 'shy','Pools', 5),
(2, 'zyb','Darts', 3),
(3, 'xzc','TableTennis', 10),
(2, 'xx','TableTennis', 4);
(1, 'shy','Pools', 0),
(1, 'shy','Darts', 0),
(1, 'shy','TableTennis', 0),
(2, 'zyb','Pools', 0),
(2, 'zyb','Darts', 0),
(2, 'zyb','TableTennis', 0),
(3, 'xzc','Pools', 0),
(3, 'xzc','Darts', 0),
(3, 'xzc','TableTennis', 0),
(4, 'xx','Pools', 0),
(4, 'xx','Darts', 0),
(4, 'xx','TableTennis', 0);
INSERT INTO userdetails(id, email, password, username, role) VALUES
......
drop table if exists match_item;
drop table if exists rankings;
drop table if exists ranking;
drop table if exists information;
drop table if exists images;
CREATE TABLE information (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
......@@ -32,17 +33,13 @@ create table if not exists match_item (
);
drop table if exists ranking;
CREATE TABLE IF NOT EXISTS ranking (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
sport ENUM('Pools', 'Darts', 'TableTennis') NOT NULL,
wins INT DEFAULT 0,
username VARCHAR(255)DEFAULT NULL
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
sport ENUM('Pools', 'Darts', 'TableTennis') NOT NULL,
wins INT DEFAULT 0,
username VARCHAR(255) DEFAULT NULL,
UNIQUE (user_id, sport) -- 添加唯一约束
);
CREATE TABLE images (
id INT AUTO_INCREMENT PRIMARY KEY,
......
......@@ -37,6 +37,29 @@ body {
color: white;
}
/* Update 按钮样式 */
.update-button {
display: inline-block;
padding: 10px 20px;
font-size: 14px;
font-weight: bold;
color: #fff;
background: linear-gradient(90deg, #ff7eb3, #ff758c);
border: none;
border-radius: 25px;
cursor: pointer;
text-transform: uppercase;
transition: all 0.3s ease;
box-shadow: 0 4px 8px rgba(255, 117, 140, 0.3);
margin-left: 10px;
}
.update-button:hover {
background: linear-gradient(90deg, #ff758c, #ff7eb3);
box-shadow: 0 6px 12px rgba(255, 117, 140, 0.5);
transform: scale(1.05);
}
/* 表格容器样式 */
.table-container {
width: 90%;
......
......@@ -21,9 +21,12 @@
<label for="sort-option">Sort by:</label>
<select id="sort-option" onchange="loadRankings()">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
<option value="asc">Ascending</option>
</select>
<button class="update-button" onclick="updateRankings()">Update</button>
</div>
<div class="table-container">
<h1 class="table-title">Rankings</h1>
......
......@@ -45,6 +45,22 @@ async function loadRankings() {
console.error('Error fetching rankings:', error);
}
}
async function updateRankings() {
try {
const response = await fetch('/rankings/updatedata', {
method: 'POST',
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
// Reload rankings after update
loadRankings();
} catch (error) {
console.error('Error updating data:', error);
}
}
// Add event listeners
document.getElementById('filter-sport').addEventListener('change', loadRankings);
......
uploads/701b2c48-7dfa-4a2a-ab96-ec86cca2a5e3_dart-1418245.webp

26.7 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment