Delayed function calls in ruby or rails - ruby-on-rails-3

I'm coding a poker variant game, and need either ruby or rails to run one big loop to keep the game automated. Ie:
check every 10 seconds or so that there are enough players in the game;
when there are, deal the cards, then count down ~45 seconds, upon which showdown will happen;
in my poker variant there are 3 consecutive showdowns - therefore each showdown needs to happen slowly so the players know what's going on: ie 5 seconds to show down the first portion of the hand and declare the winner of that showdown, 5 seconds for the second and 5 seconds the third
pause a few seconds after the hand
start loop from the top again
I've had a quick look at delayed_job and resque, but they look quite complicated for what I'm trying to do. I've also considered using a javascript/jquery loop to push the server along the loop, but that seems inelegant, seeing as each of the potentially numerous players on a table will be seeking to push the loop along, rather than the server doing it centrally.
Am I missing a fairly simple function, or is it time to look deeper into delayed_job?

You pretty much need some kind daemon process, but you don't necessarily need any library.
You can write your code as a method on some module, and invoke it using rails runner. The method can execute a loop containing sleep <seconds> to execute code and wait. The easy way to stop the process is have it watch for a file to exist in /tmp or something, but you can also do things like write a pid file, and then kill the process later using its pid.

Related

Repast - Exchange information between batch runs

Batch runs in Repast are independent runs without interactions. However, in my model I have a need to enable such interaction. E.g. run-2 needs to get some data from run-1 in order to run completely.
Is there a way to exchange information between batch runs?
The order in which individual batch runs are executed is not predetermined. For example, if you distribute the runs among several resources two may run at the same time as or 2 even before 1. So, in the general case, I don't think this is possible.
That said, I think you have three options:
If possible, do all the independent runs (e.g. 1 in your example), gather the data, and then do the dependent runs. That won't work well obviously if you are actually talking about a chain of runs 1->2->3...
If all the runs are running on the same resource, you could experiment a bit to find out where run 1 is running. I suspect its probably in "instance_1" and run 2 is in instance_2 etc. By experiment here, I just mean look at the file system manually to see what is where. You could then use Java's various file IO classes (note - not Repast functionality) to get run 2's location and find the location of run 1's data with that. For example, if you know run 2 runs in /x/y/z/instance_2 (maybe by doing a Paths.get("./") or something) and that run 1 is then in /x/z/y/instance_1, you should be able to get the data. I don't know what data from run 1 you want but you'll have to make sure that the data you want has been completely written.
If run 2 really depends on run 1, perhaps it makes sense to update the model to run them as single run.
Nick

Having trouble making my VI work as a Sub VI

I am having trouble getting the terminals to pass any data to what they are connected to because the controls they connect to are in a while loop. My frustration level is high since I would have already had this done if I wrote it in C.
First, let me say this might get a little long so if you don't want to read it, then don't. Here goes. I have watched a couple of tutorials, read a lot, and even tried a few things out in code. I get why this can't be done directly in a while loop. Having said that, it seems that I have no choice but to use while loop(s) in my VI.
My VI is loosely based on Queued Message Handler in the Templates section of creating a new VI. I have 2 things that must take place. One - I have created a TCP client where I constantly send messages to get status from the equipment I am communicating with. This is a timed event and must be handled in a while loop so I can maintain the connection to the server. I am not doing the Open, Send, Close, Reopen, Send, Close, etc. type of message handling. Too inefficient. This is the lower half of the example template.
Second - On occasion the user will press a button on the front panel which creates a message that is sent to the equipment to make it do something. And this, it would seem, needs to be in a while loop also, hence my problem. Some/most of the controls exist with the event structure. This is the top half of the example template.
I actually have this working as a front panel, but every thing is in just one while loop and I cannot get the terminals to work. Here is where my confusion comes in, if I am passing something to the while loop, I only get its value once and if it changes, you don't get that change, and if you are passing the data out of the while loop, you only get it when the loop ends. These two things are really baffling me. How can pass data that changes while using a while loop, because I have to, but the while loop breaks using the terminals. Seems circular. The TCP communications cannot stop, and I cannot find an example of how to do this using my friend Google. Am I the only person on this planet that needs to do this? Doubt it.
Not going to show my code, as this in not a code problem. It is an understanding how LabView does things vs. how you would just write the code in C using some library. And also just being unfamiliar with all the things you can do in LabView, not to mention how things are different. I don't know what I don't know, but I can learn.
I want to be able to give the VI I have created to any user and let them use it to control my equipment. If they just want to run it as a front panel, or if they want to use it as a Sub VI that is OK too. I just need to be able to make the terminals actually pass data when used that way.
Thanks, I did order a book on LabView today, but I won't get it soon. I really need to put this problem to bed.
Cannot help that much without seeing the code. But I can try to give you a little bit of an idea of what is going on.
Dataflow is an important concept to understand in LabVIEW. elements (VIs, loops, etc.) will not start until all of their inputs (ie. terminals) have been received or set by something called before, and then they only take their inputs once. If your terminal is outside the loop, then the loop can only read it's starting value. (See "Infinite Loops" on this page). A simple way of solving this would be to put the terminal inside of the loop rather than outside, so it is then read on every iteration of the loop.
As for passing values outside of the loop, there are a number of methods for this. Again, because of dataflow, you will not usually be able to access the value of something inside the loop until the loop finishes executing. However, there are a number of ways to read those values in a different loop. Local or global variables would be the simplest way, but they are not recommended by NI. The proper way of handling this is using something on the synchronization pallet. More info on the options can be found here.
Seeing as you are basing something on the Queued Message Handler, a queue might be a good way to start. LabVIEW has built in examples of code to show you how to use these functions.
Loop synchronization and asynchronous programming are fundamental concepts for writing LabVIEW code. If these are not concepts you are familiar with, I would say that you will gain a lot from showing others your actual code and having people help you with the issues. If you are concerned about sharing something proprietary, try making a simple example and posting that code instead to understand the concepts better.
Event structure to react to evr8and functional global to pass data out.
Suggest pasting block diagram.

