small fixes, cleans up API

This commit is contained in:
2019-08-05 14:41:36 +02:00
parent 3b39428e76
commit 894f5f8200
5 changed files with 86 additions and 57 deletions

View File

@@ -2,7 +2,7 @@
<PlayerView :id="state.player_id" <PlayerView :id="state.player_id"
v-slot="{ player, loot, notifications, actions }"> v-slot="{ player, loot, notifications, actions }">
<main id="app" class="container"> <main id="app" class="container">
<header class="section"> <header style="padding-bottom: 1.5em;">
<HeaderBar :app_state="state"> <HeaderBar :app_state="state">
<template v-slot:title>{{ player.name }}</template> <template v-slot:title>{{ player.name }}</template>
<template v-slot:links> <template v-slot:links>
@@ -21,12 +21,12 @@
{{ p.name }}</a> {{ p.name }}</a>
</template> </template>
</HeaderBar> </HeaderBar>
<p v-show="notifications.length > 0">{{ notifications }}</p>
<Wealth <Wealth
:wealth="[player.cp, player.sp, player.gp, player.pp]" :wealth="[player.cp, player.sp, player.gp, player.pp]"
:debt="player.debt" :debt="player.debt"
@update="actions.updateWealth"> @update="actions.updateWealth">
</wealth> </Wealth>
<p v-show="notifications.length > 0">{{ notifications }}</p>
</header> </header>
<nav> <nav>
<div class="tabs is-centered is-boxed is-medium"> <div class="tabs is-centered is-boxed is-medium">
@@ -44,7 +44,7 @@
</ul> </ul>
</div> </div>
</nav> </nav>
<main class=""> <main class="section">
<template v-if="isAdding"> <template v-if="isAdding">
<h2 v-show="playerIsGroup">ItemInput</h2> <h2 v-show="playerIsGroup">ItemInput</h2>
<AddingChest <AddingChest

View File

@@ -6,8 +6,19 @@ const API_ENDPOINT = function (tailString) {
} }
export const Api = { export const Api = {
__doFetch (endpoint, method, payload) {
return fetch(API_ENDPOINT(endpoint),
{
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
})
.then(r => r.json())
},
fetchPlayerList () { fetchPlayerList () {
return fetch(API_ENDPOINT("players")) return fetch(API_ENDPOINT("players/all"))
.then(r => r.json()) .then(r => r.json())
}, },
fetchClaims () { fetchClaims () {
@@ -15,36 +26,20 @@ export const Api = {
.then(r => r.json()) .then(r => r.json())
}, },
fetchLoot (playerId) { fetchLoot (playerId) {
return fetch(API_ENDPOINT(playerId + "/loot")) return fetch(API_ENDPOINT("players/loot/" + playerId))
.then(r => r.json()) .then(r => r.json())
}, },
putClaim (player_id, item_id) { putClaim (player_id, item_id) {
const payload = { player_id, item_id }; const payload = { player_id, item_id };
return fetch(API_ENDPOINT("claims"), return this.__doFetch("claims", 'PUT', payload);
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
})
.then(r => r.json())
}, },
unClaim (player_id, item_id) { unClaim (player_id, item_id) {
const payload = { player_id, item_id }; const payload = { player_id, item_id };
return fetch(API_ENDPOINT("claims"), return this.__doFetch("claims", 'DELETE', payload);
{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
}, },
body: JSON.stringify(payload) updateWealth (player_id, value_in_gp) {
}) const payload = { player_id, value_in_gp: Number(value_in_gp) };
.then(r => r.json()) return this.__doFetch("players/update-wealth", 'PUT', payload);
},
updateWealth (playerId, goldValue) {
return fetch(API_ENDPOINT(playerId + "/update-wealth/" + goldValue))
.then(r => r.json())
} }
}; };

View File

@@ -2,17 +2,18 @@
<nav class="navbar is-info"> <nav class="navbar is-info">
<div class="navbar-brand"> <div class="navbar-brand">
<p class="navbar-item is-size-4"><slot name="title">...</slot></p> <p class="navbar-item is-size-4"><slot name="title">...</slot></p>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false"> <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false"
@click="switchMobileVisibility">
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
</a> </a>
</div> </div>
<div id="menu" class="navbar-menu"> <div id="menu" class="navbar-menu" :class="{'is-active': showOnMobile }">
<div class="navbar-end"> <div class="navbar-end">
<div class="navbar-item has-dropdown is-hoverable"> <div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">Autres</a> <a class="navbar-link">Autres</a>
<div class="navbar-dropdown is-right"> <div class="navbar-dropdown is-right" @click="switchMobileVisibility">
<slot name="links"> <slot name="links">
<a class="navbar-item">History of Loot</a> <a class="navbar-item">History of Loot</a>
</slot> </slot>
@@ -23,3 +24,13 @@
</nav> </nav>
</template> </template>
<script>
export default {
data () { return { showOnMobile: false } },
methods: {
switchMobileVisibility () {
this.showOnMobile = !this.showOnMobile
}
}
}
</script>

