Skip to content

Commit

Permalink
Merge pull request #536 from anstadnik/english
Browse files Browse the repository at this point in the history
english
  • Loading branch information
anstadnik authored May 27, 2024
2 parents f64f691 + 830d594 commit 0a342e8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ jobs:
release: ${{ github.base_ref == 'main' }}
upload-artifact: true
secrets:
SHEET_ID: ${{ vars.SHEET_ID }}
SHEET_ID: ${{ github.base_ref == 'main' && secrets.SHEET_ID_main || secrets.SHEET_ID_dev }}

build_flutter_apk:
uses: ./.github/workflows/build_flutter_apk.yml
with:
upload-artifact: true
secrets:
SHEET_ID: ${{ vars.SHEET_ID }}
SHEET_ID: ${{ github.base_ref == 'main' && secrets.SHEET_ID_main || secrets.SHEET_ID_dev }}

# It should wait untill all checks will pass
auto-merge:
Expand Down
2 changes: 1 addition & 1 deletion bot/src/bot/dialogue/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn commands_handler(
dialogue: FADialogue,
) -> anyhow::Result<()> {
match cmd {
FACommands::Start => start_endpoint(bot, msg, dialogue).await,
FACommands::Start => start_endpoint(bot, msg, dialogue, Lang::default()).await,
}
}

Expand Down
22 changes: 14 additions & 8 deletions bot/src/bot/dialogue/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ use first_aid_bot_core::prelude::*;
use rand::random;
use teloxide::{requests::Requester, types::Message};

