Skip to content

Commit

Permalink
RPCN 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
RipleyTom committed Feb 23, 2024
1 parent 46f245d commit ca1a3a4
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- run: cp rpcn.cfg artifact/rpcn.cfg
- run: cp servers.cfg artifact/servers.cfg
- run: cp server_redirs.cfg artifact/server_redirs.cfg
- run: cp scoreboards.cfg artifact/scoreboards.cfg
- name: Create artifact
run: cd artifact && tar -zcvf ../rpcn-linux.tar.gz ./ && cd ..
- name: Upload artifact
Expand Down Expand Up @@ -44,6 +45,7 @@ jobs:
- run: copy rpcn.cfg artifact-win/rpcn.cfg
- run: copy servers.cfg artifact-win/servers.cfg
- run: copy server_redirs.cfg artifact-win/server_redirs.cfg
- run: copy scoreboards.cfg artifact-win/scoreboards.cfg
- name: Create artifact
run: Compress-Archive -Path artifact-win/* -Destination rpcn-win.zip
- name: Upload artifact
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2024-02-23

### Added

- Added a configuration file for scoreboards

### Misc

- Version change triggered by a protocol change on rpcs3's side


## [1.1.0] - 2024-02-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rpcn"
version = "1.1.0"
version = "1.2.0"
authors = ["RipleyTom <RipleyTom@users.noreply.github.com>"]
edition = "2021"

Expand Down
11 changes: 11 additions & 0 deletions scoreboards.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Syntax is communication_id|board_ids|rank_limit|update_mode|sort_mode|upload_num_limit|upload_size_limit
#board_ids can be multiple board ids separated by a comma
#update_mode values:
#NORMAL_UPDATE
#FORCE_UPDATE
#sort_mode values:
#DESCENDING_ORDER
#ASCENDING_ORDER

#GTI Club
NPWR00284|0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16|100|NORMAL_UPDATE|ASCENDING_ORDER|10|6000000
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Config {
let config_data: HashMap<&str, &str> = buf_file
.lines()
.filter_map(|l| {
if l.is_empty() || l.chars().nth(0).unwrap() == '#' {
if l.trim().is_empty() || l.trim().chars().nth(0).unwrap() == '#' {
return None;
}

Expand Down Expand Up @@ -204,7 +204,7 @@ impl Config {
self.server_redirs = buf_file
.lines()
.filter_map(|line| {
let parsed: Vec<&[u8]> = line.trim().split("=>").map(|x| x.as_bytes()).collect();
let parsed: Vec<&[u8]> = line.trim().split("=>").map(|x| x.trim()).map(|x| x.as_bytes()).collect();
if line.is_empty() || line.chars().nth(0).unwrap() == '#' || parsed.len() != 2 || parsed[0].len() != 9 || parsed[1].len() != 9 {
None
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::Config;
#[allow(non_snake_case, dead_code)]
mod stream_extractor;

const PROTOCOL_VERSION: u32 = 23;
const PROTOCOL_VERSION: u32 = 24;

pub struct Server {
config: Arc<RwLock<Config>>,
Expand Down
2 changes: 1 addition & 1 deletion src/server/client/cmd_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Client {
n_msg.push(0);
let friend_n = Client::create_notification(NotificationType::SignalingInfo, &n_msg);
self.send_single_notification(&friend_n, user_id).await;
return Ok(ErrorType::NoError);
Ok(ErrorType::NoError)
}

pub fn req_ticket(&mut self, data: &mut StreamExtractor, reply: &mut Vec<u8>) -> Result<ErrorType, ErrorType> {
Expand Down
96 changes: 95 additions & 1 deletion src/server/client/cmd_score.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Score Commands

use std::fs::File;
use std::io::Read;
use std::sync::atomic::{AtomicU64, Ordering};

use tokio::fs;

use crate::server::client::*;
use crate::server::database::db_score::DbScoreInfo;
use crate::server::database::db_score::{DbBoardInfo, DbScoreInfo};
use crate::server::database::DbError;
use crate::server::Server;

Expand All @@ -14,6 +16,98 @@ const SCORE_FILE_EXTENSION: &str = "sdt";
static SCORE_DATA_ID_DISPENSER: AtomicU64 = AtomicU64::new(1);

impl Server {
pub fn setup_config_scoreboards(db: &Database) -> Result<(), String> {
let file = File::open("scoreboards.cfg");

if let Err(e) = file {
return Err(format!("Unexpected error opening scoreboards.cfg: {}", e));
}
let mut file = file.unwrap();

let mut buf_file = String::new();
file.read_to_string(&mut buf_file).map_err(|e| format!("Failed to read scoreboards.cfg: {}", e))?;

struct ScoreboardConfig {
communication_id: String,
tables: Vec<u32>,
rank_limit: u32,
update_mode: u32,
sort_mode: u32,
upload_num_limit: u32,
upload_size_limit: u32,
}

let config_scoreboards: Vec<ScoreboardConfig> = buf_file
.lines()
.filter_map(|l| {
if l.trim().is_empty() || l.trim().chars().nth(0).unwrap() == '#' {
return None;
}

let board_infos: Vec<&str> = l.trim().split('|').map(|x| x.trim()).collect();
if board_infos.len() != 7 {
println!("scoreboards.cfg: line({}) is invalid", l);
return None;
}

let communication_id = board_infos[0].to_owned();
let tables: Result<Vec<u32>, _> = board_infos[1].split(',').map(|x| x.trim()).map(|x| x.parse::<u32>()).collect();

if tables.is_err() || tables.as_ref().unwrap().is_empty() {
println!("scoreboards.cfg: line({}) contains invalid table ids", l);
return None;
}

let rank_limit = board_infos[2].parse::<u32>();
let update_mode = match board_infos[3] {
"NORMAL_UPDATE" => Some(0u32),
"FORCE_UPDATE" => Some(1u32),
_ => None,
};
let sort_mode = match board_infos[4] {
"DESCENDING_ORDER" => Some(0u32),
"ASCENDING_ORDER" => Some(1u32),
_ => None,
};
let upload_num_limit = board_infos[5].parse::<u32>();
let upload_size_limit = board_infos[6].parse::<u32>();

if rank_limit.is_err() || update_mode.is_none() || sort_mode.is_none() || upload_num_limit.is_err() || upload_size_limit.is_err() {
println!("scoreboards.cfg: line({}) contains invalid data", l);
return None;
}

Some(ScoreboardConfig {
communication_id,
tables: tables.unwrap(),
rank_limit: rank_limit.unwrap(),
update_mode: update_mode.unwrap(),
sort_mode: sort_mode.unwrap(),
upload_num_limit: upload_num_limit.unwrap(),
upload_size_limit: upload_size_limit.unwrap(),
})
})
.collect();

for config in &config_scoreboards {
let db_infos = DbBoardInfo {
rank_limit: config.rank_limit,
update_mode: config.update_mode,
sort_mode: config.sort_mode,
upload_num_limit: config.upload_num_limit,
upload_size_limit: config.upload_size_limit,
};

for table in &config.tables {
if db.create_or_set_score_board_details(&config.communication_id, *table, &db_infos).is_err() {
return Err("scoreboards.cfg: error setting up table".to_string());
}
}
}

Ok(())
}

pub fn initialize_score_data_handler() -> Result<(), String> {
let max = Server::create_data_directory(SCORE_DATA_DIRECTORY, SCORE_FILE_EXTENSION)?;
SCORE_DATA_ID_DISPENSER.store(max, Ordering::SeqCst);
Expand Down
4 changes: 2 additions & 2 deletions src/server/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ impl Server {
let config_servers: Vec<(String, u16, u32, Vec<u64>)> = buf_file
.lines()
.filter_map(|l| {
if l.trim().is_empty() || l.chars().nth(0).unwrap() == '#' {
if l.trim().is_empty() || l.trim().chars().nth(0).unwrap() == '#' {
return None;
}

let servers_infos: Vec<&str> = l.trim().split('|').collect();
let servers_infos: Vec<&str> = l.trim().split('|').map(|v| v.trim()).collect();
if servers_infos.len() != 4 || servers_infos[0].len() != 9 {
println!("servers.cfg: line({}) was considered invalid and was skipped", l);
return None;
Expand Down
22 changes: 22 additions & 0 deletions src/server/database/db_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ impl Database {
Ok(())
}

pub fn create_or_set_score_board_details(&self, com_id: &str, board_id: u32, board_infos: &DbBoardInfo) -> Result<(), DbError> {
let res = self.conn.execute(
"INSERT INTO score_table ( communication_id, board_id, rank_limit, update_mode, sort_mode, upload_num_limit, upload_size_limit ) VALUES ( ?1, ?2, ?3, ?4, ?5, ?6, ?7 ) ON CONFLICT( communication_id, board_id ) DO UPDATE SET rank_limit = excluded.rank_limit, update_mode = excluded.update_mode, sort_mode = excluded.sort_mode, upload_num_limit = excluded.upload_num_limit, upload_size_limit = excluded.upload_size_limit",
rusqlite::params![
com_id,
board_id,
board_infos.rank_limit,
board_infos.update_mode,
board_infos.sort_mode,
board_infos.upload_num_limit,
board_infos.upload_size_limit,
],
);

if let Err(e) = res {
println!("Unexpected error setting score table details: {}", e);
return Err(DbError::Internal);
}

Ok(())
}

pub fn get_score_tables(&self) -> Result<HashMap<(String, u32), DbBoardInfo>, DbError> {
let mut stmt = self
.conn
Expand Down
4 changes: 3 additions & 1 deletion src/server/score_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ impl TableDescription {

impl Server {
pub fn initialize_score(conn: r2d2::PooledConnection<r2d2_sqlite::SqliteConnectionManager>) -> Result<Arc<ScoresCache>, String> {
let db = Database::new(conn);

Server::setup_config_scoreboards(&db)?;
Server::initialize_score_data_handler()?;

let cache = Arc::new(ScoresCache::new());

// Populate cache from database
let db = Database::new(conn);
let tables = db.get_score_tables().map_err(|_| "Failed to read database scores for the cache")?;

let mut users_list: HashSet<i64> = HashSet::new();
Expand Down

0 comments on commit ca1a3a4

Please sign in to comment.