routing the rest of implemented features, hits response problem
This commit is contained in:
@@ -49,13 +49,20 @@ impl<'q> Players<'q> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Notify all players of an event
|
/// Notify all players of an event
|
||||||
pub fn notifiy_all<S: Into<String>>(&self, text: S) -> QueryResult<()> {
|
pub fn notifiy_all(&self, text: &str) -> QueryResult<()> {
|
||||||
Err(diesel::result::Error::NotFound)
|
for id in self.all()?
|
||||||
|
.into_iter()
|
||||||
|
.map(|p| p.id)
|
||||||
|
{
|
||||||
|
self.notify(id, text);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Notify a single player of an event
|
/// Notify a single player of an event
|
||||||
pub fn notify<S: Into<String>>(&self, id: i32, text: S) -> QueryResult<()> {
|
pub fn notify(&self, id: i32, text: &str) -> QueryResult<()> {
|
||||||
Err(diesel::result::Error::NotFound)
|
let _ = notification::Notification::add(self.0, id, text)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +70,10 @@ impl<'q> Players<'q> {
|
|||||||
pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32);
|
pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32);
|
||||||
|
|
||||||
impl<'q> AsPlayer<'q> {
|
impl<'q> AsPlayer<'q> {
|
||||||
|
/// Fetch notifications for this player
|
||||||
|
pub fn notifications(&self) -> QueryResult<Vec<String>> {
|
||||||
|
notification::pop_all_for(self.1, self.0)
|
||||||
|
}
|
||||||
/// Updates this player's wealth, returning the difference
|
/// Updates this player's wealth, returning the difference
|
||||||
pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult<Wealth> {
|
pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult<Wealth> {
|
||||||
use crate::schema::players::dsl::*;
|
use crate::schema::players::dsl::*;
|
||||||
|
|||||||
@@ -5,16 +5,25 @@ use crate::{
|
|||||||
models::player::Player,
|
models::player::Player,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// Pops all notification for a player, deleting the database entities
|
||||||
|
pub(super) fn pop_all_for(id: i32, conn: &DbConnection) -> QueryResult<Vec<String>> {
|
||||||
|
let select = notifications::table.filter(notifications::dsl::player_id.eq(id));
|
||||||
|
let popped = select.load(conn)?;
|
||||||
|
diesel::delete(select).execute(conn)?;
|
||||||
|
Ok(popped.into_iter().map(|n: Notification| n.text).collect())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
|
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
|
||||||
#[belongs_to(Player)]
|
#[belongs_to(Player)]
|
||||||
struct Notification {
|
pub(super) struct Notification {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub player_id: i32,
|
pub player_id: i32,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Notification {
|
impl Notification {
|
||||||
fn add<'a, S: Into<&'a str>>(conn: &DbConnection, id: i32, text: S) -> QueryResult<Notification> {
|
pub(super) fn add<'a, S: Into<&'a str>>(conn: &DbConnection, id: i32, text: S) -> QueryResult<Notification> {
|
||||||
diesel::insert_into(notifications::table)
|
diesel::insert_into(notifications::table)
|
||||||
.values(&NewNotification {
|
.values(&NewNotification {
|
||||||
player_id: id,
|
player_id: id,
|
||||||
|
|||||||
@@ -161,7 +161,10 @@ export default {
|
|||||||
this.activeView = viewId;
|
this.activeView = viewId;
|
||||||
},
|
},
|
||||||
addNewLoot () {
|
addNewLoot () {
|
||||||
api.fetch("admin/add-loot", "POST", this.pending_loot)
|
api.fetch("players/0/loot", "POST", {
|
||||||
|
source_name: "Dummy name",
|
||||||
|
items: this.pending_loot,
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.pending_loot = []
|
this.pending_loot = []
|
||||||
this.switchView('group');
|
this.switchView('group');
|
||||||
|
|||||||
13
src/api.rs
13
src/api.rs
@@ -19,6 +19,7 @@ pub enum Value {
|
|||||||
ItemList(Vec<db::Item>),
|
ItemList(Vec<db::Item>),
|
||||||
ClaimList(Vec<db::Claim>),
|
ClaimList(Vec<db::Claim>),
|
||||||
PlayerList(Vec<db::Player>),
|
PlayerList(Vec<db::Player>),
|
||||||
|
Notifications(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::Serialize for Value {
|
impl serde::Serialize for Value {
|
||||||
@@ -30,6 +31,7 @@ impl serde::Serialize for Value {
|
|||||||
Value::ItemList(v) => v.serialize(serializer),
|
Value::ItemList(v) => v.serialize(serializer),
|
||||||
Value::ClaimList(v) => v.serialize(serializer),
|
Value::ClaimList(v) => v.serialize(serializer),
|
||||||
Value::PlayerList(v) => v.serialize(serializer),
|
Value::PlayerList(v) => v.serialize(serializer),
|
||||||
|
Value::Notifications(v) => v.serialize(serializer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,6 +87,7 @@ pub enum ApiActions {
|
|||||||
FetchClaims,
|
FetchClaims,
|
||||||
// Player actions
|
// Player actions
|
||||||
FetchPlayer(i32),
|
FetchPlayer(i32),
|
||||||
|
FetchNotifications(i32),
|
||||||
FetchLoot(i32),
|
FetchLoot(i32),
|
||||||
UpdateWealth(i32, f64),
|
UpdateWealth(i32, f64),
|
||||||
BuyItems(i32, Vec<(i32, Option<f64>)>),
|
BuyItems(i32, Vec<(i32, Option<f64>)>),
|
||||||
@@ -120,6 +123,9 @@ pub fn execute(
|
|||||||
ApiActions::FetchPlayer(id) => {
|
ApiActions::FetchPlayer(id) => {
|
||||||
response.set_value(Value::Player(db::Players(conn).find(id)?));
|
response.set_value(Value::Player(db::Players(conn).find(id)?));
|
||||||
}
|
}
|
||||||
|
ApiActions::FetchNotifications(id) => {
|
||||||
|
response.set_value(Value::Notifications(db::AsPlayer(conn, id).notifications()?));
|
||||||
|
}
|
||||||
ApiActions::FetchLoot(id) => {
|
ApiActions::FetchLoot(id) => {
|
||||||
response.set_value(Value::ItemList(db::LootManager(conn, id).all()?));
|
response.set_value(Value::ItemList(db::LootManager(conn, id).all()?));
|
||||||
}
|
}
|
||||||
@@ -190,7 +196,12 @@ pub fn execute(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
response.notify(format!("{} objets lootés !", added_items));
|
response.notify(format!("{} objets lootés !", added_items));
|
||||||
// TODO: notify players throught persistent notifications
|
// Notify players through persistent notifications
|
||||||
|
if let Err(e) = db::Players(conn)
|
||||||
|
.notifiy_all("De nouveaux objets ont été ajoutés au coffre de groupe !")
|
||||||
|
{
|
||||||
|
response.push_error(format!("Erreur durant la notification : {:?}", e));
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer};
|
use actix_web::{web, middleware, App, Error, HttpResponse, HttpServer};
|
||||||
use futures::Future;
|
use futures::{Future, IntoFuture};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use lootalot_db as db;
|
use lootalot_db as db;
|
||||||
@@ -12,6 +12,12 @@ type PlayerId = web::Path<i32>;
|
|||||||
type ItemId = web::Json<i32>;
|
type ItemId = web::Json<i32>;
|
||||||
type ItemListWithMods = web::Json<Vec<(i32, Option<f64>)>>;
|
type ItemListWithMods = web::Json<Vec<(i32, Option<f64>)>>;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct NewGroupLoot {
|
||||||
|
source_name: String,
|
||||||
|
items: Vec<db::Item>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Wraps call to the database query and convert its result as a async HttpResponse
|
/// Wraps call to the database query and convert its result as a async HttpResponse
|
||||||
pub fn db_call(
|
pub fn db_call(
|
||||||
pool: AppPool,
|
pool: AppPool,
|
||||||
@@ -44,6 +50,9 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
|||||||
.route("/", web::get().to_async(|pool, player: PlayerId| {
|
.route("/", web::get().to_async(|pool, player: PlayerId| {
|
||||||
db_call(pool, Q::FetchPlayer(*player))
|
db_call(pool, Q::FetchPlayer(*player))
|
||||||
}))
|
}))
|
||||||
|
.route("/notifications", web::get().to_async(|pool, player: PlayerId| {
|
||||||
|
db_call(pool, Q::FetchNotifications(*player))
|
||||||
|
}))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/claims")
|
web::resource("/claims")
|
||||||
//.route(web::get().to_async(endpoints::player_claims))
|
//.route(web::get().to_async(endpoints::player_claims))
|
||||||
@@ -83,6 +92,14 @@ fn configure_app(config: &mut web::ServiceConfig) {
|
|||||||
db_call(pool, Q::BuyItems(*player, data.into_inner()))
|
db_call(pool, Q::BuyItems(*player, data.into_inner()))
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
|
.route(web::post().to_async(
|
||||||
|
move |pool, (player, data): (PlayerId, web::Json<NewGroupLoot>)| {
|
||||||
|
match *player {
|
||||||
|
0 => db_call(pool, Q::AddLoot(data.items.clone())),
|
||||||
|
_ => HttpResponse::Forbidden().finish().into_future(),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
))
|
||||||
.route(web::delete().to_async(
|
.route(web::delete().to_async(
|
||||||
move |pool, (player, data): (PlayerId, ItemListWithMods)| {
|
move |pool, (player, data): (PlayerId, ItemListWithMods)| {
|
||||||
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
db_call(pool, Q::SellItems(*player, data.into_inner()))
|
||||||
|
|||||||
Reference in New Issue
Block a user