OPENWORM GAMAING (can send messages to server)

This commit is contained in:
2025-11-15 02:10:30 -05:00
parent e4746b9171
commit 2e00389033
5 changed files with 120 additions and 32 deletions

View File

@@ -5,7 +5,10 @@ use iris::prelude::*;
use render::Renderer;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop};
use crate::client::ui::{Submit, UiData};
use crate::{
client::ui::{Submit, main_view},
net::client::NetSender,
};
mod app;
mod input;
@@ -16,7 +19,7 @@ pub use app::AppHandle;
#[derive(Debug)]
pub enum ClientEvent {
Connect,
Connect(NetSender),
}
pub struct Client {
@@ -24,7 +27,6 @@ pub struct Client {
input: Input,
ui: Ui,
focus: Option<WidgetId<TextEdit>>,
ui_data: UiData,
clipboard: Clipboard,
}
@@ -32,13 +34,12 @@ impl Client {
pub fn new(handle: AppHandle) -> Self {
let renderer = Renderer::new(handle.window.clone());
let (ui, ui_data) = ui::ui(handle);
let ui = ui::ui(handle);
Self {
renderer,
input: Input::default(),
ui,
ui_data,
focus: None,
clipboard: Clipboard::new().unwrap(),
}
@@ -46,8 +47,8 @@ impl Client {
pub fn event(&mut self, event: ClientEvent, _: &ActiveEventLoop) {
match event {
ClientEvent::Connect => {
self.ui.set_root(self.ui_data.main_view.clone());
ClientEvent::Connect(send) => {
main_view(&mut self.ui, send).set_root(&mut self.ui);
}
}
}

View File

@@ -3,7 +3,7 @@ use len_fns::*;
use crate::{
client::{Client, app::AppHandle},
net::client::connect,
net::client::{NetCmd, NetSender, connect},
};
#[derive(Eq, PartialEq, Hash, Clone)]
@@ -13,21 +13,17 @@ impl DefaultEvent for Submit {
type Data = ();
}
pub struct UiData {
pub main_view: WidgetId<AnyWidget>,
pub fn ui(handle: AppHandle) -> Ui {
let mut ui = Ui::new();
login_screen(&mut ui, handle).set_root(&mut ui);
ui
}
pub fn ui(handle: AppHandle) -> (Ui, UiData) {
let mut ui = Ui::new();
let msg_panel = msg_panel(&mut ui);
pub fn main_view(ui: &mut Ui, network: NetSender) -> WidgetId<AnyWidget> {
let msg_panel = msg_panel(ui, network);
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
let main_view = (side_bar, msg_panel).span(Dir::RIGHT).add(&mut ui).any();
login_screen(&mut ui, handle).set_root(&mut ui);
let data = UiData { main_view };
(ui, data)
(side_bar, msg_panel).span(Dir::RIGHT).add(ui).any()
}
fn login_screen(ui: &mut Ui, handle: AppHandle) -> WidgetId<AnyWidget> {
@@ -100,7 +96,7 @@ pub fn focus(id: WidgetId<TextEdit>) -> impl Fn(&mut Client, CursorData) {
}
}
pub fn msg_panel(ui: &mut Ui) -> impl WidgetFn<Stack> + use<> {
pub fn msg_panel(ui: &mut Ui, network: NetSender) -> impl WidgetFn<Stack> + use<> {
let msg_area = Span::empty(Dir::DOWN).gap(15).add(ui);
let send_text = text("some stuff idk")
@@ -120,6 +116,7 @@ pub fn msg_panel(ui: &mut Ui) -> impl WidgetFn<Stack> + use<> {
.clone()
.id_on(Submit, move |id, client: &mut Client, _| {
let content = client.ui.text(id).take();
network.send(NetCmd::SendMsg(content.clone())).unwrap();
let msg = msg_widget(content).add(&mut client.ui);
client.ui[&msg_area].children.push(msg.any());
})

View File

@@ -7,6 +7,7 @@ use std::{
str::FromStr,
sync::Arc,
};
use tokio::sync::mpsc::UnboundedSender;
pub fn connect(handle: AppHandle) {
std::thread::spawn(|| {
@@ -14,6 +15,12 @@ pub fn connect(handle: AppHandle) {
});
}
pub enum NetCmd {
SendMsg(String),
}
pub type NetSender = UnboundedSender<NetCmd>;
#[tokio::main]
async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
@@ -43,18 +50,26 @@ async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
.await
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
let (mut send, recv) = conn
.open_bi()
.await
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
drop(recv);
handle.send(ClientEvent::Connect(client_send));
handle.send(ClientEvent::Connect);
while let Some(msg) = client_recv.recv().await {
match msg {
NetCmd::SendMsg(content) => {
let (mut send, recv) = conn
.open_bi()
.await
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
send.write_all(&[39]).await.expect("failed to send");
send.finish().unwrap();
send.stopped().await.unwrap();
drop(recv);
send.write_all(content.as_bytes()).await.expect("failed to send");
send.finish().unwrap();
send.stopped().await.unwrap();
}
}
}
Ok(())
}

View File

@@ -112,11 +112,11 @@ async fn handle_stream(
(send, mut recv): (quinn::SendStream, quinn::RecvStream),
) -> Result<(), String> {
drop(send);
let msg = recv
let bytes = recv
.read_to_end(std::mem::size_of::<ClientMsg>())
.await
.map_err(|e| format!("failed reading request: {}", e))?;
println!("received message");
println!("{:?}", msg);
let msg = String::from_utf8(bytes).unwrap();
println!("received message: {:?}", msg);
Ok(())
}