How to check if SQLite database is locked

I have an app that makes quite a few calls to a local SQLite3 database and sometimes these calls happen very close together (from different areas of the app). How can I check, before a call to the database is made, if the database is currently locked?
Ideally I would rewrite the app (which has grown far beyond its original scope) but won't have time in this iteration.
I have no idea what to do in objective-c, but I have been using sqlite3 with c from quite long time And I also faced same issue. I used below method.
use busy_timeout and keep it configurable.
use busy_handler to keep retry for n number of time.
This two improvement works well for me, but I had observed some performance issue which i am able to handle via above configuration parameter. You need to do some trade of between fail-safe and performance.

execution management

I started using VB just a few days ago (programming in general). Now I made some interesting things, but am stuck on just 1.
I have multiple timers that counts down their own specific times.
Once the timer runs out, the program executes another .exe for each timer.
But I'm wondering what would be the smartest way to say, 'Queue' exe's when one is already running, Queue all the others until the current once's process doesn't exist. Then execute the next.....so it will keep going.
But my only problem is, how will the program know which one to execute next and delete the previously completed one from the queue?
Also does something that can handle this exist? Or would I have to make one from scratch?
Thanks

Cutscene system ruled just by time?

This is my first time I need to create a cutscene system. I have read a lot on the web about different ways to accomplish this task and have mixed them with my own ideas. Now, it is implementation time, but I need some info from other people with more experience than me in this field. Here we go:
1) Some years ago, I implemented a system that actions could be queued in a serial/parallel way, building a tree of actions that when executed created the final result. This can be sure used as the cutscene system director, but, wouldn't it be so much simple to just have a timeline with actions ran at a certain time? An example:
playMp3(0, "Song.mp3)
createObject(0, "Ogre", "Ogre1")
moveObject(1, "Ogre1", Vector3(100,100,1))
This way everything would be really simple to script. Serial actions are supported buy spreading them correctly in time and parallel actions just need to have shared time ranges.
One problem I have seen is that an action like Sync() (This just waits for all actions to finish before start the other that come afterwards) can't be used because we're using absolute time to trigger our actions. Anyway, a solution could be to have our actions layered based on last "sync()". I mean something like this:
playMp3(0, "Song.mp3)
createObject(0, "Ogre", "Ogre1")
moveObject(1, "Ogre1", Vector3(100,100,1))
sync()
moveObject(0,....);
createObject(1,....);
As you may notice, times after sync() starts again from 0, so, when a sync() is ran, and it determines all previous actions from last sync() are finished, timeLine elapsed time would be 0 again. This can be seen as Little cutscene action groups.
2) The previous explanation needs all actions to be added at the beginning of the cutscene playing. Is this how it usually is done? Or do you usually add actions to the timeline as they are needed?
Well, I could be wrong here, but I think this could be a nice & simple way to lay the actions for a cutscene. What do you think?.
Thanks in advance.
I've done a few of these systems. I'll tell you what I like to use I hope this will answer your questions.
One of the first cutscenes system I did used LISP dialect because it is just couple of hours work to get a parser working. It used to be something like...
(play "song1.mp3")
(set "ogre1" (create "ogre"))
(moveTo "ogre1" '(100, 100, 100))
(wait 1)
I created something like virtual machine (VM) that was processing my scripts. The VM didn't use separate thread instead it had update function that was executing X amount of instructions or until it hits some synchronization instruction like wait for 1 sec.
At that time this system had to work on J2ME device which didn't have XML parser and XML parser was too much code to add. These days I'm using XML for everything except sounds and textures.
These days I'm using keyframe systems as BRPocock suggested. The problem is that this will be harder to manage without proper tools. If you using already some 3D software for your models I'll suggest you to investigate the option to use that product. I use Blender for cutscenes for personal projects since it's free at my work place we use Maya and 3ds Max, but the idea is the same. I export to COLLADA and then I have my animation tracks with keyframes. The problem is that COLLADA format is not the simplest it is made to be flexible and require decent amount of work to extract what you need from it.
The problem you will have with your format is to describe the interpolation so you want to move the ogre from one position to another... how long is this going to take? The advantage of keyframe system is that you can specify the position of the ogre in time... but scripting for such system without a tool will be difficult. Still here is a simple suggestion for format:
(play 'song1.mp3')
(entity 'ogre'
(position (
0 (100 100 100)
2 (100 200 100)
5 (100 300 100)
7 (100 300 400)
8 (100 300 500))
(mood (
0 'happy'
7 'angry'))
(... more tracks for that entity if you need ...))
(entity 'human'
(position (.....)))
Now with format like this you can see at what time where the ogre has to be. So if you have time 1.5 sec in the cutscene you can interpolate between the keyframes with time 0 and 2 sec. Where mood can be something you don't interpolate just swich when the right tome comes. I think this format is going to be more flexible and will solve your sync issues but I wouldn't suggest you writing it by hand without tools for big scripts. If your cutscenes are going to be just few sec with a few entities then it may be good for your.