Added Event struct which can be modified by callbacks

master
noah metz 2024-01-18 18:01:39 -07:00
parent 52d7cfe2f7
commit 005fb13e21
1 changed files with 36 additions and 6 deletions

@ -166,9 +166,9 @@ fn get_game_score(scores: tm::MatchScore) -> Option<GameScore> {
return Some(out); return Some(out);
} }
fn on_score_change(notice: tm::Notice) -> Vec<MQTTMessage> { fn on_score_change(notice: tm::Notice, event: Event) -> (Vec<MQTTMessage>, Event) {
match notice.match_score { match notice.match_score {
None => return Vec::new(), None => return (Vec::new(), event),
Some(game_scores) => { Some(game_scores) => {
match get_game_score(game_scores) { match get_game_score(game_scores) {
Some(score_json) => { Some(score_json) => {
@ -179,9 +179,9 @@ fn on_score_change(notice: tm::Notice) -> Vec<MQTTMessage> {
topic: arena_topic, topic: arena_topic,
payload: serialized, payload: serialized,
}); });
return out; return (out, event);
}, },
None => return Vec::new(), None => return (Vec::new(), event),
} }
}, },
} }
@ -192,9 +192,38 @@ fn get_next_notice() -> tm::Notice {
return tm::Notice::default(); return tm::Notice::default();
} }
type NoticeCallback = fn(tm::Notice) -> Vec<MQTTMessage>; struct Event<'a> {
name: String,
divisions: Vec<Division<'a>>,
}
type NoticeCallback = fn(tm::Notice, Event) -> (Vec<MQTTMessage>, Event);
struct Division<'a> {
name: String,
matches: Vec<Match>,
field_set: FieldSet<'a>,
}
struct FieldSet<'a> {
fields: Vec<Field<'a>>,
}
struct Field<'a> {
name: String,
current_match: &'a Match,
}
struct Match {
name: String,
}
fn main() { fn main() {
let mut event = Event{
name: String::from(""),
divisions: Vec::new(),
};
let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new(); let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new();
callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change); callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change);
@ -229,7 +258,8 @@ fn main() {
} }
}, },
Some(callback) => { Some(callback) => {
let messages = callback(notice); let (messages, next_event) = callback(notice, event);
event = next_event;
for message in messages { for message in messages {
let result = client.publish(message.topic, QoS::AtMostOnce, true, message.payload); let result = client.publish(message.topic, QoS::AtMostOnce, true, message.payload);
match result { match result {