Skip to content

Commit

Permalink
add docker and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian van Dijk committed Dec 5, 2024
1 parent 6a104cd commit 18113e2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
.git
.idea
.vscode
40 changes: 40 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on:
push:
branches:
- main
pull_request_target:
branches:
- main

jobs:
common:
name: common
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{github.event.pull_request.head.repo.full_name}}
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
name: format check
with:
command: fmt
args: --all -- --check
- uses: actions-rs/cargo@v1
name: clippy check
with:
command: clippy
args: -- -D warnings
37 changes: 37 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Docker
on:
push:
branches:
- main
schedule:
- cron: "54 2 2 * *"
workflow_dispatch:
jobs:
buildDockerImage:
name: Build Docker image
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/wisvch/aocbot
tags: type=sha, prefix={{date 'YYYYMMDD'}}-
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ github.ref == 'refs/heads/main' }}
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM rust:latest AS builder

WORKDIR /usr/src/aocbot
COPY . .
RUN cargo install --path .

FROM rust:1-alpine3.20

COPY --from=builder /usr/local/cargo/bin/aocbot /usr/local/bin/aocbot
CMD ["aocbot"]
47 changes: 28 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,28 @@ async fn leaderboard_total(
) -> Result<(), Error> {
let aoch_data = get_leaderboard_data().await;


let start_width: usize = aoch_data.total.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap();
let max_stars: usize = aoch_data.total.iter().map(|x| {x.stars.clone().iter().sum::<i32>()}).max().unwrap() as usize;
let row_size: usize= start_width+11+max_stars;
let row_count = 1900/row_size;

let mut table_rows = vec![];
table_rows.push(format!("{1:0$} | {2:5} | {3:5}",start_width ,"Name" ,"Score" ,"Stars"));
table_rows.push(format!("{:-^row_size$}", ""));

let mut l= aoch_data.total.into_iter().map(|x| {
table_rows.append(aoch_data.total.into_iter().map(|x| {
let name: String = remove_emoji(&x.name.unwrap_or_else(|| ANON_USER.to_string()));

let star_count = x.stars.iter().sum();
let mut stars: String = String::new();
for _ in 0..star_count {
stars += "*";
}
let mut width = start_width;
format!("{name:0$} | {1:5} | {stars} ",width ,x.score)
}).collect::<Vec<String>>();

let row_size: usize= start_width+11+max_stars;
let row_count = 1900/row_size;
let mut stylized_answer = l[0..row_count].join("\n");
format!("{name:0$} | {1:5} | {stars} ",start_width ,x.score)
}).collect::<Vec<String>>().as_mut());

let mut stylized_answer = table_rows[0..row_count].join("\n");
stylized_answer = format!("```\n{}```", truncate(&*stylized_answer, 1990));

ctx.say(stylized_answer).await?;
Expand All @@ -81,27 +85,32 @@ async fn leaderboard_total(
async fn leaderboard_today(
ctx: Context<'_>
) -> Result<(), Error> {
let resp = reqwest::get("https://aoch.wisv.ch/data").await?.text().await?;
let p: AochData = serde_json::from_str(&*resp).unwrap();
let aoch_data = get_leaderboard_data().await;

let start_width = aoch_data.today.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap();
let row_size: usize= start_width+16;
let row_count = 1900/row_size;

let ANON_USER: &str = "Anon";
let start_width = p.today.iter().map(|x| {x.name.clone().unwrap_or_else(|| ANON_USER.to_string()).len()}).max().unwrap();
let mut table_rows = vec![];
table_rows.push(format!("{1:0$} | {2:5} | {3:5}",start_width ,"Name" ,"Stars" ,"Score"));
table_rows.push(format!("{:-^row_size$}", ""));

let l= p.today.into_iter().map(|x| {
table_rows.append(aoch_data.today.into_iter().map(|x| {
let name: String = remove_emoji(&x.name.unwrap_or_else(|| ANON_USER.to_string()));
let mut width = start_width;

let mut stars: String = String::new();
if x.star1.is_some(){
stars += "*";
}
if x.star2.is_some() {
stars += "*";
}
format!("{name:0$} | {stars:2} | {1:5}",width ,x.score)
}).collect::<Vec<String>>();
let row_size: usize= start_width+13;
let row_count = 1900/row_size;
let mut stylized_answer = l[0..row_count].join("\n");

format!("{name:0$} | {stars:^5} | {1:<5}",start_width ,x.score)
}).collect::<Vec<String>>().as_mut());


let mut stylized_answer = table_rows[0..row_count].join("\n");
stylized_answer = format!("```\n{}```", truncate(&*stylized_answer, 1990));
println!("{}", stylized_answer);
ctx.say(stylized_answer).await?;
Expand Down

0 comments on commit 18113e2

Please sign in to comment.