Log optimistic UI now fixed, rolls are now instant
This commit is contained in:
@@ -3,8 +3,8 @@ import {insertCreatureLog} from '/imports/api/creature/log/CreatureLogs.js';
|
||||
|
||||
export default function applyAction({prop, creature}){
|
||||
spendResources(prop);
|
||||
insertCreatureLog({
|
||||
insertCreatureLog.call({
|
||||
log: prop.name,
|
||||
creature,
|
||||
creatureId: creature._id,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ export default function applyAttack({
|
||||
//actionContext
|
||||
}){
|
||||
let result = math.roll(1, 20) + prop.rollBonusResult;
|
||||
insertCreatureLog({
|
||||
insertCreatureLog.call({
|
||||
log: `${prop.name} attack. ${result} to hit`,
|
||||
creature,
|
||||
creatureId: creature._id,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,16 +41,7 @@ let CreatureLogSchema = new SimpleSchema({
|
||||
|
||||
CreatureLogs.attachSchema(CreatureLogSchema);
|
||||
|
||||
// This function should only be called by trusted code. No permission checks
|
||||
const insertCreatureLog = function({log, creature}){
|
||||
const creatureId = creature._id;
|
||||
// Build the new log
|
||||
if (typeof log === 'string'){
|
||||
log = {text: log};
|
||||
}
|
||||
log.creatureId = creatureId;
|
||||
// Insert it
|
||||
let id = CreatureLogs.insert(log);
|
||||
function removeOldLogs(creatureId){
|
||||
// Find the first log that is over the limit
|
||||
let firstExpiredLog = CreatureLogs.find({
|
||||
creatureId
|
||||
@@ -63,16 +54,61 @@ const insertCreatureLog = function({log, creature}){
|
||||
creatureId,
|
||||
date: {$lte: firstExpiredLog.date},
|
||||
});
|
||||
//TODO unblock before sending webhooks
|
||||
// Send webhooks
|
||||
}
|
||||
|
||||
function logWebhook({log, creature}){
|
||||
if (Meteor.isServer){
|
||||
sendWebhookAsCreature({
|
||||
creature,
|
||||
content: log.text,
|
||||
});
|
||||
}
|
||||
return id;
|
||||
};
|
||||
}
|
||||
|
||||
const insertCreatureLog = new ValidatedMethod({
|
||||
name: 'creatureLogs.methods.insertCreatureLog',
|
||||
mixins: [RateLimiterMixin],
|
||||
rateLimit: {
|
||||
numRequests: 5,
|
||||
timeInterval: 5000,
|
||||
},
|
||||
validate: new SimpleSchema({
|
||||
log: CreatureLogSchema.omit('type', 'date'),
|
||||
creatureId: {
|
||||
type: String,
|
||||
regEx: SimpleSchema.RegEx.Id,
|
||||
},
|
||||
}).validator(),
|
||||
run({log, creatureId}){
|
||||
const creature = Creatures.findOne(creatureId, {fields: {
|
||||
readers: 1,
|
||||
writers: 1,
|
||||
owner: 1,
|
||||
'settings.discordWebhook': 1,
|
||||
name: 1,
|
||||
avatarPicture: 1,
|
||||
}});
|
||||
assertEditPermission(creature, this.userId);
|
||||
// Build the new log
|
||||
if (typeof log === 'string'){
|
||||
log = {text: log};
|
||||
}
|
||||
if (Meteor.isServer){
|
||||
Meteor._sleepForMs(5000);
|
||||
}
|
||||
log.creatureId = creatureId;
|
||||
log.date = new Date();
|
||||
// Insert it
|
||||
let id = CreatureLogs.insert(log);
|
||||
if (Meteor.isServer){
|
||||
this.unblock();
|
||||
removeOldLogs(creatureId);
|
||||
logWebhook({log, creature});
|
||||
}
|
||||
return id;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
function equalIgnoringWhitespace(a, b){
|
||||
if (typeof a !== 'string' || typeof b !== 'string') return a === b;
|
||||
@@ -96,15 +132,23 @@ const logRoll = new ValidatedMethod({
|
||||
},
|
||||
}).validator(),
|
||||
run({roll, creatureId}){
|
||||
const creature = Creatures.findOne(creatureId);
|
||||
const creature = Creatures.findOne(creatureId, {fields: {
|
||||
variables: 1,
|
||||
readers: 1,
|
||||
writers: 1,
|
||||
owner: 1,
|
||||
'settings.discordWebhook': 1,
|
||||
name: 1,
|
||||
avatarPicture: 1,
|
||||
}});
|
||||
assertEditPermission(creature, this.userId);
|
||||
let parsedResult = parse(roll);
|
||||
let log;
|
||||
let logText;
|
||||
if (parsedResult === null) {
|
||||
log = 'Unexpected end of input';
|
||||
logText = 'Unexpected end of input';
|
||||
}
|
||||
else try {
|
||||
let logText = [];
|
||||
logText = [];
|
||||
let rollContext = new CompilationContext();
|
||||
let compiled = parsedResult.compile(creature.variables, rollContext);
|
||||
let compiledString = compiled.toString();
|
||||
@@ -116,11 +160,22 @@ const logRoll = new ValidatedMethod({
|
||||
let result = rolled.reduce(creature.variables, rollContext);
|
||||
let resultString = result.toString();
|
||||
if (resultString !== rolledString) logText.push(resultString);
|
||||
log = logText.join('\n\n');
|
||||
logText = logText.join('\n\n');
|
||||
} catch (e){
|
||||
log = 'Calculation error';
|
||||
logText = 'Calculation error';
|
||||
}
|
||||
return insertCreatureLog({log, creature});
|
||||
const log = {
|
||||
text: logText,
|
||||
creatureId,
|
||||
date: new Date(),
|
||||
};
|
||||
let id = CreatureLogs.insert(log);
|
||||
if (Meteor.isServer){
|
||||
this.unblock();
|
||||
removeOldLogs(creatureId);
|
||||
logWebhook({log, creature});
|
||||
}
|
||||
return id;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -101,8 +101,8 @@ export default {
|
||||
return CreatureLogs.find({
|
||||
creatureId: this.creatureId,
|
||||
}, {
|
||||
limit: 20,
|
||||
sort: {date: -1},
|
||||
limit: 20
|
||||
});
|
||||
},
|
||||
creature(){
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
}).observe({
|
||||
added(doc){
|
||||
if (!that.$subReady.singleCharacter) return;
|
||||
if (this.$store.state.rightDrawer) return;
|
||||
if (that.$store.state.rightDrawer) return;
|
||||
if (that.snackbars.some(o => o._id === doc._id)) return;
|
||||
doc.open = true;
|
||||
that.snackbars.push(doc);
|
||||
|
||||
Reference in New Issue
Block a user