Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 2645390

Browse files
committed
Fix that games could be merged twice (by name and by host)
1 parent ea5ae05 commit 2645390

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/state/updater.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ fn merge_games(curr_game_id: GameId, prev_game_id: Option<GameId>, state: &mut S
163163

164164
let server_id = match prev_game_id {
165165
Some(prev_game_id) if prev_game_id < curr_game_id => {
166+
println!(
167+
"[info] [updater] merging games {} and {} (`{}` and `{}`)",
168+
prev_game_id, curr_game_id,
169+
state.get_game_name(prev_game_id), state.get_game_name(curr_game_id)
170+
);
171+
166172
let prev_game = state.get_game_mut(prev_game_id);
167173
assert!(prev_game.time_end.is_some());
168174
// todo: что если prev_game.next_game_id != None (мб такое возможно при приостановке)
@@ -173,7 +179,7 @@ fn merge_games(curr_game_id: GameId, prev_game_id: Option<GameId>, state: &mut S
173179
let server_id = state.game_ids.iter()
174180
.position(|&game_id| game_id == prev_game_id)
175181
// prev_game_id был добавлен в state.game_ids когда происходило объединение множеств {...} и {..., prev_game_id}
176-
.unwrap();
182+
.unwrap_or_else(|| panic!("Can't find prev game: prev_game_id={}, curr_game_id={}", prev_game_id, curr_game_id));
177183
state.game_ids[server_id] = curr_game_id;
178184
server_id
179185
}
@@ -251,9 +257,10 @@ fn try_merge_host(prev_game_ids_host: &[GameId], curr_game_ids_host: &[GameId],
251257
} else {
252258
let get_game_name = |&game_id: &GameId, state: &State| state.get_game_name(game_id).to_owned();
253259
let get_game_host = |&game_id: &GameId, state: &State| state.get_game_host(game_id).unwrap().to_owned();
254-
let matched_by_name = try_match_by_property(&prev_game_ids_host, &curr_game_ids_host, state, get_game_name);
255-
let matched_by_host = try_match_by_property(&prev_game_ids_host, &curr_game_ids_host, state, get_game_host);
256-
let matched = matched_by_name || matched_by_host;
260+
// It is extremely important that we call second [try_match_by_property] only if first returns false
261+
let matched = false
262+
|| try_match_by_property(&prev_game_ids_host, &curr_game_ids_host, state, get_game_name)
263+
|| try_match_by_property(&prev_game_ids_host, &curr_game_ids_host, state, get_game_host);
257264
if !matched {
258265
eprintln!("[warn] [updater] can't match games: {:?} with {:?}", prev_game_ids_host, curr_game_ids_host);
259266
for game_id in curr_game_ids_host {
@@ -278,11 +285,17 @@ pub fn try_merge_host_ids(updater_state: &mut UpdaterState, state: &mut State, t
278285
let curr_game_ids_host = curr_game_ids_by_host.get(&host_id);
279286
if curr_game_ids_host.is_none() {
280287
// новых game_id не появилось: нечего объединять
288+
println!("[info] [updater] host {}: merge failed - no new games", base64::encode(host_id));
281289
return None;
282290
}
283291
let curr_game_ids_host = curr_game_ids_host.unwrap();
284292

293+
println!(
294+
"[info] [updater] host {}: merging games {:?} and {:?}",
295+
base64::encode(host_id), prev_game_ids_host, curr_game_ids_host
296+
);
285297
if try_merge_host(prev_game_ids_host, curr_game_ids_host, state) {
298+
println!("[info] [updater] host {}: merge successful", base64::encode(host_id));
286299
if time.get() - merge_info.time_begin.get() > 24 * 60 /* 1 day */ {
287300
eprintln!("[error] [updater] host {} waited merge too long: {:?}-{:?}",
288301
base64::encode(host_id), merge_info.time_begin, merge_info.time_end);

0 commit comments

Comments
 (0)