mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement non-event part of singleplayer for #7
This commit is contained in:
13
rc_singleplayer_room/Cargo.toml
Normal file
13
rc_singleplayer_room/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "rc_singleplayer_room"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
log.workspace = true
|
||||
env_logger.workspace = true
|
||||
tokio = { version = "1.43", features = [ "net", "macros", "rt-multi-thread", "io-util" ] }
|
||||
clap.workspace = true
|
||||
polariton.workspace = true
|
||||
polariton_auth = { version = "*", path = "../polariton_auth" }
|
||||
polariton_server.workspace = true
|
||||
3
rc_singleplayer_room/build_arm64.sh
Executable file
3
rc_singleplayer_room/build_arm64.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
cargo build --release --target aarch64-unknown-linux-musl
|
||||
3
rc_singleplayer_room/run_debug.sh
Executable file
3
rc_singleplayer_room/run_debug.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
RUST_BACKTRACE=1 RUST_LOG=debug cargo run -- -1
|
||||
23
rc_singleplayer_room/src/cli.rs
Normal file
23
rc_singleplayer_room/src/cli.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct CliArgs {
|
||||
/// TCP port on which to accept connections
|
||||
#[arg(short, long, default_value_t = 4539)]
|
||||
pub port: u16,
|
||||
|
||||
/// IP Address on which to accept connections
|
||||
#[arg(long, default_value_t = {"127.0.0.1".to_string()})]
|
||||
pub ip: String,
|
||||
|
||||
/// Handle one connection and then exit
|
||||
#[arg(short = '1', long)]
|
||||
pub once: bool,
|
||||
}
|
||||
|
||||
impl CliArgs {
|
||||
pub fn get() -> Self {
|
||||
Self::parse()
|
||||
}
|
||||
}
|
||||
23
rc_singleplayer_room/src/data/mod.rs
Normal file
23
rc_singleplayer_room/src/data/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
pub mod player_data;
|
||||
|
||||
pub(self) fn encode_7_bit_i32(mut src: i32) -> Vec<u8> {
|
||||
if src == 0 { return vec![0] }
|
||||
let mut out = Vec::with_capacity(5);
|
||||
while src != 0 {
|
||||
let last_7 = (src & 0x7F) as u8;
|
||||
src = src >> 7;
|
||||
if src != 0 {
|
||||
out.push(last_7 | 0x80);
|
||||
} else {
|
||||
out.push(last_7);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(self) fn write_str_for_binreader(s: &str, writer: &mut dyn std::io::Write) -> std::io::Result<usize> {
|
||||
let s_bytes = s.as_bytes();
|
||||
let mut total_len = writer.write(&encode_7_bit_i32(s_bytes.len() as i32))?;
|
||||
total_len += writer.write(s_bytes)?;
|
||||
Ok(total_len)
|
||||
}
|
||||
77
rc_singleplayer_room/src/data/player_data.rs
Normal file
77
rc_singleplayer_room/src/data/player_data.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use polariton::operation::Typed;
|
||||
|
||||
pub struct PlayerData {
|
||||
pub name: String,
|
||||
pub display_name: String,
|
||||
pub mastery: i32,
|
||||
pub tier: i32,
|
||||
pub robot_name: String,
|
||||
pub robot_map: Vec<u8>,
|
||||
// -- unused i32 here --
|
||||
pub team: i32,
|
||||
pub has_premium: bool,
|
||||
pub robot_uuid: String,
|
||||
pub cpu: i32,
|
||||
pub weapon_order: Vec<i32>,
|
||||
pub colour_map: Vec<u8>,
|
||||
pub is_ai: bool,
|
||||
pub spawn_effect: String,
|
||||
pub death_effect: String,
|
||||
pub player_rank: i32,
|
||||
pub weapon_rank: std::collections::HashMap<i32, i32>,
|
||||
}
|
||||
|
||||
impl PlayerData {
|
||||
fn dump(&self, writer: &mut dyn std::io::Write) -> std::io::Result<usize> {
|
||||
let mut total_len = super::write_str_for_binreader(&self.name, writer)?;
|
||||
total_len += super::write_str_for_binreader(&self.display_name, writer)?;
|
||||
writer.write_all(&self.mastery.to_le_bytes())?;
|
||||
writer.write_all(&self.tier.to_le_bytes())?;
|
||||
total_len += super::write_str_for_binreader(&self.robot_name, writer)?;
|
||||
writer.write_all(&(self.robot_map.len() as i32).to_le_bytes())?;
|
||||
writer.write_all(&self.robot_map)?;
|
||||
writer.write_all(&[0xDE, 0xAD, 0xBE, 0xEF])?;
|
||||
writer.write_all(&self.team.to_le_bytes())?;
|
||||
writer.write_all(&[self.has_premium as u8])?;
|
||||
total_len += super::write_str_for_binreader(&self.robot_uuid, writer)?;
|
||||
writer.write_all(&self.cpu.to_le_bytes())?;
|
||||
writer.write_all(&(self.weapon_order.len() as i32).to_le_bytes())?;
|
||||
for weapon_key in self.weapon_order.iter() {
|
||||
writer.write_all(&weapon_key.to_le_bytes())?;
|
||||
}
|
||||
writer.write_all(&(self.colour_map.len() as i32).to_le_bytes())?;
|
||||
writer.write_all(&self.colour_map)?;
|
||||
writer.write_all(&[self.is_ai as u8])?;
|
||||
total_len += super::write_str_for_binreader(&self.spawn_effect, writer)?;
|
||||
total_len += super::write_str_for_binreader(&self.death_effect, writer)?;
|
||||
writer.write_all(&self.player_rank.to_le_bytes())?;
|
||||
writer.write_all(&(self.weapon_rank.len() as i32).to_le_bytes())?;
|
||||
for (key, val) in self.weapon_rank.iter() {
|
||||
writer.write_all(&key.to_le_bytes())?;
|
||||
writer.write_all(&val.to_le_bytes())?;
|
||||
}
|
||||
Ok(42 + self.robot_map.len() + (self.weapon_order.len() * 4) + self.colour_map.len() + (self.weapon_rank.len() * 8) + total_len)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PlayerDatas {
|
||||
pub players: Vec<PlayerData>,
|
||||
}
|
||||
|
||||
impl PlayerDatas {
|
||||
fn dump(&self, writer: &mut dyn std::io::Write) -> std::io::Result<usize> {
|
||||
writer.write_all(&(self.players.len() as i32).to_le_bytes())?;
|
||||
let mut total_len = 4;
|
||||
for data in self.players.iter() {
|
||||
total_len += data.dump(writer)?;
|
||||
}
|
||||
Ok(total_len)
|
||||
}
|
||||
|
||||
pub fn as_transmissible<C>(&self) -> Typed<C> {
|
||||
let mut buf = Vec::new();
|
||||
let write_size = self.dump(&mut std::io::Cursor::new(&mut buf)).unwrap();
|
||||
log::debug!("PlayerDatas serialized to {} bytes: {:?}", write_size, buf);
|
||||
Typed::Bytes(buf.into())
|
||||
}
|
||||
}
|
||||
256
rc_singleplayer_room/src/main.rs
Normal file
256
rc_singleplayer_room/src/main.rs
Normal file
@@ -0,0 +1,256 @@
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
mod data;
|
||||
mod operations;
|
||||
|
||||
use polariton_auth::Handshake;
|
||||
use tokio::net;
|
||||
|
||||
use polariton::packet::{Data, Message, Packet, StandardMessage};
|
||||
use polariton::operation::{OperationResponse, Typed};
|
||||
|
||||
pub type UserTy = std::sync::RwLock<state::UserState>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init();
|
||||
let args = cli::CliArgs::get();
|
||||
log::debug!("Got cli args {:?}", args);
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler()));
|
||||
|
||||
let ip_addr: std::net::IpAddr = args.ip.parse().expect("Invalid IP address");
|
||||
|
||||
let listener = net::TcpListener::bind(std::net::SocketAddr::new(ip_addr, args.port)).await?;
|
||||
|
||||
if args.once {
|
||||
log::warn!("Handling first connection and then exiting");
|
||||
let (socket, address) = listener.accept().await?;
|
||||
process_socket(socket, address, server).await;
|
||||
Ok(())
|
||||
} else {
|
||||
loop {
|
||||
let (socket, address) = listener.accept().await?;
|
||||
tokio::spawn(process_socket(socket, address, server.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: net::TcpStream, address: std::net::SocketAddr, server: std::sync::Arc<polariton_server::Server<crate::UserTy>>) {
|
||||
log::debug!("Accepting connection from address {}", address);
|
||||
let enc = match do_connect_handshake(&mut socket).await {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
log::error!("Failed to do connect handshake with {}", address);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let user_state = state::UserState::new();
|
||||
server.handle_async(socket, user_state, enc, Default::default()).await;
|
||||
log::debug!("Goodbye connection from address {}", address);
|
||||
}
|
||||
|
||||
const APP_ID: &str = "SinglePlayerServer";
|
||||
|
||||
struct AuthImpl;
|
||||
|
||||
const TOKEN_KEY: u8 = 216; // token;refresh_token
|
||||
//const UNKNOWN_BYTE_KEY: u8 = 217;
|
||||
const SERVICE_KEY: u8 = 224;
|
||||
const USERNAME_KEY: u8 = 225;
|
||||
|
||||
//const CCU_KEY: u8 = 245;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum AuthError {
|
||||
WrongService { expected: String, actual: String },
|
||||
MissingService,
|
||||
MissingToken,
|
||||
MissingUsername,
|
||||
}
|
||||
|
||||
impl AuthError {
|
||||
fn log_err(&self) {
|
||||
match self {
|
||||
Self::WrongService { expected, actual } => log::error!("(auth fail) Got unexpected service {}, expected {}", actual, expected),
|
||||
Self::MissingService => log::error!("(auth fail) No service name param ({}) received", SERVICE_KEY),
|
||||
Self::MissingToken => log::error!("(auth fail) No token param ({}) received", TOKEN_KEY),
|
||||
Self::MissingUsername => log::error!("(auth fail) No username param ({}) received", USERNAME_KEY),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_auth::AuthProvider<AuthError> for AuthImpl {
|
||||
fn validate(&mut self, params: &std::collections::HashMap<u8, Typed>) -> Result<std::collections::HashMap<u8, Typed>, AuthError> {
|
||||
if let Some(Typed::Str(token)) = params.get(&TOKEN_KEY) {
|
||||
if let Some(Typed::Str(service)) = params.get(&SERVICE_KEY) {
|
||||
if let Some(Typed::Str(user)) = params.get(&USERNAME_KEY) {
|
||||
if service.string == APP_ID {
|
||||
let params_resp = std::collections::HashMap::<u8, Typed>::new();
|
||||
//params_resp.insert(CCU_KEY, Typed::Byte(0));
|
||||
log::debug!("Auth success for {} (token: {})", user.string, token.string);
|
||||
Ok(params_resp)
|
||||
} else { Err(AuthError::WrongService { expected: APP_ID.to_owned(), actual: service.string.to_owned() }) }
|
||||
} else { Err(AuthError::MissingUsername) }
|
||||
} else { Err(AuthError::MissingService) }
|
||||
} else { Err(AuthError::MissingToken) }
|
||||
}
|
||||
}
|
||||
|
||||
async fn do_connect_handshake(
|
||||
socket: &mut net::TcpStream,
|
||||
) -> Option<polariton_auth::CryptoImpl> {
|
||||
let handshake = Handshake::new(APP_ID);
|
||||
// connect
|
||||
log::debug!("(connect) Handling first packet");
|
||||
let packet1 = match polariton_server::utils::receive_packet_async(socket, &Default::default()).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read connect packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let (handshake, to_send) = match handshake.connect(&packet1) {
|
||||
Ok(x) => (x.handshake, x.extra),
|
||||
Err(e) => {
|
||||
log::error!("Failed to handle connect handshake: {:?}", e.extra);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match polariton_server::utils::send_packet_async(&to_send, socket, &Default::default()).await {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
log::error!("Failed to send connect ack packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// encrypt
|
||||
log::debug!("(connect) Handling second packet");
|
||||
let mut packet2 = match polariton_server::utils::receive_packet_async(socket, &Default::default()).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) public key packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
while let Packet::Ping(ping) = packet2 {
|
||||
polariton_server::utils::handle_ping_async(ping, socket, &Default::default()).await.unwrap_or_default();
|
||||
packet2 = match polariton_server::utils::receive_packet_async(socket, &Default::default()).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) public key packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
}
|
||||
let (handshake, to_send, crypto) = match handshake.encrypt(&packet2) {
|
||||
Ok(x) => (x.handshake, x.extra.0, x.extra.1),
|
||||
Err(e) => {
|
||||
log::error!("Failed to handle encryption handshake: {:?}", e.extra);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match polariton_server::utils::send_packet_async(&to_send, socket, &Default::default()).await {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
log::error!("Failed to send encryption ack packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// pre-auth
|
||||
let handshake = handshake.with_auth(AuthImpl);
|
||||
let op_ctx = polariton::serdes::SerdesContext::default();
|
||||
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 {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) auth packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
while let Packet::Ping(ping) = packet3 {
|
||||
polariton_server::utils::handle_ping_async(ping, socket, &Default::default()).await.unwrap_or_default();
|
||||
packet3 = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) auth packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
}
|
||||
let to_send = match handshake.authenticate(&packet3, &crypto) {
|
||||
Ok(x) => x,
|
||||
Err(h) => match h.extra {
|
||||
polariton_auth::AuthError::Validation(e) => {
|
||||
e.log_err();
|
||||
return None;
|
||||
},
|
||||
e => {
|
||||
log::error!("Failed to handle auth handshake: {:?}", e);
|
||||
return None;
|
||||
},
|
||||
},
|
||||
};
|
||||
match polariton_server::utils::send_packet_async(&to_send, socket, &ctx).await {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
log::error!("Failed to send auth ack packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
// join lobby
|
||||
log::debug!("(join lobby) Handling fourth packet");
|
||||
let mut packet_j = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) join packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
while let Packet::Ping(ping) = packet_j {
|
||||
polariton_server::utils::handle_ping_async(ping, socket, &Default::default()).await.unwrap_or_default();
|
||||
packet_j = match polariton_server::utils::receive_packet_async(socket, &ctx).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read (maybe) join packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
}
|
||||
if let Packet::Packet(msg) = &packet_j {
|
||||
if let Message::Standard(st) = &msg.message {
|
||||
if let Data::OpReq(req) = &st.data {
|
||||
if req.code == 226 { // join lobby (but for real this time)
|
||||
let mut params = std::collections::HashMap::<u8, Typed>::new();
|
||||
//params.insert(252 /* actors in game */, Typed::Str(game_server_url.into()));
|
||||
params.insert(254 /* game server address */, Typed::Int(42));
|
||||
params.insert(249 /* actor properties */, Typed::HashMap(Vec::new().into()));
|
||||
params.insert(248 /* game properties */, Typed::HashMap(Vec::new().into()));
|
||||
let resp = Packet::from_message(
|
||||
Message::Standard(
|
||||
StandardMessage { flags: 0,
|
||||
data: Data::OpResp(OperationResponse {
|
||||
code: req.code,
|
||||
return_code: 0,
|
||||
message: Typed::Null,
|
||||
params: params.into(),
|
||||
}),
|
||||
}.encrypt(true)), 0, true, &ctx).unwrap();
|
||||
match polariton_server::utils::send_packet_async(&resp, socket, &ctx).await {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
log::error!("Failed to send lobby ack packet: {}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(crypto)
|
||||
}
|
||||
23
rc_singleplayer_room/src/operations/eac.rs
Normal file
23
rc_singleplayer_room/src/operations/eac.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
|
||||
pub struct EacChallengeIgnorer;
|
||||
|
||||
impl <C> Operation<C> for EacChallengeIgnorer {
|
||||
type State = ();
|
||||
type User = crate::UserTy;
|
||||
|
||||
fn handle(&self, params: polariton::operation::ParameterTable<C>, _: &mut Self::State, _: &Self::User) -> polariton::operation::OperationResponse<C> {
|
||||
polariton::operation::OperationResponse {
|
||||
code: 5, // skip the challenge (hopefully)
|
||||
return_code: 0,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationCode for EacChallengeIgnorer {
|
||||
fn op_code() -> u8 {
|
||||
4
|
||||
}
|
||||
}
|
||||
855
rc_singleplayer_room/src/operations/load_ai_robots.rs
Normal file
855
rc_singleplayer_room/src/operations/load_ai_robots.rs
Normal file
@@ -0,0 +1,855 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton::operation::ParameterTable;
|
||||
|
||||
use crate::data::player_data::*;
|
||||
|
||||
const PARAM_KEY: u8 = 8;
|
||||
|
||||
const VALID_ROBOT: &[u8] = &[64,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
38,
|
||||
190,
|
||||
25,
|
||||
77,
|
||||
24,
|
||||
6,
|
||||
29,
|
||||
0,
|
||||
80,
|
||||
135,
|
||||
103,
|
||||
211,
|
||||
27,
|
||||
4,
|
||||
27,
|
||||
6,
|
||||
80,
|
||||
135,
|
||||
103,
|
||||
211,
|
||||
21,
|
||||
4,
|
||||
27,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
27,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
28,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
26,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
25,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
27,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
28,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
26,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
25,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
28,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
27,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
26,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
25,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
27,
|
||||
23,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
28,
|
||||
23,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
5,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
5,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
5,
|
||||
29,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
5,
|
||||
28,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
5,
|
||||
28,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
5,
|
||||
26,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
5,
|
||||
26,
|
||||
0,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
24,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
23,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
22,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
21,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
20,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
19,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
18,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
17,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
16,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
15,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
24,
|
||||
4,
|
||||
14,
|
||||
22,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
17,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
17,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
16,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
16,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
15,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
15,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
23,
|
||||
4,
|
||||
14,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
25,
|
||||
4,
|
||||
14,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
17,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
17,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
16,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
16,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
15,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
15,
|
||||
6,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
22,
|
||||
4,
|
||||
14,
|
||||
12,
|
||||
198,
|
||||
224,
|
||||
138,
|
||||
13,
|
||||
26,
|
||||
4,
|
||||
14,
|
||||
6,
|
||||
240,
|
||||
75,
|
||||
110,
|
||||
137,
|
||||
21,
|
||||
4,
|
||||
15,
|
||||
12,
|
||||
240,
|
||||
75,
|
||||
110,
|
||||
137,
|
||||
27,
|
||||
4,
|
||||
15,
|
||||
6];
|
||||
|
||||
const VALID_COLOUR: &[u8] = &[64,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
24,
|
||||
6,
|
||||
29,
|
||||
0,
|
||||
27,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
21,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
29,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
25,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
26,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
27,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
28,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
29,
|
||||
1,
|
||||
25,
|
||||
5,
|
||||
29,
|
||||
1,
|
||||
23,
|
||||
5,
|
||||
29,
|
||||
1,
|
||||
24,
|
||||
5,
|
||||
29,
|
||||
1,
|
||||
22,
|
||||
5,
|
||||
28,
|
||||
1,
|
||||
26,
|
||||
5,
|
||||
28,
|
||||
1,
|
||||
25,
|
||||
5,
|
||||
26,
|
||||
1,
|
||||
23,
|
||||
5,
|
||||
26,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
24,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
23,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
22,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
21,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
20,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
19,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
18,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
17,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
16,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
24,
|
||||
4,
|
||||
14,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
17,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
17,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
16,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
16,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
23,
|
||||
4,
|
||||
14,
|
||||
0,
|
||||
25,
|
||||
4,
|
||||
14,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
17,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
17,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
16,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
16,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
22,
|
||||
4,
|
||||
14,
|
||||
0,
|
||||
26,
|
||||
4,
|
||||
14,
|
||||
0,
|
||||
21,
|
||||
4,
|
||||
15,
|
||||
0,
|
||||
27,
|
||||
4,
|
||||
15];
|
||||
|
||||
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 mut params = params.to_dict();
|
||||
params.insert(PARAM_KEY, PlayerDatas {
|
||||
players: vec![
|
||||
PlayerData {
|
||||
name: ulock.uuid.clone(),
|
||||
display_name: ulock.uuid.clone(),
|
||||
mastery: 1,
|
||||
tier: 1,
|
||||
robot_name: "RE_machine_name_mine_sp".to_owned(),
|
||||
robot_map: VALID_ROBOT.iter().map(|x| *x).collect(),
|
||||
team: 0,
|
||||
has_premium: false,
|
||||
robot_uuid: "12345_12345".to_owned(),
|
||||
cpu: 0,
|
||||
weapon_order: vec![20000200, 0, 0],
|
||||
colour_map: VALID_COLOUR.iter().map(|x| *x).collect(),
|
||||
is_ai: false,
|
||||
spawn_effect: "Spawn_Warp".to_owned(),
|
||||
death_effect: "Explosion_Warp".to_owned(),
|
||||
player_rank: 1,
|
||||
weapon_rank: vec![(20000200, 1), (0, 0)].into_iter().collect(),
|
||||
},
|
||||
// TODO: use SingleplayerEvent 3 (SpawnRobot) to spawn enemy bots instead
|
||||
// doing it through this op response seems to have a bug/flaw (intentional?) in the code
|
||||
/*PlayerData {
|
||||
name: "RE_username0".to_owned(),
|
||||
display_name: "RE_displayname0".to_owned(),
|
||||
mastery: 1,
|
||||
tier: 1,
|
||||
robot_name: "RE_machine_name0_sp".to_owned(),
|
||||
robot_map: VALID_ROBOT.iter().map(|x| *x).collect(),
|
||||
team: 1,
|
||||
has_premium: false,
|
||||
robot_uuid: "123_123".to_owned(),
|
||||
cpu: 0,
|
||||
weapon_order: vec![20000200, 0, 0],
|
||||
colour_map: VALID_COLOUR.iter().map(|x| *x).collect(),
|
||||
is_ai: true,
|
||||
spawn_effect: "Spawn_Warp".to_owned(),
|
||||
death_effect: "Explosion_Warp".to_owned(),
|
||||
player_rank: 1,
|
||||
weapon_rank: vec![(20000200, 1), (0, 0)].into_iter().collect(),
|
||||
},
|
||||
PlayerData {
|
||||
name: "RE_username1".to_owned(),
|
||||
display_name: "RE_displayname1".to_owned(),
|
||||
mastery: 1,
|
||||
tier: 1,
|
||||
robot_name: "RE_machine_name1_sp".to_owned(),
|
||||
robot_map: VALID_ROBOT.iter().map(|x| *x).collect(),
|
||||
team: 1,
|
||||
has_premium: false,
|
||||
robot_uuid: "1_1".to_owned(),
|
||||
cpu: 0,
|
||||
weapon_order: vec![20000200, 0, 0],
|
||||
colour_map: VALID_COLOUR.iter().map(|x| *x).collect(),
|
||||
is_ai: true,
|
||||
spawn_effect: "Spawn_Warp".to_owned(),
|
||||
death_effect: "Explosion_Warp".to_owned(),
|
||||
player_rank: 1,
|
||||
weapon_rank: vec![(20000200, 1), (0, 0)].into_iter().collect(),
|
||||
},*/
|
||||
]
|
||||
}.as_transmissible());
|
||||
Ok(params.into())
|
||||
})
|
||||
}
|
||||
13
rc_singleplayer_room/src/operations/mod.rs
Normal file
13
rc_singleplayer_room/src/operations/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod more_auth;
|
||||
mod eac;
|
||||
mod load_ai_robots;
|
||||
|
||||
use polariton_server::operations::OperationsHandler;
|
||||
|
||||
pub fn handler() -> OperationsHandler<crate::UserTy> {
|
||||
OperationsHandler::<crate::UserTy>::new()
|
||||
.without_state(more_auth::MoreLobbyAuth)
|
||||
.without_state(eac::EacChallengeIgnorer)
|
||||
.without_state(load_ai_robots::tdm_machines_provider())
|
||||
//.without_state(polariton_server::operations::Ack::<33, _>::default()) // get user clan info (this is equivalent to not being in a clan)
|
||||
}
|
||||
42
rc_singleplayer_room/src/operations/more_auth.rs
Normal file
42
rc_singleplayer_room/src/operations/more_auth.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use polariton::operation::Typed;
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
|
||||
pub struct MoreLobbyAuth;
|
||||
|
||||
impl MoreLobbyAuth {
|
||||
const AUTH_PAYLOAD_KEY: u8 = 245;
|
||||
}
|
||||
|
||||
impl <C> Operation<C> for MoreLobbyAuth {
|
||||
type State = ();
|
||||
type User = crate::UserTy;
|
||||
|
||||
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 resp_params = std::collections::HashMap::new();
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return polariton::operation::OperationResponse {
|
||||
code: 230,
|
||||
return_code: 0,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: resp_params.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
polariton::operation::OperationResponse {
|
||||
code: 230,
|
||||
return_code: 120,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: std::collections::HashMap::new().into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationCode for MoreLobbyAuth {
|
||||
fn op_code() -> u8 {
|
||||
230
|
||||
}
|
||||
}
|
||||
27
rc_singleplayer_room/src/state.rs
Normal file
27
rc_singleplayer_room/src/state.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct UserState {
|
||||
pub uuid: String,
|
||||
pub token: String,
|
||||
pub refresh_token: String,
|
||||
}
|
||||
|
||||
impl UserState {
|
||||
pub fn update_with_auth(&mut 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();
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> crate::UserTy {
|
||||
RwLock::new(UserState::default())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user