View File

@@ -2,11 +2,11 @@
<section class="level is-mobile"> <section class="level is-mobile">
<div class="level-left"> <div class="level-left">
<div class="level-item"> <div class="level-item">
<span class="icon is-large" @click="edit = !edit"> <span class="icon is-large" @click="editing = !editing">
<i class="fas fa-2x fa-piggy-bank"></i> <i class="fas fa-2x fa-piggy-bank"></i>
</span> </span>
</div> </div>
<template v-if="edit"> <template v-if="editing">
<div class="level-item"> <div class="level-item">
<div class="field has-addons"> <div class="field has-addons">
<p class="control"> <p class="control">
@@ -56,7 +56,7 @@
props: ["wealth", "debt"], props: ["wealth", "debt"],
data () { data () {
return { return {
edit: false, editing: false,
edit_value: 0, edit_value: 0,
}; };
}, },
@@ -66,9 +66,13 @@
this.resetValues(); this.resetValues();
}, },
resetValues () { resetValues () {
this.edit = false; this.editing = false;
this.edit_value = 0; this.edit_value = 0;
} }
} }
} }
</script> </script>
<style scoped>
.input { max-width: 9em; }
</style>

View File

@@ -55,6 +55,18 @@ struct PlayerClaim {
item_id: i32, item_id: i32,
} }
#[derive(Serialize, Deserialize, Debug)]
struct WealthUpdate {
player_id: i32,
value_in_gp: f32,
}
#[derive(Serialize, Deserialize, Debug)]
struct NewPlayer {
name: String,
wealth: f32,
}
pub(crate) fn serve() -> std::io::Result<()> { pub(crate) fn serve() -> std::io::Result<()> {
let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set"); let www_root: String = env::var("WWW_ROOT").expect("WWW_ROOT must be set");
dbg!(&www_root); dbg!(&www_root);
@@ -71,12 +83,30 @@ pub(crate) fn serve() -> std::io::Result<()> {
) )
.service( .service(
web::scope("/api") web::scope("/api")
.service(
web::scope("/players")
.route( .route(
"/players", "/all",
web::get().to_async(move |pool: AppPool| { web::get().to_async(move |pool: AppPool| {
db_call(pool, move |api| api.fetch_players()) db_call(pool, move |api| api
.fetch_players())
}), }),
) )
.route(
"/loot/{player_id}",
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
db_call(pool, move |api| api.as_player(*player_id).loot())
}),
)
.route(
"/update-wealth",
web::put().to_async(move |pool: AppPool, data: web::Json<WealthUpdate>| {
db_call(pool, move |api| api
.as_player(data.player_id)
.update_wealth(data.value_in_gp))
})
)
)
.service( .service(
web::resource("/claims") web::resource("/claims")
.route(web::get() .route(web::get()
@@ -97,18 +127,6 @@ pub(crate) fn serve() -> std::io::Result<()> {
.unclaim(data.item_id)) .unclaim(data.item_id))
})) }))
) )
.route(
"/{player_id}/update-wealth/{amount}",
web::get().to_async(move |pool: AppPool, data: web::Path<(i32, f32)>| {
db_call(pool, move |api| api.as_player(data.0).update_wealth(data.1))
}),
)
.route(
"/{player_id}/loot",
web::get().to_async(move |pool: AppPool, player_id: web::Path<i32>| {
db_call(pool, move |api| api.as_player(*player_id).loot())
}),
)
.service(web::scope("/admin") .service(web::scope("/admin")
.route( .route(
"/resolve-claims", "/resolve-claims",
@@ -117,12 +135,13 @@ pub(crate) fn serve() -> std::io::Result<()> {
}), }),
) )
.route( .route(
"/add-player/{name}/{wealth}", "/add-player",
web::get().to_async( web::get().to_async(
move |pool: AppPool, data: web::Path<(String, f32)>| { move |pool: AppPool, data: web::Json<NewPlayer>| {
db_call(pool, move |api| { db_call(pool, move |api| api
api.as_admin().add_player(&data.0, data.1) .as_admin()
}) .add_player(&data.name, data.wealth)
)
}, },
), ),
) )