Compare commits

...

46 Commits

Author SHA1 Message Date
be454cce03 Update READMEs
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-10-17 20:29:41 +02:00
a91a4c5ef6 Improve READMEs
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 20:25:19 +02:00
02477e6728 Specify crate version
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-10-17 19:18:13 +02:00
e389b59ab9 Add information to crates 2022-10-17 19:16:16 +02:00
dfaa5ce30b Rename crate 2022-10-17 19:13:16 +02:00
cf1d77f445 Fix appearance issues of game maps
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 19:04:27 +02:00
0280daf6d2 Fix appearance issues
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 19:00:13 +02:00
38656661b4 Fix appearance issues
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 18:54:03 +02:00
5b228de285 Fix issue on rematch request screen 2022-10-17 18:49:52 +02:00
d8f96f732a Can play using invites 2022-10-17 18:47:33 +02:00
e760bcbe33 Handle better small screens
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 09:42:24 +02:00
ccb3d36fae Complete previous test
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-17 08:38:03 +02:00
fcc7f30e10 Test strike timeout 2022-10-17 08:35:49 +02:00
171c88f303 Move result structures to a more appropriate location 2022-10-17 08:24:40 +02:00
9162c5eb24 Display timeout in game UI 2022-10-17 08:21:42 +02:00
b4772aa88e Fix automatic fire 2022-10-17 08:03:13 +02:00
42b0d84f9d Implement strike timeout on server side 2022-10-17 07:59:42 +02:00
ba1ed84b33 Add strike timeout setting 2022-10-17 07:42:17 +02:00
8c1a3f2c5f Fix typo 2022-10-16 20:29:34 +02:00
25871de084 Add a message to explain why connection are closed in case of invalid player names 2022-10-16 20:23:12 +02:00
9a38a634eb Can run cli as server
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-16 19:52:34 +02:00
8990badaa4 Handle screens too small for setting boats layout 2022-10-16 19:42:43 +02:00
b1145cc362 Limit player name length
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-16 18:51:05 +02:00
e0132b68ed Can notify a player that the server is waiting for opponent play configuration 2022-10-16 18:42:18 +02:00
e97f4b593a Can fire directly with the mouse 2022-10-16 18:38:22 +02:00
1c08e2ec01 Connections are properly closed 2022-10-16 18:35:17 +02:00
70d70c2851 Handle bug that happens when a player leaves the game in an early stage 2022-10-16 18:35:17 +02:00
04ee20dac2 Fix a few bugs 2022-10-16 18:06:31 +02:00
3c2b96f3a2 Can play against random player
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-16 17:47:41 +02:00
161391db04 Update dependencies
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-16 15:02:35 +02:00
19e1d559f6 Merge pull request 'Configure Renovate' (#1) from renovate/configure into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #1
2022-10-16 12:55:52 +00:00
341556636c Ignore Flutter & React client (in advance) 2022-10-16 12:54:28 +00:00
7d4cc52d7b Add drone CI pipeline 2022-10-16 14:50:05 +02:00
4501be6a43 Create docker image for release 2022-10-16 14:36:57 +02:00
146b009fe7 Add renovate.json 2022-10-16 00:33:30 +00:00
f2ec85b46f Can request & respond to rematch 2022-10-15 17:46:14 +02:00
a2c880814c Can get opponent last fire location 2022-10-15 16:33:45 +02:00
b832ef82ed Improve grid appearance 2022-10-15 16:27:22 +02:00
4341bdc682 Improve colors 2022-10-15 16:11:30 +02:00
3455559d33 Can fire with mouse 2022-10-15 16:06:47 +02:00
acd13af227 Can fire with keyboard 2022-10-15 15:53:27 +02:00
454dff923b Can change bot type in rules screen 2022-10-15 15:07:17 +02:00
26d5f85c3c Display maps on play 2022-10-15 14:46:10 +02:00
375127eeee Query boats layout 2022-10-15 13:19:33 +02:00
a9f29e24fe Show popup message while connecting to server 2022-10-15 11:54:57 +02:00
19993c560a Ready to implement game screen 2022-10-15 11:45:45 +02:00
56 changed files with 2118 additions and 398 deletions

14
.drone.yml Normal file
View File

@@ -0,0 +1,14 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: cargo_check
image: rust
commands:
- cd rust
- rustup component add clippy
- cargo clippy -- -D warnings
- cargo test

View File

@@ -1,5 +1,17 @@
# SeaBattle
[![Build Status](https://drone.communiquons.org/api/badges/pierre/SeaBattle/status.svg)](https://drone.communiquons.org/pierre/SeaBattle)
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
![Shell implementation example](rust/sea_battle_cli_player/img/SeaBattleCli.png)

4
renovate.json Normal file
View File

@@ -0,0 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"ignorePaths": ["**/flutter/**", "**/react/**"]
}

429
rust/Cargo.lock generated
View File

@@ -421,26 +421,24 @@ dependencies = [
[[package]]
name = "clap"
version = "3.2.22"
version = "4.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750"
checksum = "6bf8832993da70a4c6d13c581f4463c2bdda27b9bf1c5498dc4365543abe6d6f"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
version = "4.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
checksum = "c42f169caba89a7d512b5418b09864543eeb4d497416c917d7137863bd2076ad"
dependencies = [
"heck",
"proc-macro-error",
@@ -451,33 +449,21 @@ dependencies = [
[[package]]
name = "clap_lex"
version = "0.2.4"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "cli_player"
version = "0.1.0"
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"clap",
"crossterm",
"env_logger",
"futures",
"lazy_static",
"log",
"num",
"num-derive",
"num-traits",
"sea_battle_backend",
"serde_json",
"serde_urlencoded",
"textwrap",
"tokio",
"tokio-tungstenite",
"tui",
"termcolor",
"unicode-width",
]
[[package]]
@@ -497,6 +483,16 @@ dependencies = [
"version_check",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@@ -575,6 +571,50 @@ dependencies = [
"typenum",
]
[[package]]
name = "cxx"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f83d0ebf42c6eafb8d7c52f7e5f2d3003b89c7aa4fd2b79229209459a849af8"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07d050484b55975889284352b0ffc2ecbda25c0c55978017c132b29ba0818a86"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d2199b00553eda8012dfec8d3b1c75fce747cf27c169a270b3b99e3448ab78"
[[package]]
name = "cxxbridge-macro"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb67a6de1f602736dd7eaead0080cf3435df806c61b24b13328db128c58868f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "darling"
version = "0.14.1"
@@ -839,6 +879,17 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hostname"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867"
dependencies = [
"libc",
"match_cfg",
"winapi",
]
[[package]]
name = "http"
version = "0.2.8"
@@ -850,6 +901,17 @@ dependencies = [
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [
"bytes",
"http",
"pin-project-lite",
]
[[package]]
name = "httparse"
version = "1.8.0"
@@ -869,18 +931,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "iana-time-zone"
version = "0.1.50"
name = "hyper"
version = "0.14.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd911b35d940d2bd0bea0f9100068e5b97b51a1cbe13d13382f132e0365257a0"
checksum = "02c929dc5c39e335a03c405292728118860721b10190d98c2a0f0efd5baafbac"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"http",
"http-body",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "hyper-rustls"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d87c48c02e0dc5e3b849a2041db3029fd066650f8f717c07bf8ed78ccb895cac"
dependencies = [
"http",
"hyper",
"log",
"rustls",
"rustls-native-certs",
"tokio",
"tokio-rustls",
]
[[package]]
name = "iana-time-zone"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5a6ef98976b22b3b7f2f3a806f858cb862044cfa66805aa3ad84cb3d3b785ed"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
]
[[package]]
name = "ident_case"
version = "1.0.1"
@@ -910,9 +1021,9 @@ dependencies = [
[[package]]
name = "itoa"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "jobserver"
@@ -946,9 +1057,18 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.134"
version = "0.2.135"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb"
checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c"
[[package]]
name = "link-cplusplus"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369"
dependencies = [
"cc",
]
[[package]]
name = "local-channel"
@@ -987,6 +1107,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "match_cfg"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
[[package]]
name = "memchr"
version = "2.5.0"
@@ -1132,6 +1258,12 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1"
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "os_str_bytes"
version = "6.3.0"
@@ -1217,9 +1349,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.46"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b"
checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
dependencies = [
"unicode-ident",
]
@@ -1289,6 +1421,21 @@ version = "0.6.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "ring"
version = "0.16.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]]
name = "rustc_version"
version = "0.4.0"
@@ -1298,18 +1445,77 @@ dependencies = [
"semver",
]
[[package]]
name = "rustls"
version = "0.20.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033"
dependencies = [
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls-native-certs"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50"
dependencies = [
"openssl-probe",
"rustls-pemfile",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pemfile"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
dependencies = [
"base64",
]
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "schannel"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2"
dependencies = [
"lazy_static",
"windows-sys",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scratch"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898"
[[package]]
name = "sct"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "sea_battle_backend"
version = "0.1.0"
@@ -1333,6 +1539,54 @@ dependencies = [
"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]]
name = "security-framework"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "semver"
version = "1.0.14"
@@ -1361,9 +1615,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.85"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074"
dependencies = [
"itoa",
"ryu",
@@ -1473,9 +1727,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.9.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
[[package]]
name = "smawk"
@@ -1493,6 +1747,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "strsim"
version = "0.10.0"
@@ -1501,9 +1761,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.101"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1"
dependencies = [
"proc-macro2",
"quote",
@@ -1552,9 +1812,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.14"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3f9a28b618c3a6b9251b6908e9c99e04b9e5c02e6581ccbb67d59c34ef7f9b"
checksum = "d634a985c4d4238ec39cacaed2e7ae552fbd3c476b552c1deac3021b7d7eaf0c"
dependencies = [
"itoa",
"libc",
@@ -1615,6 +1875,17 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-rustls"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59"
dependencies = [
"rustls",
"tokio",
"webpki",
]
[[package]]
name = "tokio-tungstenite"
version = "0.17.2"
@@ -1623,8 +1894,12 @@ checksum = "f714dd15bead90401d77e04243611caec13726c2408afd5b31901dfcdcb3b181"
dependencies = [
"futures-util",
"log",
"rustls",
"rustls-native-certs",
"tokio",
"tokio-rustls",
"tungstenite",
"webpki",
]
[[package]]
@@ -1642,10 +1917,16 @@ dependencies = [
]
[[package]]
name = "tracing"
version = "0.1.36"
name = "tower-service"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fce9567bd60a67d08a16488756721ba392f24f29006402881e43b19aac64307"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]]
name = "tracing"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
dependencies = [
"cfg-if",
"log",
@@ -1655,13 +1936,19 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.29"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeea4303076558a00714b823f9ad67d58a3bbda1df83d8827d21193156e22f7"
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
dependencies = [
"once_cell",
]
[[package]]
name = "try-lock"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "tui"
version = "0.19.0"
@@ -1688,10 +1975,12 @@ dependencies = [
"httparse",
"log",
"rand",
"rustls",
"sha-1",
"thiserror",
"url",
"utf-8",
"webpki",
]
[[package]]
@@ -1708,9 +1997,9 @@ checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
[[package]]
name = "unicode-ident"
version = "1.0.4"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
name = "unicode-linebreak"
@@ -1743,6 +2032,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "url"
version = "2.3.1"
@@ -1762,9 +2057,9 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uuid"
version = "1.1.2"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83"
dependencies = [
"getrandom",
]
@@ -1775,6 +2070,16 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "want"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
dependencies = [
"log",
"try-lock",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
@@ -1835,6 +2140,26 @@ version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "web-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "webpki"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd"
dependencies = [
"ring",
"untrusted",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@@ -2,5 +2,5 @@
members = [
"sea_battle_backend",
"cli_player"
"sea_battle_cli_player"
]

6
rust/Dockerfile Normal file
View File

@@ -0,0 +1,6 @@
FROM debian:bullseye-slim
COPY sea_battle_backend /usr/local/bin/sea_battle_backend
ENTRYPOINT /usr/local/bin/sea_battle_backend

10
rust/build_docker_image.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
cargo build --release --bins
TEMP_DIR=$(mktemp -d)
cp target/release/sea_battle_backend "$TEMP_DIR"
docker build -f Dockerfile "$TEMP_DIR" -t pierre42100/seabattleapi
rm -r $TEMP_DIR

View File

@@ -1,24 +0,0 @@
[package]
name = "cli_player"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sea_battle_backend = { path = "../sea_battle_backend" }
clap = { version = "3.2.17", features = ["derive"] }
log = "0.4.17"
env_logger = "0.9.0"
tui = "0.19.0"
crossterm = "0.25.0"
lazy_static = "1.4.0"
tokio = "1.21.2"
num = "0.4.0"
num-traits = "0.2.15"
num-derive = "0.3.3"
textwrap = "0.15.1"
tokio-tungstenite = "0.17.2"
serde_urlencoded = "0.7.1"
futures = "0.3.23"
serde_json = "1.0.85"

View File

@@ -1,95 +0,0 @@
use crate::cli_args::cli_args;
use crate::server;
use futures::{SinkExt, StreamExt};
use sea_battle_backend::data::GameRules;
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
use sea_battle_backend::server::BotPlayQuery;
use sea_battle_backend::utils::{boxed_error, Res};
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
/// Connection client
///
/// This structure acts as a wrapper around websocket connection that handles automatically parsing
/// of incoming messages and encoding of outgoing messages
pub struct Client {
socket: WebSocketStream<MaybeTlsStream<TcpStream>>,
}
impl Client {
/// Start to play against a bot
///
/// When playing against a bot, local server is always used
pub async fn start_bot_play(rules: &GameRules) -> Res<Self> {
server::start_server_if_missing().await;
Self::connect_url(
&cli_args().local_server_address(),
&format!(
"/play/bot?{}",
serde_urlencoded::to_string(&BotPlayQuery {
rules: rules.clone(),
player_name: "Human".to_string()
})
.unwrap()
),
)
.await
}
/// Do connect to a server, returning
async fn connect_url(server: &str, uri: &str) -> Res<Self> {
let mut url = server.replace("http", "ws");
url.push_str(uri);
log::debug!("Connecting to {}", url);
let (socket, _) = tokio_tungstenite::connect_async(url).await?;
Ok(Self { socket })
}
/// Receive next message from stream
async fn recv_next_msg(&mut self) -> Res<ServerMessage> {
loop {
let chunk = match self.socket.next().await {
None => return Err(boxed_error("No more message in queue!")),
Some(d) => d,
};
match chunk? {
Message::Text(t) => {
log::debug!("TEXT Got a text message from server!");
let msg: ServerMessage = serde_json::from_str(&t)?;
return Ok(msg);
}
Message::Binary(_) => {
log::debug!("BINARY Got an unexpected binary message");
return Err(boxed_error("Received an unexpected binary message!"));
}
Message::Ping(_) => {
log::debug!("PING Got a ping message from server");
}
Message::Pong(_) => {
log::debug!("PONG Got a pong message");
}
Message::Close(_) => {
log::debug!("CLOSE Got a close websocket message");
return Err(boxed_error("Server requested to close connection!"));
}
Message::Frame(_) => {
log::debug!("FRAME Got an unexpected frame from server!");
return Err(boxed_error("Got an unexpected frame!"));
}
}
}
}
/// Send a message through the stream
pub async fn send_message(&mut self, msg: &ClientMessage) -> Res {
self.socket
.send(Message::Text(serde_json::to_string(&msg)?))
.await?;
Ok(())
}
}

View File

@@ -1,106 +0,0 @@
use std::error::Error;
use std::io;
use std::io::ErrorKind;
use cli_player::cli_args::{cli_args, TestDevScreen};
use crossterm::event::DisableMouseCapture;
use crossterm::event::EnableMouseCapture;
use crossterm::execute;
use crossterm::terminal::EnterAlternateScreen;
use crossterm::terminal::LeaveAlternateScreen;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use env_logger::Env;
use tui::backend::{Backend, CrosstermBackend};
use tui::Terminal;
use cli_player::server::start_server_if_missing;
use cli_player::ui_screens::*;
use sea_battle_backend::data::GameRules;
/// Test code screens
async fn run_dev<B: Backend>(
terminal: &mut Terminal<B>,
d: TestDevScreen,
) -> Result<(), Box<dyn Error>> {
let res = match d {
TestDevScreen::Popup => popup_screen::PopupScreen::new("Welcome there!!")
.show(terminal)?
.as_string(),
TestDevScreen::Input => input_screen::InputScreen::new("What it your name ?")
.set_title("A custom title")
.show(terminal)?
.as_string(),
TestDevScreen::Confirm => {
confirm_dialog::ConfirmDialogScreen::new("Do you really want to quit game?")
.show(terminal)?
.as_string()
}
TestDevScreen::SelectBotType => select_bot_type::SelectBotTypeScreen::default()
.show(terminal)?
.as_string(),
TestDevScreen::SelectPlayMode => select_play_mode::SelectPlayModeScreen::default()
.show(terminal)?
.as_string(),
TestDevScreen::SetBoatsLayout => {
let rules = GameRules {
boats_can_touch: true,
..Default::default()
};
set_boats_layout::SetBoatsLayoutScreen::new(&rules)
.show(terminal)?
.as_string()
}
TestDevScreen::ConfigureGameRules => {
configure_game_rules::GameRulesConfigurationScreen::new(GameRules::default())
.show(terminal)?
.as_string()
}
};
Err(io::Error::new(
ErrorKind::Other,
format!("DEV result: {:?}", res),
))?
}
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
if let Some(d) = cli_args().dev_screen {
run_dev(terminal, d).await
} else {
// TODO : run app
Ok(())
}
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default()).init();
start_server_if_missing().await;
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// create app and run it
let res = run_app(&mut terminal).await;
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = res {
println!("{:?}", err)
}
Ok(())
}

View File

@@ -1,22 +0,0 @@
use std::fmt::Debug;
pub mod configure_game_rules;
pub mod confirm_dialog;
pub mod input_screen;
pub mod popup_screen;
pub mod select_bot_type;
pub mod select_play_mode;
pub mod set_boats_layout;
pub mod utils;
#[derive(Debug)]
pub enum ScreenResult<E> {
Ok(E),
Canceled,
}
impl<E: Debug> ScreenResult<E> {
pub fn as_string(&self) -> String {
format!("{:#?}", self)
}
}

View File

@@ -2,14 +2,20 @@
name = "sea_battle_backend"
version = "0.1.0"
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
[dependencies]
clap = { version = "3.2.17", features = ["derive"] }
clap = { version = "4.0.15", features = ["derive"] }
log = "0.4.17"
env_logger = "0.9.0"
serde = { version = "1.0.144", features = ["derive"] }
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85"
actix-web = "4.1.0"
actix-cors = "0.6.2"

View File

@@ -0,0 +1,30 @@
# Sea battle backend
[![Build Status](https://drone.communiquons.org/api/badges/pierre/SeaBattle/status.svg)](https://drone.communiquons.org/pierre/SeaBattle)
[![Crate](https://img.shields.io/crates/v/sea_battle_backend.svg)](https://crates.io/crates/sea_battle_backend)
[![Documentation](https://docs.rs/sea_battle_backend/badge.svg)](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.

View File

@@ -58,6 +58,8 @@ impl Player for BotPlayer {
unreachable!()
}
fn waiting_for_opponent_boats_layout(&self) {}
fn notify_other_player_ready(&self) {}
fn notify_game_starting(&self) {}

View File

@@ -21,3 +21,9 @@ pub const MULTI_PLAYER_PLAYER_BOATS: [usize; 5] = [2, 3, 3, 4, 5];
pub const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
pub const INVITE_CODE_LENGTH: usize = 5;
pub const MIN_PLAYER_NAME_LENGTH: usize = 1;
pub const MAX_PLAYER_NAME_LENGTH: usize = 10;
pub const MIN_STRIKE_TIMEOUT: u64 = 5;
pub const MAX_STRIKE_TIMEOUT: u64 = 90;

View File

@@ -85,6 +85,10 @@ impl Coordinates {
}
}
pub fn invalid() -> Self {
Self { x: -1, y: -1 }
}
pub fn is_valid(&self, rules: &GameRules) -> bool {
self.x >= 0
&& self.y >= 0
@@ -485,6 +489,7 @@ mod test {
boats_str: "1,1".to_string(),
boats_can_touch: false,
player_continue_on_hit: false,
strike_timeout: None,
bot_type: BotType::Random,
};

View File

@@ -78,6 +78,7 @@ impl PrintableMap for PrintableCurrentGameMapStatus {
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Default)]
pub struct CurrentGameStatus {
pub remaining_time_for_strike: Option<u64>,
pub rules: GameRules,
pub your_map: CurrentGameMapStatus,
pub opponent_map: CurrentGameMapStatus,
@@ -300,6 +301,12 @@ impl CurrentGameStatus {
BotType::Smart => self.find_smart_bot_fire_location(),
}
}
/// Check out whether game is over or not
pub fn is_game_over(&self) -> bool {
self.opponent_map.sunk_boats.len() == self.rules.boats_list().len()
|| self.your_map.sunk_boats.len() == self.rules.boats_list().len()
}
}
#[cfg(test)]

View File

@@ -1,6 +1,6 @@
use crate::consts::*;
use crate::data::{BotType, PlayConfiguration};
use serde_with::{serde_as, DisplayFromStr};
use serde_with::{serde_as, DisplayFromStr, NoneAsEmptyString};
#[serde_as]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Eq, PartialEq)]
@@ -15,6 +15,8 @@ pub struct GameRules {
pub boats_can_touch: bool,
#[serde_as(as = "DisplayFromStr")]
pub player_continue_on_hit: bool,
#[serde_as(as = "NoneAsEmptyString")]
pub strike_timeout: Option<u64>,
pub bot_type: BotType,
}
@@ -36,7 +38,8 @@ impl GameRules {
.join(","),
boats_can_touch: MULTI_PLAYER_BOATS_CAN_TOUCH,
player_continue_on_hit: MULTI_PLAYER_PLAYER_CAN_CONTINUE_AFTER_HIT,
bot_type: BotType::Random,
strike_timeout: Some(30),
bot_type: BotType::Smart,
}
}
@@ -50,6 +53,11 @@ impl GameRules {
self
}
pub fn with_strike_timeout(mut self, timeout: u64) -> Self {
self.strike_timeout = Some(timeout);
self
}
/// Set the list of boats for this configuration
pub fn set_boats_list(&mut self, boats: &[usize]) {
self.boats_str = boats
@@ -121,6 +129,16 @@ impl GameRules {
}
}
if let Some(timeout) = self.strike_timeout {
if timeout < config.min_strike_timeout {
errors.push("Strike timeout is too short!");
}
if timeout > config.max_strike_timeout {
errors.push("Strike timeout is too long!");
}
}
errors
}

View File

@@ -9,13 +9,42 @@ pub enum BotType {
Smart,
}
#[derive(serde::Serialize)]
impl BotType {
pub fn description(&self) -> &'static BotDescription {
BOTS_TYPES.iter().find(|d| d.r#type == *self).unwrap()
}
}
#[derive(serde::Serialize, Clone)]
pub struct BotDescription {
pub r#type: BotType,
pub name: &'static str,
pub description: &'static str,
}
const BOTS_TYPES: [BotDescription; 4] = [
BotDescription {
r#type: BotType::Linear,
name: "Linear",
description: "Linear strike. Shoot A1, A2, A3, ..., B1, B2, ...",
},
BotDescription {
r#type: BotType::Random,
name: "Random",
description: "Random search. Random strike.",
},
BotDescription {
r#type: BotType::Intermediate,
name: "Intermediate",
description: "Random search. Intelligent strike.",
},
BotDescription {
r#type: BotType::Smart,
name: "Smart",
description: "Smart search. Smart strike.",
},
];
#[derive(serde::Serialize)]
pub struct PlayConfiguration {
pub min_boat_len: usize,
@@ -26,8 +55,12 @@ pub struct PlayConfiguration {
pub max_map_height: usize,
pub min_boats_number: usize,
pub max_boats_number: usize,
pub bot_types: Vec<BotDescription>,
pub bot_types: &'static [BotDescription],
pub ordinate_alphabet: &'static str,
pub min_player_name_len: usize,
pub max_player_name_len: usize,
pub min_strike_timeout: u64,
pub max_strike_timeout: u64,
}
impl Default for PlayConfiguration {
@@ -41,29 +74,12 @@ impl Default for PlayConfiguration {
max_map_height: MAX_MAP_HEIGHT,
min_boats_number: MIN_BOATS_NUMBER,
max_boats_number: MAX_BOATS_NUMBER,
bot_types: vec![
BotDescription {
r#type: BotType::Linear,
name: "Linear",
description: "Linear strike. Shoot A1, A2, A3, ..., B1, B2, ...",
},
BotDescription {
r#type: BotType::Random,
name: "Ranom",
description: "Random search. Random strike.",
},
BotDescription {
r#type: BotType::Intermediate,
name: "Intermediate",
description: "Random search. Intelligent strike.",
},
BotDescription {
r#type: BotType::Smart,
name: "Smart",
description: "Smart search. Smart strike.",
},
],
bot_types: &BOTS_TYPES,
ordinate_alphabet: ALPHABET,
min_player_name_len: MIN_PLAYER_NAME_LENGTH,
max_player_name_len: MAX_PLAYER_NAME_LENGTH,
min_strike_timeout: MIN_STRIKE_TIMEOUT,
max_strike_timeout: MAX_STRIKE_TIMEOUT,
}
}
}

View File

@@ -89,7 +89,6 @@ impl Handler<CreateInvite> for DispatcherActor {
msg.1.do_send(ServerMessage::SetInviteCode {
code: invite_code.clone(),
});
msg.1.do_send(ServerMessage::WaitingForAnotherPlayer);
self.with_invite.insert(
invite_code,
PendingPlayer {

View File

@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::time::Duration;
use actix::prelude::*;
use actix::{Actor, Context, Handler};
@@ -6,6 +7,7 @@ use uuid::Uuid;
use crate::bot_player::BotPlayer;
use crate::data::*;
use crate::utils::time_utils::time;
pub trait Player {
fn get_name(&self) -> &str;
@@ -22,6 +24,8 @@ pub trait Player {
fn rejected_boats_layout(&self, errors: Vec<&'static str>);
fn waiting_for_opponent_boats_layout(&self);
fn notify_other_player_ready(&self);
fn notify_game_starting(&self);
@@ -49,6 +53,9 @@ pub trait Player {
fn opponent_replaced_by_bot(&self);
}
/// How often strike timeout controller is run
const STRIKE_TIMEOUT_CONTROL: Duration = Duration::from_secs(1);
fn opponent(index: usize) -> usize {
match index {
0 => 1,
@@ -68,6 +75,14 @@ enum GameStatus {
RematchRejected,
}
impl GameStatus {
pub fn can_game_continue_with_bot(&self) -> bool {
*self != GameStatus::Finished
&& *self != GameStatus::RematchRejected
&& *self != GameStatus::RematchRequested
}
}
pub struct Game {
rules: GameRules,
players: Vec<Arc<dyn Player>>,
@@ -75,6 +90,7 @@ pub struct Game {
map_0: Option<GameMap>,
map_1: Option<GameMap>,
turn: usize,
curr_strike_request_started: u64,
}
impl Game {
@@ -86,6 +102,7 @@ impl Game {
map_0: None,
map_1: None,
turn: 0,
curr_strike_request_started: 0,
}
}
@@ -120,10 +137,14 @@ impl Game {
self.turn
);
self.request_fire();
self.request_fire(true);
}
fn request_fire(&mut self, reset_counter: bool) {
if reset_counter {
self.curr_strike_request_started = time();
}
fn request_fire(&self) {
self.players[self.turn].request_fire(self.get_game_status_for_player(self.turn));
self.players[opponent(self.turn)]
.opponent_must_fire(self.get_game_status_for_player(opponent(self.turn)));
@@ -138,6 +159,27 @@ impl Game {
.unwrap()
}
/// Replace user for a fire in case of timeout
fn force_fire_in_case_of_timeout(&mut self) {
if self.status != GameStatus::Started || self.rules.strike_timeout.is_none() {
return;
}
let timeout = self.rules.strike_timeout.unwrap_or_default();
if time() <= self.curr_strike_request_started + timeout {
return;
}
// Determine target of fire
let target = self
.get_game_status_for_player(self.turn)
.find_fire_coordinates_for_bot_type(self.rules.bot_type);
// Fire as player
self.handle_fire(target);
}
fn player_map_mut(&mut self, id: usize) -> &mut GameMap {
match id {
0 => self.map_0.as_mut(),
@@ -156,7 +198,7 @@ impl Game {
// Easiest case : player missed his fire
if result == FireResult::Missed {
self.turn = opponent(self.turn);
self.request_fire();
self.request_fire(true);
return;
}
@@ -178,7 +220,9 @@ impl Game {
self.turn = opponent(self.turn);
}
self.request_fire();
self.request_fire(
result != FireResult::AlreadyTargetedPosition && result != FireResult::Rejected,
);
}
fn handle_request_rematch(&mut self, player_id: Uuid) {
@@ -213,6 +257,9 @@ impl Game {
/// Get current game status for a specific player
fn get_game_status_for_player(&self, id: usize) -> CurrentGameStatus {
CurrentGameStatus {
remaining_time_for_strike: self.rules.strike_timeout.map(|v| {
((self.curr_strike_request_started + v) as i64 - time() as i64).max(0) as u64
}),
rules: self.rules.clone(),
your_map: self.player_map(id).current_map_status(false),
opponent_map: self
@@ -224,6 +271,14 @@ impl Game {
impl Actor for Game {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
if self.rules.strike_timeout.is_some() {
ctx.run_interval(STRIKE_TIMEOUT_CONTROL, |act, _ctx| {
act.force_fire_in_case_of_timeout();
});
}
}
}
#[derive(Message)]
@@ -286,6 +341,8 @@ impl Handler<SetBoatsLayout> for Game {
if self.map_0.is_some() && self.map_1.is_some() {
self.players.iter().for_each(|p| p.notify_game_starting());
self.start_fire_exchanges();
} else {
self.players[player_index].waiting_for_opponent_boats_layout();
}
}
}
@@ -363,7 +420,9 @@ impl Handler<PlayerLeftGame> for Game {
self.players[opponent(offline_player)].opponent_left_game();
// If the other player is a bot or if the game is not running, stop the game
if self.status != GameStatus::Started || self.players[opponent(offline_player)].is_bot() {
if !self.status.can_game_continue_with_bot()
|| self.players[opponent(offline_player)].is_bot()
{
ctx.stop();
} else {
// Replace the player with a bot
@@ -371,8 +430,11 @@ impl Handler<PlayerLeftGame> for Game {
Arc::new(BotPlayer::new(self.rules.bot_type, ctx.address()));
self.players[opponent(offline_player)].opponent_replaced_by_bot();
if self.turn == offline_player {
self.request_fire();
// Re-do current action
if self.status == GameStatus::Started {
self.request_fire(true);
} else if self.status == GameStatus::WaitingForBoatsDisposition {
self.players[offline_player].query_boats_layout(&self.rules);
}
}
}

View File

@@ -47,6 +47,11 @@ impl Player for HumanPlayer {
});
}
fn waiting_for_opponent_boats_layout(&self) {
self.player
.do_send(ServerMessage::WaitingForOtherPlayerConfiguration);
}
fn notify_other_player_ready(&self) {
self.player.do_send(ServerMessage::OpponentReady);
}
@@ -109,6 +114,7 @@ impl Player for HumanPlayer {
impl HumanPlayer {
pub fn handle_client_message(&self, msg: ClientMessage) {
log::debug!("Got message from client: {:?}", msg);
match msg {
ClientMessage::StopGame => self.game.do_send(PlayerLeftGame(self.uuid)),
ClientMessage::BoatsLayout { layout } => {

View File

@@ -8,6 +8,7 @@ use actix_web_actors::ws::{CloseCode, CloseReason, Message, ProtocolError, Webso
use uuid::Uuid;
use crate::bot_player::BotPlayer;
use crate::consts::{MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH};
use crate::data::{BoatsLayout, Coordinates, CurrentGameStatus, FireResult, GameRules};
use crate::dispatcher_actor::{AcceptInvite, CreateInvite, DispatcherActor, PlayRandom};
use crate::game::{AddPlayer, Game};
@@ -26,6 +27,7 @@ pub enum StartMode {
PlayRandom,
}
/// The messages a client could send to the server
#[derive(serde::Deserialize, serde::Serialize, Debug)]
#[serde(tag = "type")]
pub enum ClientMessage {
@@ -37,6 +39,10 @@ pub enum ClientMessage {
RejectRematch,
}
/// The list of messages that can be sent from the server to the client
///
/// Messages types are ordered in the enum in a "kind of" chronogical order: most messages should be
/// sent only if the messages type below it have not already been sent.
#[derive(Message)]
#[rtype(result = "()")]
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
@@ -144,6 +150,13 @@ impl Actor for HumanPlayerWS {
type Context = WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
// Check player name length
if self.name.len() < MIN_PLAYER_NAME_LENGTH || self.name.len() > MAX_PLAYER_NAME_LENGTH {
log::error!("Close connection due to invalid user name!");
ctx.stop();
return;
}
self.hb(ctx);
self.send_message(ServerMessage::WaitingForAnotherPlayer, ctx);
@@ -217,7 +230,7 @@ impl StreamHandler<Result<ws::Message, ProtocolError>> for HumanPlayerWS {
log::warn!("Got unsupported continuation message!");
}
Ok(Message::Pong(_)) => {
log::info!("Got pong message");
log::debug!("Got pong message");
self.hb = Instant::now();
}
Ok(Message::Close(reason)) => {
@@ -233,6 +246,7 @@ impl Handler<ServerMessage> for HumanPlayerWS {
type Result = ();
fn handle(&mut self, msg: ServerMessage, ctx: &mut Self::Context) -> Self::Result {
log::debug!("Send message through WS: {:?}", msg);
ctx.text(serde_json::to_string(&msg).unwrap());
}
}

View File

@@ -131,6 +131,8 @@ async fn start_random(
resp
}
pub async fn start_server(args: Args) -> std::io::Result<()> {
log::info!("Start to listen on {}", args.listen_address);
let args_clone = args.clone();
let dispatcher_actor = DispatcherActor::default().start();
@@ -161,6 +163,7 @@ pub async fn start_server(args: Args) -> std::io::Result<()> {
#[cfg(test)]
mod test {
use crate::data::GameRules;
use crate::server::BotPlayQuery;
#[test]
@@ -175,4 +178,20 @@ mod test {
assert_eq!(query, des)
}
#[test]
fn simple_bot_request_serialize_deserialize_no_timeout() {
let query = BotPlayQuery {
rules: GameRules {
strike_timeout: None,
..Default::default()
},
player_name: "Player".to_string(),
};
let string = serde_urlencoded::to_string(&query).unwrap();
let des = serde_urlencoded::from_str(&string).unwrap();
assert_eq!(query, des)
}
}

View File

@@ -39,7 +39,7 @@ pub struct BotClient {
requested_rules: GameRules,
layout: Option<BoatsLayout>,
number_plays: usize,
server_msg_callback: Option<Box<dyn FnMut(&ServerMessage)>>,
server_msg_callback: Option<Box<dyn FnMut(&mut ServerMessage)>>,
play_as_bot_type: BotType,
}
@@ -81,7 +81,7 @@ impl BotClient {
pub fn with_server_msg_callback<F>(mut self, cb: F) -> Self
where
F: FnMut(&ServerMessage) + 'static,
F: FnMut(&mut ServerMessage) + 'static,
{
self.server_msg_callback = Some(Box::new(cb));
self
@@ -152,7 +152,7 @@ impl BotClient {
};
while let Some(chunk) = socket.next().await {
let message = match chunk? {
let mut message = match chunk? {
Message::Text(message) => {
log::trace!("TEXT message from server: {}", message);
@@ -182,7 +182,7 @@ impl BotClient {
};
if let Some(cb) = &mut self.server_msg_callback {
(cb)(&message)
(cb)(&mut message)
}
match message {

View File

@@ -9,7 +9,7 @@ use crate::test::play_utils::check_no_replay_on_hit;
use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
fn check_strikes_are_linear(msg: &ServerMessage) {
fn check_strikes_are_linear(msg: &mut ServerMessage) {
if let ServerMessage::RequestFire { status } = msg {
let mut in_fire_location = true;
for y in 0..status.rules.map_height {

View File

@@ -1,13 +1,16 @@
use tokio::task;
use crate::args::Args;
use crate::consts::MIN_STRIKE_TIMEOUT;
use crate::data::{BoatsLayout, GameRules};
use crate::human_player_ws::ServerMessage;
use crate::server::start_server;
use crate::test::bot_client;
use crate::test::bot_client::ClientEndResult;
use crate::test::play_utils::check_no_replay_on_hit;
use crate::test::TestPort;
use crate::utils::network_utils::wait_for_port;
use crate::utils::time_utils::time;
#[tokio::test]
async fn invalid_port() {
@@ -201,3 +204,37 @@ async fn full_game_no_replay_on_hit() {
})
.await;
}
#[tokio::test]
async fn check_fire_time_out() {
let _ = env_logger::builder().is_test(true).try_init();
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
task::spawn_local(start_server(Args::for_test(TestPort::RandomCheckTimeout)));
wait_for_port(TestPort::RandomCheckTimeout.port()).await;
let start = time();
let mut did_skip_one = false;
let res = bot_client::BotClient::new(TestPort::RandomCheckTimeout.as_url())
.with_rules(
GameRules::random_players_rules().with_strike_timeout(MIN_STRIKE_TIMEOUT),
)
.with_server_msg_callback(move |msg| {
if matches!(msg, ServerMessage::RequestFire { .. }) && !did_skip_one {
*msg = ServerMessage::OpponentReplacedByBot;
did_skip_one = true;
}
})
.run_client()
.await
.unwrap();
assert!(matches!(res, ClientEndResult::Finished { .. }));
assert!(time() - start >= MIN_STRIKE_TIMEOUT);
})
.await;
}

View File

@@ -10,6 +10,7 @@ enum TestPort {
RandomBotInvalidBoatsLayoutLenOfABoat,
RandomBotFullGameMultipleRematch,
RandomBotNoReplayOnHit,
RandomCheckTimeout,
LinearBotFullGame,
LinearBotNoReplayOnHit,
IntermediateBotFullGame,

View File

@@ -1,7 +1,7 @@
use crate::human_player_ws::ServerMessage;
/// Make sure player can not replay after successful hit
pub fn check_no_replay_on_hit(msg: &ServerMessage) {
pub fn check_no_replay_on_hit(msg: &mut ServerMessage) {
if let ServerMessage::OpponentMustFire { status } | ServerMessage::RequestFire { status } = msg
{
let diff =

View File

@@ -1,12 +1,4 @@
use std::error::Error;
use std::fmt::Display;
use std::io::ErrorKind;
pub mod network_utils;
pub mod res_utils;
pub mod string_utils;
pub type Res<E = ()> = Result<E, Box<dyn Error>>;
pub fn boxed_error<D: Display>(msg: D) -> Box<dyn Error> {
Box::new(std::io::Error::new(ErrorKind::Other, msg.to_string()))
}
pub mod time_utils;

View File

@@ -0,0 +1,9 @@
use std::error::Error;
use std::fmt::Display;
use std::io::ErrorKind;
pub type Res<E = ()> = Result<E, Box<dyn Error>>;
pub fn boxed_error<D: Display>(msg: D) -> Box<dyn Error> {
Box::new(std::io::Error::new(ErrorKind::Other, msg.to_string()))
}

View File

@@ -0,0 +1,9 @@
use std::time::{SystemTime, UNIX_EPOCH};
/// Get the current time since epoch
pub fn time() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
}

View File

@@ -0,0 +1,33 @@
[package]
name = "sea_battle_cli_player"
version = "0.1.0"
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
[dependencies]
sea_battle_backend = { path = "../sea_battle_backend", version = "0.1.0" }
clap = { version = "4.0.15", features = ["derive"] }
log = "0.4.17"
env_logger = "0.9.0"
tui = "0.19.0"
crossterm = "0.25.0"
lazy_static = "1.4.0"
tokio = "1.21.2"
num = "0.4.0"
num-traits = "0.2.15"
num-derive = "0.3.3"
textwrap = "0.15.1"
tokio-tungstenite = { version = "0.17.2", features = ["__rustls-tls", "rustls-tls-native-roots"] }
serde_urlencoded = "0.7.1"
futures = "0.3.23"
serde_json = "1.0.85"
hostname = "0.3.1"
rustls = "0.20.6"
hyper-rustls = { version = "0.23.0", features = ["rustls-native-certs"] }

View File

@@ -0,0 +1,41 @@
# Sea battle cli player
[![Build Status](https://drone.communiquons.org/api/badges/pierre/SeaBattle/status.svg)](https://drone.communiquons.org/pierre/SeaBattle)
[![Crate](https://img.shields.io/crates/v/sea_battle_cli_player.svg)](https://crates.io/crates/sea_battle_cli_player)
[![Documentation](https://docs.rs/sea_battle_cli_player/badge.svg)](https://docs.rs/sea_battle_cli_player/)
![](img/SeaBattleCli.png)
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
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View File

@@ -13,15 +13,14 @@ pub enum TestDevScreen {
#[derive(Parser, Debug)]
pub struct CliArgs {
// TODO: switch default sever uri to real one when we get one
/// Upstream server to use
#[clap(
short,
long,
value_parser,
default_value = "https://fixme.communiquons.org"
default_value = "https://seabattleapi.communiquons.org"
)]
pub server_uri: String,
pub remote_server_uri: String,
/// Local server listen address
#[clap(short, long, default_value = "127.0.0.1:5679")]
@@ -29,6 +28,10 @@ pub struct CliArgs {
#[clap(long, value_enum)]
pub dev_screen: Option<TestDevScreen>,
/// Run as server instead of as client
#[clap(long, short)]
pub serve: bool,
}
impl CliArgs {

View File

@@ -0,0 +1,206 @@
use crate::cli_args::cli_args;
use crate::server;
use futures::stream::{SplitSink, SplitStream};
use futures::{SinkExt, StreamExt};
use hyper_rustls::ConfigBuilderExt;
use sea_battle_backend::data::GameRules;
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
use sea_battle_backend::server::{
AcceptInviteQuery, BotPlayQuery, CreateInviteQuery, PlayRandomQuery,
};
use sea_battle_backend::utils::res_utils::{boxed_error, Res};
use std::fmt::Display;
use std::sync::mpsc::TryRecvError;
use std::sync::{mpsc, Arc};
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
type WsStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
/// Connection client
///
/// This structure acts as a wrapper around websocket connection that handles automatically parsing
/// of incoming messages and encoding of outgoing messages
pub struct Client {
sink: SplitSink<WsStream, Message>,
receiver: mpsc::Receiver<ServerMessage>,
}
impl Client {
/// Start to play against a bot
///
/// When playing against a bot, local server is always used
pub async fn start_bot_play(rules: &GameRules) -> Res<Self> {
server::start_server_if_missing().await;
Self::connect_url(
&cli_args().local_server_address(),
&format!(
"/play/bot?{}",
serde_urlencoded::to_string(&BotPlayQuery {
rules: rules.clone(),
player_name: "Human".to_string()
})
.unwrap()
),
)
.await
}
/// Start to play against a random player
pub async fn start_random_play<D: Display>(player_name: D) -> Res<Self> {
Self::connect_url(
&cli_args().remote_server_uri,
&format!(
"/play/random?{}",
serde_urlencoded::to_string(&PlayRandomQuery {
player_name: player_name.to_string()
})
.unwrap()
),
)
.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
async fn connect_url(server: &str, uri: &str) -> Res<Self> {
let mut ws_url = server.replace("http", "ws");
ws_url.push_str(uri);
log::debug!("Connecting to {}", ws_url);
let (socket, _) = if ws_url.starts_with("wss") {
// Perform a connection over TLS
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_native_roots()
.with_no_client_auth();
let connector = tokio_tungstenite::Connector::Rustls(Arc::new(config));
tokio_tungstenite::connect_async_tls_with_config(ws_url, None, Some(connector)).await?
} else {
// Perform an unsecure connection
tokio_tungstenite::connect_async(ws_url).await?
};
let (sink, mut stream) = socket.split();
// Receive server message on a separate task
let (sender, receiver) = mpsc::channel();
tokio::task::spawn(async move {
loop {
match Self::recv_next_msg(&mut stream).await {
Ok(msg) => {
if let Err(e) = sender.send(msg.clone()) {
log::debug!("Failed to forward ws message! {} (msg={:?})", e, msg);
break;
}
}
Err(e) => {
log::debug!("Failed receive next message from websocket! {}", e);
break;
}
}
}
});
Ok(Self { sink, receiver })
}
/// Receive next message from stream
async fn recv_next_msg(stream: &mut SplitStream<WsStream>) -> Res<ServerMessage> {
loop {
let chunk = match stream.next().await {
None => return Err(boxed_error("No more message in queue!")),
Some(d) => d,
};
match chunk? {
Message::Text(t) => {
log::debug!("TEXT Got a text message from server!");
let msg: ServerMessage = serde_json::from_str(&t)?;
return Ok(msg);
}
Message::Binary(_) => {
log::debug!("BINARY Got an unexpected binary message");
return Err(boxed_error("Received an unexpected binary message!"));
}
Message::Ping(_) => {
log::debug!("PING Got a ping message from server");
}
Message::Pong(_) => {
log::debug!("PONG Got a pong message");
}
Message::Close(_) => {
log::debug!("CLOSE Got a close websocket message");
return Err(boxed_error("Server requested to close connection!"));
}
Message::Frame(_) => {
log::debug!("FRAME Got an unexpected frame from server!");
return Err(boxed_error("Got an unexpected frame!"));
}
}
}
}
/// Send a message through the stream
pub async fn send_message(&mut self, msg: &ClientMessage) -> Res {
self.sink
.send(Message::Text(serde_json::to_string(&msg)?))
.await?;
Ok(())
}
/// Try to receive next message from websocket, in a non-blocking way
pub async fn try_recv_next_message(&self) -> Res<Option<ServerMessage>> {
match self.receiver.try_recv() {
Ok(msg) => Ok(Some(msg)),
Err(TryRecvError::Empty) => Ok(None),
Err(TryRecvError::Disconnected) => Err(boxed_error("Receiver channel disconnected!")),
}
}
/// Block until the next message from websocket is availabl
pub async fn recv_next_message(&self) -> Res<ServerMessage> {
Ok(self.receiver.recv()?)
}
/// Close connection
pub async fn close_connection(&mut self) {
if let Err(e) = self.sink.send(Message::Close(None)).await {
log::debug!("Failed to close WS connection! {:?}", e);
}
}
}

View File

@@ -1,3 +1,5 @@
extern crate core;
pub mod cli_args;
pub mod client;
pub mod constants;

View File

@@ -0,0 +1,196 @@
use std::error::Error;
use std::io;
use std::io::ErrorKind;
use crossterm::event::DisableMouseCapture;
use crossterm::event::EnableMouseCapture;
use crossterm::execute;
use crossterm::terminal::EnterAlternateScreen;
use crossterm::terminal::LeaveAlternateScreen;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use env_logger::Env;
use tui::backend::{Backend, CrosstermBackend};
use tui::Terminal;
use sea_battle_backend::consts::{
INVITE_CODE_LENGTH, MAX_PLAYER_NAME_LENGTH, MIN_PLAYER_NAME_LENGTH,
};
use sea_battle_backend::data::GameRules;
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
async fn run_dev<B: Backend>(
terminal: &mut Terminal<B>,
d: TestDevScreen,
) -> Result<(), Box<dyn Error>> {
let res = match d {
TestDevScreen::Popup => popup_screen::PopupScreen::new("Welcome there!!")
.show(terminal)?
.as_string(),
TestDevScreen::Input => input_screen::InputScreen::new("What it your name ?")
.set_title("A custom title")
.show(terminal)?
.as_string(),
TestDevScreen::Confirm => {
confirm_dialog_screen::ConfirmDialogScreen::new("Do you really want to quit game?")
.show(terminal)?
.as_string()
}
TestDevScreen::SelectBotType => select_bot_type_screen::SelectBotTypeScreen::default()
.show(terminal)?
.as_string(),
TestDevScreen::SelectPlayMode => select_play_mode_screen::SelectPlayModeScreen::default()
.show(terminal)?
.as_string(),
TestDevScreen::SetBoatsLayout => {
let rules = GameRules {
boats_can_touch: true,
..Default::default()
};
set_boats_layout_screen::SetBoatsLayoutScreen::new(&rules)
.show(terminal)?
.as_string()
}
TestDevScreen::ConfigureGameRules => {
configure_game_rules::GameRulesConfigurationScreen::new(GameRules::default())
.show(terminal)?
.as_string()
}
};
Err(io::Error::new(
ErrorKind::Other,
format!("DEV result: {:?}", res),
))?
}
/// Ask the user to specify the name he should be identified with
fn query_player_name<B: Backend>(terminal: &mut Terminal<B>) -> Res<String> {
let mut hostname = hostname::get()?.to_string_lossy().to_string();
if hostname.len() > MAX_PLAYER_NAME_LENGTH {
hostname = hostname[0..MAX_PLAYER_NAME_LENGTH].to_string();
}
let res =
InputScreen::new("Please specify the name to which other players should identify you:")
.set_title("Player name")
.set_value(&hostname)
.set_min_length(MIN_PLAYER_NAME_LENGTH)
.set_max_length(MAX_PLAYER_NAME_LENGTH)
.show(terminal)?;
Ok(res.value().unwrap_or(hostname))
}
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
if let Some(d) = cli_args().dev_screen {
return run_dev(terminal, d).await;
}
let mut rules = GameRules::default();
let mut username = "".to_string();
loop {
let choice = SelectPlayModeScreen::default().show(terminal)?;
if let ScreenResult::Ok(c) = choice {
if c.need_player_name() && username.is_empty() {
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 {
ScreenResult::Ok(SelectPlayModeResult::PlayRandom) => {
Client::start_random_play(&username).await?
}
// Play against bot
ScreenResult::Ok(SelectPlayModeResult::PlayAgainstBot) => {
Client::start_bot_play(&rules).await?
}
// 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,
};
PopupScreen::new("🔌 Connecting...").show_once(terminal)?;
Client::start_accept_invite(code, &username).await?
}
ScreenResult::Canceled | ScreenResult::Ok(SelectPlayModeResult::Exit) => return Ok(()),
};
// Display game screen
GameScreen::new(client).show(terminal).await?;
}
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default()).init();
if cli_args().serve {
run_server().await;
return Ok(());
}
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// create app and run it
let res = run_app(&mut terminal).await;
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = res {
println!("{:?}", err)
}
Ok(())
}

View File

@@ -6,6 +6,21 @@ use sea_battle_backend::utils::network_utils;
use crate::cli_args::cli_args;
pub async fn run_server() {
let local_set = task::LocalSet::new();
local_set
.run_until(async move {
sea_battle_backend::server::start_server(Args {
listen_address: cli_args().listen_address.clone(),
cors: None,
})
.await
.expect("Failed to run local server!")
})
.await;
}
pub async fn start_server_if_missing() {
if !network_utils::is_port_open(cli_args().listen_port()).await {
log::info!(
@@ -15,16 +30,9 @@ pub async fn start_server_if_missing() {
std::thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().unwrap();
let local_set = task::LocalSet::new();
rt.block_on(local_set.run_until(async move {
sea_battle_backend::server::start_server(Args {
listen_address: cli_args().listen_address.clone(),
cors: None,
})
.await
.expect("Failed to run local server!")
}));
rt.block_on(run_server());
});
network_utils::wait_for_port(cli_args().listen_port()).await;
}
}

View File

@@ -5,6 +5,9 @@ use std::time::{Duration, Instant};
use crossterm::event;
use crossterm::event::{Event, KeyCode};
use sea_battle_backend::consts::{
MAX_BOATS_NUMBER, MAX_MAP_HEIGHT, MAX_MAP_WIDTH, MAX_STRIKE_TIMEOUT,
};
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Margin};
use tui::style::{Color, Style};
@@ -13,7 +16,9 @@ use tui::{Frame, Terminal};
use sea_battle_backend::data::GameRules;
use crate::constants::TICK_RATE;
use crate::constants::{HIGHLIGHT_COLOR, TICK_RATE};
use crate::ui_screens::popup_screen::show_screen_too_small_popup;
use crate::ui_screens::select_bot_type_screen::SelectBotTypeScreen;
use crate::ui_screens::utils::centered_rect_size;
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::button_widget::ButtonWidget;
@@ -25,8 +30,10 @@ enum EditingField {
MapWidth = 0,
MapHeight,
BoatsList,
StrikeTimeout,
BoatsCanTouch,
PlayerContinueOnHit,
BotType,
Cancel,
OK,
}
@@ -56,7 +63,7 @@ impl GameRulesConfigurationScreen {
.checked_sub(last_tick.elapsed())
.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;
if let Event::Key(key) = event::read()? {
@@ -70,6 +77,14 @@ impl GameRulesConfigurationScreen {
// Submit results
KeyCode::Enter => {
if self.curr_field == EditingField::BotType {
if let ScreenResult::Ok(t) =
SelectBotTypeScreen::default().show(terminal)?
{
self.rules.bot_type = t;
}
}
if self.curr_field == EditingField::Cancel {
return Ok(ScreenResult::Canceled);
}
@@ -104,24 +119,46 @@ impl GameRulesConfigurationScreen {
{
self.rules.remove_last_boat();
}
if self.curr_field == EditingField::StrikeTimeout {
match self.rules.strike_timeout.unwrap_or(0) / 10 {
0 => self.rules.strike_timeout = None,
v => self.rules.strike_timeout = Some(v),
}
}
}
KeyCode::Char(c) if ('0'..='9').contains(&c) => {
let val = c.to_string().parse::<usize>().unwrap_or_default();
if self.curr_field == EditingField::MapWidth {
if self.curr_field == EditingField::MapWidth
&& self.rules.map_width <= MAX_MAP_WIDTH
{
self.rules.map_width *= 10;
self.rules.map_width += val;
}
if self.curr_field == EditingField::MapHeight {
if self.curr_field == EditingField::MapHeight
&& self.rules.map_height <= MAX_MAP_HEIGHT
{
self.rules.map_height *= 10;
self.rules.map_height += val;
}
if self.curr_field == EditingField::BoatsList {
if self.curr_field == EditingField::BoatsList
&& self.rules.boats_list().len() < MAX_BOATS_NUMBER
{
self.rules.add_boat(val);
}
if self.curr_field == EditingField::StrikeTimeout {
let mut timeout = self.rules.strike_timeout.unwrap_or(0);
if timeout <= MAX_STRIKE_TIMEOUT {
timeout *= 10;
timeout += val as u64;
self.rules.strike_timeout = Some(timeout);
}
}
}
_ => {}
@@ -143,20 +180,31 @@ impl GameRulesConfigurationScreen {
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
let area = centered_rect_size(50, 16, &f.size());
let (w, h) = (50, 23);
let block = Block::default().title("Game rules").borders(Borders::ALL);
if f.size().width < w || f.size().height < h {
show_screen_too_small_popup(f);
return;
}
let area = centered_rect_size(w, h, &f.size());
let block = Block::default()
.title("📓 Game rules")
.borders(Borders::ALL);
f.render_widget(block, area);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(3), // Map width
Constraint::Length(3), // Map height
Constraint::Length(3), // Boats list
Constraint::Length(3), // Strike timeout
Constraint::Length(1), // Boats can touch
Constraint::Length(1), // Player continue on hit
Constraint::Length(3), // Bot type
Constraint::Length(1), // Margin
Constraint::Length(1), // Buttons
Constraint::Length(1), // Error message (if any)
])
@@ -192,6 +240,13 @@ impl GameRulesConfigurationScreen {
);
f.render_widget(editor, chunks[EditingField::BoatsList as usize]);
let editor = TextEditorWidget::new(
"Strike timeout (0 to disable)",
&self.rules.strike_timeout.unwrap_or(0).to_string(),
self.curr_field == EditingField::StrikeTimeout,
);
f.render_widget(editor, chunks[EditingField::StrikeTimeout as usize]);
let editor = CheckboxWidget::new(
"Boats can touch",
self.rules.boats_can_touch,
@@ -206,6 +261,22 @@ impl GameRulesConfigurationScreen {
);
f.render_widget(editor, chunks[EditingField::PlayerContinueOnHit as usize]);
// Select bot type
let bot_type_text = format!("Bot type: {}", self.rules.bot_type.description().name);
let text = Paragraph::new(bot_type_text.as_str()).style(
match self.curr_field == EditingField::BotType {
false => Style::default(),
true => Style::default().fg(HIGHLIGHT_COLOR),
},
);
f.render_widget(
text,
chunks[EditingField::BotType as usize].inner(&Margin {
horizontal: 0,
vertical: 1,
}),
);
// Buttons
let buttons_chunk = Layout::default()
.direction(Direction::Horizontal)

View File

@@ -14,6 +14,16 @@ use crate::ui_screens::utils::centered_rect_size;
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::button_widget::ButtonWidget;
/// Convenience function to ask for user confirmation
pub fn confirm<B: Backend>(terminal: &mut Terminal<B>, msg: &str) -> bool {
matches!(
ConfirmDialogScreen::new(msg)
.show(terminal)
.unwrap_or(ScreenResult::Canceled),
ScreenResult::Ok(true)
)
}
pub struct ConfirmDialogScreen<'a> {
title: &'a str,
msg: &'a str,

View File

@@ -0,0 +1,620 @@
use std::cmp::max;
use std::collections::HashMap;
use std::time::{Duration, Instant};
use crossterm::event;
use crossterm::event::{Event, KeyCode, MouseButton, MouseEventKind};
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout};
use tui::style::Color;
use tui::widgets::Paragraph;
use tui::{Frame, Terminal};
use sea_battle_backend::data::{Coordinates, CurrentGameMapStatus, CurrentGameStatus};
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
use sea_battle_backend::utils::res_utils::Res;
use sea_battle_backend::utils::time_utils::time;
use crate::client::Client;
use crate::constants::*;
use crate::ui_screens::confirm_dialog_screen::confirm;
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::utils::{
centered_rect_size, centered_rect_size_horizontally, centered_text,
};
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::button_widget::ButtonWidget;
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
type CoordinatesMapper = HashMap<Coordinates, Coordinates>;
#[derive(Eq, PartialEq, Ord, PartialOrd)]
enum GameStatus {
Connecting,
WaitingForAnotherPlayer,
OpponentConnected,
WaitingForOpponentBoatsConfig,
OpponentReady,
Starting,
MustFire,
OpponentMustFire,
WonGame,
LostGame,
RematchRequestedByOpponent,
RematchRequestedByPlayer,
RematchAccepted,
RematchRejected,
OpponentLeftGame,
}
impl GameStatus {
pub fn can_show_game_maps(&self) -> bool {
self > &GameStatus::Starting
}
pub fn status_text(&self) -> &str {
match self {
GameStatus::Connecting => "🔌 Connecting...",
GameStatus::WaitingForAnotherPlayer => "🕑 Waiting for another player...",
GameStatus::OpponentConnected => "✅ Opponent connected!",
GameStatus::WaitingForOpponentBoatsConfig => "🕑 Waiting for ### boats configuration",
GameStatus::OpponentReady => "✅ ### is ready!",
GameStatus::Starting => "🕑 Game is starting...",
GameStatus::MustFire => "🚨 You must fire!",
GameStatus::OpponentMustFire => "💣 ### must fire!",
GameStatus::WonGame => "🎉 You win the game!",
GameStatus::LostGame => "😿 ### wins the game. You loose.",
GameStatus::RematchRequestedByOpponent => "❓ Rematch requested by ###",
GameStatus::RematchRequestedByPlayer => "❓ Rematch requested by you",
GameStatus::RematchAccepted => "✅ Rematch accepted!",
GameStatus::RematchRejected => "❌ Rematch rejected!",
GameStatus::OpponentLeftGame => "⛔ Opponent left game!",
}
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
enum Buttons {
RequestRematch,
AcceptRematch,
RejectRematch,
QuitGame,
}
impl Buttons {
pub fn text(&self) -> &str {
match self {
Buttons::RequestRematch => "❓ Request rematch",
Buttons::AcceptRematch => "✅ Accept rematch",
Buttons::RejectRematch => "❌ Reject rematch",
Buttons::QuitGame => "❌ Quit game",
}
}
}
pub struct GameScreen {
client: Client,
invite_code: Option<String>,
status: GameStatus,
opponent_name: Option<String>,
game_last_update: u64,
game: CurrentGameStatus,
curr_shoot_position: Coordinates,
last_opponent_fire_position: Coordinates,
curr_button: usize,
}
impl GameScreen {
pub fn new(client: Client) -> Self {
Self {
client,
invite_code: None,
status: GameStatus::Connecting,
opponent_name: None,
game_last_update: 0,
game: Default::default(),
curr_shoot_position: Coordinates::new(0, 0),
last_opponent_fire_position: Coordinates::invalid(),
curr_button: 0,
}
}
pub async fn show<B: Backend>(mut self, terminal: &mut Terminal<B>) -> Res<ScreenResult> {
let mut last_tick = Instant::now();
let mut coordinates_mapper = CoordinatesMapper::new();
loop {
if !self.visible_buttons().is_empty() {
self.curr_button %= self.visible_buttons().len();
}
// Update UI
terminal.draw(|f| coordinates_mapper = self.ui(f))?;
let timeout = TICK_RATE
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
// Handle terminal events
if event::poll(timeout)? {
let event = event::read()?;
// Keyboard event
if let Event::Key(key) = &event {
let mut new_shoot_pos = self.curr_shoot_position;
match key.code {
// Leave game
KeyCode::Char('q')
if confirm(terminal, "Do you really want to leave game?") =>
{
self.client.close_connection().await;
return Ok(ScreenResult::Canceled);
}
// Move shoot cursor
KeyCode::Left if self.can_fire() => new_shoot_pos = new_shoot_pos.add_x(-1),
KeyCode::Right if self.can_fire() => new_shoot_pos = new_shoot_pos.add_x(1),
KeyCode::Up if self.can_fire() => new_shoot_pos = new_shoot_pos.add_y(-1),
KeyCode::Down if self.can_fire() => new_shoot_pos = new_shoot_pos.add_y(1),
// Shoot
KeyCode::Enter if self.can_fire() => {
if self.game.can_fire_at_location(self.curr_shoot_position) {
self.client
.send_message(&ClientMessage::Fire {
location: self.curr_shoot_position,
})
.await?;
}
}
// Change buttons
KeyCode::Left if self.game_over() => {
self.curr_button += self.visible_buttons().len() - 1
}
KeyCode::Right if self.game_over() => self.curr_button += 1,
KeyCode::Tab if self.game_over() => self.curr_button += 1,
// Submit button
KeyCode::Enter if self.game_over() => match self.curr_button() {
Buttons::RequestRematch => {
self.client
.send_message(&ClientMessage::RequestRematch)
.await?;
self.status = GameStatus::RematchRequestedByPlayer;
}
Buttons::AcceptRematch => {
self.client
.send_message(&ClientMessage::AcceptRematch)
.await?;
self.status = GameStatus::RematchAccepted;
}
Buttons::RejectRematch => {
self.client
.send_message(&ClientMessage::RejectRematch)
.await?;
self.status = GameStatus::RematchRejected;
}
Buttons::QuitGame => {
self.client.close_connection().await;
return Ok(ScreenResult::Ok(()));
}
},
_ => {}
}
if new_shoot_pos.is_valid(&self.game.rules) {
self.curr_shoot_position = new_shoot_pos;
}
}
// Mouse event
if let Event::Mouse(mouse) = event {
if mouse.kind == MouseEventKind::Up(MouseButton::Left) {
if let Some(c) =
coordinates_mapper.get(&Coordinates::new(mouse.column, mouse.row))
{
self.curr_shoot_position = *c;
if self.can_fire()
&& self.game.can_fire_at_location(self.curr_shoot_position)
{
self.client
.send_message(&ClientMessage::Fire {
location: self.curr_shoot_position,
})
.await?;
}
}
}
}
}
// Handle incoming messages
while let Some(msg) = self.client.try_recv_next_message().await? {
match msg {
ServerMessage::SetInviteCode { code } => {
self.status = GameStatus::WaitingForAnotherPlayer;
self.invite_code = Some(code);
}
ServerMessage::InvalidInviteCode => {
PopupScreen::new("❌ Invalid invite code!").show(terminal)?;
return Ok(ScreenResult::Ok(()));
}
ServerMessage::WaitingForAnotherPlayer => {
self.status = GameStatus::WaitingForAnotherPlayer;
}
ServerMessage::OpponentConnected => {
self.status = GameStatus::OpponentConnected;
}
ServerMessage::SetOpponentName { name } => self.opponent_name = Some(name),
ServerMessage::QueryBoatsLayout { rules } => {
match SetBoatsLayoutScreen::new(&rules)
.set_confirm_on_cancel(true)
.show(terminal)?
{
ScreenResult::Ok(layout) => {
self.client
.send_message(&ClientMessage::BoatsLayout { layout })
.await?
}
ScreenResult::Canceled => {
self.client.close_connection().await;
return Ok(ScreenResult::Canceled);
}
};
}
ServerMessage::RejectedBoatsLayout { .. } => {
PopupScreen::new("Server rejected boats layout!! (is your version of SeaBattle up to date?)")
.show(terminal)?;
}
ServerMessage::WaitingForOtherPlayerConfiguration => {
self.status = GameStatus::WaitingForOpponentBoatsConfig;
}
ServerMessage::OpponentReady => {
self.status = GameStatus::OpponentReady;
}
ServerMessage::GameStarting => {
self.status = GameStatus::Starting;
}
ServerMessage::OpponentMustFire { status } => {
self.status = GameStatus::OpponentMustFire;
self.game_last_update = time();
self.game = status;
}
ServerMessage::RequestFire { status } => {
self.status = GameStatus::MustFire;
self.game_last_update = time();
self.game = status;
}
ServerMessage::FireResult { .. } => { /* not used */ }
ServerMessage::OpponentFireResult { pos, .. } => {
self.last_opponent_fire_position = pos;
}
ServerMessage::LostGame { status } => {
self.game_last_update = time();
self.game = status;
self.status = GameStatus::LostGame;
}
ServerMessage::WonGame { status } => {
self.game_last_update = time();
self.game = status;
self.status = GameStatus::WonGame;
}
ServerMessage::OpponentRequestedRematch => {
self.status = GameStatus::RematchRequestedByOpponent;
}
ServerMessage::OpponentAcceptedRematch => {
self.status = GameStatus::RematchAccepted;
}
ServerMessage::OpponentRejectedRematch => {
self.status = GameStatus::RematchRejected;
}
ServerMessage::OpponentLeftGame => {
self.status = GameStatus::OpponentLeftGame;
}
ServerMessage::OpponentReplacedByBot => {
PopupScreen::new("Opponent was replaced by a bot.").show(terminal)?;
}
}
}
if last_tick.elapsed() >= TICK_RATE {
last_tick = Instant::now();
}
}
}
fn can_fire(&self) -> bool {
matches!(self.status, GameStatus::MustFire)
}
fn game_over(&self) -> bool {
self.game.is_game_over()
}
fn visible_buttons(&self) -> Vec<Buttons> {
let mut buttons = vec![];
if self.game_over() && self.status != GameStatus::RematchAccepted {
// Respond to rematch request / quit
if self.status == GameStatus::RematchRequestedByOpponent {
buttons.push(Buttons::AcceptRematch);
buttons.push(Buttons::RejectRematch);
} else if self.status != GameStatus::OpponentLeftGame
&& self.status != GameStatus::RematchRejected
&& self.status != GameStatus::RematchRequestedByPlayer
{
buttons.push(Buttons::RequestRematch);
}
buttons.push(Buttons::QuitGame);
}
buttons
}
fn opponent_name(&self) -> &str {
self.opponent_name.as_deref().unwrap_or("opponent")
}
fn curr_button(&self) -> Buttons {
self.visible_buttons()[self.curr_button]
}
fn player_map(&self, map: &CurrentGameMapStatus, opponent_map: bool) -> GameMapWidget {
let mut map_widget = GameMapWidget::new(&self.game.rules).set_default_empty_char(' ');
// Current shoot position
if opponent_map {
map_widget = map_widget.add_colored_cells(ColoredCells {
color: match (
self.game.can_fire_at_location(self.curr_shoot_position),
self.game
.opponent_map
.successful_strikes
.contains(&self.curr_shoot_position),
) {
(true, _) => Color::Green,
(false, false) => Color::LightYellow,
(false, true) => Color::LightRed,
},
cells: vec![self.curr_shoot_position],
});
} else {
map_widget = map_widget.add_colored_cells(ColoredCells {
color: Color::Green,
cells: vec![self.last_opponent_fire_position],
});
}
// Sunk boats
for b in &map.sunk_boats {
for c in b.all_coordinates() {
map_widget =
map_widget.set_char(c, b.len.to_string().chars().next().unwrap_or('9'));
}
}
let sunk_boats = ColoredCells {
color: Color::LightRed,
cells: map
.sunk_boats
.iter()
.flat_map(|b| b.all_coordinates())
.collect::<Vec<_>>(),
};
// Touched boats
for b in &map.successful_strikes {
map_widget = map_widget.set_char_no_overwrite(*b, 'T');
}
let touched_areas = ColoredCells {
color: Color::Red,
cells: map.successful_strikes.clone(),
};
// Failed strikes
for b in &map.failed_strikes {
map_widget = map_widget.set_char_no_overwrite(*b, '.');
}
let failed_strikes = ColoredCells {
color: Color::Black,
cells: map.failed_strikes.clone(),
};
// Boats
for b in &map.boats.0 {
for c in b.all_coordinates() {
map_widget = map_widget.set_char_no_overwrite(c, 'B');
}
}
let boats = ColoredCells {
color: Color::Blue,
cells: map
.boats
.0
.iter()
.flat_map(|b| b.all_coordinates())
.collect::<Vec<_>>(),
};
map_widget
.add_colored_cells(sunk_boats)
.add_colored_cells(touched_areas)
.add_colored_cells(failed_strikes)
.add_colored_cells(boats)
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) -> CoordinatesMapper {
let mut status_text = self
.status
.status_text()
.replace("###", self.opponent_name());
// If the game is in a state where game maps can not be shown
if !self.status.can_show_game_maps() {
if self.status == GameStatus::WaitingForAnotherPlayer {
if let Some(code) = &self.invite_code {
status_text.push_str(&format!("\n\n🎫 Invite code: {}", code));
}
}
PopupScreen::new(&status_text).show_in_frame(f);
return HashMap::default();
}
// Add timeout (if required)
let mut timeout_str = String::new();
if self.status == GameStatus::MustFire || self.status == GameStatus::OpponentMustFire {
if let Some(remaining) = self.game.remaining_time_for_strike {
let timeout = self.game_last_update + remaining;
if time() < timeout {
timeout_str = format!(" {} seconds left", timeout - time());
}
}
}
// Draw main ui (default play UI)
let player_map = self
.player_map(&self.game.your_map, false)
.set_title("YOUR map");
let mut coordinates_mapper = HashMap::new();
let mut opponent_map = self
.player_map(&self.game.opponent_map, true)
.set_title(self.opponent_name())
.set_yield_func(|c, r| {
for i in 0..r.width {
for j in 0..r.height {
coordinates_mapper.insert(Coordinates::new(r.x + i, r.y + j), c);
}
}
});
if self.can_fire() {
opponent_map = opponent_map
.set_legend("Use arrows + Enter\nor click on the place\nwhere you want\nto shoot");
}
// Prepare buttons
let buttons = self
.visible_buttons()
.iter()
.map(|b| ButtonWidget::new(b.text(), self.curr_button() == *b))
.collect::<Vec<_>>();
// Show both maps if there is enough room on the screen
let player_map_size = player_map.estimated_size();
let opponent_map_size = opponent_map.estimated_size();
let both_maps_width = player_map_size.0 + opponent_map_size.0 + 3;
let show_both_maps = both_maps_width <= f.size().width;
let maps_height = max(player_map_size.1, opponent_map_size.1);
let maps_width = match show_both_maps {
true => both_maps_width,
false => max(player_map_size.0, opponent_map_size.0),
};
let buttons_width = buttons.iter().fold(0, |a, b| a + b.estimated_size().0 + 4);
let max_width = max(maps_width, status_text.len() as u16)
.max(buttons_width)
.max(timeout_str.len() as u16);
let total_height = 3 + 1 + maps_height + 3;
// Check if frame is too small
if max_width > f.size().width || total_height > f.size().height {
show_screen_too_small_popup(f);
return HashMap::default();
}
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(2),
Constraint::Length(2),
Constraint::Length(maps_height),
Constraint::Length(3),
])
.split(centered_rect_size(max_width, total_height, &f.size()));
// Render status
let paragraph = Paragraph::new(status_text.as_str());
f.render_widget(paragraph, centered_text(&status_text, &chunks[0]));
// Render timeout
let paragraph = Paragraph::new(timeout_str.as_str());
f.render_widget(paragraph, centered_text(&timeout_str, &chunks[1]));
// Render maps
if show_both_maps {
let maps_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(chunks[2]);
f.render_widget(
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 {
// Render a single map
if self.can_fire() {
f.render_widget(opponent_map, chunks[2]);
} else {
f.render_widget(player_map, chunks[2]);
drop(opponent_map);
}
}
// Render buttons
if !buttons.is_empty() {
let buttons_area = Layout::default()
.direction(Direction::Horizontal)
.constraints(
(0..buttons.len())
.map(|_| Constraint::Percentage(100 / buttons.len() as u16))
.collect::<Vec<_>>(),
)
.split(chunks[3]);
for (idx, b) in buttons.into_iter().enumerate() {
let target = centered_rect_size(
b.estimated_size().0,
b.estimated_size().1,
&buttons_area[idx],
);
f.render_widget(b, target);
}
}
coordinates_mapper
}
}

View File

@@ -1,3 +1,4 @@
use std::fmt::Display;
use std::io;
use std::time::{Duration, Instant};
use tui::style::*;
@@ -49,6 +50,21 @@ impl<'a> InputScreen<'a> {
self
}
pub fn set_value<D: Display>(mut self, value: D) -> Self {
self.value = value.to_string();
self
}
pub fn set_min_length(mut self, v: usize) -> Self {
self.min_len = v;
self
}
pub fn set_max_length(mut self, v: usize) -> Self {
self.max_len = v;
self
}
/// Get error contained in input
fn error(&self) -> Option<&'static str> {
if self.value.len() > self.max_len {
@@ -109,7 +125,7 @@ impl<'a> InputScreen<'a> {
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
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,
&f.size(),
);

View File

@@ -0,0 +1,30 @@
use std::fmt::Debug;
pub mod configure_game_rules;
pub mod confirm_dialog_screen;
pub mod game_screen;
pub mod input_screen;
pub mod popup_screen;
pub mod select_bot_type_screen;
pub mod select_play_mode_screen;
pub mod set_boats_layout_screen;
pub mod utils;
#[derive(Debug)]
pub enum ScreenResult<E = ()> {
Ok(E),
Canceled,
}
impl<E: Debug> ScreenResult<E> {
pub fn value(self) -> Option<E> {
match self {
ScreenResult::Ok(v) => Some(v),
ScreenResult::Canceled => None,
}
}
pub fn as_string(&self) -> String {
format!("{:#?}", self)
}
}

View File

@@ -14,9 +14,16 @@ use crate::ui_screens::utils::centered_rect_size;
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::button_widget::ButtonWidget;
/// Convenience function to inform user that his terminal window is too small to display the current
/// screen
pub fn show_screen_too_small_popup<B: Backend>(f: &mut Frame<B>) {
PopupScreen::new("🖵 Screen too small!").show_in_frame(f)
}
pub struct PopupScreen<'a> {
title: &'a str,
msg: &'a str,
can_close: bool,
}
impl<'a> PopupScreen<'a> {
@@ -24,9 +31,15 @@ impl<'a> PopupScreen<'a> {
Self {
title: "Message",
msg,
can_close: true,
}
}
pub fn set_can_close(mut self, can_close: bool) -> Self {
self.can_close = can_close;
self
}
pub fn show<B: Backend>(mut self, terminal: &mut Terminal<B>) -> io::Result<ScreenResult<()>> {
let mut last_tick = Instant::now();
loop {
@@ -53,12 +66,33 @@ impl<'a> PopupScreen<'a> {
}
}
/// Show message once message, without polling messages
pub fn show_once<B: Backend>(mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
self.can_close = false;
terminal.draw(|f| self.ui(f))?;
Ok(())
}
/// Show message once message in a frame, without polling messages
pub fn show_in_frame<B: Backend>(mut self, frame: &mut Frame<B>) {
self.can_close = false;
self.ui(frame)
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
// Preprocess message
let lines = textwrap::wrap(self.msg, f.size().width as usize - 20);
let line_max_len = lines.iter().map(|l| l.len()).max().unwrap();
let area = centered_rect_size(line_max_len as u16 + 4, 5 + lines.len() as u16, &f.size());
let area = centered_rect_size(
line_max_len as u16 + 4,
match self.can_close {
// reserve space for button
true => 5,
false => 2,
} + lines.len() as u16,
&f.size(),
);
let block = Block::default().borders(Borders::ALL).title(self.title);
f.render_widget(block, area);
@@ -85,7 +119,9 @@ impl<'a> PopupScreen<'a> {
let paragraph = Paragraph::new(text);
f.render_widget(paragraph, chunks[0]);
if self.can_close {
let ok_button = ButtonWidget::new("OK", true);
f.render_widget(ok_button, chunks[1]);
}
}
}

View File

@@ -27,7 +27,7 @@ impl Default for SelectBotTypeScreen {
Self {
state: Default::default(),
curr_selection: types.len() - 1,
types,
types: types.to_vec(),
}
}
}

View File

@@ -17,26 +17,49 @@ pub enum SelectPlayModeResult {
#[default]
PlayAgainstBot,
PlayRandom,
CreateInvite,
AcceptInvite,
Exit,
}
impl SelectPlayModeResult {
/// Specify whether a selected play mode requires a user name or not
pub fn need_player_name(&self) -> bool {
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)]
struct PlayModeDescription {
name: &'static str,
value: SelectPlayModeResult,
}
const AVAILABLE_PLAY_MODES: [PlayModeDescription; 3] = [
const AVAILABLE_PLAY_MODES: [PlayModeDescription; 5] = [
PlayModeDescription {
name: "Play against bot (offline)",
name: "🤖 Play against bot (offline)",
value: SelectPlayModeResult::PlayAgainstBot,
},
PlayModeDescription {
name: "Play against random player (online)",
name: "🎲 Play against random player (online)",
value: SelectPlayModeResult::PlayRandom,
},
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,
},
];
@@ -85,7 +108,7 @@ impl SelectPlayModeScreen {
}
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
let items = AVAILABLE_PLAY_MODES

View File

@@ -13,6 +13,8 @@ use tui::{Frame, Terminal};
use sea_battle_backend::data::*;
use crate::constants::*;
use crate::ui_screens::confirm_dialog_screen::confirm;
use crate::ui_screens::popup_screen::show_screen_too_small_popup;
use crate::ui_screens::utils::{centered_rect_size, centered_text};
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
@@ -23,6 +25,7 @@ pub struct SetBoatsLayoutScreen<'a> {
curr_boat: usize,
layout: BoatsLayout,
rules: &'a GameRules,
confirm_on_cancel: bool,
}
impl<'a> SetBoatsLayoutScreen<'a> {
@@ -32,9 +35,16 @@ impl<'a> SetBoatsLayoutScreen<'a> {
layout: BoatsLayout::gen_random_for_rules(rules)
.expect("Failed to generate initial boats layout"),
rules,
confirm_on_cancel: false,
}
}
/// Specify whether user should confirm his choice to cancel screen or not
pub fn set_confirm_on_cancel(mut self, confirm: bool) -> Self {
self.confirm_on_cancel = confirm;
self
}
pub fn show<B: Backend>(
mut self,
terminal: &mut Terminal<B>,
@@ -56,7 +66,13 @@ impl<'a> SetBoatsLayoutScreen<'a> {
let event = event::read()?;
if let Event::Key(key) = &event {
match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
KeyCode::Char('q') => {
if !self.confirm_on_cancel
|| confirm(terminal, "Do you really want to quit?")
{
return Ok(ScreenResult::Canceled);
}
}
// Select next boat
KeyCode::Char('n') => self.curr_boat += self.layout.number_of_boats() - 1,
@@ -212,6 +228,14 @@ impl<'a> SetBoatsLayoutScreen<'a> {
}
let (w, h) = game_map_widget.estimated_size();
if f.size().width < w || f.size().height < h + 3 {
// +3 = for errors
show_screen_too_small_popup(f);
drop(game_map_widget);
return coordinates_mapper;
}
let area = centered_rect_size(w, h, &f.size());
f.render_widget(game_map_widget, area);

View File

@@ -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
pub fn centered_text(text: &str, container: &Rect) -> Rect {
if text.len() > container.width as usize {

View File

@@ -34,14 +34,18 @@ impl ButtonWidget {
self.min_width = min_width;
self
}
pub fn estimated_size(&self) -> (u16, u16) {
((self.label.len() + 2).max(self.min_width) as u16, 1)
}
}
impl Widget for ButtonWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let expected_len = (self.label.len() + 2).max(self.min_width);
let expected_len = self.estimated_size().0;
let mut label = self.label.clone();
while label.len() < expected_len {
while label.len() < expected_len as usize {
label.insert(0, ' ');
label.push(' ');
}

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Display;
use tui::buffer::Buffer;
@@ -21,6 +22,7 @@ pub struct GameMapWidget<'a> {
title: Option<String>,
legend: Option<String>,
yield_coordinates: Option<Box<dyn 'a + FnMut(Coordinates, Rect)>>,
chars: HashMap<Coordinates, char>,
}
impl<'a> GameMapWidget<'a> {
@@ -32,6 +34,7 @@ impl<'a> GameMapWidget<'a> {
title: None,
legend: None,
yield_coordinates: None,
chars: Default::default(),
}
}
@@ -63,9 +66,19 @@ impl<'a> GameMapWidget<'a> {
self
}
pub fn set_char(mut self, coordinates: Coordinates, c: char) -> Self {
self.chars.insert(coordinates, c);
self
}
pub fn set_char_no_overwrite(mut self, coordinates: Coordinates, c: char) -> Self {
self.chars.entry(coordinates).or_insert(c);
self
}
pub fn grid_size(&self) -> (u16, u16) {
let w = self.rules.map_width as u16 * 2 + 1;
let h = self.rules.map_height as u16 * 2 + 1;
let w = (self.rules.map_width as u16 * 2) + 2;
let h = (self.rules.map_height as u16 * 2) + 2;
(w, h)
}
@@ -157,9 +170,12 @@ impl<'a> Widget for GameMapWidget<'a> {
}
if x < self.rules.map_width && y < self.rules.map_height {
let cell = buf
.get_mut(o_x + 1, o_y + 1)
.set_char(self.default_empty_character);
let cell = buf.get_mut(o_x + 1, o_y + 1).set_char(
*self
.chars
.get(&coordinates)
.unwrap_or(&self.default_empty_character),
);
if let Some(c) = color {
cell.set_bg(c.color);