aboutsummaryrefslogtreecommitdiff
path: root/src/game/server.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-06-04 20:48:43 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-04 20:48:43 +0200
commit4ebe819106d82459def54561cf8dc71ec22ba6e4 (patch)
tree991046e078bf9de21cc392343401ef1d46a8962c /src/game/server.rs
parente49a85505701740b195a03892e1fc5cf8d6382a2 (diff)
downloadgpn-tron-rust-4ebe819106d82459def54561cf8dc71ec22ba6e4.tar
gpn-tron-rust-4ebe819106d82459def54561cf8dc71ec22ba6e4.tar.bz2
gpn-tron-rust-4ebe819106d82459def54561cf8dc71ec22ba6e4.tar.zst
save creds
Diffstat (limited to 'src/game/server.rs')
-rw-r--r--src/game/server.rs41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/game/server.rs b/src/game/server.rs
index f5de1bf..0e983b9 100644
--- a/src/game/server.rs
+++ b/src/game/server.rs
@@ -1,5 +1,5 @@
use super::{protocol::Packet, Config, Game};
-use crate::State;
+use crate::{database::DatabaseExt, State};
use anyhow::{anyhow, bail, Result};
use log::{debug, error, info};
use std::{ops::ControlFlow, sync::Arc, time::Duration};
@@ -29,7 +29,7 @@ pub async fn game_server(config: Config, state: Arc<State>) -> Result<()> {
}
async fn game_loop(config: Config, state: Arc<State>) {
- let mut speed = config.tickrate;
+ let mut speed = config.tickrate;
loop {
sleep(Duration::from_secs_f32(1. / speed)).await;
@@ -43,9 +43,18 @@ async fn game_loop(config: Config, state: Arc<State>) {
}
ControlFlow::Break(winner) => {
info!("winner: {winner:?}");
+ if let Some(winner) = winner {
+ if let Some(winner) = state.players.write().await.get(&winner).cloned() {
+ let mut h = state.win_history.write().await;
+ h.push_front(winner);
+ while h.len() > 64 {
+ h.pop_back();
+ }
+ }
+ }
let p = state.players.read().await;
*g = Game::new(p.clone().into_iter().collect());
- speed = config.tickrate;
+ speed = config.tickrate;
let _ = state.tick.send(true);
}
}
@@ -81,7 +90,7 @@ async fn handle_client_inner(
let mut lines = rx.lines();
let mut ticks = state.tick.subscribe();
let mut chat = state.chat.subscribe();
- tx.send_packet(Packet::Motd("This is the GPN-Tron Rust rewrite. The protocol should be compatible with the original: https://github.com/freehuntx/gpn-tron/blob/master/PROTOCOL.md".to_string())).await?;
+ tx.send_packet(Packet::Motd("This is the GPN-Tron Rust rewrite. It should be compatible with the original protocol: https://github.com/freehuntx/gpn-tron/blob/master/PROTOCOL.md".to_string())).await?;
loop {
tokio::select! {
message = chat.recv() => {
@@ -127,6 +136,9 @@ async fn handle_tick(
}
cstate.alive = true;
}
+ if !cstate.alive {
+ return Ok(());
+ }
{
let g = state.game.read().await;
if new_game {
@@ -153,9 +165,11 @@ async fn handle_tick(
events.push(Packet::Die(g.dead.clone()));
}
if g.dead.contains(&pid) {
+ cstate.alive = false;
events.push(Packet::Lose(0, 0)); // TODO implement stats
+ } else {
+ events.push(Packet::Tick);
}
- events.push(Packet::Tick);
}
for e in events {
tx.send_packet(e).await?;
@@ -179,10 +193,19 @@ async fn handle_packet(
};
debug!("<- {packet:?}");
match packet {
- Packet::Join {
- username,
- password: _,
- } => {
+ Packet::Join { username, password } => {
+ if username.len() > 64 || password.len() > 64 {
+ tx.send_packet(Packet::Error(
+ "password or username too long (> 64)".to_string(),
+ ))
+ .await?;
+ return Ok(());
+ }
+ if !state.db.check_or_insert_creds(&username, &password)? {
+ tx.send_packet(Packet::Error("incorrect password".to_string()))
+ .await?;
+ return Ok(());
+ }
if cstate.pid.is_some() {
tx.send_packet(Packet::Error("already joined".to_string()))
.await?