Skip to content

Commit 75e45ad

Browse files
authored
Fix head/tail customizations not saving from top-level info response (#81)
1 parent c17b4ab commit 75e45ad

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

server/src/game_runner.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ pub async fn run_game(app_state: &AppState, game_id: Uuid) -> cja::Result<()> {
8686
.customizations
8787
.as_ref()
8888
.map(|c| c.head.clone())
89+
.or_else(|| info.head.clone())
8990
.unwrap_or_default();
9091
let tail = info
9192
.customizations
9293
.as_ref()
9394
.map(|c| c.tail.clone())
95+
.or_else(|| info.tail.clone())
9496
.unwrap_or_default();
9597

9698
if let Err(e) = crate::models::battlesnake::update_battlesnake_customizations(

server/src/snake_client.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ pub struct SnakeInfoResponse {
309309
#[serde(default)]
310310
pub color: Option<String>,
311311
#[serde(default)]
312+
pub head: Option<String>,
313+
#[serde(default)]
314+
pub tail: Option<String>,
315+
#[serde(default)]
312316
pub customizations: Option<InfoCustomizations>,
313317
}
314318

@@ -806,4 +810,38 @@ mod tests {
806810
assert_eq!(c.head, "", "default head should be empty");
807811
assert_eq!(c.tail, "", "default tail should be empty");
808812
}
813+
814+
#[test]
815+
fn test_snake_info_response_top_level_head_and_tail() {
816+
let json = r##"{"apiversion":"1","author":"coreyja","color":"#AA66CC","head":"trans-rights-scarf","tail":"bolt","version":null}"##;
817+
let info: SnakeInfoResponse = serde_json::from_str(json).unwrap();
818+
assert_eq!(
819+
info.color,
820+
Some("#AA66CC".to_string()),
821+
"top-level color should be captured"
822+
);
823+
assert_eq!(
824+
info.head,
825+
Some("trans-rights-scarf".to_string()),
826+
"top-level head should be captured"
827+
);
828+
assert_eq!(
829+
info.tail,
830+
Some("bolt".to_string()),
831+
"top-level tail should be captured"
832+
);
833+
assert!(
834+
info.customizations.is_none(),
835+
"customizations should be None when not present"
836+
);
837+
}
838+
839+
#[test]
840+
fn test_snake_info_response_top_level_head_null_tail() {
841+
let json =
842+
r##"{"apiversion":"1","color":"#AA66CC","head":"trans-rights-scarf","tail":null}"##;
843+
let info: SnakeInfoResponse = serde_json::from_str(json).unwrap();
844+
assert_eq!(info.head, Some("trans-rights-scarf".to_string()));
845+
assert_eq!(info.tail, None, "null tail should deserialize as None");
846+
}
809847
}

0 commit comments

Comments
 (0)