Commit bd2d4d09 authored by 陈冲's avatar 陈冲

fix: 更新后台调用

parent 3f06f803
......@@ -723,6 +723,14 @@ async fn save_room_final_scores() {
};
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;
}
}
......@@ -1145,14 +1153,20 @@ async fn handle_room_command(
if wechat.is_empty() {
return;
}
let (score, rank, ranking, already_top_ranked, batch_no) =
let (rank, ranking, already_top_ranked) =
if let Ok(state) = ROOM_STATE.lock() {
let batch_no = state.batch_no.clone();
let mut state = state;
//防止客户端提交的状态还在Running中
if !matches!(state.status, RoomStatus::Submit | RoomStatus::Running) {
drop(state);
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 ranking = RoomState::ranking_payload(
&eligible_ranking
......@@ -1161,11 +1175,6 @@ async fn handle_room_command(
.cloned()
.collect::<Vec<_>>(),
);
let server_score = state
.players
.get(userid)
.map(|player| player.score)
.unwrap_or(score);
let already_top_ranked = state
.players
.get(userid)
......@@ -1182,44 +1191,31 @@ async fn handle_room_command(
};
drop(state);
(
server_score,
server_rank,
ranking,
already_top_ranked,
batch_no,
)
} else {
(score, rank, Vec::new(), false, "".to_string())
(rank, Vec::new(), false)
};
let game = Game {
wechat_id: wechat.to_string(),
nickname: nickname.to_string(),
avatar: avatar.to_string(),
item_num: item_num as i32,
game_rank: rank as i32,
score: score as i32,
create_date: None,
batch_no: batch_no,
};
if insert_game_once(&game).await {
if !already_top_ranked && (1..=ROOM_SCORE_RANK_LIMIT as i64).contains(&rank) {
CLIENT_TOP_RANKED_MAP.insert(userid.to_string(), true);
}
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));
dbg!(json!({
"event": "submit_score_save_deferred",
"wechat_id": wechat,
"item_num": item_num,
"score": score,
"rank": rank,
"already_top_ranked": already_top_ranked,
}));
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)));
}
}
// 关闭房间
......
......@@ -26,7 +26,7 @@ export function stopAllMusic() {
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)
// 检查当前背景音乐是否是同一首(比较路径末尾,处理相对路径和完整URL的情况)
......@@ -40,17 +40,19 @@ function getBackgroundMusic(music: string, force: boolean = false) {
stopAllMusic();
}
backgroundMusic = new Audio(music)
backgroundMusic.loop = false
backgroundMusic.loop = loop
backgroundMusic.preload = 'auto'
backgroundMusic.volume = 0.6
backgroundMusic.addEventListener('ended', () => {
if (!backgroundMusic || !backgroundMusic.src.endsWith(music)) {
return
}
backgroundMusic.pause()
backgroundMusic.currentTime = 0
backgroundMusic = null
}, { once: true })
if (!loop) {
backgroundMusic.addEventListener('ended', () => {
if (!backgroundMusic || !backgroundMusic.src.endsWith(music)) {
return
}
backgroundMusic.pause()
backgroundMusic.currentTime = 0
backgroundMusic = null
}, { once: true })
}
backgroundMusic.load()
console.log('[Music] 新背景音乐创建完成:', backgroundMusic.src)
}
......@@ -60,7 +62,7 @@ function getBackgroundMusic(music: string, force: boolean = false) {
//播放庆祝音乐
export async function playApplauseMusic() {
const audio = getBackgroundMusic(applause)
const audio = getBackgroundMusic(applause, false, false)
try {
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