<template lang="jade">
div#vue-market
div.market-menager
h3 Market manager ({{outSize}})
header
input(type="text" v-model="sort.name" placeholder="Nazwa przedmiotu")
input(type="text" v-model="sort.priceMin" placeholder="Cena min")
input(type="text" v-model="sort.priceMax" placeholder="Cena max")
select-template(:selected.sync="sort.price" v-bind:list="['min - max','max - min']" placeholder="Sortowanie ceny")
select-template(:selected.sync="sort.available" v-bind:list="['włączone','wyłączone']" placeholder="Dortowanie dostępności")
select-template(:selected.sync="sort.error" v-bind:list="['Alert api','Alert ceny','Alert Brak']" placeholder="Sortowanie Alertów")
header
button.gradient(@click="setAllProfile({allow_in_case: 1})") Włącz wszystkie
button.gradient(@click="setAllProfile({allow_in_case: 0})") Wyłącz wszystkie
button.gradient(@click="UpdateAllProfilePrice") Aktualizuj ceny
nav
select-template(:selected.sync="page.limit" v-bind:list="['5','10','20']" placeholder="Sztuk")
i.fas.fa-angle-left(v-on:click="page.active--")
div
input(v-model="page.active" type="number")
p {{page.count}}
i.fas.fa-angle-right(v-on:click="page.active++")
section
item(v-for="profile in out" v-bind:profile="profile" @edit="setProfile")
</template>
<script>
import mySelect from "../../../../components/select.vue";
import item from "./item.vue";
export default {
name: "market-manager",
data() {
return {
query:{where:{market_hash_name:[],current_price:[],allow_in_case:[]}},
sort: { // wortości sortowań profili
price:'',
name:'',
available: '',
error: '',
priceMin: '',
priceMax: ''
},
page: {
count: 0,
limit: null,
active: 1
},
profiles:[], // Wszystkie profile
out:[], // Wyjściowe profile pod widok
outSize: 0,
temp: 0
}},
created() {
this.getProfiles(); // Pierwsze pobieranie profili
},
watch: {
'sort.available': function() {this.renderProfiles()},
'sort.price': function() {this.renderProfiles()},
'sort.name': function() {this.renderProfiles()},
'sort.error': function() {this.renderProfiles()},
'sort.priceMin': function() {this.renderProfiles()},
'sort.priceMax': function() {this.renderProfiles()},
'page.limit': function() {this.renderProfiles()},
'page.priceMin': function() {this.renderProfiles()},
'page.priceMax': function() {this.renderProfiles()},
'page.active': function(n,p) {
if(n < 1 || n > this.page.count) {
this.page.active = p
} else {
this.renderProfiles()
}
}
},
components: {
"select-template": mySelect,
'item': item
},
methods: {
setProfile(data) {
let params = {id:data.id,edit:data.edit,query:this.query};
Server.CallEvent("@Admin->editMarketProfiles",params,this.getProfiles);
},
setAllProfile(edit) {
let ids = [];
this.out.forEach((v)=>{ids.push(v.id)})
let params = {edit: edit,query: {whereIn:{id:ids}}};
if(ids.length != 0) {
Server.CallEvent("@Admin->editMarketProfiles", params,this.getProfiles);
}
},
UpdateAllProfilePrice() {
let index = 0;
this.out.forEach((v)=>{
if(v.api != 0) {
let params = {id:v.id,edit:{current_price: v.api},query:this.query};
Server.CallEvent("@Admin->editMarketProfiles",params,()=>{
index++
if(index == this.outSize) {this.getProfiles();}
});
} else {
index++;
if(index == this.outSize) {this.getProfiles();}
}
})
},
getProfiles() {
let params = {query:this.query}
Server.CallEvent("@Admin->getMarketProfiles",params,(e,profiles)=>{
if(e==null) {
Server.CallEvent("@Admin->getSteamPrices",(er,apiPrice)=> {
profiles.forEach((profile,index)=>{
if(apiPrice[profile.market_hash_name]) {
profile.api = apiPrice[profile.market_hash_name];
}
else {
profile.api = 0;
}
profile.newPrice = profile.current_price;
profile.status = false
profile.error = this.getError(profile)
})
this.profiles = profiles;
this.renderProfiles();
})
}
});
},
getError(profile) {
if (profile.api == 0) {
return 'Alert api'
}
else if (Math.abs(profile.api - profile.current_price) > 0.5 && Math.abs(profile.api - profile.current_price) * 100 / profile.api > 10) {
return 'Alert ceny'
}
else {
return 'Alert Brak'
}
},
renderProfiles() {
// var start = +new Date(); // log start timestamp
let list = Object.assign([],this.profiles);
if(this.sort.available != '' & this.sort.available != null) { list = this.sortByAvailable(list) }
if(this.sort.error != '' & this.sort.error != null) { list = this.sortByError(list) }
if(this.sort.name != '' & this.sort.name != null) { list = this.sortByName(list) }
if(this.sort.price != '' & this.sort.price != null) { list = this.sortByPrice(list) }
if(this.sort.priceMin != '' || this.sort.priceMax != '') { list = this.sortByPriceMinMax(list) }
list = this.sortByPage(list);
this.out = list
this.outSize = list.length
// var end = +new Date(); // log end timestamp
// var diff = end - start;
// alert('Time: ' + diff + 'ms.');
},
sortByPrice(props){
let list = Object.assign([],props);
let out = list.sort((a,b)=>{
if(this.sort.price == 'min - max') {
return parseFloat(a.current_price) - parseFloat(b.current_price)
}
else if(this.sort.price == 'max - min') {
return parseFloat(b.current_price) - parseFloat(a.current_price)
}
})
return out
},
sortByName(props){
let reg = new RegExp(this.sort.name.toUpperCase());
let list = Object.assign([],props)
let out = [];
list.forEach((v,i,a)=>{
let name = v.market_hash_name.toUpperCase()
if (reg.test(name)) { out.push(v) }
})
return out
},
sortByAvailable(props){
let list = Object.assign([],props)
let out = [];
list.forEach((v,i,a)=>{
let available = v.allow_in_case
let number = 1;
if(this.sort.available == 'włączone') {number = 1}
if(this.sort.available == 'wyłączone') {number = 0}
if (available == number) {
out.push(v)
}
})
return out
},
sortByPriceMinMax(props){
console.log(12)
let list = Object.assign([],props);
let min = this.sort.priceMin==''?0:this.sort.priceMin;
let max = this.sort.priceMax==''?Infinity:this.sort.priceMax;
let out = [];
list.forEach((v,i,a)=>{
let price = parseFloat(v.current_price)
if (price >= min & price <= max) {
out.push(v)
}
})
return out
},
sortByError(props){
let list = Object.assign([],props)
let out = [];
list.forEach((v,i,a)=>{
if (this.sort.error == v.error) {
out.push(v)
}
})
return out
},
sortByPage(props){
let list = Object.assign([],props)
if(this.page.limit == null) {
this.page.count = 1;
this.page.active = 1;
return list
}
else {
this.page.count = Math.ceil(list.length / this.page.limit)
let tempSite = (parseInt(this.page.active)-1) * parseInt(this.page.limit);
let tempLimit = parseInt(this.page.limit);
let out = list.splice(tempSite, tempLimit);
return out
}
}
}
};
</script>