init
CI/CD / fmt (push) Failing after 21s
CI/CD / clippy (push) Successful in 20s
CI/CD / test (push) Successful in 24s
CI/CD / Release (push) Skipped

This commit is contained in:
nicknase27 committed 2026-09-26 20:48:40 +02:00
commit f5b31ad2a6
7 files changed
+2491

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
name: CI/CD
on:
pull_request:
push:
branches:
- main
- master
- develop
tags:
- "v*"
env:
CARGO_TERM_COLOR: always
# Ensure the workflow is only triggered once per PR; subsequent pushes cancel in-progress runs
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
fmt:
name: fmt
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check formatting
run: cargo fmt -- --check
clippy:
name: clippy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2
- name: Run clippy action
uses: clechasseur/rs-clippy-check@v3
test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Generate lockfile if missing
if: hashFiles('Cargo.lock') == ''
run: cargo generate-lockfile
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --locked --all-features --all-targets
release:
name: Release
needs: [fmt, clippy, test]
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: |
x86_64-unknown-linux-gnu
x86_64-pc-windows-gnu
- uses: Swatinem/rust-cache@v2
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y \
pkg-config \
libasound2-dev \
gcc-mingw-w64-x86-64
- name: Build Linux
run: cargo build --release --target x86_64-unknown-linux-gnu
- name: Build Windows
env:
CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER: x86_64-w64-mingw32-gcc
run: cargo build --release --target x86_64-pc-windows-gnu
- name: Collect artifacts
run: |
mkdir artifacts
cp target/x86_64-unknown-linux-gnu/release/pongrs artifacts/pongrs
cp target/x86_64-pc-windows-gnu/release/pongrs.exe artifacts/pongrs.exe
- name: Create Release
uses: akkuman/gitea-release-action@v1
with:
files: |
artifacts/pongrs
artifacts/pongrs.exe
+1
View File
@@ -0,0 +1 @@
/target
Generated
+1998
View File
File diff suppressed because it is too large. Load diff
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "pongrs"
version = "0.1.0"
description = "An example generated using the simple-async template"
authors = ["nicknase27 <nick@nicknase27.com>"]
license = "MIT"
edition = "2024"
[dependencies]
color-eyre = "0.6.5"
crossterm = { version = "0.29.0", features = ["event-stream"] }
futures = "0.3.32"
rand = "0.10.3"
ratatui = "0.30.2"
tokio = { version = "1.52.3", features = ["full"] }
# Read the optimization guideline for more details: https://ratatui.rs/recipes/apps/release-your-app/#optimizations
[profile.release]
codegen-units = 1
lto = true
opt-level = "s"
strip = true
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) nicknase27 <nick@nicknase27.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+14
View File
@@ -0,0 +1,14 @@
# pongrs
This is a [Ratatui] app generated by the [Simple async template].
[Ratatui]: https://ratatui.rs
[Simple Template]: https://github.com/ratatui/templates/tree/main/simple-async
## License
Copyright (c) nicknase27 <nick@nicknase27.com>
This project is licensed under the MIT license ([LICENSE] or <http://opensource.org/licenses/MIT>)
[LICENSE]: ./LICENSE
+326
View File
@@ -0,0 +1,326 @@
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use futures::{FutureExt, StreamExt};
use rand::random_range;
use ratatui::layout::Constraint::Percentage;
use ratatui::layout::HorizontalAlignment::Center;
use ratatui::style::Stylize;
use ratatui::symbols::Marker;
use ratatui::widgets::canvas::{Canvas, Points, Rectangle};
use ratatui::widgets::{Clear, Paragraph};
use ratatui::{
DefaultTerminal, Frame,
layout::{Constraint, Direction, Layout},
widgets::Block,
};
use tokio::time::{Duration, interval};
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let result = App::new().run(terminal).await;
ratatui::restore();
result
}
#[derive(Debug, Default)]
enum Gamestate {
#[default]
Playing,
Paused,
Won,
Lost,
}
#[derive(Debug)]
pub struct Player {
x: f64,
y: f64,
}
impl Default for Player {
fn default() -> Self {
Player { x: 0.0, y: 5.0 }
}
}
#[derive(Debug)]
pub struct Enemy {
x: f64,
y: f64,
}
impl Default for Enemy {
fn default() -> Self {
Enemy { x: 49.0, y: 5.0 }
}
}
#[derive(Debug)]
pub struct Ball {
x: f64,
y: f64,
x_velocity: f64,
y_velocity: f64,
}
impl Default for Ball {
fn default() -> Self {
Ball {
x: 25.0,
y: 7.0,
x_velocity: 0.5,
y_velocity: 0.5,
}
}
}
impl Ball {
pub fn update(&mut self) {
self.x += self.x_velocity;
self.y += self.y_velocity;
}
pub fn reset(&mut self) {
self.x = random_range(25.0..=40.0);
self.y = 7.0;
self.x_velocity = -0.5;
self.y_velocity = 0.5;
}
}
#[derive(Debug, Default)]
pub struct App {
running: bool,
paused: bool,
event_stream: EventStream,
player: Player,
player_score: i32,
enemy_score: i32,
enemy: Enemy,
ball: Ball,
state: Gamestate,
}
impl App {
pub fn new() -> Self {
Self::default()
}
pub fn restart(&mut self) {
self.player_score = 0;
self.enemy_score = 0;
self.state = Gamestate::Playing;
self.player = Player::default();
self.enemy = Enemy::default();
self.ball = Ball::default();
}
pub fn update(&mut self) {
if self.paused != true {
self.ball.update();
self.update_enemy();
self.check_collisions();
self.check_state();
}
}
pub fn check_state(&mut self) {
if self.player_score >= 5 {
self.state = Gamestate::Won;
} else if self.enemy_score >= 5 {
self.state = Gamestate::Lost;
}
}
pub fn update_enemy(&mut self) {
let enemy_center = self.enemy.y + 2.0;
if self.ball.y > enemy_center {
if self.enemy.y + 4.0 < 15.0 {
self.enemy.y += 0.25;
}
} else if self.ball.y < enemy_center {
if self.enemy.y > 0.0 {
self.enemy.y -= 0.25;
}
}
}
pub fn check_collisions(&mut self) {
// Top/bottom walls
if self.ball.y <= 0.0 || self.ball.y >= 15.0 {
self.ball.y_velocity *= -1.0;
}
if self.ball.x <= 0.0 {
self.enemy_score += 1;
self.ball.reset();
return;
}
if self.ball.x >= 50.0 {
self.player_score += 1;
self.ball.reset();
return;
}
// Player paddle
if self.ball.x <= self.player.x + 1.0
&& self.ball.y >= self.player.y
&& self.ball.y <= self.player.y + 4.0
&& self.ball.x_velocity < 0.0
{
self.ball.x_velocity *= -1.0;
}
// Enemy paddle
if self.ball.x >= self.enemy.x - 1.0
&& self.ball.y >= self.enemy.y
&& self.ball.y <= self.enemy.y + 4.0
&& self.ball.x_velocity > 0.0
{
self.ball.x_velocity *= -1.0;
}
}
pub async fn run(mut self, mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
self.running = true;
let mut interval = interval(Duration::from_millis(50));
while self.running {
terminal.draw(|frame| self.draw(frame))?;
tokio::select! {
_ = interval.tick() => {
self.update();
}
_ = self.handle_crossterm_events() => {}
}
}
Ok(())
}
fn draw(&mut self, frame: &mut Frame) {
let window = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100)])
.split(frame.area());
let popup_block = Block::new();
let centered_area = window[0].centered(Percentage(60), Percentage(20));
match self.state {
Gamestate::Playing => {
let canvas = Canvas::default()
.block(Block::bordered().title(format!(
"PongRS | {} - {}",
self.player_score, self.enemy_score
)))
.x_bounds([0.0, 50.0])
.y_bounds([0.0, 15.0])
.marker(Marker::Block)
.paint(|ctx| {
ctx.draw(&Points {
coords: &[(self.ball.x, self.ball.y)],
color: ratatui::style::Color::Yellow,
});
ctx.draw(&Rectangle {
x: self.player.x,
y: self.player.y,
width: 1.0,
height: 4.0,
color: ratatui::style::Color::Green,
});
ctx.draw(&Rectangle {
x: self.enemy.x,
y: self.enemy.y,
width: 1.0,
height: 4.0,
color: ratatui::style::Color::Red,
});
});
frame.render_widget(canvas, window[0]);
}
Gamestate::Paused => {
frame.render_widget(Clear, centered_area);
let pause_message = Paragraph::new(format!(
"Paused\nScore: {} - {}",
self.player_score, self.enemy_score
))
.block(popup_block.clone())
.alignment(Center);
frame.render_widget(pause_message, centered_area);
}
Gamestate::Won => {
frame.render_widget(Clear, centered_area);
let win_message = Paragraph::new("You win!".green())
.block(popup_block)
.alignment(Center);
frame.render_widget(win_message, centered_area);
}
Gamestate::Lost => {
frame.render_widget(Clear, centered_area);
let lose_message = Paragraph::new("You lose!".red())
.block(popup_block)
.alignment(Center);
frame.render_widget(lose_message, centered_area);
}
}
}
async fn handle_crossterm_events(&mut self) -> color_eyre::Result<()> {
let event = self.event_stream.next().fuse().await;
match event {
Some(Ok(evt)) => match evt {
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
_ => {}
},
_ => {}
}
Ok(())
}
fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) {
(_, KeyCode::Char('q')) | (KeyModifiers::CONTROL, KeyCode::Char('c')) => self.quit(),
(_, KeyCode::Up | KeyCode::Char('k')) => self.move_up(),
(_, KeyCode::Down | KeyCode::Char('j')) => self.move_down(),
(_, KeyCode::Esc) => match self.state {
Gamestate::Playing => self.state = Gamestate::Paused,
Gamestate::Paused => self.state = Gamestate::Playing,
Gamestate::Won => self.restart(),
Gamestate::Lost => self.restart(),
},
_ => {}
}
}
pub fn move_up(&mut self) {
let new_position = self.player.y + 1.0;
if new_position + 3.0 < 15.0 {
self.player.y = new_position;
}
}
pub fn move_down(&mut self) {
let new_position = self.player.y - 1.0;
if new_position >= 0.0 {
self.player.y -= 1.0;
}
}
fn quit(&mut self) {
self.running = false;
}
}