Skip to content
This repository was archived by the owner on May 8, 2022. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,010 changes: 1,314 additions & 696 deletions Cargo.lock

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ ssl-secure = ["openssl", "actix-web/openssl"]
vendored = ["openssl", "openssl/vendored"]

[dependencies]
actix = "0.9.0"
actix-web = { version = "2.0" }
actix-web-actors = "2.0"
actix-rt = "1.1"
actix-files = "0.2.2"
rand = "0.7.3"
actix = "0.10"
actix-web = { version = "3.3" }
actix-web-actors = "3.0"
actix-rt = "2.2"
actix-files = "0.5"
rand = "0.8"
log = "0.4.8"
serde = "1.0.111"
uuid = { version = "0.8", features = ["v4", "serde"] }
jsonwebtoken = "7.1.1"
futures = "0.3.5"
actix-service = "1.0.5"
actix-service = "2.0"
serde_json = "1.0.55"
openssl = { version = "0.10.30", optional = true }
env_logger = "0.7.1"
sqlx = { version = "0.3", default-features = false, features = [ "runtime-async-std", "macros", "postgres", "json", "uuid", "chrono" ] }
refinery = "0.3.0"
sqlx-core = { version = "0.3.5", default-features = false, features = [ "runtime-async-std", "postgres", "json" ] }
env_logger = "0.8"
sqlx = { version = "0.5", default-features = false, features = [ "runtime-async-std-native-tls", "macros", "postgres", "json", "uuid", "chrono" ] }
refinery = "0.6"
sqlx-core = { version = "0.5", default-features = false, features = [ "runtime-async-std-native-tls", "postgres", "json" ] }
galaxy-rs = { git = "https://github.com/ablanleuil/galaxy-rs", version = "1.2.1" }
petgraph = "0.5.1"
petgraph = "0.6"
chrono = { version = "0.4", features = ["serde"] }
File renamed without changes.
18 changes: 9 additions & 9 deletions src/game/faction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
lib::{Result, error::{ServerError, InternalError}},
};
use uuid::Uuid;
use sqlx::{PgPool, postgres::{PgRow, PgQueryAs}, FromRow, Executor, Error, Postgres};
use sqlx::{PgPool, postgres::{ PgRow, PgQueryResult }, FromRow, Executor, Error, Postgres};
use sqlx_core::row::Row;

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -25,7 +25,7 @@ pub struct GameFaction{
pub victory_points: i32,
}

