Added Event struct which can be modified by callbacks
parent
52d7cfe2f7
commit
005fb13e21
42
src/main.rs
42
src/main.rs
|
|
@ -166,9 +166,9 @@ fn get_game_score(scores: tm::MatchScore) -> Option<GameScore> {
|
|||
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 {
|
||||
None => return Vec::new(),
|
||||
None => return (Vec::new(), event),
|
||||
Some(game_scores) => {
|
||||
match get_game_score(game_scores) {
|
||||
Some(score_json) => {
|
||||
|
|
@ -179,9 +179,9 @@ fn on_score_change(notice: tm::Notice) -> Vec<MQTTMessage> {
|
|||
topic: arena_topic,
|
||||
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();
|
||||
}
|
||||
|
||||
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() {
|
||||
let mut event = Event{
|
||||
name: String::from(""),
|
||||
divisions: Vec::new(),
|
||||
};
|
||||
|
||||
let mut callbacks: HashMap<tm::NoticeId, NoticeCallback> = HashMap::new();
|
||||
callbacks.insert(tm::NoticeId::NoticeRealtimeScoreChanged, on_score_change);
|
||||
|
||||
|
|
@ -229,7 +258,8 @@ fn main() {
|
|||
}
|
||||
},
|
||||
Some(callback) => {
|
||||
let messages = callback(notice);
|
||||
let (messages, next_event) = callback(notice, event);
|
||||
event = next_event;
|
||||
for message in messages {
|
||||
let result = client.publish(message.topic, QoS::AtMostOnce, true, message.payload);
|
||||
match result {
|
||||
|
|
|
|||
Loading…
Reference in New Issue