pub async fn start_endpoint(bot: FABot, msg: Message, dialogue: FADialogue) -> Result<()> {
pub async fn start_endpoint(
bot: FABot,
msg: Message,
dialogue: FADialogue,
lang: Lang,
) -> Result<()> {
if is_admin(&msg) && random::<u8>() % 50 == 0 {
easter_egg(&bot, &msg).await?;
}
let ctx = FAContext::default();
let ctx = FAContext {
lang,
..FAContext::default()
};
move_to_state(&bot, &msg, &dialogue, &*DATA.get_state(&ctx).await?, ctx).await
}

Expand All @@ -21,13 +29,11 @@ pub async fn transition_endpoint(
dialogue: FADialogue,
(lang, context): (String, Vec<String>),
) -> Result<()> {
let f = || async {
let lang = lang.as_str().try_into()?;
transition_logic(&bot, &msg, &dialogue, FAContext { lang, context }).await
};
let lang = lang.as_str().try_into()?;
let f = || async { transition_logic(&bot, &msg, &dialogue, FAContext { lang, context }).await };

if let Err(e) = f().await {
start_endpoint(bot, msg, dialogue).await?;
start_endpoint(bot, msg, dialogue, lang).await?;
bail!(e);
}
Ok(())
Expand All @@ -43,7 +49,7 @@ pub async fn broadcast_endpoint(
let _ = bot
.send_message(msg.chat.id, "WTF you are not an admin bye")
.await;
return start_endpoint(bot, msg, dialogue).await;
return start_endpoint(bot, msg, dialogue, Lang::default()).await;
}
process_broadcast(&bot, &msg, &dialogue, message).await?;
Ok(())
Expand Down
22 changes: 12 additions & 10 deletions bot/src/bot/dialogue/logic/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ pub fn make_keyboard(keys: &[&str], lang: Lang, depth: usize, is_admin: bool) ->
keyboard.push(vec![KeyboardButton::new(lang.details().broadcast)]);
}

let special_keys = if depth == 0 {
let f = |lang: Lang| KeyboardButton::new(lang.details().button_lang);
Lang::iter().filter(|&l| l != lang).map(f).collect()
} else {
let mut special_keys = vec![KeyboardButton::new(lang.details().button_back)];
if depth > 1 {
special_keys.push(KeyboardButton::new(lang.details().button_home));
};
special_keys
};
let ld = lang.details();
let special_keys = match depth {
0 => Lang::iter()
.filter(|&l| l != lang)
.map(|l| l.details().button_lang)
.collect(),
1 => vec![ld.button_back],
2.. => vec![ld.button_back, ld.button_home],
}
.into_iter()
.map(KeyboardButton::new)
.collect();
keyboard.push(special_keys);

ReplyMarkup::Keyboard(KeyboardMarkup::new(keyboard).resize_keyboard(true))
Expand Down
27 changes: 16 additions & 11 deletions core/src/model/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ impl Data {
Self { data: None }
}
pub fn cached() -> Result<Self> {
log::info!("Cached data!");
let data = Some(get_data_from_file("table.csv")?);
Ok(Self { data })
unimplemented!("There is no multiple language support yet!");
// log::info!("Cached data!");
// let data = Some(get_data_from_file("table.csv")?);
// Ok(Self { data })
}
pub async fn download() -> Result<Self> {
log::info!("Downloading data!");
Expand All @@ -42,16 +43,20 @@ impl<'a> CowMultLangFsExt<'a> for Cow<'a, MultilangFs> {

match self {
Cow::Borrowed(v) => {
Ok(Cow::Borrowed(ctx.context.iter().try_fold(
v.get(&ctx.lang).ok_or(err_lang)?,
|fs, key| fs.next_states.get(key).ok_or_else(err_ctx(key)),
)?))
let init = v.get(&ctx.lang).ok_or(err_lang)?;
Ok(Cow::Borrowed(
ctx.context.iter().try_fold(init, |fs, key| {
fs.next_states.get(key).ok_or_else(err_ctx(key))
})?,
))
}
Cow::Owned(mut v) => {
Ok(Cow::Owned(ctx.context.iter().try_fold(
v.remove(&ctx.lang).ok_or(err_lang)?,
|mut fs, key| fs.next_states.swap_remove(key).ok_or_else(err_ctx(key)),
)?))
let init = v.remove(&ctx.lang).ok_or(err_lang)?;
Ok(Cow::Owned(
ctx.context.iter().try_fold(init, |mut fs, key| {
fs.next_states.swap_remove(key).ok_or_else(err_ctx(key))
})?,
))
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions core/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ fn get_finite_state(rdr: Reader<impl Read>, lang: Lang) -> anyhow::Result<Fs> {
Ok(Fs::entry(lang, get_next_states_for_key(&rows, "")?))
}

// This file is only for Ukrainian. If we will want to add more languages, it should be changed
pub fn get_data_from_file(filename: &str) -> anyhow::Result<MultilangFs> {
pub fn get_data_from_file(_filename: &str) -> anyhow::Result<MultilangFs> {
unimplemented!("There is no multiple language support yet! There has to be multiple filenames");
// let rdr = Reader::from_reader(BufReader::new(File::open(filename)?));
let rdr = Reader::from_path(filename)?;
assert!(Lang::iter().count() == 1, "Only one language is supported");
let lang = Lang::iter().next().unwrap();
Ok([(lang, get_finite_state(rdr, lang)?)].into())
// assert!(Lang::iter().count() == 1, "Only one language is supported");
// let lang = Lang::iter().next().unwrap();
// Ok([(lang, get_finite_state(rdr, lang)?)].into())
// Lang::iter()
// .map(|lang| Ok((lang, get_finite_state(Reader::from_path(filename)?, lang)?)))
// .collect()
}

pub async fn get_data_from_web() -> anyhow::Result<MultilangFs> {
let sheet_id = env!("SHEET_ID");
// assert!(Lang::iter().count() == 1, "Only one language is supported");
let mut ret = MultilangFs::default();
for lang in Lang::iter() {
let url = format!(
Expand Down
6 changes: 6 additions & 0 deletions core/tests/test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ pre-formatted fixed-width code block written in the Python programming language
assert!(!data.is_empty());
assert!(data.iter().all(|(_, fs)| fs.num_nodes() > 1));
assert!(Lang::iter().all(|lang| data.contains_key(&lang)));
for (lang, fs) in &data {
log::info!("First keys for lang {lang} are: ");
for key in fs.next_states.keys() {
log::info!("{}", key);
}
}
data.into_iter()
.inspect(|(lang, fs)| log::info!("Testing {lang} with {} nodes", fs.num_nodes()))
.try_for_each(|(_, fs)| test_fs(fs))?;
Expand Down

0 comments on commit 0a342e8

Please sign in to comment.