2020-09-28 21:35:05 +00:00
|
|
|
use crate::config::*;
|
|
|
|
use actix::prelude::*;
|
|
|
|
use log::info;
|
2020-10-03 20:31:42 +00:00
|
|
|
use std::sync::Arc;
|
2020-09-28 21:35:05 +00:00
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "bool")]
|
|
|
|
pub struct LogSkippedOldMessages;
|
|
|
|
|
|
|
|
/// Holds state of the dice bot, for anything requiring mutable
|
|
|
|
/// transitions. This is a simple mutable trait whose values represent
|
|
|
|
/// the current state of the dicebot. It provides mutable methods to
|
|
|
|
/// change state.
|
|
|
|
pub struct DiceBotState {
|
|
|
|
logged_skipped_old_messages: bool,
|
2020-10-03 20:31:42 +00:00
|
|
|
config: Arc<Config>,
|
2020-09-28 21:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor for DiceBotState {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
|
|
|
|
fn started(&mut self, _ctx: &mut Self::Context) {
|
|
|
|
info!(
|
|
|
|
"Oldest allowable message time is {} seconds ago",
|
2020-10-03 20:31:42 +00:00
|
|
|
&self.config.oldest_message_age()
|
2020-09-28 21:35:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiceBotState {
|
|
|
|
/// Create initial dice bot state.
|
2020-10-03 20:31:42 +00:00
|
|
|
pub fn new(config: &Arc<Config>) -> DiceBotState {
|
2020-09-28 21:35:05 +00:00
|
|
|
DiceBotState {
|
|
|
|
logged_skipped_old_messages: false,
|
|
|
|
config: config.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Log and record that we have skipped some old messages. This
|
|
|
|
/// method will log once, and then no-op from that point on.
|
|
|
|
pub fn skipped_old_messages(&mut self) {
|
|
|
|
if !self.logged_skipped_old_messages {
|
|
|
|
info!("Skipped some messages received while offline because they are too old.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.logged_skipped_old_messages = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<LogSkippedOldMessages> for DiceBotState {
|
|
|
|
type Result = bool;
|
|
|
|
|
|
|
|
fn handle(&mut self, _msg: LogSkippedOldMessages, _ctx: &mut Context<Self>) -> Self::Result {
|
|
|
|
self.skipped_old_messages();
|
|
|
|
self.logged_skipped_old_messages
|
|
|
|
}
|
|
|
|
}
|