mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-18 16:28:55 +00:00
Use the last contest rating change id, instead of rating change time
This commit is contained in:
parent
20a78c1c14
commit
bacb781896
3 changed files with 30 additions and 25 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::db::{CfSavedUsers, CfUser};
|
use crate::db::{CfSavedUsers, CfUser};
|
||||||
use announcer::MemberToChannels;
|
use announcer::MemberToChannels;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::Utc;
|
||||||
use codeforces::{RatingChange, User};
|
use codeforces::{RatingChange, User};
|
||||||
use serenity::{
|
use serenity::{
|
||||||
framework::standard::{CommandError, CommandResult},
|
framework::standard::{CommandError, CommandResult},
|
||||||
|
@ -53,7 +53,7 @@ fn update_user(
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut channels_list: Option<Vec<ChannelId>> = None;
|
let mut channels_list: Option<Vec<ChannelId>> = None;
|
||||||
let last_update = std::mem::replace(&mut cfu.last_update, Utc::now());
|
cfu.last_update = Utc::now();
|
||||||
// Update the rating
|
// Update the rating
|
||||||
cfu.rating = info.rating;
|
cfu.rating = info.rating;
|
||||||
|
|
||||||
|
@ -81,16 +81,24 @@ fn update_user(
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let rating_changes = match cfu.last_contest_id {
|
||||||
|
None => rating_changes,
|
||||||
|
Some(v) => rating_changes
|
||||||
|
.into_iter()
|
||||||
|
.take_while(|rc| rc.contest_id != v)
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
|
||||||
|
cfu.last_contest_id = rating_changes
|
||||||
|
.iter()
|
||||||
|
.last()
|
||||||
|
.map(|v| v.contest_id)
|
||||||
|
.or(cfu.last_contest_id);
|
||||||
|
|
||||||
// Check for any good announcements to make
|
// Check for any good announcements to make
|
||||||
for rc in rating_changes {
|
for rc in rating_changes {
|
||||||
let date: DateTime<Utc> = DateTime::from_utc(
|
if let Err(v) = send_message(rc) {
|
||||||
chrono::NaiveDateTime::from_timestamp(rc.rating_update_time_seconds as i64, 0),
|
dbg!(v);
|
||||||
Utc,
|
|
||||||
);
|
|
||||||
if &date > &last_update {
|
|
||||||
if let Err(v) = send_message(rc) {
|
|
||||||
dbg!(v);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
after.recv().ok();
|
after.recv().ok();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use codeforces::User;
|
use codeforces::{RatingChange, User};
|
||||||
use serenity::model::id::UserId;
|
use serenity::model::id::UserId;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use youmubot_db::DB;
|
use youmubot_db::DB;
|
||||||
|
@ -12,24 +12,19 @@ pub type CfSavedUsers = DB<HashMap<UserId, CfUser>>;
|
||||||
pub struct CfUser {
|
pub struct CfUser {
|
||||||
pub handle: String,
|
pub handle: String,
|
||||||
pub last_update: DateTime<Utc>,
|
pub last_update: DateTime<Utc>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub last_contest_id: Option<u64>,
|
||||||
pub rating: Option<i64>,
|
pub rating: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CfUser {
|
impl CfUser {
|
||||||
fn default() -> Self {
|
/// Save a new user as an internal CFUser.
|
||||||
Self {
|
/// Requires a vector of rating changes because we must rely on the Codeforces rating_changes API's return order to properly announce.
|
||||||
handle: "".to_owned(),
|
pub(crate) fn save(u: User, rc: Vec<RatingChange>) -> Self {
|
||||||
last_update: Utc::now(),
|
|
||||||
rating: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<User> for CfUser {
|
|
||||||
fn from(u: User) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
handle: u.handle,
|
handle: u.handle,
|
||||||
last_update: Utc::now(),
|
last_update: Utc::now(),
|
||||||
|
last_contest_id: rc.into_iter().last().map(|v| v.contest_id),
|
||||||
rating: u.rating,
|
rating: u.rating,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ mod hook;
|
||||||
/// Live-commentating a Codeforces round.
|
/// Live-commentating a Codeforces round.
|
||||||
mod live;
|
mod live;
|
||||||
|
|
||||||
use db::CfSavedUsers;
|
use db::{CfSavedUsers, CfUser};
|
||||||
|
|
||||||
pub use hook::codeforces_info_hook;
|
pub use hook::codeforces_info_hook;
|
||||||
|
|
||||||
|
@ -97,13 +97,15 @@ pub fn save(ctx: &mut Context, m: &Message, mut args: Args) -> CommandResult {
|
||||||
m.reply(&ctx, "cannot find an account with such handle")?;
|
m.reply(&ctx, "cannot find an account with such handle")?;
|
||||||
}
|
}
|
||||||
Some(acc) => {
|
Some(acc) => {
|
||||||
|
// Collect rating changes data.
|
||||||
|
let rating_changes = acc.rating_changes(&http)?;
|
||||||
let db = CfSavedUsers::open(&*ctx.data.read());
|
let db = CfSavedUsers::open(&*ctx.data.read());
|
||||||
let mut db = db.borrow_mut()?;
|
let mut db = db.borrow_mut()?;
|
||||||
m.reply(
|
m.reply(
|
||||||
&ctx,
|
&ctx,
|
||||||
format!("account `{}` has been linked to your account.", &acc.handle),
|
format!("account `{}` has been linked to your account.", &acc.handle),
|
||||||
)?;
|
)?;
|
||||||
db.insert(m.author.id, acc.into());
|
db.insert(m.author.id, CfUser::save(acc, rating_changes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue