makes adding loot working
This commit is contained in:
@@ -138,7 +138,7 @@ impl<'q> AsPlayer<'q> {
|
||||
if let Ok((item, diff)) = self.conn.transaction(|| {
|
||||
use schema::looted::dsl::*;
|
||||
let item = schema::items::table.find(item_id).first::<models::Item>(self.conn)?;
|
||||
let new_item = models::item::NewLoot::to_player(self.id, (&item.name, item.base_price));
|
||||
let new_item = models::item::NewLoot::to_player(self.id, &item);
|
||||
diesel::insert_into(schema::looted::table)
|
||||
.values(&new_item)
|
||||
.execute(self.conn)?;
|
||||
@@ -285,8 +285,8 @@ impl<'q> AsAdmin<'q> {
|
||||
///
|
||||
/// # Params
|
||||
/// List of (name, base_price) values for the new items
|
||||
pub fn add_loot(self, items: Vec<(&str, i32)>) -> ActionResult<()> {
|
||||
for item_desc in items.into_iter() {
|
||||
pub fn add_loot(self, items: Vec<models::item::Item>) -> ActionResult<()> {
|
||||
for item_desc in items.iter() {
|
||||
let new_item = models::item::NewLoot::to_group(item_desc);
|
||||
diesel::insert_into(schema::looted::table)
|
||||
.values(&new_item)
|
||||
|
||||
@@ -12,7 +12,7 @@ type OwnedBy = Select<OwnedLoot, ItemColumns>;
|
||||
/// It is also used as a public representation of Loot, since owner
|
||||
/// information is implicit.
|
||||
/// Or maybe this is a little too confusing ??
|
||||
#[derive(Debug, Queryable, Serialize)]
|
||||
#[derive(Debug, Queryable, Serialize, Deserialize, Clone)]
|
||||
pub struct Item {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
@@ -54,9 +54,6 @@ impl Loot {
|
||||
}
|
||||
}
|
||||
|
||||
/// Description of an item : (name, value in gold)
|
||||
pub type ItemDesc<'a> = (&'a str, i32);
|
||||
|
||||
/// An item being looted or bought.
|
||||
///
|
||||
/// The owner is set to 0 in case of looting,
|
||||
@@ -71,19 +68,19 @@ pub(crate) struct NewLoot<'a> {
|
||||
|
||||
impl<'a> NewLoot<'a> {
|
||||
/// A new loot going to the group (loot procedure)
|
||||
pub(crate) fn to_group(desc: ItemDesc<'a>) -> Self {
|
||||
pub(crate) fn to_group(desc: &'a Item) -> Self {
|
||||
Self {
|
||||
name: desc.0,
|
||||
base_price: desc.1,
|
||||
name: &desc.name,
|
||||
base_price: desc.base_price,
|
||||
owner_id: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// A new loot going to a specific player (buy procedure)
|
||||
pub(crate) fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
|
||||
pub(crate) fn to_player(player: i32, desc: &'a Item) -> Self {
|
||||
Self {
|
||||
name: desc.0,
|
||||
base_price: desc.1,
|
||||
name: &desc.name,
|
||||
base_price: desc.base_price,
|
||||
owner_id: player,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@ pub(super) mod item;
|
||||
pub(super) mod player;
|
||||
|
||||
pub use claim::Claim;
|
||||
pub use item::Item;
|
||||
pub use item::{Item};
|
||||
pub(crate) use item::Loot;
|
||||
pub use player::{Player, Wealth};
|
||||
|
||||
@@ -56,13 +56,11 @@
|
||||
</nav>
|
||||
<main class="section">
|
||||
<template v-if="isAdding">
|
||||
<div v-if="playerIsGroup" class="box">
|
||||
<ItemInput v-if="playerIsGroup"
|
||||
:source="state.inventory"
|
||||
<Loot v-if="playerIsGroup"
|
||||
:inventory="state.inventory"
|
||||
@addItem="item => pending_loot.push(item)"
|
||||
></ItemInput>
|
||||
<button>Envoyer</button>
|
||||
</div>
|
||||
@confirmAction="addNewLoot"
|
||||
></Loot>
|
||||
<AddingChest
|
||||
:items="playerIsGroup ? pending_loot : state.inventory"
|
||||
:perms="playerIsGroup ? {} : { canBuy: true }"
|
||||
@@ -89,8 +87,8 @@ import PlayerView from './components/PlayerView.js'
|
||||
import HeaderBar from './components/HeaderBar.vue'
|
||||
import Wealth from './components/Wealth.vue'
|
||||
import Chest from './components/Chest.vue'
|
||||
import ItemInput from './components/ItemInput.vue'
|
||||
import { AppStorage } from './AppStorage'
|
||||
import Loot from './components/Loot.vue'
|
||||
import { Api, AppStorage } from './AppStorage'
|
||||
|
||||
function getCookie(cname) {
|
||||
var name = cname + "=";
|
||||
@@ -124,7 +122,7 @@ export default {
|
||||
'AddingChest': Chest, // Alias to prevent component re-use
|
||||
Chest,
|
||||
Wealth,
|
||||
ItemInput,
|
||||
Loot,
|
||||
},
|
||||
created () {
|
||||
// Initiate with active player set to value found in cookie
|
||||
@@ -149,7 +147,16 @@ export default {
|
||||
}
|
||||
this.activeView = viewId;
|
||||
},
|
||||
switchPlayerChestVisibility () { AppStorage.switchPlayerChestVisibility(); },
|
||||
switchPlayerChestVisibility () {
|
||||
AppStorage.switchPlayerChestVisibility();
|
||||
},
|
||||
addNewLoot () {
|
||||
Api.newLoot(this.pending_loot)
|
||||
.then(_ => {
|
||||
this.pending_loot = []
|
||||
this.switchView('group');
|
||||
});
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
showPlayerChest () { return this.activeView == 'player' },
|
||||
|
||||
@@ -52,7 +52,9 @@ export const Api = {
|
||||
sellItems (player_id, items) {
|
||||
const payload = { player_id, items };
|
||||
return this.__doFetch("players/sell", 'POST', payload);
|
||||
|
||||
},
|
||||
newLoot (items) {
|
||||
return this.__doFetch("admin/add-loot", 'POST', items);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -165,6 +167,9 @@ export const AppStorage = {
|
||||
if (this.debug) console.log("cancel a non-existent request")
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
addNewLoot (items) {
|
||||
return Api.newLoot(items);
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
<template>
|
||||
<div class="container is-paddingless">
|
||||
<p class="heading">Ajouter un objet</p>
|
||||
<div class="field is-horizontal">
|
||||
<div class="field-label">
|
||||
<label class="label">Nouvel objet</label>
|
||||
</div>
|
||||
<div class="field-body">
|
||||
<div class="field">
|
||||
<label for="item_name" class="label">Nom</label>
|
||||
<div class="control is-expanded" :class="{'is-loading': is_loading }">
|
||||
<div class="control is-expanded">
|
||||
<input type="text"
|
||||
id="item_name"
|
||||
placeholder="Nom de l'objet"
|
||||
v-model="item.name"
|
||||
@input="autoCompletion"
|
||||
class="input"
|
||||
autocomplete="on"
|
||||
></input>
|
||||
>
|
||||
</div>
|
||||
<div class="dropdown" :class="{'is-active': showCompletionFrame}">
|
||||
<div class="dropdown-menu">
|
||||
@@ -24,21 +26,26 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<div class="field is-expanded has-addons" v-show="item.name != ''">
|
||||
<p class="control"><a class="button is-static">PO</a></p>
|
||||
<p class="control">
|
||||
<input type="text"
|
||||
placeholder="Prix"
|
||||
class="input"
|
||||
:class="{'is-danger': item.base_price == ''}"
|
||||
v-model.number="item.base_price"
|
||||
></input>
|
||||
>
|
||||
</p>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-primary"
|
||||
@click="addItem"
|
||||
>+</button>
|
||||
:disabled="!isItemValid"
|
||||
>Ajouter</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -49,6 +56,7 @@
|
||||
return {
|
||||
is_loading: false,
|
||||
item: {
|
||||
id: 0,
|
||||
name: '',
|
||||
base_price: '',
|
||||
},
|
||||
@@ -59,7 +67,6 @@
|
||||
autoCompletion () {
|
||||
// Unset any previous value on input (for every field except item's name)
|
||||
this.item.base_price = '';
|
||||
|
||||
if (this.item.name == '') {
|
||||
this.results = [];
|
||||
} else {
|
||||
@@ -69,13 +76,13 @@
|
||||
}
|
||||
},
|
||||
setResult(result) {
|
||||
this.item.id = result.id;
|
||||
this.item.name = result.name;
|
||||
this.item.base_price = result.base_price;
|
||||
// Clear results to close completionFrame
|
||||
this.results = [];
|
||||
},
|
||||
addItem () {
|
||||
// TODO: check item is valid
|
||||
this.$emit("addItem", this.item);
|
||||
this.item = {
|
||||
name: '',
|
||||
@@ -86,11 +93,15 @@
|
||||
},
|
||||
computed: {
|
||||
showCompletionFrame () { return this.results.length > 0 },
|
||||
isItemValid () { return this.item.name != '' && this.item.base_price != '' },
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.dropdown, .dropdown-menu { min-width: 100%; margin-top: 0; padding-top: 0;}
|
||||
/*.dropdown { top: -0.75rem; }*/
|
||||
.dropdown, .dropdown-menu {
|
||||
min-width: 100%;
|
||||
margin-top: 0;
|
||||
padding-top: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,37 +1,33 @@
|
||||
<template>
|
||||
<div>
|
||||
<p class="heading has-text-left is-size-5">
|
||||
Nouveau loot - {{ looted.length }} objet(s)</p>
|
||||
<ItemInput @addItem="onAddItem" :source="inventory"></ItemInput>
|
||||
<p v-for="(item, idx) in looted" :key="idx"
|
||||
class="has-text-left is-size-5">
|
||||
{{ item.name }} ({{ item.sell_value }}po)
|
||||
</p>
|
||||
<div class="box">
|
||||
<ItemInput
|
||||
@addItem="onAddItem"
|
||||
:source="inventory"
|
||||
></ItemInput>
|
||||
<div class="field is-horizontal">
|
||||
<div class="field-label"><label class="label">ou</label></div>
|
||||
<div class="field-body">
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-primary">Depuis une liste</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="button is-danger" @click="$emit('confirmAction')">Finaliser</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import ItemInput from './ItemInput.vue'
|
||||
// List of items for autocomplete
|
||||
const MOCK_ITEMS = [
|
||||
{id: 35, name: "Cape d'invisibilité", sell_value: 30000},
|
||||
{id: 8, name: "Arc long", sell_value: 10},
|
||||
{id: 9, name: "Arc court", sell_value: 10},
|
||||
];
|
||||
|
||||
export default {
|
||||
props: ["inventory"],
|
||||
components: { ItemInput },
|
||||
data () { return {
|
||||
looted: [],
|
||||
inventory: MOCK_ITEMS,
|
||||
};
|
||||
},
|
||||
data () { return {}; },
|
||||
methods: {
|
||||
onAddItem (item) {
|
||||
this.looted.push(item);
|
||||
},
|
||||
onClose () {
|
||||
this.$emit('done');
|
||||
this.$emit('addItem', item);
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ use actix_files as fs;
|
||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
use lootalot_db::{DbApi, Pool, QueryResult};
|
||||
use lootalot_db::models::Item;
|
||||
use std::env;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
@@ -162,6 +163,17 @@ pub(crate) fn serve() -> std::io::Result<()> {
|
||||
db_call(pool, move |api| api.as_admin().resolve_claims())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/add-loot",
|
||||
web::post().to_async(
|
||||
move |pool: AppPool, data: web::Json<Vec<Item>>| {
|
||||
db_call(pool, move |api| api
|
||||
.as_admin()
|
||||
.add_loot(data.to_vec()),
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
.route(
|
||||
"/add-player",
|
||||
web::get().to_async(
|
||||
|
||||
Reference in New Issue
Block a user