Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/map-exclusion' into merge-yeet-d…
Browse files Browse the repository at this point in the history
…iesel
  • Loading branch information
Razer2015 committed Nov 22, 2022
2 parents e9a1e25 + 145915f commit 93ef3b9
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 43 deletions.
78 changes: 54 additions & 24 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: Build and Deploy
name: CI

on:
push:
tags:
- "*"
# branches:
# - main
pull_request:
workflow_dispatch: # enable button on github to manually trigger this

Expand All @@ -17,21 +19,21 @@ defaults:
shell: bash

jobs:
# test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Cargo cache
# uses: actions/cache@v2
# with:
# path: |
# ~/.cargo/registry
# ./target
# key: test-cargo-registry
# - name: List
# run: find ./
# - name: Run tests
# run: cargo test --verbose
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cargo cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
./target
key: test-cargo-registry
- name: List
run: find ./
- name: Run tests
run: cargo test --verbose

build:
strategy:
Expand Down Expand Up @@ -62,7 +64,8 @@ jobs:
# needs: test
runs-on: ${{ matrix.OS }}
env:
NAME: battlefox
BINARY_1: battlefox
BINARY_2: battlefox_discord
TARGET: ${{ matrix.TARGET }}
OS: ${{ matrix.OS }}
steps:
Expand Down Expand Up @@ -105,22 +108,36 @@ jobs:
run: cargo build --release --verbose --target $TARGET
- name: List target
run: find ./target
- name: Compress
- name: Compress battlefox
run: |
mkdir -p ./artifacts
# windows is the only OS using a different convention for executable file name
if [[ $OS =~ ^windows.*$ ]]; then
EXEC=$NAME.exe
EXEC=$BINARY_1.exe
else
EXEC=$NAME
EXEC=$BINARY_1
fi
if [[ $GITHUB_REF_TYPE =~ ^tag$ ]]; then
TAG=$GITHUB_REF_NAME
else
TAG=$GITHUB_SHA
fi
mv ./target/$TARGET/release/$EXEC ./$EXEC
tar -czf ./artifacts/$NAME-$TARGET-$TAG.tar.gz $EXEC
tar -czf ./artifacts/$BINARY_1-$TARGET-$TAG.tar.gz -C ./target/$TARGET/release/ $EXEC
- name: Compress battlefox_discord
run: |
mkdir -p ./artifacts
# windows is the only OS using a different convention for executable file name
if [[ $OS =~ ^windows.*$ ]]; then
EXEC=$BINARY_2.exe
else
EXEC=$BINARY_2
fi
if [[ $GITHUB_REF_TYPE =~ ^tag$ ]]; then
TAG=$GITHUB_REF_NAME
else
TAG=$GITHUB_SHA
fi
tar -czf ./artifacts/$BINARY_2-$TARGET-$TAG.tar.gz -C ./target/$TARGET/release/ $EXEC
- name: Archive artifact
uses: actions/upload-artifact@v2
with:
Expand Down Expand Up @@ -171,12 +188,25 @@ jobs:
- name: Build battlefox and push
uses: docker/build-push-action@v2
with:
context: .
context: ./battlefox
platforms: linux/amd64
# platforms: linux/amd64,linux/arm64
file: ./Dockerfile.cross-platform
file: ./battlefox/Dockerfile.cross-platform
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/battlefox:${{ env.COMMIT_TAG }},${{ secrets.DOCKER_USERNAME }}/battlefox:latest
build-args: |
REPO_URL=${{ github.server_url }}/${{ github.repository }}
TAG=${{ env.COMMIT_TAG }}
# Building image and pushing it to DockerHub
- name: Build battlefox_discord and push
uses: docker/build-push-action@v2
with:
context: ./battlefox_discord
platforms: linux/amd64
# platforms: linux/amd64,linux/arm64
file: ./battlefox_discord/Dockerfile.cross-platform
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/battlefox_discord:${{ env.COMMIT_TAG }},${{ secrets.DOCKER_USERNAME }}/battlefox_discord:latest
build-args: |
REPO_URL=${{ github.server_url }}/${{ github.repository }}
TAG=${{ env.COMMIT_TAG }}
24 changes: 24 additions & 0 deletions battlefield_rcon/src/bf4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use error::Bf4Error;
use futures_core::Stream;
use player_info_block::{parse_pib, PlayerInfo};
use server_info::{parse_serverinfo, ServerInfo};
use team_scores::{parse_team_scores};
use tokio::{
net::ToSocketAddrs,
sync::{broadcast, mpsc, oneshot},
Expand All @@ -22,6 +23,7 @@ pub mod map_list;
pub(crate) mod player_cache;
pub mod player_info_block;
pub mod server_info;
pub mod team_scores;
mod util;
pub mod ban_list;

Expand Down Expand Up @@ -350,6 +352,28 @@ impl Bf4Client {
winning_team: Team::rcon_decode(&packet.words[1])?,
})
}
"server.onRoundOverTeamScores" => {
if packet.words.len() < 5 {
return Err(Bf4Error::Rcon(RconError::malformed_packet(
packet.words.clone(),
format!("{} packet must have at least {} words", &packet.words[0], 5),
)));
}

let team_scores = parse_team_scores(&packet.words)?;

Ok(Event::RoundOverTeamScores {
number_of_entries: team_scores.number_of_entries,
scores: team_scores.scores,
target_score: team_scores.target_score,
})
}
"server.onRoundOverPlayers" => {
let pib = parse_pib(&packet.words[1..])?;
Ok(Event::RoundOverPlayers {
players: pib,
})
}
"punkBuster.onMessage" => {
assert_len(&packet, 2)?;
Ok(Event::PunkBusterMessage(packet.words[1].to_string()))
Expand Down
8 changes: 8 additions & 0 deletions battlefield_rcon/src/bf4/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ pub enum Event {
RoundOver {
winning_team: Team,
},
RoundOverTeamScores {
number_of_entries: i32,
scores: Vec<i32>,
target_score: i32,
},
RoundOverPlayers {
players: Vec<PlayerInfo>
},
Join {
player: Player,
},
Expand Down
36 changes: 36 additions & 0 deletions battlefield_rcon/src/bf4/team_scores.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::bf4::util::{parse_int};
use crate::rcon::{RconError, RconResult};
use ascii::AsciiString;
use serde::{Deserialize, Serialize};

///
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TeamScores {
pub number_of_entries: i32,
pub scores: Vec<i32>,
pub target_score: i32,
}

/// Expects the TeamScores, without any leading "OK".
pub fn parse_team_scores(words: &[AsciiString]) -> RconResult<TeamScores> {
if words.is_empty() {
return Err(RconError::protocol_msg(
"Failed to parse TeamScores: Zero length?",
));
}

let teams_count = parse_int(&words[1])? as usize;
let mut offset = 2;
let mut teamscores: Vec<i32> = Vec::new();
for _ in 0..teams_count {
teamscores.push(parse_int(&words[offset]).unwrap());

offset += 1;
}

Ok(TeamScores {
number_of_entries: teams_count as i32,
scores: teamscores,
target_score: parse_int(&words[offset]).unwrap(),
})
}
33 changes: 33 additions & 0 deletions battlefox/src/mapmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct PopState {
pub pool: MapPool,
/// At `min_players` or more players, activate this pool. Unless a pool with even higher `min_players` exists.
pub min_players: usize,
/// The amount of map history to be excluded when generating a new vote
pub excluded_maps_count: Option<usize>,
}

impl PopState {
Expand Down Expand Up @@ -128,6 +130,9 @@ impl Plugin for MapManager {
// Err(MapListError::Rcon(r)) => return Err(r),
Err(mle) => error!("While starting up MapManager: {:?}. MapManager is *not* starting now!", mle),
}

// Populate the current map in the history
let _ = self.current_map(&bf4).await;
}

async fn event(self: Arc<Self>, bf4: Arc<Bf4Client>, event: Event) -> RconResult<()> {
Expand Down Expand Up @@ -393,6 +398,34 @@ impl MapManager {
Some(hist[0])
}
}

pub fn recent_maps(&self) -> Vec<Map> {
let hist = {
let inner = self.inner.lock().unwrap();
inner.map_history.clone()
};

let popstate = {
let lock = self.inner.lock().unwrap();
lock.pop_state.clone()
};

hist.iter()
.take(popstate.excluded_maps_count.unwrap_or(1))
.cloned()
.collect()
}

pub fn is_recently_played(&self, map: &Map) -> bool {
let recent_maps = self.recent_maps();

for m in recent_maps.iter() {
if map.eq(&m) {
return true;
}
};
false
}
}

impl std::fmt::Debug for MapManager {
Expand Down
12 changes: 12 additions & 0 deletions battlefox/src/mapmanager/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ impl MapPool {
.collect(),
}
}

/// Returns a new map pool which contains the same items, except with any with `map` removed.
pub fn without_many_vec(&self, maps: &Vec<Map>) -> Self {
Self {
pool: self
.pool
.iter()
.filter(|&mip| !maps.contains(&mip.map))
.cloned()
.collect(),
}
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 93ef3b9

Please sign in to comment.