impl<'a> FromRow<'a, PgRow<'a>> for Faction {
impl<'a> FromRow<'a, PgRow> for Faction {
fn from_row(row: &PgRow) -> std::result::Result<Self, Error> {
Ok(Faction {
id: row.try_get::<i32, _>("id").map(|id| FactionID(id as u8))?,
Expand All @@ -35,7 +35,7 @@ impl<'a> FromRow<'a, PgRow<'a>> for Faction {
}
}

impl<'a> FromRow<'a, PgRow<'a>> for GameFaction {
impl<'a> FromRow<'a, PgRow> for GameFaction {
fn from_row(row: &PgRow) -> std::result::Result<Self, Error> {
Ok(GameFaction{
faction: row.try_get::<i32, _>("faction_id").map(|id| FactionID(id as u8))?,
Expand Down Expand Up @@ -99,22 +99,22 @@ impl GameFaction {
.fetch_one(db_pool).await.map_err(ServerError::if_row_not_found(InternalError::FactionUnknown))
}

pub async fn insert<E>(&self, exec: &mut E) -> Result<u64>
where E: Executor<Database = Postgres> {
pub async fn insert<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where E: Executor<'a, Database = Postgres> {
sqlx::query("INSERT INTO game__factions(game_id, faction_id, victory_points) VALUES($1, $2, $3)")
.bind(Uuid::from(self.game))
.bind(i32::from(self.faction))
.bind(self.victory_points as i16)
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn update<E>(&self, exec: &mut E) -> Result<u64>
where E: Executor<Database = Postgres> {
pub async fn update<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where E: Executor<'a, Database = Postgres> {
sqlx::query("UPDATE game__factions SET victory_points = $1 WHERE game_id = $2 AND faction_id = $3")
.bind(self.victory_points as i16)
.bind(Uuid::from(self.game))
.bind(i32::from(self.faction))
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}
}

Expand Down
32 changes: 16 additions & 16 deletions src/game/fleet/combat/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
ws::protocol,
};
use serde::{Deserialize, Serialize};
use sqlx::{PgPool, PgConnection, pool::PoolConnection, postgres::PgQueryAs, Executor, Transaction, Postgres, types::Json};
use sqlx::{PgPool, postgres::PgQueryResult, Executor, Transaction, Postgres, types::Json};
use rand::prelude::*;
use uuid::Uuid;
use std::time::Duration;
Expand Down Expand Up @@ -53,29 +53,29 @@ impl From<BattleID> for Uuid {
}

impl Battle {
pub async fn insert<E>(&self, exec: &mut E) -> Result<u64>
pub async fn insert<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where
E: Executor<Database = Postgres> {
E: Executor<'a, Database = Postgres> {
sqlx::query("INSERT INTO fleet__combat__battles(id, system_id, fleets, rounds, begun_at, ended_at) VALUES($1, $2, $3, $4, $5, $6)")
.bind(Uuid::from(self.id))
.bind(Uuid::from(self.system))
.bind(Json(&self.fleets))
.bind(Json(&self.rounds))
.bind(self.begun_at)
.bind(self.ended_at)
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn update<E>(&self, exec: &mut E) -> Result<u64>
pub async fn update<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where
E: Executor<Database = Postgres> {
E: Executor<'a, Database = Postgres> {
sqlx::query("UPDATE fleet__combat__battles SET fleets = $2, rounds = $3, victor_id = $4, ended_at = $5 WHERE id = $1")
.bind(Uuid::from(self.id))
.bind(Json(&self.fleets))
.bind(Json(&self.rounds))
.bind(self.victor.map(i32::from))
.bind(self.ended_at)
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn get_joined_fleets(&self, db_pool: &PgPool) -> Result<Vec<Fleet>> {
Expand All @@ -93,9 +93,9 @@ impl Battle {
.map_err(ServerError::from)
}

pub async fn generate_reports<E>(&self, exec: &mut E) -> Result<()>
pub async fn generate_reports<'a, E>(&self, exec: &E) -> Result<()>
where
E: Executor<Database = Postgres> {
E: Executor<'a, Database = Postgres> + Copy {
let mut players: HashSet<PlayerID> = HashSet::new();

for fleets in self.fleets.values() {
Expand All @@ -121,7 +121,7 @@ impl Battle {
for fleet in fleets.values() {
for squadron in &fleet.squadrons {
if squadron.quantity > 0 {
let initiative = (f64::from(squadron.category.to_data().combat_speed) * rng.gen_range(0.5, 1.5)).round() as i32;
let initiative = (f64::from(squadron.category.to_data().combat_speed) * rng.gen_range(0.5..1.5)).round() as i32;

squadrons.entry(initiative)
.or_default()
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Battle {

if let Some(round) = fight_round(&mut battle, round_number, new_fleets).await {
battle.rounds.push(round);
battle.update(&mut &server.state.db_pool).await?;
battle.update(&server.state.db_pool).await?;
round_number += 1;

thread::sleep(Duration::new(1, 0));
Expand All @@ -199,20 +199,20 @@ impl Battle {
}
battle.victor = Some(battle.process_victor()?);
battle.ended_at = Some(Time::now());
battle.update(&mut &server.state.db_pool).await?;
battle.update(&server.state.db_pool).await?;
battle.fleets = update_fleets(&battle, &server.state.db_pool).await?;
Ok(battle)
}
}

impl Report {
pub async fn insert<E>(&self, exec: &mut E) -> Result<u64>
pub async fn insert<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where
E: Executor<Database = Postgres> {
E: Executor<'a, Database = Postgres> + Copy {
sqlx::query("INSERT INTO fleet__combat__reports(battle_id, player_id) VALUES($1, $2)")
.bind(Uuid::from(self.battle))
.bind(Uuid::from(self.player))
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ async fn update_fleets(battle: &Battle, db_pool: &PgPool) -> Result<HashMap<Fact
Ok(remaining_fleets)
}

async fn update_fleet(mut fleet: Fleet, tx: &mut Transaction<PoolConnection<PgConnection>>) -> Result<bool> {
async fn update_fleet(mut fleet: Fleet, tx: &mut Transaction<'_, Postgres>) -> Result<bool> {
for s in &fleet.squadrons {
if s.quantity > 0 {
s.update(tx).await?;
Expand Down
34 changes: 17 additions & 17 deletions src/game/fleet/combat/conquest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use futures::{
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use sqlx::{PgPool, PgConnection, pool::PoolConnection, postgres::{PgRow, PgQueryAs}, FromRow, Executor, Error, Transaction, Postgres, types::Json};
use sqlx::{PgPool, postgres::{ PgRow, PgQueryResult}, FromRow, Executor, Error, Postgres};
use sqlx_core::row::Row;

const CONQUEST_DURATION_MAX: f64 = 60000.0;
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct ConquestData {
pub fleets: Vec<Fleet>,
}

impl<'a> FromRow<'a, PgRow<'a>> for Conquest {
impl<'a> FromRow<'a, PgRow> for Conquest {
fn from_row(row: &PgRow) -> std::result::Result<Self, Error> {
Ok(Conquest {
id: row.try_get("id").map(ConquestID)?,
Expand Down Expand Up @@ -93,19 +93,19 @@ impl GameServerTask for Conquest {
}

impl Conquest {
pub async fn insert<E>(&self, exec: &mut E) -> Result<u64>
where E: Executor<Database = Postgres> {
pub async fn insert<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where E: Executor<'a, Database = Postgres> {
sqlx::query("INSERT INTO fleet__combat__conquests (id, player_id, system_id, started_at, ended_at) VALUES($1, $2, $3, $4, $5)")
.bind(Uuid::from(self.id))
.bind(Uuid::from(self.player))
.bind(Uuid::from(self.system))
.bind(self.started_at)
.bind(self.ended_at)
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn update<E>(&self, exec: &mut E) -> Result<u64>
where E: Executor<Database = Postgres> {
pub async fn update<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where E: Executor<'a, Database = Postgres> {
sqlx::query("UPDATE fleet__combat__conquests SET
started_at = $2,
ended_at = $3,
Expand All @@ -118,14 +118,14 @@ impl Conquest {
.bind(self.is_successful)
.bind(self.is_stopped)
.bind(self.is_over)
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn remove<E>(&self, exec: &mut E) -> Result<u64>
where E: Executor<Database = Postgres> {
pub async fn remove<'a, E>(&self, exec: E) -> Result<PgQueryResult>
where E: Executor<'a, Database = Postgres> {
sqlx::query("DELETE FROM fleet__combat__conquests WHERE id = $1")
.bind(Uuid::from(self.id))
.execute(&mut *exec).await.map_err(ServerError::from)
.execute(exec).await.map_err(ServerError::from)
}

pub async fn find_by_system(sid: &SystemID, db_pool: &PgPool) -> Result<Vec<Self>> {
Expand Down Expand Up @@ -165,15 +165,15 @@ impl Conquest {
self.is_stopped = false;
self.ended_at = ms_to_time(get_conquest_time(&fleets, self.percent, game_speed));
self.started_at = Time::now();
self.update(&mut db_pool).await?;
self.update(db_pool).await?;

Ok(())
}

pub async fn cancel(&mut self, server: &GameServer) -> Result<()> {
self.ended_at = Time::now();
self.is_over = true;
self.update(&mut &server.state.db_pool).await?;
self.update(&server.state.db_pool).await?;

let conquest = self.clone();
server.ws_broadcast(&protocol::Message::new(
Expand All @@ -198,7 +198,7 @@ impl Conquest {
pub async fn halt(&mut self, state: &web::Data<AppState>, game_id: &GameID) -> Result<()> {
self.is_stopped = true;
self.percent = self.calculate_progress();
self.update(&mut &state.db_pool).await?;
self.update(&state.db_pool).await?;

state.games().get(&game_id).unwrap().do_send(cancel_task!(self));

Expand Down Expand Up @@ -269,7 +269,7 @@ impl Conquest {
is_successful: false,
is_over: false,
};
conquest.insert(&mut &server.state.db_pool).await?;
conquest.insert(&server.state.db_pool).await?;

server.ws_broadcast(&protocol::Message::new(
protocol::Action::ConquestStarted,
Expand All @@ -287,10 +287,10 @@ impl Conquest {
let fleets = system.retrieve_orbiting_fleets(&server.state.db_pool).await?.values().cloned().collect();

self.is_over = true;
self.update(&mut &server.state.db_pool).await?;
self.update(&server.state.db_pool).await?;

system.player = Some(self.player.clone());
system.update(&mut &server.state.db_pool).await?;
system.update(&server.state.db_pool).await?;

server.ws_broadcast(&protocol::Message::new(
protocol::Action::SystemConquerred,
Expand Down
2 changes: 1 addition & 1 deletion src/game/fleet/combat/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn fire(attacker: &FleetSquadron, defender: &FleetSquadron) -> (u16, u16) {
let defender_model = defender.category.to_data();

let mut rng = thread_rng();
let percent = rng.gen_range(attacker_model.precision as f64 / 2.0, attacker_model.precision as f64);
let percent = rng.gen_range((attacker_model.precision as f64 / 2.0)..(attacker_model.precision as f64));

let quantity = attacker.quantity as f64 * percent / 100.0;
let damage = (quantity * attacker_model.damage as f64 * attack_coeff).ceil() as u16;
Expand Down
Loading