How can I fire an event when a user attribute updates? - python-3.7

I have a FastAPI endpoints for game state updates. Each user has some guess count and if that becomes 0, then there will be cool down timer for x seconds before guesses can be replenished.
So how should this be supported from the server end? Should server fire an event when a user guess count becomes 0, that it can sleep for cool down time until guesses are replenished. If yes, how can that be done? Is there something FastAPI provides on that?

Yes, you can do that, without code it's hard to provide a exact answer, but you can achieve that by Background Tasks
So it could look something like this:
from fastapi import BackgroundTasks
async def refresh_user_stats(user: User = Depends(get_current_user)):
do something here
#app.update()
async def refresh_user(background_tasks: BackgroundTasks):
background_tasks.add_task(refresh_user_stats())

Related

Trouble with agent state chart

I'm trying to create an agent statechart where a transition should happen every day at 4 pm (except weekends).
I have already tried:
1. a conditional transition (condition: getHourOfDay() == 16)
2: A timeout transition that will "reinsert" my agent into the chart every 1 s and check if time = 16.
My code is still not running, does anyone have any idea how to solve it?
This is my statechart view. Customer is a single resource that is supposed to "get" the products out of my stock everyday at 4pm. It is supposed to happen in the "Active" state.
I have set a timeout transition (from Active-Active) that runs every 1s.
Inside my "Active" state in the "entrance action" i wrote my code to check if it is 4 pm and run my action if so.
I thought since i set a timeout transition it would check my condition every 1s, but apparently it is not working.
Your agent does not enter the Active state via an internal transition.
Redraw the transition to actually go out of the Active state and then enter it again as below:
Don't use condition-based transitions, for performance reasons. In your case, it also never triggers because it is only evaluated when something happens in the model. Incidentally, that is not the case at 4pm.
re your timeout approach: Why would you "reinsert" your agent into its own statechart? Not sure I understand...
Why not set up a schedule or event with your recurrence requirement and make it send a message to the statechart: stateChart.fireEvent("trigger!");. In your statechart, add a message-based transition that waits for this message. This will work.
Be careful to understand the difference between the Statechart.fireEvent() and the Statechart.receiveMessage() functions, though.
PS: and agree with Felipe: please start using SOF the way it is meant, i.e. also mark replies as solved. It helps us but also future users to quickly find solutions :-) cheers

google action conversation exit: not waiting for user response

