Compare commits
10 Commits
e760bcbe33
...
0.1.0
Author | SHA1 | Date | |
---|---|---|---|
be454cce03 | |||
a91a4c5ef6 | |||
02477e6728 | |||
e389b59ab9 | |||
dfaa5ce30b | |||
cf1d77f445 | |||
0280daf6d2 | |||
38656661b4 | |||
5b228de285 | |||
d8f96f732a |
14
README.md
14
README.md
@@ -1,5 +1,17 @@
|
|||||||
# SeaBattle
|
# SeaBattle
|
||||||
|
[](https://drone.communiquons.org/pierre/SeaBattle)
|
||||||
|
|
||||||
|
|
||||||
Full stack sea battle game.
|
Full stack sea battle game.
|
||||||
|
|
||||||
Current status: working on backend, and then building web ui...
|
|
||||||
|
## Implementations
|
||||||
|
Current implementations:
|
||||||
|
- [x] Rust shell implementations ([server](rust/sea_battle_backend) and [client](rust/sea_battle_cli_player))
|
||||||
|
- [ ] web implementation
|
||||||
|
- [ ] mobile implementation
|
||||||
|
|
||||||
|
## Screenshots
|
||||||
|
|
||||||
|
### Shell implementation
|
||||||
|

|
||||||
|
50
rust/Cargo.lock
generated
50
rust/Cargo.lock
generated
@@ -456,31 +456,6 @@ dependencies = [
|
|||||||
"os_str_bytes",
|
"os_str_bytes",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cli_player"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"clap",
|
|
||||||
"crossterm",
|
|
||||||
"env_logger",
|
|
||||||
"futures",
|
|
||||||
"hostname",
|
|
||||||
"hyper-rustls",
|
|
||||||
"lazy_static",
|
|
||||||
"log",
|
|
||||||
"num",
|
|
||||||
"num-derive",
|
|
||||||
"num-traits",
|
|
||||||
"rustls",
|
|
||||||
"sea_battle_backend",
|
|
||||||
"serde_json",
|
|
||||||
"serde_urlencoded",
|
|
||||||
"textwrap",
|
|
||||||
"tokio",
|
|
||||||
"tokio-tungstenite",
|
|
||||||
"tui",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "codespan-reporting"
|
name = "codespan-reporting"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
@@ -1564,6 +1539,31 @@ dependencies = [
|
|||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sea_battle_cli_player"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"crossterm",
|
||||||
|
"env_logger",
|
||||||
|
"futures",
|
||||||
|
"hostname",
|
||||||
|
"hyper-rustls",
|
||||||
|
"lazy_static",
|
||||||
|
"log",
|
||||||
|
"num",
|
||||||
|
"num-derive",
|
||||||
|
"num-traits",
|
||||||
|
"rustls",
|
||||||
|
"sea_battle_backend",
|
||||||
|
"serde_json",
|
||||||
|
"serde_urlencoded",
|
||||||
|
"textwrap",
|
||||||
|
"tokio",
|
||||||
|
"tokio-tungstenite",
|
||||||
|
"tui",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "security-framework"
|
name = "security-framework"
|
||||||
version = "2.7.0"
|
version = "2.7.0"
|
||||||
|
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
members = [
|
members = [
|
||||||
"sea_battle_backend",
|
"sea_battle_backend",
|
||||||
"cli_player"
|
"sea_battle_cli_player"
|
||||||
]
|
]
|
||||||
|
@@ -2,6 +2,12 @@
|
|||||||
name = "sea_battle_backend"
|
name = "sea_battle_backend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
license = "GPL-2.0-or-later"
|
||||||
|
description = "A Sea Battle game backend server"
|
||||||
|
repository = "https://gitea.communiquons.org/pierre/SeaBattle"
|
||||||
|
readme = "README.md"
|
||||||
|
authors = ["Pierre Hubert <pierre.git@communiquons.org>"]
|
||||||
|
categories = [ "games" ]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
30
rust/sea_battle_backend/README.md
Normal file
30
rust/sea_battle_backend/README.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Sea battle backend
|
||||||
|
[](https://drone.communiquons.org/pierre/SeaBattle)
|
||||||
|
[](https://crates.io/crates/sea_battle_backend)
|
||||||
|
[](https://docs.rs/sea_battle_backend/)
|
||||||
|
|
||||||
|
A backend HTTP server for the Sea Battle game. The binary included in
|
||||||
|
this crate can be used to deploy a server that will allow players to
|
||||||
|
connect to play together.
|
||||||
|
|
||||||
|
The `actix-web` library is used to spawn HTTP server. The games are encapsulated
|
||||||
|
inside websockets.
|
||||||
|
|
||||||
|
An official server is running at https://seabattleapi.communiquons.org/
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
You can install the backend using the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install sea_battle_backend
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
```bash
|
||||||
|
sea_battle_backend -l 0.0.0.0:7000
|
||||||
|
```
|
||||||
|
|
||||||
|
> Note: a reverse-proxy must be used to protect
|
||||||
|
|
||||||
|
## Client
|
||||||
|
A command-line client is available in the [sea_battle_cli_player](https://crates.io/crates/sea_battle_cli_player) crate.
|
@@ -1,12 +1,18 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cli_player"
|
name = "sea_battle_cli_player"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
license = "GPL-2.0-or-later"
|
||||||
|
description = "A Sea Battle game shell client"
|
||||||
|
repository = "https://gitea.communiquons.org/pierre/SeaBattle"
|
||||||
|
readme = "README.md"
|
||||||
|
authors = ["Pierre Hubert <pierre.git@communiquons.org>"]
|
||||||
|
categories = [ "games" ]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
sea_battle_backend = { path = "../sea_battle_backend" }
|
sea_battle_backend = { path = "../sea_battle_backend", version = "0.1.0" }
|
||||||
clap = { version = "4.0.15", features = ["derive"] }
|
clap = { version = "4.0.15", features = ["derive"] }
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
env_logger = "0.9.0"
|
env_logger = "0.9.0"
|
41
rust/sea_battle_cli_player/README.md
Normal file
41
rust/sea_battle_cli_player/README.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Sea battle cli player
|
||||||
|
[](https://drone.communiquons.org/pierre/SeaBattle)
|
||||||
|
[](https://crates.io/crates/sea_battle_cli_player)
|
||||||
|
[](https://docs.rs/sea_battle_cli_player/)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
A sea battle shell client player for the [sea_battle_backend](https://crates.io/crates/sea_battle_backend) crate, based on the [tui](https://crates.io/crates/tui) library.
|
||||||
|
|
||||||
|
## Available play modes
|
||||||
|
* 🤖 Play against bot (this mode does not require any Internet connection, a local server is automatically spawn)
|
||||||
|
* 🎲 Play against a random player
|
||||||
|
* ➕ Create play invite (online). In this mode, the server returns an invitation code to give to the opponent
|
||||||
|
* 🎫 Accept play invite (online)
|
||||||
|
|
||||||
|
For the 🤖 bot and ➕ create invite modes, game rules can be customized before starting the game.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
You can install the backend using the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install sea_battle_cli_player
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
Simply launch using:
|
||||||
|
```bash
|
||||||
|
sea_battle_cli_player
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Offline LAN
|
||||||
|
If you want to run a local server to play offline LAN games, the cli player can also act as the server:
|
||||||
|
```bash
|
||||||
|
RUST_LOG=info sea_battle_cli_player -s -l 0.0.0.0:7000
|
||||||
|
```
|
||||||
|
|
||||||
|
Then all the players must specify the address of this server to use it instead of the default official one:
|
||||||
|
```bash
|
||||||
|
sea_battle_cli_player -r http://IP_OF_TARGET:7000
|
||||||
|
```
|
BIN
rust/sea_battle_cli_player/img/SeaBattleCli.png
Normal file
BIN
rust/sea_battle_cli_player/img/SeaBattleCli.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
@@ -5,7 +5,9 @@ use futures::{SinkExt, StreamExt};
|
|||||||
use hyper_rustls::ConfigBuilderExt;
|
use hyper_rustls::ConfigBuilderExt;
|
||||||
use sea_battle_backend::data::GameRules;
|
use sea_battle_backend::data::GameRules;
|
||||||
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
|
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
|
||||||
use sea_battle_backend::server::{BotPlayQuery, PlayRandomQuery};
|
use sea_battle_backend::server::{
|
||||||
|
AcceptInviteQuery, BotPlayQuery, CreateInviteQuery, PlayRandomQuery,
|
||||||
|
};
|
||||||
use sea_battle_backend::utils::res_utils::{boxed_error, Res};
|
use sea_battle_backend::utils::res_utils::{boxed_error, Res};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::sync::mpsc::TryRecvError;
|
use std::sync::mpsc::TryRecvError;
|
||||||
@@ -61,6 +63,38 @@ impl Client {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start a play by creating an invite
|
||||||
|
pub async fn start_create_invite<D: Display>(rules: &GameRules, player_name: D) -> Res<Self> {
|
||||||
|
Self::connect_url(
|
||||||
|
&cli_args().remote_server_uri,
|
||||||
|
&format!(
|
||||||
|
"/play/create_invite?{}",
|
||||||
|
serde_urlencoded::to_string(&CreateInviteQuery {
|
||||||
|
rules: rules.clone(),
|
||||||
|
player_name: player_name.to_string()
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start a play by accepting an invite
|
||||||
|
pub async fn start_accept_invite<D: Display>(code: String, player_name: D) -> Res<Self> {
|
||||||
|
Self::connect_url(
|
||||||
|
&cli_args().remote_server_uri,
|
||||||
|
&format!(
|
||||||
|
"/play/accept_invite?{}",
|
||||||
|
serde_urlencoded::to_string(&AcceptInviteQuery {
|
||||||
|
code,
|
||||||
|
player_name: player_name.to_string()
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
/// Do connect to a server, returning
|
/// Do connect to a server, returning
|
||||||
async fn connect_url(server: &str, uri: &str) -> Res<Self> {
|
async fn connect_url(server: &str, uri: &str) -> Res<Self> {
|
||||||
let mut ws_url = server.replace("http", "ws");
|
let mut ws_url = server.replace("http", "ws");
|
@@ -12,18 +12,22 @@ use env_logger::Env;
|
|||||||
use tui::backend::{Backend, CrosstermBackend};
|
use tui::backend::{Backend, CrosstermBackend};
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
|
|
||||||
use cli_player::cli_args::{cli_args, TestDevScreen};
|
use sea_battle_backend::consts::{
|
||||||
use cli_player::client::Client;
|
INVITE_CODE_LENGTH, MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH,
|
||||||
use cli_player::server::run_server;
|
};
|
||||||
use cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen;
|
|
||||||
use cli_player::ui_screens::game_screen::GameScreen;
|
|
||||||
use cli_player::ui_screens::input_screen::InputScreen;
|
|
||||||
use cli_player::ui_screens::popup_screen::PopupScreen;
|
|
||||||
use cli_player::ui_screens::select_play_mode_screen::{SelectPlayModeResult, SelectPlayModeScreen};
|
|
||||||
use cli_player::ui_screens::*;
|
|
||||||
use sea_battle_backend::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
|
|
||||||
use sea_battle_backend::data::GameRules;
|
use sea_battle_backend::data::GameRules;
|
||||||
use sea_battle_backend::utils::res_utils::Res;
|
use sea_battle_backend::utils::res_utils::Res;
|
||||||
|
use sea_battle_cli_player::cli_args::{cli_args, TestDevScreen};
|
||||||
|
use sea_battle_cli_player::client::Client;
|
||||||
|
use sea_battle_cli_player::server::run_server;
|
||||||
|
use sea_battle_cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen;
|
||||||
|
use sea_battle_cli_player::ui_screens::game_screen::GameScreen;
|
||||||
|
use sea_battle_cli_player::ui_screens::input_screen::InputScreen;
|
||||||
|
use sea_battle_cli_player::ui_screens::popup_screen::PopupScreen;
|
||||||
|
use sea_battle_cli_player::ui_screens::select_play_mode_screen::{
|
||||||
|
SelectPlayModeResult, SelectPlayModeScreen,
|
||||||
|
};
|
||||||
|
use sea_battle_cli_player::ui_screens::*;
|
||||||
|
|
||||||
/// Test code screens
|
/// Test code screens
|
||||||
async fn run_dev<B: Backend>(
|
async fn run_dev<B: Backend>(
|
||||||
@@ -72,8 +76,8 @@ async fn run_dev<B: Backend>(
|
|||||||
))?
|
))?
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ask the user to specify its username
|
/// Ask the user to specify the name he should be identified with
|
||||||
fn query_username<B: Backend>(terminal: &mut Terminal<B>) -> Res<String> {
|
fn query_player_name<B: Backend>(terminal: &mut Terminal<B>) -> Res<String> {
|
||||||
let mut hostname = hostname::get()?.to_string_lossy().to_string();
|
let mut hostname = hostname::get()?.to_string_lossy().to_string();
|
||||||
if hostname.len() > MAX_PLAYER_NAME_LENGTH {
|
if hostname.len() > MAX_PLAYER_NAME_LENGTH {
|
||||||
hostname = hostname[0..MAX_PLAYER_NAME_LENGTH].to_string();
|
hostname = hostname[0..MAX_PLAYER_NAME_LENGTH].to_string();
|
||||||
@@ -103,29 +107,49 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
|
|||||||
let choice = SelectPlayModeScreen::default().show(terminal)?;
|
let choice = SelectPlayModeScreen::default().show(terminal)?;
|
||||||
|
|
||||||
if let ScreenResult::Ok(c) = choice {
|
if let ScreenResult::Ok(c) = choice {
|
||||||
if c.need_user_name() && username.is_empty() {
|
if c.need_player_name() && username.is_empty() {
|
||||||
username = query_username(terminal)?;
|
username = query_player_name(terminal)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.need_custom_rules() {
|
||||||
|
rules = match GameRulesConfigurationScreen::new(rules.clone()).show(terminal)? {
|
||||||
|
ScreenResult::Ok(r) => r,
|
||||||
|
ScreenResult::Canceled => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PopupScreen::new("🔌 Connecting...").show_once(terminal)?;
|
||||||
|
|
||||||
let client = match choice {
|
let client = match choice {
|
||||||
ScreenResult::Ok(SelectPlayModeResult::PlayRandom) => {
|
ScreenResult::Ok(SelectPlayModeResult::PlayRandom) => {
|
||||||
PopupScreen::new("Connecting...").show_once(terminal)?;
|
|
||||||
|
|
||||||
Client::start_random_play(&username).await?
|
Client::start_random_play(&username).await?
|
||||||
}
|
}
|
||||||
|
|
||||||
// Play against bot
|
// Play against bot
|
||||||
ScreenResult::Ok(SelectPlayModeResult::PlayAgainstBot) => {
|
ScreenResult::Ok(SelectPlayModeResult::PlayAgainstBot) => {
|
||||||
// First, ask for custom rules
|
Client::start_bot_play(&rules).await?
|
||||||
rules = match GameRulesConfigurationScreen::new(rules.clone()).show(terminal)? {
|
}
|
||||||
ScreenResult::Ok(r) => r,
|
|
||||||
ScreenResult::Canceled => continue,
|
// Create invite
|
||||||
|
ScreenResult::Ok(SelectPlayModeResult::CreateInvite) => {
|
||||||
|
Client::start_create_invite(&rules, &username).await?
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join invite
|
||||||
|
ScreenResult::Ok(SelectPlayModeResult::AcceptInvite) => {
|
||||||
|
let code = match InputScreen::new("Invite code")
|
||||||
|
.set_min_length(INVITE_CODE_LENGTH)
|
||||||
|
.set_max_length(INVITE_CODE_LENGTH)
|
||||||
|
.show(terminal)?
|
||||||
|
.value()
|
||||||
|
{
|
||||||
|
None => continue,
|
||||||
|
Some(v) => v,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Then connect to server
|
PopupScreen::new("🔌 Connecting...").show_once(terminal)?;
|
||||||
PopupScreen::new("Connecting...").show_once(terminal)?;
|
Client::start_accept_invite(code, &username).await?
|
||||||
Client::start_bot_play(&rules).await?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScreenResult::Canceled | ScreenResult::Ok(SelectPlayModeResult::Exit) => return Ok(()),
|
ScreenResult::Canceled | ScreenResult::Ok(SelectPlayModeResult::Exit) => return Ok(()),
|
@@ -63,7 +63,7 @@ impl GameRulesConfigurationScreen {
|
|||||||
.checked_sub(last_tick.elapsed())
|
.checked_sub(last_tick.elapsed())
|
||||||
.unwrap_or_else(|| Duration::from_secs(0));
|
.unwrap_or_else(|| Duration::from_secs(0));
|
||||||
|
|
||||||
if crossterm::event::poll(timeout)? {
|
if event::poll(timeout)? {
|
||||||
let mut cursor_pos = self.curr_field as i32;
|
let mut cursor_pos = self.curr_field as i32;
|
||||||
|
|
||||||
if let Event::Key(key) = event::read()? {
|
if let Event::Key(key) = event::read()? {
|
||||||
@@ -189,7 +189,9 @@ impl GameRulesConfigurationScreen {
|
|||||||
|
|
||||||
let area = centered_rect_size(w, h, &f.size());
|
let area = centered_rect_size(w, h, &f.size());
|
||||||
|
|
||||||
let block = Block::default().title("Game rules").borders(Borders::ALL);
|
let block = Block::default()
|
||||||
|
.title("📓 Game rules")
|
||||||
|
.borders(Borders::ALL);
|
||||||
f.render_widget(block, area);
|
f.render_widget(block, area);
|
||||||
|
|
||||||
let chunks = Layout::default()
|
let chunks = Layout::default()
|
@@ -20,7 +20,9 @@ use crate::constants::*;
|
|||||||
use crate::ui_screens::confirm_dialog_screen::confirm;
|
use crate::ui_screens::confirm_dialog_screen::confirm;
|
||||||
use crate::ui_screens::popup_screen::{show_screen_too_small_popup, PopupScreen};
|
use crate::ui_screens::popup_screen::{show_screen_too_small_popup, PopupScreen};
|
||||||
use crate::ui_screens::set_boats_layout_screen::SetBoatsLayoutScreen;
|
use crate::ui_screens::set_boats_layout_screen::SetBoatsLayoutScreen;
|
||||||
use crate::ui_screens::utils::{centered_rect_size, centered_text};
|
use crate::ui_screens::utils::{
|
||||||
|
centered_rect_size, centered_rect_size_horizontally, centered_text,
|
||||||
|
};
|
||||||
use crate::ui_screens::ScreenResult;
|
use crate::ui_screens::ScreenResult;
|
||||||
use crate::ui_widgets::button_widget::ButtonWidget;
|
use crate::ui_widgets::button_widget::ButtonWidget;
|
||||||
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
|
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
|
||||||
@@ -53,21 +55,21 @@ impl GameStatus {
|
|||||||
|
|
||||||
pub fn status_text(&self) -> &str {
|
pub fn status_text(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
GameStatus::Connecting => "Connecting...",
|
GameStatus::Connecting => "🔌 Connecting...",
|
||||||
GameStatus::WaitingForAnotherPlayer => "Waiting for another player...",
|
GameStatus::WaitingForAnotherPlayer => "🕑 Waiting for another player...",
|
||||||
GameStatus::OpponentConnected => "Opponent connected!",
|
GameStatus::OpponentConnected => "✅ Opponent connected!",
|
||||||
GameStatus::WaitingForOpponentBoatsConfig => "Waiting for ### boats configuration",
|
GameStatus::WaitingForOpponentBoatsConfig => "🕑 Waiting for ### boats configuration",
|
||||||
GameStatus::OpponentReady => "### is ready!",
|
GameStatus::OpponentReady => "✅ ### is ready!",
|
||||||
GameStatus::Starting => "Game is starting...",
|
GameStatus::Starting => "🕑 Game is starting...",
|
||||||
GameStatus::MustFire => "You must fire!",
|
GameStatus::MustFire => "🚨 You must fire!",
|
||||||
GameStatus::OpponentMustFire => "### must fire!",
|
GameStatus::OpponentMustFire => "💣 ### must fire!",
|
||||||
GameStatus::WonGame => "You win the game!",
|
GameStatus::WonGame => "🎉 You win the game!",
|
||||||
GameStatus::LostGame => "### wins the game. You loose.",
|
GameStatus::LostGame => "😿 ### wins the game. You loose.",
|
||||||
GameStatus::RematchRequestedByOpponent => "Rematch requested by ###",
|
GameStatus::RematchRequestedByOpponent => "❓ Rematch requested by ###",
|
||||||
GameStatus::RematchRequestedByPlayer => "Rematch requested by you",
|
GameStatus::RematchRequestedByPlayer => "❓ Rematch requested by you",
|
||||||
GameStatus::RematchAccepted => "Rematch accepted!",
|
GameStatus::RematchAccepted => "✅ Rematch accepted!",
|
||||||
GameStatus::RematchRejected => "Rematch rejected!",
|
GameStatus::RematchRejected => "❌ Rematch rejected!",
|
||||||
GameStatus::OpponentLeftGame => "Opponent left game!",
|
GameStatus::OpponentLeftGame => "⛔ Opponent left game!",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,10 +85,10 @@ enum Buttons {
|
|||||||
impl Buttons {
|
impl Buttons {
|
||||||
pub fn text(&self) -> &str {
|
pub fn text(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Buttons::RequestRematch => "Request rematch",
|
Buttons::RequestRematch => "❓ Request rematch",
|
||||||
Buttons::AcceptRematch => "Accept rematch",
|
Buttons::AcceptRematch => "✅ Accept rematch",
|
||||||
Buttons::RejectRematch => "Reject rematch",
|
Buttons::RejectRematch => "❌ Reject rematch",
|
||||||
Buttons::QuitGame => "Quit game",
|
Buttons::QuitGame => "❌ Quit game",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,7 +138,7 @@ impl GameScreen {
|
|||||||
.unwrap_or_else(|| Duration::from_secs(0));
|
.unwrap_or_else(|| Duration::from_secs(0));
|
||||||
|
|
||||||
// Handle terminal events
|
// Handle terminal events
|
||||||
if crossterm::event::poll(timeout)? {
|
if event::poll(timeout)? {
|
||||||
let event = event::read()?;
|
let event = event::read()?;
|
||||||
|
|
||||||
// Keyboard event
|
// Keyboard event
|
||||||
@@ -241,7 +243,7 @@ impl GameScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::InvalidInviteCode => {
|
ServerMessage::InvalidInviteCode => {
|
||||||
PopupScreen::new("Invalid invite code!").show(terminal)?;
|
PopupScreen::new("❌ Invalid invite code!").show(terminal)?;
|
||||||
return Ok(ScreenResult::Ok(()));
|
return Ok(ScreenResult::Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,6 +366,7 @@ impl GameScreen {
|
|||||||
buttons.push(Buttons::RejectRematch);
|
buttons.push(Buttons::RejectRematch);
|
||||||
} else if self.status != GameStatus::OpponentLeftGame
|
} else if self.status != GameStatus::OpponentLeftGame
|
||||||
&& self.status != GameStatus::RematchRejected
|
&& self.status != GameStatus::RematchRejected
|
||||||
|
&& self.status != GameStatus::RematchRequestedByPlayer
|
||||||
{
|
{
|
||||||
buttons.push(Buttons::RequestRematch);
|
buttons.push(Buttons::RequestRematch);
|
||||||
}
|
}
|
||||||
@@ -475,7 +478,7 @@ impl GameScreen {
|
|||||||
if !self.status.can_show_game_maps() {
|
if !self.status.can_show_game_maps() {
|
||||||
if self.status == GameStatus::WaitingForAnotherPlayer {
|
if self.status == GameStatus::WaitingForAnotherPlayer {
|
||||||
if let Some(code) = &self.invite_code {
|
if let Some(code) = &self.invite_code {
|
||||||
status_text.push_str(&format!("\n Invite code: {}", code));
|
status_text.push_str(&format!("\n\n🎫 Invite code: {}", code));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,15 +573,17 @@ impl GameScreen {
|
|||||||
if show_both_maps {
|
if show_both_maps {
|
||||||
let maps_chunks = Layout::default()
|
let maps_chunks = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints([
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
Constraint::Length(player_map_size.0),
|
|
||||||
Constraint::Length(3),
|
|
||||||
Constraint::Length(opponent_map_size.0),
|
|
||||||
])
|
|
||||||
.split(chunks[2]);
|
.split(chunks[2]);
|
||||||
|
|
||||||
f.render_widget(player_map, maps_chunks[0]);
|
f.render_widget(
|
||||||
f.render_widget(opponent_map, maps_chunks[2]);
|
player_map,
|
||||||
|
centered_rect_size_horizontally(player_map_size.0, &maps_chunks[0]),
|
||||||
|
);
|
||||||
|
f.render_widget(
|
||||||
|
opponent_map,
|
||||||
|
centered_rect_size_horizontally(opponent_map_size.0, &maps_chunks[1]),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// Render a single map
|
// Render a single map
|
||||||
if self.can_fire() {
|
if self.can_fire() {
|
@@ -125,7 +125,7 @@ impl<'a> InputScreen<'a> {
|
|||||||
|
|
||||||
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
|
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
|
||||||
let area = centered_rect_size(
|
let area = centered_rect_size(
|
||||||
(self.msg.len() + 4).max(self.max_len + 4) as u16,
|
(self.msg.len() + 4).max(self.max_len + 4).max(25) as u16,
|
||||||
7,
|
7,
|
||||||
&f.size(),
|
&f.size(),
|
||||||
);
|
);
|
@@ -17,7 +17,7 @@ use crate::ui_widgets::button_widget::ButtonWidget;
|
|||||||
/// Convenience function to inform user that his terminal window is too small to display the current
|
/// Convenience function to inform user that his terminal window is too small to display the current
|
||||||
/// screen
|
/// screen
|
||||||
pub fn show_screen_too_small_popup<B: Backend>(f: &mut Frame<B>) {
|
pub fn show_screen_too_small_popup<B: Backend>(f: &mut Frame<B>) {
|
||||||
PopupScreen::new("Screen too small!").show_in_frame(f)
|
PopupScreen::new("🖵 Screen too small!").show_in_frame(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PopupScreen<'a> {
|
pub struct PopupScreen<'a> {
|
@@ -17,14 +17,22 @@ pub enum SelectPlayModeResult {
|
|||||||
#[default]
|
#[default]
|
||||||
PlayAgainstBot,
|
PlayAgainstBot,
|
||||||
PlayRandom,
|
PlayRandom,
|
||||||
|
CreateInvite,
|
||||||
|
AcceptInvite,
|
||||||
Exit,
|
Exit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SelectPlayModeResult {
|
impl SelectPlayModeResult {
|
||||||
/// Specify whether a selected play mode requires a user name or not
|
/// Specify whether a selected play mode requires a user name or not
|
||||||
pub fn need_user_name(&self) -> bool {
|
pub fn need_player_name(&self) -> bool {
|
||||||
self != &SelectPlayModeResult::PlayAgainstBot && self != &SelectPlayModeResult::Exit
|
self != &SelectPlayModeResult::PlayAgainstBot && self != &SelectPlayModeResult::Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specify whether a selected play mode requires a the user to specify its own game rules or
|
||||||
|
/// not
|
||||||
|
pub fn need_custom_rules(&self) -> bool {
|
||||||
|
self == &SelectPlayModeResult::PlayAgainstBot || self == &SelectPlayModeResult::CreateInvite
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -33,17 +41,25 @@ struct PlayModeDescription {
|
|||||||
value: SelectPlayModeResult,
|
value: SelectPlayModeResult,
|
||||||
}
|
}
|
||||||
|
|
||||||
const AVAILABLE_PLAY_MODES: [PlayModeDescription; 3] = [
|
const AVAILABLE_PLAY_MODES: [PlayModeDescription; 5] = [
|
||||||
PlayModeDescription {
|
PlayModeDescription {
|
||||||
name: "Play against bot (offline)",
|
name: "🤖 Play against bot (offline)",
|
||||||
value: SelectPlayModeResult::PlayAgainstBot,
|
value: SelectPlayModeResult::PlayAgainstBot,
|
||||||
},
|
},
|
||||||
PlayModeDescription {
|
PlayModeDescription {
|
||||||
name: "Play against random player (online)",
|
name: "🎲 Play against random player (online)",
|
||||||
value: SelectPlayModeResult::PlayRandom,
|
value: SelectPlayModeResult::PlayRandom,
|
||||||
},
|
},
|
||||||
PlayModeDescription {
|
PlayModeDescription {
|
||||||
name: "Exit app",
|
name: "➕ Create play invite (online)",
|
||||||
|
value: SelectPlayModeResult::CreateInvite,
|
||||||
|
},
|
||||||
|
PlayModeDescription {
|
||||||
|
name: "🎫 Accept play invite (online)",
|
||||||
|
value: SelectPlayModeResult::AcceptInvite,
|
||||||
|
},
|
||||||
|
PlayModeDescription {
|
||||||
|
name: "❌ Exit app",
|
||||||
value: SelectPlayModeResult::Exit,
|
value: SelectPlayModeResult::Exit,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
@@ -92,7 +108,7 @@ impl SelectPlayModeScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
|
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
|
||||||
let area = centered_rect_size(50, 5, &f.size());
|
let area = centered_rect_size(50, 2 + AVAILABLE_PLAY_MODES.len() as u16, &f.size());
|
||||||
|
|
||||||
// Create a List from all list items and highlight the currently selected one
|
// Create a List from all list items and highlight the currently selected one
|
||||||
let items = AVAILABLE_PLAY_MODES
|
let items = AVAILABLE_PLAY_MODES
|
@@ -46,6 +46,25 @@ pub fn centered_rect_size(width: u16, height: u16, parent: &Rect) -> Rect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// helper function to create a centered rect using up certain container size, only horizontally
|
||||||
|
pub fn centered_rect_size_horizontally(width: u16, parent: &Rect) -> Rect {
|
||||||
|
if parent.width < width {
|
||||||
|
return Rect {
|
||||||
|
x: parent.x,
|
||||||
|
y: parent.y,
|
||||||
|
width: parent.width,
|
||||||
|
height: parent.height,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Rect {
|
||||||
|
x: parent.x + (parent.width - width) / 2,
|
||||||
|
y: parent.y,
|
||||||
|
width,
|
||||||
|
height: parent.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get coordinates to render centered text
|
/// Get coordinates to render centered text
|
||||||
pub fn centered_text(text: &str, container: &Rect) -> Rect {
|
pub fn centered_text(text: &str, container: &Rect) -> Rect {
|
||||||
if text.len() > container.width as usize {
|
if text.len() > container.width as usize {
|
Reference in New Issue
Block a user