Tabletop targeted actions now work

This commit is contained in:
Stefan Zermatten
2023-01-15 14:18:17 +02:00
parent 25e6b19b49
commit ceb170cbcf
9 changed files with 188 additions and 201 deletions

View File

@@ -125,33 +125,16 @@ const insertCreatureLog = new ValidatedMethod({
}, },
}); });
const insertTabletopLog = new ValidatedMethod({ export function insertCreatureLogWork({ log, creature, tabletopId, method }) {
name: 'creatureLogs.methods.insertTabletopLog',
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
validate: new SimpleSchema({
log: CreatureLogSchema.omit('date'),
}).validator(),
run({ log }) {
const tabletopId = log.tabletopId;
assertUserInTabletop(tabletopId, this.userId);
// Build the new log
let id = insertCreatureLogWork({ log, method: this })
return id;
},
});
export function insertCreatureLogWork({ log, creature, method }) {
// Build the new log // Build the new log
if (typeof log === 'string') { if (typeof log === 'string') {
log = { content: [{ value: log }] }; log = { content: [{ value: log }] };
} }
if (!log.content?.length) return; if (!log.content?.length) return;
log.date = new Date(); log.date = new Date();
if (creature) log.tabletopId = creature.tabletop; if (tabletopId) log.tabletopId = tabletopId;
if (creature && creature.tabletop) log.tabletopId = creature.tabletop;
console.log(log.tabletopId);
// Insert it // Insert it
let id = CreatureLogs.insert(log); let id = CreatureLogs.insert(log);
if (Meteor.isServer) { if (Meteor.isServer) {
@@ -185,24 +168,39 @@ const logRoll = new ValidatedMethod({
roll: { roll: {
type: String, type: String,
}, },
tabletopId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
},
creatureId: { creatureId: {
type: String, type: String,
regEx: SimpleSchema.RegEx.Id, regEx: SimpleSchema.RegEx.Id,
optional: true,
}, },
}).validator(), }).validator(),
run({ roll, creatureId }) { run({ roll, tabletopId, creatureId }) {
const creature = Creatures.findOne(creatureId, { if (!creatureId && !tabletopId) throw new Meteor.Error('no-id',
fields: { 'A creature id or tabletop id must be given'
readers: 1, );
writers: 1, let creature;
owner: 1, if (creatureId) {
'settings.discordWebhook': 1, creature = Creatures.findOne(creatureId, {
name: 1, fields: {
avatarPicture: 1, readers: 1,
} writers: 1,
}); owner: 1,
assertEditPermission(creature, this.userId); 'settings.discordWebhook': 1,
const variables = CreatureVariables.findOne({ _creatureId: creatureId }); name: 1,
avatarPicture: 1,
}
});
assertEditPermission(creature, this.userId);
}
if (tabletopId) {
assertUserInTabletop(tabletopId, this.userId);
}
const variables = CreatureVariables.findOne({ _creatureId: creatureId }) || {};
let logContent = [] let logContent = []
let parsedResult = undefined; let parsedResult = undefined;
try { try {
@@ -243,11 +241,11 @@ const logRoll = new ValidatedMethod({
date: new Date(), date: new Date(),
}; };
let id = insertCreatureLogWork({ log, creature, method: this }); let id = insertCreatureLogWork({ log, creature, tabletopId, method: this });
return id; return id;
}, },
}); });
export default CreatureLogs; export default CreatureLogs;
export { CreatureLogSchema, insertCreatureLog, logRoll, insertTabletopLog, PER_CREATURE_LOG_LIMIT }; export { CreatureLogSchema, insertCreatureLog, logRoll, PER_CREATURE_LOG_LIMIT };

View File