I am trying to seek user feedback at the end of a google action session using actions sdk.
As per the Actions documentation (https://developers.google.com/actions/assistant/conversation-exits), I have set the cancel intent
"conversations" : {
...
"inDialogIntents": [
{
"name": "actions.intent.CANCEL"
},
...
and in my functions code I am calling a SimpleResponse as below:
app.intent('actions.intent.CANCEL', (conv, input) => {
conv.close(new SimpleResponse({
speech: 'Kindly rate between 1 and 5,
text: 'Rate between 1 and 5'
}));
});
When the user says goodbye, the above code gets called. The speech is heard and text displayed but the conversation immediately exits with an earcon.
The documentation mentions that the system will wait for 2 seconds before exiting but it seems to do so immediately. Is there a way to get this working? Thanks
It's not quite clear to me what you'd like to achieve. First of all, as long as you're using conv.close() method, you are ending the conversation. If you want to keep the mic open, you need to use conv.ask() method. But I'm not sure id you can use conv.ask() with actions.intent.CANCEL, because that event is there to get the user out, not to keep the conversation going. But I'm not %100 sure, you need to try it.
If what you're trying to achieve is something like this:
User: cancel
System: How would you rate our interaction?
User: Good.
earcon
Then, in my humble opinion, you probably shouldn't (and as I said you probably can't) do it. The docs clearly state that the purpose for custom exit behavior is:
to cleanup your fulfillment logic and respond to the user one last time.
Also, the docs doesn't say the system will wait for the user response for 2 second. It says the execution of the request (the time out for your fulfillment) is 2 seconds. Plus; if a user is canceling mid-conversation, chances are that the interaction was unsuccessful anyway. I'd rather create feedback dialog turns that are connected to the fallback intents and happy path(s).
Still, try to use conv.ask() instead and let me know if that works. (And please let me know by commenting.) Either case, though, I'd consider getting the feedback some other way.

How to setBusy(false) Indicator SAPUI5 for all controls at one time

we want to take care that all running busy indicators will be stopped after a couple of time. How can we do that? For the moment we use setBusy(false) for each control.
Thanks a lot!
I think that you should change your overall approach because it's not a good UI/UX pattern.
First of all, why do you have more than one busy control in your view? For instance, you if you are loading record in a list you just set busy the list, not the whole page. If you are submitting a form data, you set busy only the form not everything else.
Second of all, why do you say "For the moment we use setBusy(false) for each control"? You should remove the busy state after a specific event. For istance when you finished to load list's result or you get the result of a form submission.
Anyway, to solve your current issue, the best approach is to use XML binding with a temporary JSON model.
You could have a JSON model like with content like this:
{
busy: false
}
and you bind the busy property of the control to youtJSONModel>/busy at this point when you need to set the control to a busy state you can do this.getView().getModel("youtJSONModel").setProperty("/busy", true); and when you have finished the operation you can do this.getView().getModel("youtJSONModel").setProperty("/busy", false);

Is there a correct alternative to cancelAndHoldAtTime when the browser does not support this?

Using WAA (Web Audio Api) I am using exponentialRampToValueAtTime to create a fade in and out that can be cancelled for the user pressing the "stop" button.
The stops function looks something like this:
this._gainNode.gain.cancelAndHoldAtTime(this._AudioContext.currentTime);
this._gainNode.gain.exponentialRampToValueAtTime(0.000001, this._AudioContext.currentTime + fadeDuration);
The complete function is wrapped in a Promise because I need to do something else after the stop (fadeDuration);
The problem is that in some browsers cancelAndHoldAtTime does not exit and I get some nasty clips that I do not know how to prevent. The same happens with cancelValuesAndHoldAtTime that is even less supported.
I tried with cancelScheduledValues but it does not help.
Is there a workaround or correct alternative to cancelAndHoldAtTime
You can do an approximation by calling setValueAtTime(v, t) where t is the context time at which the user pressed the stop button and v is the estimated value of the exponential at time t.
You'll probably still get a glitch, but it will probably be much better than using just cancelScheduledValues.
This is the reason why cancelAndHoldAtTime was added to the API.
Although maybe the best alternative for cancelAndHoldAtTime is the one recommended for Raymond Toy, to completely remove the clip and any glitches what I ended up doing was making a "Main Gain" between the oscillator Gain and the destination
OSC -> oscGain -> mainGain -> speakers.
Before I do anything I close the mainGain and then cancelScheduledValues and all the other stuff to the oscGain

Is there any way to inter communicate with modules in boilerplatejs?

Regarding the BoilerplateJs example, how should we adjust those modules to be intercommunicate in such a way once the user done any change to one module, the other related modules should be updated with that change done.
For example, if there is a module to retrieve inputs from user as name and sales and another module to update those retrieved data in a table or a graph, can you explain with some example ,how those inter connection occurs considering event handling?
Thanks!!
In BoilerplateJS, each of your module will have it's own moduleContext object. This module context object contains two methods 'listen' and 'notify'. Have a look at the context class at '/src/core/context.js' for more details.
The component that need to 'listen' to the event, should register for the event by specifying the name of the event and callback handler. Component that raise the event should use 'notify' method to let others know something interesting happened (optionally passing a parameter).
Get an update of the latest BoilerplateJS code from GitHub. I just committed changes with making clickCounter a composite component where 'clickme component' raising an event and 'lottery component' listening to the event to respond.
Code for notifying the Event:
moduleContext.notify('LOTTERY_ACTIVITY', this.numberOfClicks());
Code for listening to the Event:
moduleContext.listen("LOTTERY_ACTIVITY", function(activityNumber) {
var randomNum = Math.floor(Math.random() * 3) + 1;
self.hasWon(randomNum === activityNumber);
});
I would look at using a Publish-Subscribe library, such as Amplify. Using this technique it is easy for one module to act as a publisher of events and others to register as subscribers, listening and responding to these events in a highly decoupled manner.
As you are already using Knockout you might be interested in first trying Ryan Niemeyer's knockout-postbox plugin first. More background on this library is available here including a demo fiddle. You can always switch to Amplify later if you require.