mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Update for event system in (failed) attempt to get Singleplayer to spawn robots
This commit is contained in:
@@ -10,7 +10,7 @@ use tokio::net;
|
||||
use polariton::packet::{Data, Message, Packet, StandardMessage};
|
||||
use polariton::operation::{OperationResponse, Typed};
|
||||
|
||||
pub type UserTy = std::sync::RwLock<state::UserState>;
|
||||
pub type UserTy = state::UserState;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler()));
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(), polariton_server::events::EventsHandler::new()));
|
||||
|
||||
let ip_addr: std::net::IpAddr = args.ip.parse().expect("Invalid IP address");
|
||||
|
||||
@@ -27,14 +27,16 @@ async fn main() -> std::io::Result<()> {
|
||||
if args.once {
|
||||
log::warn!("Handling first connection and then exiting");
|
||||
let (socket, address) = listener.accept().await?;
|
||||
process_socket(socket, address, server).await;
|
||||
Ok(())
|
||||
process_socket(socket, address, server.clone()).await;
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
}
|
||||
}
|
||||
server.join();
|
||||
server.join_async().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>) {
|
||||
@@ -46,8 +48,11 @@ async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAdd
|
||||
return;
|
||||
}
|
||||
};
|
||||
let user_state = state::UserState::new();
|
||||
server.handle_async(socket, user_state, enc, Default::default()).await;
|
||||
let (socket_r, socket_w) = socket.into_split();
|
||||
let (chann_tx, chann_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let user_state = state::UserState::new(chann_tx.clone());
|
||||
let ctx = polariton::packet::SerdesContext::from_boxed(Default::default(), enc);
|
||||
server.handle_async_with_channel(socket_r, socket_w, user_state, ctx, chann_tx, chann_rx).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
@@ -100,7 +105,7 @@ impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
) -> Option<Box<dyn polariton::packet::Cryptographer>> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
@@ -161,7 +166,7 @@ async fn do_connect_handshake(
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
let ctx = polariton::packet::SerdesContext::new(&op_ctx, &crypto);
|
||||
let ctx = polariton::packet::SerdesContext::new(op_ctx, crypto);
|
||||
// authenticate
|
||||
log::debug!("(connect) Handling third packet");
|
||||
let mut packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
@@ -181,7 +186,7 @@ async fn do_connect_handshake(
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
let to_send = match handshake.authenticate(&packet3, &ctx) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
@@ -252,5 +257,5 @@ async fn do_connect_handshake(
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
Some(ctx.into_crypto())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton::operation::ParameterTable;
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
use crate::data::player_data::*;
|
||||
|
||||
@@ -785,7 +785,7 @@ const VALID_COLOUR: &[u8] = &[64,
|
||||
|
||||
pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, user: &crate::UserTy| {
|
||||
let ulock = user.read().unwrap();
|
||||
let ulock = user.auth.read().unwrap();
|
||||
let mut params = params.to_dict();
|
||||
params.insert(PARAM_KEY, PlayerDatas {
|
||||
players: vec![
|
||||
@@ -838,7 +838,7 @@ pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(P
|
||||
robot_map: VALID_ROBOT.iter().map(|x| *x).collect(),
|
||||
team: 1,
|
||||
has_premium: false,
|
||||
robot_uuid: "1_1".to_owned(),
|
||||
robot_uuid: "12_12".to_owned(),
|
||||
cpu: 0,
|
||||
weapon_order: vec![20000200, 0, 0],
|
||||
colour_map: VALID_COLOUR.iter().map(|x| *x).collect(),
|
||||
@@ -850,6 +850,30 @@ pub(super) fn tdm_machines_provider() -> SimpleFunc<1, crate::UserTy, impl (Fn(P
|
||||
},*/
|
||||
]
|
||||
}.as_transmissible());
|
||||
let event_tx = user.event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
||||
log::debug!("Sending singleplayer events");
|
||||
let mut spawn_params = std::collections::HashMap::with_capacity(4);
|
||||
spawn_params.insert(2 /* robot GUID */, Typed::Str("12_12".into()));
|
||||
spawn_params.insert(3 /* machine model */, Typed::Bytes(VALID_ROBOT.iter().map(|x| *x).collect::<Vec<_>>().into()));
|
||||
spawn_params.insert(4 /* robot name */, Typed::Str("RE_robot_spawn_name0".into()));
|
||||
spawn_params.insert(7 /* color model */, Typed::Bytes(VALID_COLOUR.iter().map(|x| *x).collect::<Vec<_>>().into()));
|
||||
event_tx.send(polariton_server::ToSend::Data {
|
||||
data: polariton::packet::Data::Event(polariton::operation::Event { code: 3, params: spawn_params.into() }),
|
||||
encrypt: true,
|
||||
channel: 0,
|
||||
reliable: true,
|
||||
}).unwrap();
|
||||
let mut update_params = std::collections::HashMap::with_capacity(1);
|
||||
update_params.insert(6 /* ??? */, Typed::Int(5));
|
||||
event_tx.send(polariton_server::ToSend::Data {
|
||||
data: polariton::packet::Data::Event(polariton::operation::Event { code: 5, params: update_params.into() }),
|
||||
encrypt: true,
|
||||
channel: 0,
|
||||
reliable: true,
|
||||
}).unwrap();
|
||||
});
|
||||
Ok(params.into())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ impl <C> Operation<C> for MoreLobbyAuth {
|
||||
fn handle(&self, params: polariton::operation::ParameterTable<C>, _: &mut Self::State, user: &Self::User) -> polariton::operation::OperationResponse<C> {
|
||||
let params_dict = params.to_dict();
|
||||
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
|
||||
let mut write_lock = user.write().unwrap();
|
||||
if write_lock.update_with_auth(&auth_payload.string) {
|
||||
//let mut write_lock = user.write().unwrap();
|
||||
if user.update_with_auth(&auth_payload.string) {
|
||||
let mut resp_params = std::collections::HashMap::new();
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return polariton::operation::OperationResponse {
|
||||
|
||||
@@ -1,27 +1,40 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct UserState {
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use polariton_server::ToSend;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct UserAuthInfo {
|
||||
pub uuid: String,
|
||||
pub token: String,
|
||||
pub refresh_token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UserState {
|
||||
pub auth: RwLock<UserAuthInfo>,
|
||||
pub event_tx: UnboundedSender<ToSend>,
|
||||
}
|
||||
|
||||
impl UserState {
|
||||
pub fn update_with_auth(&mut self, auth_str: &str) -> bool {
|
||||
pub fn update_with_auth(&self, auth_str: &str) -> bool {
|
||||
let splits: Vec<&str> = auth_str.split(';').collect();
|
||||
if splits.len() != 3 {
|
||||
log::warn!("Invalid auth payload: {}", auth_str);
|
||||
false
|
||||
} else {
|
||||
self.uuid = splits[0].to_owned();
|
||||
self.token = splits[1].to_owned();
|
||||
self.refresh_token = splits[2].to_owned();
|
||||
let mut lock = self.auth.write().unwrap();
|
||||
lock.uuid = splits[0].to_owned();
|
||||
lock.token = splits[1].to_owned();
|
||||
lock.refresh_token = splits[2].to_owned();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> crate::UserTy {
|
||||
RwLock::new(UserState::default())
|
||||
pub fn new(event_tx: UnboundedSender<ToSend>) -> crate::UserTy {
|
||||
UserState {
|
||||
auth: RwLock::new(Default::default()),
|
||||
event_tx,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user