@@ -9,8 +9,7 @@ export default function computeAction(computation, node) {
computeResources(computation, node); computeResources(computation, node);
if (!prop.resources) return; if (!prop.resources) return;
prop.resources.itemsConsumed.forEach(itemConsumed => { prop.resources.itemsConsumed.forEach(itemConsumed => {
if (!itemConsumed.itemId) return; if (!itemConsumed.itemId || itemConsumed.available < itemConsumed.quantity?.value) {
if (itemConsumed.available < itemConsumed.quantity?.value) {
prop.insufficientResources = true; prop.insufficientResources = true;
} }
}); });

View File

@@ -1,98 +0,0 @@
<template lang="html">
<div
style="height: 100%; overflow: hidden;"
class="character-log layout column justify-end"
>
<v-slide-y-reverse-transition
group
hide-on-leave
class="card-raised-background flex layout column reverse align-end pa-3"
style="overflow: auto;"
>
<log-entry
v-for="log in logs"
:key="log._id"
:model="log"
:show-name="showName"
/>
</v-slide-y-reverse-transition>
<v-card>
<v-text-field
v-model="input"
class="mx-2 mb-2"
persistent-hint
style="flex-grow: 0"
append-outer-icon="mdi-send"
:hint="inputHint"
:error-messages="inputError"
:disabled="!editPermission"
@click:append-outer="submit"
@keyup.enter="submit"
/>
</v-card>
</div>
</template>
<script lang="js">
import LogEntry from '/imports/client/ui/log/LogEntry.vue';
import { parse, prettifyParseError } from '/imports/parser/parser.js';
import resolve, { toString } from '/imports/parser/resolve.js';
export default {
components: {
LogEntry,
},
props: {
logs: {
type: Array,
required: true,
},
editPermission: Boolean,
showName: Boolean,
},
data(){return {
inputHint: undefined,
inputError: undefined,
input: undefined,
}},
watch: {
input(value){
this.input = value;
this.inputHint = this.inputError = undefined;
if (!this.input) return;
let result;
try {
result = parse(value);
} catch (e){
if (e.constructor.name === 'EndOfInputError'){
this.inputError = '...';
} else {
let error = prettifyParseError(e);
this.inputError = error;
}
return;
}
try {
let {result: compiled} = resolve('compile', result, this.creature.variables);
this.inputHint = toString(compiled);
return;
} catch (e){
console.warn(e);
this.inputError = 'Compilation error';
return;
}
},
},
methods: {
submit(){
if (this.inputError || !this.input) return;
this.$emit('submit', this.input);
this.input = '';
},
},
}
</script>
<style lang="css" scoped>
</style>

View File

@@ -25,8 +25,11 @@
:hint="inputHint" :hint="inputHint"
:error-messages="inputError" :error-messages="inputError"
:disabled="!editPermission" :disabled="!editPermission"
:loading="submitLoading"
@click:append-outer="submit" @click:append-outer="submit"
@keyup.enter="submit" @keyup.enter="submit"
@keyup.up="decrementHistory"
@keyup.down="incrementHistory"
/> />
</v-card> </v-card>
</div> </div>
@@ -40,6 +43,7 @@ import { assertEditPermission } from '/imports/api/creature/creatures/creaturePe
import { parse, prettifyParseError } from '/imports/parser/parser.js'; import { parse, prettifyParseError } from '/imports/parser/parser.js';
import resolve, { toString } from '/imports/parser/resolve.js'; import resolve, { toString } from '/imports/parser/resolve.js';
import LogEntry from '/imports/client/ui/log/LogEntry.vue'; import LogEntry from '/imports/client/ui/log/LogEntry.vue';
import { Tracker } from 'meteor/tracker'
export default { export default {
components: { components: {
@@ -48,22 +52,70 @@ export default {
props: { props: {
creatureId: { creatureId: {
type: String, type: String,
required: true, default: undefined,
},
tabletopId: {
type: String,
default: undefined,
}, },
}, },
data(){return { data(){return {
inputHint: undefined, inputHint: undefined,
inputError: undefined, inputError: undefined,
input: undefined, input: undefined,
history: [],
historyIndex: 1,
submitLoading: false,
}}, }},
watch: { watch: {
input(value){ input(value){
this.input = value; this.input = value;
this.recalculate();
},
creatureId() {
Tracker.afterFlush(() => this.recalculate())
},
historyIndex(i) {
if (typeof this.history[i] === 'string') {
this.input = this.history[i];
}
}
},
methods: {
submit() {
if (!this.input) return;
if (this.submitLoading) return;
const log = {
roll: this.input,
};
if (this.tabletopId) log.tabletopId = this.tabletopId;
if (this.creatureId) log.creatureId = this.creatureId;
this.submitLoading = true;
logRoll.call(log, (error) => {
this.submitLoading = false;
if (!error) {
this.addHistory(this.input);
this.input = '';
this.inputError = undefined;
return;
}
this.inputError = error.message || error.toString();
console.error(error);
});
},
addHistory(string) {
// Don't add duplicates back to back in history
if (string === this.history[this.history.length - 1]) return;
this.history.push(string);
if (this.history.length > 50) this.history.shift();
this.historyIndex = this.history.length;
},
recalculate() {
this.inputHint = this.inputError = undefined; this.inputHint = this.inputError = undefined;
if (!this.input) return; if (!this.input) return;
let result; let result;
try { try {
result = parse(value); result = parse(this.input);
} catch (e){ } catch (e){
if (e.constructor.name === 'EndOfInputError'){ if (e.constructor.name === 'EndOfInputError'){
this.inputError = '...'; this.inputError = '...';
@@ -83,24 +135,29 @@ export default {
return; return;
} }
}, },
}, incrementHistory() {
methods: { if (this.historyIndex < this.history.length) {
submit(input){ this.historyIndex += 1;
logRoll.call({ }
roll: input,
creatureId: this.creatureId,
}, (error) => {
if (error) console.error(error);
});
}, },
decrementHistory() {
if (this.historyIndex > 0) {
this.historyIndex -= 1;
}
}
}, },
// @ts-ignore
meteor: { meteor: {
logs(){ logs() {
return CreatureLogs.find({ const filter = {};
creatureId: this.creatureId, if (this.tabletopId) {
}, { filter.tabletopId = this.tabletopId;
} else if (this.creatureId) {
filter.creatureId = this.creatureId;
}
return CreatureLogs.find(filter, {
sort: {date: -1}, sort: {date: -1},
limit: 20 limit: 100
}); });
}, },
creature(){ creature(){

View File

@@ -53,7 +53,11 @@
:loading="doActionLoading" :loading="doActionLoading"
:disabled="model.insufficientResources || !context.editPermission || !!targetingError" :disabled="model.insufficientResources || !context.editPermission || !!targetingError"
v-on="active ? { v-on="active ? {
'click.stop': doAction click: e => {
if (!active) return;
e.stopPropagation();
doAction({});
}
} : {}" } : {}"
> >
<template v-if="rollBonus && !rollBonusTooLong"> <template v-if="rollBonus && !rollBonusTooLong">
@@ -224,12 +228,14 @@ export default {
actionTypeIcon() { actionTypeIcon() {
return `$vuetify.icons.${this.model.actionType}`; return `$vuetify.icons.${this.model.actionType}`;
}, },
targetingError(){ targetingError() {
// Can always do an action without a target if (!this.active) return;
if (!this.targets || !this.targets.length) return undefined; const targets = this.targets || [];
if (this.targets.length > 1 && this.model.target !== 'multipleTargets'){ if (this.model.target === 'singleTarget' && targets.length === 0) {
return 'Single target'; return 'Select target';
} else if (this.model.target === 'self' && this.targets[0] !== this.model.ancestors[0]._id){ } else if (targets.length > 1 && this.model.target !== 'multipleTargets'){
return 'Single target only';
} else if (this.model.target === 'self' && targets.length > 0){
return 'Can only target self'; return 'Can only target self';
} }
return undefined; return undefined;
@@ -280,6 +286,7 @@ export default {
} }
}, error => { }, error => {
this.doActionLoading = false; this.doActionLoading = false;
this.$emit('deactivate');
if (error) { if (error) {
console.error(error); console.error(error);
snackbar({ text: error.reason }); snackbar({ text: error.reason });

View File

@@ -28,20 +28,22 @@
:model="creature" :model="creature"
:active="activeCreatureId === creature._id" :active="activeCreatureId === creature._id"
:targeted="targets.includes(creature._id)" :targeted="targets.includes(creature._id)"
:show-target-btn="!!activeActionId" :show-target-btn="targets.includes(creature._id) || moreTargets"
@click="() => { v-on="(!activeActionId || (targets.includes(creature._id) || moreTargets)) ? {
if (activeActionId) { click: () => {
if (targets.includes(creature._id)) { if (activeActionId) {
untarget(creature._id) if (targets.includes(creature._id)) {
untarget(creature._id)
} else {
if (moreTargets) targets.push(creature._id);
}
} else { } else {
targets.push(creature._id); activeCreatureId = creature._id;
targets = [];
activeActionId = undefined;
} }
} else {
activeCreatureId = creature._id;
targets = [];
activeActionId = undefined;
} }
}" } : {}"
@target="targets.push(creature._id)" @target="targets.push(creature._id)"
@untarget="untarget(creature._id)" @untarget="untarget(creature._id)"
/> />
@@ -95,6 +97,7 @@
:key="action._id" :key="action._id"
:model="action" :model="action"
:active="activeActionId === action._id" :active="activeActionId === action._id"
:targets="targets"
@activate="activeActionId = action._id" @activate="activeActionId = action._id"
@deactivate="activeActionId = undefined; targets = [];" @deactivate="activeActionId = undefined; targets = [];"
/> />
@@ -157,6 +160,11 @@ export default {
targets: [], targets: [],
} }
}, },
watch: {
activeCreatureId(id) {
this.$root.$emit('active-tabletop-character-change', id);
}
},
meteor: { meteor: {
$subscribe: { $subscribe: {
'tabletop'() { 'tabletop'() {
@@ -169,6 +177,15 @@ export default {
actions(){ actions(){
return getProperties(this.activeCreatureId, { type: 'action', actionType: { $ne: 'event'} }); return getProperties(this.activeCreatureId, { type: 'action', actionType: { $ne: 'event'} });
}, },
moreTargets(){
const activeAction = CreatureProperties.findOne(this.activeActionId);
if (!activeAction) return;
if (activeAction.target === 'singleTarget') {
return this.targets.length === 0;
} else if (activeAction.target === 'multipleTargets') {
return true;
}
},
editPermission(){ editPermission(){
try { try {
assertEditPermission(this.activeCreatureId, Meteor.userId()); assertEditPermission(this.activeCreatureId, Meteor.userId());

View File

@@ -2,12 +2,12 @@
<v-card <v-card
style="height: 100px; width: 70px;" style="height: 100px; width: 70px;"
class="tabletop-creature-card" class="tabletop-creature-card"
:color="active ? 'accent' : ''" :class="{ active }"
hover :hover="hasClickListener"
:elevation="active ? 8 : 2" :elevation="active ? 8 : 2"
@mouseover="hover = true" @mouseover="() => { if (hasClickListener) hover = true; }"
@mouseleave="hover = false" @mouseleave="hover = false"
@click="$emit('click')" v-on="hasClickListener ? {click: () => $emit('click')} : {}"
> >
<v-progress-linear <v-progress-linear
v-if="variables.hitPoints" v-if="variables.hitPoints"
@@ -25,16 +25,18 @@
</div> </div>
<card-highlight :active="hover" /> <card-highlight :active="hover" />
<div class="d-flex justify-center"> <div class="d-flex justify-center">
<v-btn <v-scale-transition>
v-if="showTargetBtn" <v-btn
:color="targeted ? 'accent' : ''" v-if="showTargetBtn"
:elevation="targeted ? 8 : 2" :color="targeted ? 'accent' : ''"
fab :elevation="targeted ? 8 : 2"
small fab
@click.stop="targeted ? $emit('untarget') : $emit('target')" small
> @click.stop="targeted ? $emit('untarget') : $emit('target')"
<v-icon>{{ targeted ? 'mdi-target' : 'mdi-target' }}</v-icon> >
</v-btn> <v-icon>{{ targeted ? 'mdi-target' : 'mdi-target' }}</v-icon>
</v-btn>
</v-scale-transition>
</div> </div>
</v-card> </v-card>
</template> </template>
@@ -60,6 +62,11 @@ export default {
hover: false, hover: false,
} }
}, },
computed: {
hasClickListener() {
return this.$listeners && !!this.$listeners.click;
},
},
// @ts-ignore // @ts-ignore
meteor: { meteor: {
variables() { variables() {

View File

@@ -1,17 +1,17 @@
<template lang="html"> <template lang="html">
<log-component <character-log
:creature-id="tabletopId" :tabletop-id="tabletopId"
:creature-id="activeCreatureId"
/> />
</template> </template>
<script lang="js"> <script lang="js">
import CreatureLogs from '/imports/api/creature/log/CreatureLogs.js'; import { insertTabletopLog } from '/imports/api/creature/log/CreatureLogs.js';
import insertTabletopLog from '/imports/api/creature/log/CreatureLogs.js'; import CharacterLog from '/imports/client/ui/log/CharacterLog.vue';
import LogComponent from '/imports/client/ui/log/CharacterLog.vue';
export default { export default {
components: { components: {
LogComponent, CharacterLog,
}, },
inject: { inject: {
context: { context: {
@@ -21,18 +21,18 @@ export default {
props: { props: {
tabletopId: { tabletopId: {
type: String, type: String,
required: true, default: undefined,
}, },
}, },
methods: { data() {
submit(){ return {
insertTabletopLog.call({ activeCreatureId: undefined,
content: this.logContent, }
tabletopId: this.tabletopId, },
}, (error) => { mounted() {
if (error) console.error(error); this.$root.$on('active-tabletop-character-change', (id) => {
}); this.activeCreatureId = id;
}, });
}, },
} }
</script> </script>

View File

@@ -66,7 +66,7 @@ Meteor.publish('tabletop', function (tabletopId) {
const logs = CreatureLogs.find({ const logs = CreatureLogs.find({
tabletopId, tabletopId,
}, { }, {
limit: 50, limit: 100,
sort: { date: -1 }, sort: { date: -1 },
}); });
return [tabletopCursor, creatureSummaries, properties, logs, variables] return [tabletopCursor, creatureSummaries, properties, logs, variables]