Commit bd2d4d09 authored by 陈冲's avatar 陈冲

fix: 更新后台调用

parent 3f06f803
...@@ -723,6 +723,14 @@ async fn save_room_final_scores() { ...@@ -723,6 +723,14 @@ async fn save_room_final_scores() {
}; };
for game in games { for game in games {
dbg!(json!({
"event": "final_score_save",
"wechat_id": game.wechat_id.clone(),
"item_num": game.item_num,
"score": game.score,
"rank": game.game_rank,
"batch_no": game.batch_no.clone(),
}));
insert_game_once(&game).await; insert_game_once(&game).await;
} }
} }
...@@ -1145,14 +1153,20 @@ async fn handle_room_command( ...@@ -1145,14 +1153,20 @@ async fn handle_room_command(
if wechat.is_empty() { if wechat.is_empty() {
return; return;
} }
let (score, rank, ranking, already_top_ranked, batch_no) = let (rank, ranking, already_top_ranked) =
if let Ok(state) = ROOM_STATE.lock() { if let Ok(state) = ROOM_STATE.lock() {
let batch_no = state.batch_no.clone(); let mut state = state;
//防止客户端提交的状态还在Running中 //防止客户端提交的状态还在Running中
if !matches!(state.status, RoomStatus::Submit | RoomStatus::Running) { if !matches!(state.status, RoomStatus::Submit | RoomStatus::Running) {
drop(state); drop(state);
return; return;
} }
if let Some(player) = state.players.get_mut(userid) {
player.score = score;
player.nickname = nickname.to_string();
player.avatar = avatar.to_string();
player.online = true;
}
let eligible_ranking = state.eligible_score_ranking(); let eligible_ranking = state.eligible_score_ranking();
let ranking = RoomState::ranking_payload( let ranking = RoomState::ranking_payload(
&eligible_ranking &eligible_ranking
...@@ -1161,11 +1175,6 @@ async fn handle_room_command( ...@@ -1161,11 +1175,6 @@ async fn handle_room_command(
.cloned() .cloned()
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
); );
let server_score = state
.players
.get(userid)
.map(|player| player.score)
.unwrap_or(score);
let already_top_ranked = state let already_top_ranked = state
.players .players
.get(userid) .get(userid)
...@@ -1182,44 +1191,31 @@ async fn handle_room_command( ...@@ -1182,44 +1191,31 @@ async fn handle_room_command(
}; };
drop(state); drop(state);
( (
server_score,
server_rank, server_rank,
ranking, ranking,
already_top_ranked, already_top_ranked,
batch_no,
) )
} else { } else {
(score, rank, Vec::new(), false, "".to_string()) (rank, Vec::new(), false)
}; };
let game = Game { dbg!(json!({
wechat_id: wechat.to_string(), "event": "submit_score_save_deferred",
nickname: nickname.to_string(), "wechat_id": wechat,
avatar: avatar.to_string(), "item_num": item_num,
item_num: item_num as i32, "score": score,
game_rank: rank as i32, "rank": rank,
score: score as i32, "already_top_ranked": already_top_ranked,
create_date: None, }));
batch_no: batch_no, let mut payload = json!({
}; "list": ranking,
"rank": if already_top_ranked { Value::Null } else { json!(rank) }
if insert_game_once(&game).await { });
if !already_top_ranked && (1..=ROOM_SCORE_RANK_LIMIT as i64).contains(&rank) { if already_top_ranked {
CLIENT_TOP_RANKED_MAP.insert(userid.to_string(), true); payload["alreadyTopRanked"] = json!(true);
} payload["message"] =
json!("一人只能上榜领奖一次,您的福利已到手,把机会留给其他玩家吧");
let mut payload = json!({
"list": ranking,
"rank": if already_top_ranked { Value::Null } else { json!(rank) }
});
if already_top_ranked {
payload["alreadyTopRanked"] = json!(true);
payload["message"] =
json!("一人只能上榜领奖一次,您的福利已到手,把机会留给其他玩家吧");
}
emit_msgpack(&socket, evt, &ok_resp(Some(payload)));
} else {
emit_msgpack(&socket, evt, &err_resp("提交异常", None));
} }
emit_msgpack(&socket, evt, &ok_resp(Some(payload)));
} }
} }
// 关闭房间 // 关闭房间
......
...@@ -26,7 +26,7 @@ export function stopAllMusic() { ...@@ -26,7 +26,7 @@ export function stopAllMusic() {
backgroundMusic = null; backgroundMusic = null;
} }
function getBackgroundMusic(music: string, force: boolean = false) { function getBackgroundMusic(music: string, force: boolean = false, loop: boolean = true) {
console.log('[Music] getBackgroundMusic called, music:', music, 'force:', force, 'current backgroundMusic:', backgroundMusic?.src) console.log('[Music] getBackgroundMusic called, music:', music, 'force:', force, 'current backgroundMusic:', backgroundMusic?.src)
// 检查当前背景音乐是否是同一首(比较路径末尾,处理相对路径和完整URL的情况) // 检查当前背景音乐是否是同一首(比较路径末尾,处理相对路径和完整URL的情况)
...@@ -40,17 +40,19 @@ function getBackgroundMusic(music: string, force: boolean = false) { ...@@ -40,17 +40,19 @@ function getBackgroundMusic(music: string, force: boolean = false) {
stopAllMusic(); stopAllMusic();
} }
backgroundMusic = new Audio(music) backgroundMusic = new Audio(music)
backgroundMusic.loop = false backgroundMusic.loop = loop
backgroundMusic.preload = 'auto' backgroundMusic.preload = 'auto'
backgroundMusic.volume = 0.6 backgroundMusic.volume = 0.6
backgroundMusic.addEventListener('ended', () => { if (!loop) {
if (!backgroundMusic || !backgroundMusic.src.endsWith(music)) { backgroundMusic.addEventListener('ended', () => {
return if (!backgroundMusic || !backgroundMusic.src.endsWith(music)) {
} return
backgroundMusic.pause() }
backgroundMusic.currentTime = 0 backgroundMusic.pause()
backgroundMusic = null backgroundMusic.currentTime = 0
}, { once: true }) backgroundMusic = null
}, { once: true })
}
backgroundMusic.load() backgroundMusic.load()
console.log('[Music] 新背景音乐创建完成:', backgroundMusic.src) console.log('[Music] 新背景音乐创建完成:', backgroundMusic.src)
} }
...@@ -60,7 +62,7 @@ function getBackgroundMusic(music: string, force: boolean = false) { ...@@ -60,7 +62,7 @@ function getBackgroundMusic(music: string, force: boolean = false) {
//播放庆祝音乐 //播放庆祝音乐
export async function playApplauseMusic() { export async function playApplauseMusic() {
const audio = getBackgroundMusic(applause) const audio = getBackgroundMusic(applause, false, false)
try { try {
await audio.play() await audio.play()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment