Skip to content

Commit

Permalink
Enable formatting checks in CI (#89)
Browse files Browse the repository at this point in the history
* Add CI checks for code format
* Ignore generated code in formatting checks
* Ran cargo fmt --all
  • Loading branch information
james7132 authored Mar 19, 2022
1 parent c296bb5 commit 5c788c3
Show file tree
Hide file tree
Showing 18 changed files with 1,278 additions and 594 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
toolchain: [stable]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- uses: actions-rs/toolchain@v1
id: toolchain
with:
toolchain: ${{ matrix.toolchain }}
profile: minimal
components: rustfmt, clippy
override: true

- name: Install alsa and udev
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
if: runner.os == 'linux'

- name: Setup cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-test-rustc-${{ steps.toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}

- uses: actions-rs/cargo@v1
if: runner.os == 'linux'
with:
command: fmt
args: --all -- --check
188 changes: 108 additions & 80 deletions examples/chat-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use steamworks::*;
use clipboard::{ClipboardContext, ClipboardProvider};
use macroquad::prelude::*;
use macroquad::ui::*;
use std::sync::mpsc;
use clipboard::{ClipboardContext, ClipboardProvider};
use steamworks::*;

//MAYBE IT'S NOT GOOD GAME ARCHITECTURE
struct GameState {
Expand All @@ -11,7 +11,7 @@ struct GameState {

enum States {
Menu(MenuState),
Chat(ChatState)
Chat(ChatState),
}

struct MenuState {
Expand Down Expand Up @@ -51,7 +51,7 @@ async fn main() {
};

//For getting values from callback
let (sender_create_lobby, receiver_create_lobby) = mpsc::channel();
let (sender_create_lobby, receiver_create_lobby) = mpsc::channel();
let (sender_join_lobby, receiver_join_lobby) = mpsc::channel();
let (sender_accept, receiver_accept) = mpsc::channel();

Expand All @@ -73,93 +73,120 @@ async fn main() {

match state.state.as_mut() {
States::Menu(menu_state) => {
widgets::Group::new(hash!(), vec2(800.0, 600.))
.ui(&mut *root_ui(), |ui| {
//Creating lobby by button
if ui.button(vec2(20.0, 40.0), "Create Lobby") {
matchmaking.create_lobby(LobbyType::FriendsOnly, 4, move |lobby| {
match lobby {
Ok(lobby) => {
local_sender_create_lobby.send(lobby).unwrap();
}
Err(_) => {}
};
});
}

//Try to join in lobby with id from InputField
if ui.button(vec2(120.0, 40.0), "Connect") {
let lobby_id: Result<u64, _> = menu_state.lobby_input.parse();
match lobby_id {
Ok(id) => {
matchmaking.join_lobby(LobbyId::from_raw(id), move |result| {
if let Ok(lobby) = result {
local_sender_join_lobby.send(lobby).unwrap();
}
});
widgets::Group::new(hash!(), vec2(800.0, 600.)).ui(&mut *root_ui(), |ui| {
//Creating lobby by button
if ui.button(vec2(20.0, 40.0), "Create Lobby") {
matchmaking.create_lobby(LobbyType::FriendsOnly, 4, move |lobby| {
match lobby {
Ok(lobby) => {
local_sender_create_lobby.send(lobby).unwrap();
}
Err(_) => {}
};
});
}

//Try to join in lobby with id from InputField
if ui.button(vec2(120.0, 40.0), "Connect") {
let lobby_id: Result<u64, _> = menu_state.lobby_input.parse();
match lobby_id {
Ok(id) => {
matchmaking.join_lobby(LobbyId::from_raw(id), move |result| {
if let Ok(lobby) = result {
local_sender_join_lobby.send(lobby).unwrap();
}
});
}
Err(_) => {}
}
}

widgets::Group::new(hash!(), vec2(200.0, 25.0))
.position(vec2(20.0, 60.0))
.ui(ui, |ui| {
ui.input_text(hash!(), "", &mut menu_state.lobby_input);
});
});
},
widgets::Group::new(hash!(), vec2(200.0, 25.0))
.position(vec2(20.0, 60.0))
.ui(ui, |ui| {
ui.input_text(hash!(), "", &mut menu_state.lobby_input);
});
});
}
States::Chat(lobby_state) => {
widgets::Group::new(hash!(), vec2(800.0, 600.))
.ui(&mut *root_ui(), |ui| {
draw_text_ex(&format!("LobbyID: {}", lobby_state.current_lobby.raw()), 20.0, 20.0, TextParams::default());

for (id, message) in messages.iter().enumerate() {
draw_text_ex(message, 20.0, 40.0 + 20.0 * (id as f32), TextParams::default());
}
widgets::Group::new(hash!(), vec2(800.0, 600.)).ui(&mut *root_ui(), |ui| {
draw_text_ex(
&format!("LobbyID: {}", lobby_state.current_lobby.raw()),
20.0,
20.0,
TextParams::default(),
);

for (id, message) in messages.iter().enumerate() {
draw_text_ex(
message,
20.0,
40.0 + 20.0 * (id as f32),
TextParams::default(),
);
}

widgets::Group::new(hash!(), vec2(250.0, 25.0))
.position(vec2(20.0, 570.0))
.ui(ui, |ui| {
ui.input_text(hash!(), "", &mut lobby_state.message);
});

//Little Client-Server logic
//Host is server. Host sends message to all players
//Clients send message only to host
if ui.button(vec2(300.0, 570.0), "Send") {
let message = format!("{}: {}", client.friends().name(), lobby_state.message);
if lobby_state.is_host {
for peer in lobby_state.peers.iter() {
if peer.raw() != lobby_state.own_id.raw() {
println!("SENT {} TO {}", message, peer.raw());
networking.send_p2p_packet(*peer,
SendType::Reliable,
message.as_bytes());
}
widgets::Group::new(hash!(), vec2(250.0, 25.0))
.position(vec2(20.0, 570.0))
.ui(ui, |ui| {
ui.input_text(hash!(), "", &mut lobby_state.message);
});

//Little Client-Server logic
//Host is server. Host sends message to all players
//Clients send message only to host
if ui.button(vec2(300.0, 570.0), "Send") {
let message =
format!("{}: {}", client.friends().name(), lobby_state.message);
if lobby_state.is_host {
for peer in lobby_state.peers.iter() {
if peer.raw() != lobby_state.own_id.raw() {
println!("SENT {} TO {}", message, peer.raw());
networking.send_p2p_packet(
*peer,
SendType::Reliable,
message.as_bytes(),
);
}
} else {
let result = networking.send_p2p_packet(lobby_state.host_id,
SendType::Reliable,
message.as_bytes());
println!("SENT {} TO {}. RESULT: {}", message, lobby_state.host_id.raw(), result);
}
messages.push(message);
lobby_state.message = String::new();
} else {
let result = networking.send_p2p_packet(
lobby_state.host_id,
SendType::Reliable,
message.as_bytes(),
);
println!(
"SENT {} TO {}. RESULT: {}",
message,
lobby_state.host_id.raw(),
result
);
}
});
messages.push(message);
lobby_state.message = String::new();
}
});

while let Some(size) = networking.is_p2p_packet_available() {
let mut empty_array = vec![0; size];
let mut buffer= empty_array.as_mut_slice();
let mut buffer = empty_array.as_mut_slice();
if let Some((sender, _)) = networking.read_p2p_packet(&mut buffer) {
//Host gets message from one of the players and sends this message to other players
if lobby_state.is_host {
for peer in lobby_state.peers.iter() {
if peer.raw() != lobby_state.own_id.raw() && peer.raw() != sender.raw() {
networking.send_p2p_packet(*peer,
SendType::Reliable,
format!("{}: {}", client.friends().name(), lobby_state.message).as_bytes());
if peer.raw() != lobby_state.own_id.raw()
&& peer.raw() != sender.raw()
{
networking.send_p2p_packet(
*peer,
SendType::Reliable,
format!(
"{}: {}",
client.friends().name(),
lobby_state.message
)
.as_bytes(),
);
}
}
}
Expand All @@ -178,8 +205,7 @@ async fn main() {
}
networking.accept_p2p_session(user);
}

},
}
}

if let Ok(lobby) = receiver_create_lobby.try_recv() {
Expand Down Expand Up @@ -210,11 +236,13 @@ async fn main() {

//When you connected to lobby you have to send a "ping" message to host
//After that host will add you into peer list
networking.send_p2p_packet(host_id,
SendType::Reliable,
format!("{} JOINED", client.friends().name()).as_bytes());
networking.send_p2p_packet(
host_id,
SendType::Reliable,
format!("{} JOINED", client.friends().name()).as_bytes(),
);
}

next_frame().await
}
}
}
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore = [
"steamworks-sys",
]
Loading

0 comments on commit 5c788c3

Please sign in to comment.