Skip to main content

hvz/endpoints/
zombie.rs

1use actix_web::{HttpRequest, HttpResponse, Responder, get, post, web};
2use serde::{Deserialize, Serialize};
3
4use crate::{player::Player, token::Token};
5
6/// Struct representing a hunt
7#[derive(Serialize, Deserialize)]
8pub struct HuntStruct {
9    pub target: String,
10    pub feed1: Option<String>,
11    pub feed2: Option<String>,
12}
13
14/// **GET /zombie/next_feeding/{url_id}** - Gets the next feeding time for a zombie
15#[get("/zombie/next_feeding/{url_id}")]
16pub async fn next_feeding(url_id: web::Path<String>, req: HttpRequest) -> impl Responder {
17    if let Err(e) = Token::check_token(&req, &url_id).await {
18        return HttpResponse::Forbidden().body(e);
19    }
20
21    let zombie = match Player::get_player(url_id.into_inner()).await {
22        Ok(Some(player)) => {
23            if let Err(err) = player.ensure_zombie().await {
24                return HttpResponse::Unauthorized().body(err);
25            };
26            player
27        }
28        Ok(None) => return HttpResponse::NotFound().body("Player not found"),
29        Err(err) => return HttpResponse::InternalServerError().body(err),
30    };
31
32    if let Some(time) = zombie.next_feeding {
33        HttpResponse::Ok().body(format!("{:?}", time.to_string()))
34    } else {
35        HttpResponse::Ok().body("This player doesn't have a starvation date.")
36    }
37}
38
39/// **GET /zombie/starvation/{url_id}** - Gets the starvation status of the zombies
40#[get("/zombie/starvation/{url_id}")]
41pub async fn starvation(url_id: web::Path<String>, req: HttpRequest) -> impl Responder {
42    if let Err(e) = Token::check_token(&req, &url_id).await {
43        return HttpResponse::Forbidden().body(e);
44    }
45
46    let zombie = match Player::get_player(url_id.into_inner()).await {
47        Ok(Some(player)) => {
48            if let Err(err) = player.ensure_zombie().await {
49                return HttpResponse::Unauthorized().body(err);
50            };
51            player
52        }
53        Ok(None) => return HttpResponse::NotFound().body("Player not found"),
54        Err(err) => return HttpResponse::InternalServerError().body(err),
55    };
56
57    let result = zombie.starvation().await;
58
59    match result {
60        Ok(res) => HttpResponse::Ok().json(res),
61        Err(e) => HttpResponse::InternalServerError().body(e),
62    }
63}
64
65/// **POST /zombie/hunt/{url_id}** - Allows a zombie to hunt another player
66#[post("/zombie/hunt/{url_id}")]
67pub async fn hunt(
68    url_id: web::Path<String>,
69    req: HttpRequest,
70    form: web::Json<HuntStruct>,
71) -> impl Responder {
72    if let Err(e) = Token::check_token(&req, &url_id).await {
73        return HttpResponse::Forbidden().body(e);
74    }
75
76    let zombie = match Player::get_player(url_id.into_inner()).await {
77        Ok(Some(player)) => {
78            if let Err(err) = player.ensure_zombie().await {
79                return HttpResponse::Unauthorized().body(err);
80            };
81            player
82        }
83        Ok(None) => return HttpResponse::NotFound().body("Player not found\n"),
84        Err(err) => return HttpResponse::InternalServerError().body(err),
85    };
86
87    let human = match Player::get_player(form.target.clone()).await {
88        Ok(Some(player)) => {
89            if let Err(_) = player.ensure_human().await {
90                return HttpResponse::Forbidden().body("Cannot hunt a non-human player");
91            };
92            player
93        }
94        Ok(None) => return HttpResponse::NotFound().body("Player not found\n"),
95        Err(err) => return HttpResponse::InternalServerError().body(err),
96    };
97
98    let feed1 = if let Some(feed1) = form.feed1.clone() {
99        match Player::get_player_by_alias(feed1).await {
100            Ok(p) => p,
101            Err(_) => None,
102        }
103    } else {
104        None
105    };
106
107    let feed2 = if let Some(feed2) = form.feed2.clone() {
108        match Player::get_player_by_alias(feed2).await {
109            Ok(p) => p,
110            Err(_) => None,
111        }
112    } else {
113        None
114    };
115
116    let result = zombie.hunt(&human, feed1.as_ref(), feed2.as_ref()).await;
117
118    match result {
119        Ok(_) => HttpResponse::Ok().body("Player hunted\n"),
120        Err(e) => HttpResponse::InternalServerError().body(e),
121    }
122}