Initial version of choosing next story segment

This commit is contained in:
p11
2025-04-20 18:39:32 +02:00
parent dced55dcbe
commit 1866b746cb
4 changed files with 151 additions and 21 deletions

View File

@@ -300,6 +300,7 @@ fn handle_connection(
}
let mut pk_path = path.clone();
let mut mld_path = path.clone();
let mut data_path = path.clone();
let (pki_path, audio_path, start_level, relative_path) = if relative_path.is_empty() {
@@ -325,6 +326,7 @@ fn handle_connection(
}
pk_path.push(format!("{path}.pk"));
mld_path.push(format!("{path}.mld"));
if access == Access::Full {
pki_path.push(format!("{path}.pki"));
} else {
@@ -338,6 +340,7 @@ fn handle_connection(
let file_paths = DocumentPaths {
pk: &pk_path,
mld: &mld_path,
pki: pki_path.as_ref().map(PathBuf::as_ref),
audio: audio_path.as_ref().map(PathBuf::as_ref),
data: &data_path,
@@ -429,6 +432,7 @@ fn reply_binary(
#[derive(Copy, Clone)]
struct DocumentPaths<'a> {
pk: &'a Path,
mld: &'a Path,
pki: Option<&'a Path>,
audio: Option<&'a Path>,
data: &'a Path,
@@ -450,6 +454,8 @@ fn handle_relative_connection(
let mut up = false;
let mut down = false;
let mut choice = 0;
let mut progress = "";
for entry in body.split('&') {
let Some((key, input)) = entry.split_once('=') else {
@@ -486,6 +492,8 @@ fn handle_relative_connection(
}
"up" => up = true,
"down" => down = true,
"choice" => choice = input.parse().unwrap_or_default(),
"progress" => progress = input,
_ => (),
}
}
@@ -601,7 +609,17 @@ fn handle_relative_connection(
.unwrap_or_default();
if let Some(config_map) = config_map {
if render_novel(config_map, file_paths.pk, &mut stream, start_level).is_err() {
if render_novel(
config_map,
file_paths.pk,
file_paths.mld,
&mut stream,
start_level,
choice,
progress,
)
.is_err()
{
fail(stream);
return;
}

View File

@@ -1,7 +1,8 @@
use std::{collections::HashMap, io::prelude::*, net::TcpStream, path::Path};
use std::{collections::HashMap, fs::File, io::prelude::*, net::TcpStream, path::Path};
use indexmap::IndexMap;
use maud::{Markup, html};
use multilinear_parser::{NamedMultilinearInfo, parse_multilinear};
use pukram2html::convert_subheader;
use vn_settings::{Change, Parameter, PlayerSettings, SettingsContext, extract_layers};
@@ -270,11 +271,22 @@ fn interactive_script(total_sections: usize) -> Markup {
}
}
fn load_multilinear(mld_path: &Path) -> Option<NamedMultilinearInfo> {
let Ok(file) = File::open(mld_path) else {
return None;
};
parse_multilinear(file).ok()
}
pub fn render_novel(
mut config_map: IndexMap<Box<str>, Box<str>>,
pk_path: &Path,
mld_path: &Path,
stream: &mut TcpStream,
start_level: usize,
choice: usize,
progress: &str,
) -> Result<(), dialogi::ParsingError> {
let mut settings_context = SettingsContext::new();
extract_layers(&mut settings_context.layers, &mut config_map);
@@ -282,41 +294,63 @@ pub fn render_novel(
let mut player_settings = PlayerSettings::common();
player_settings.extract_settings(&mut settings_context, &mut config_map);
let named_multilinear_info = load_multilinear(mld_path);
let named_multilinear_info = named_multilinear_info.as_ref();
let dialogs = parse_map(pk_path, &mut settings_context)?;
let (scenes, sections) = process_dialogs(dialogs, &mut player_settings, start_level);
let (scenes, sections) = process_dialog(&dialogs[choice], &mut player_settings, start_level);
let html = generate_html(scenes, sections);
let _ = write!(stream, "{}", html.into_string());
if let Some(_named_multilinear_info) = named_multilinear_info {
for (i, dialog_sequence) in dialogs.iter().enumerate() {
if let Some(block) = dialog_sequence
.blocks
.iter()
.find(|block| !block.lines.is_empty())
{
let line_text = &block.lines.first().unwrap().text;
let html = html! {
form method="POST" {
input type="hidden" name="progress" value=(progress);
input type="hidden" name="choice" value=(i);
input type="submit" value=(line_text);
}
};
let _ = write!(stream, "{}", html.into_string());
}
}
}
Ok(())
}
fn process_dialogs(
dialogs: Vec<dialogi::DialogSequence<Change, Parameter>>,
fn process_dialog(
dialog_sequence: &dialogi::DialogSequence<Change, Parameter>,
player_settings: &mut PlayerSettings,
start_level: usize,
) -> (Vec<String>, Vec<Markup>) {
let mut scenes = Vec::new();
let mut sections = Vec::new();
for dialog_sequence in dialogs {
let mut states = initialize_change_states(&dialog_sequence.changes);
let mut states = initialize_change_states(&dialog_sequence.changes);
for block in dialog_sequence.blocks {
apply_block_changes(
&block,
&dialog_sequence.changes,
&mut states,
player_settings,
);
for block in &dialog_sequence.blocks {
apply_block_changes(
block,
&dialog_sequence.changes,
&mut states,
player_settings,
);
scenes.push(render_scene(player_settings, &block.name).into_string());
sections.push(render_dialog_block(&block, start_level));
}
player_settings.reset();
scenes.push(render_scene(player_settings, &block.name).into_string());
sections.push(render_dialog_block(block, start_level));
}
player_settings.reset();
(scenes, sections)
}