Can I write logic in a database? - database

I'm writing a Flutter app that users can buy stuff on, and I have a discount scheme like: Buy more than 10 and get 5% off, more than 20 gets 10% off etc.
And that is very easy to implement in the code. But I'd rather not hard code the discount logic, since I might want to change it later.
So is there a way I can create that logic on a database and just call it on checkout?
I thought about creating Switch Cases with variables from a database which I can then change. But then I am limited by the amount of cases I create.
i.e.:
int discount1 = FirebaseReference; // 5
int limit1 = FirebaseReference; // 10 (so give 5% of if more than 10 is bought)
int discount2 = FirebaseReference; // 6
int limit2 = FirebaseReference; // 20
int discount3 = FirebaseReference; // 8
int limit3 = FirebaseReference; // 30
switch(discount) {
case amountBought < limit1: {
discount1
}
break;
case amountBought >= limit1 && amountBought < limit2: {
discount2
}
break;
case amountBought >= limit3: {
discount3
}
break;
}
Is there a way I can have this type of code in a database?

I don't think directly coding that logic into the database is possible (at least in Firebase).
Maybe you meant something like Cloud Functions. This way, you can just deploy new discount logics as new functions.

Related

Dice game simulator kotlin

I'm writing a code where I am versing the computer. A player throws a six-sided die and scores as many points as the total shown on the die providing the die doesn’t roll a 1.
The player may continue rolling and accumulating points (but risk rolling a 1) or end his turn.
If the player rolls a 1 his turn is over, he loses all points he accumulated that turn, and he passes the die
to the next player.
If the player ends without throwing a 1, the turn total is then added to the player's grand total.
Play passes from player to player until a winner is determined.
We are playing against the computer. The computer will roll the die using this rule:
A random number between 1 and 2 is CALCULATED. IF the number is a 1, the computer will roll. IF the number is a 2 it will stop rolling and give the dice back to the player. This loops until the calculated random number is a 2.
If a 1 is rolled, the turn is over and the computer loses all points from that turn.
The sum of all the rolls of the dice during this turn are added to computer's grand total
The human player can choose from a menu whether to roll again or "hold" and allow the computer to have a turn.
The issue is when I press hold (2) to pass it to the computer (ai) or when I roll a 1 it does nothing. the program stops. I am trying to git the program to only stop if all conditions are met can someone please help this is what I have so far.
var player = 0
var turntotal = 0
var computerpoints = 0
var grandtotal = 0
var roll = (1..6).random()
var ai = (1..2).random()
println("1. Roll Dice")
println("2. Hold and pass to computer")
println("3. Quit")
println()
println("Please select a menu Item")
player = readLine()!!.toInt()
while (turntotal < 50 && computerpoints < 50 && player != 3)
if (player == 1) {
roll = (1..6).random()
println("You rolled a $roll")
turntotal += roll
println("Turn total: $turntotal")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
while (roll != 1) {
println("1. Roll Again")
println("2. Hold")
player = readLine()!!.toInt()
if (player == 2) break
roll = (1..6).random()
println("You rolled a $roll")
turntotal += roll
println("Turn total: $turntotal")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
}
if (roll == 1) {
turntotal = 0
println("You rolled a $roll")
println("You lose all your turn points and turn ")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
println()
println("Computer's turn to roll")
while ( ai != 1)
roll = (1..2).random()
if (roll == 1)
roll =(1..6).random()
println("Computer decides to roll: $roll")
computerpoints += roll
println("Computer's points: $computerpoints")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
if (roll== 1)
computerpoints = 0
println("Computer rolled a $roll")
println("Computer's points is $computerpoints")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
if (roll == 2 )
println("1. Roll Dice")
println("2. Hold and pass to computer")
println("3. Quit")
println()
println("Please select a menu Item")
player = readLine()!!.toInt()
}
}
}
That formatting's hard to read (it's hard to see how the ifs and whiles are nested without the indentation and the missing braces) but you're not actually handling the user pressing 2 anywhere, except breaking out of one of the while loops.
Since your whole game loop is in this outer while:
player = readLine()!!.toInt()
...
while (turntotal < 50 && computerpoints < 50 && player != 3) {
// stuff for this turn
}
you need to read the player's choice inside that loop, right? Right now it looks like your loop only does something if player == 1 (everything looks like it's supposed to be inside that if block) but if they entered 2 last turn, then player is still 2, so the while runs forever.
You could do something like this:
// inside your main turn loop
// player turn
val playerBust = false
do {
val playerChoice = showMenu()
// handle the user choosing to roll
if (playerChoice == 1) {
val result = roll()
if (result == 1) playerBust = true
}
} while (!playerBust && playerChoice != 2) // keep going until they bust or hold
By using do/while the loop always runs at least once, so you can show your menu in there, and then check if they get another go in the while check at the end. By not relying on any old player state, each turn starts "fresh" and you can set playerBust to false, read in their first choice and go from there. And you can do the same for the computer turn!
Also I simplified it by using functions like showMenu() (returns the option the player chose) and roll() (rolls the die and returns the result). It's up to you if you want to implement functions like that, but it can really clean up the repeated code and make it simpler to reason about. The computer can call roll() too, right? Same for printing out the results - you can put all that formatting in a separate function, so your game loop is shorter and just contains the core logic. Smaller blocks of code are easier to work with!

How do I remove a single life per collision of two 2D objects?

I am programming a game for fun and to get more familiar with C and GBA mode 3. Though, I have run into an issue.
I have these two blocks on the screen, one is the good guy, the other is the bad guy. When the good guy collides with the bad guy its supposed to remove a life. That is where the problem comes in.
I have this within a while loop that runs the game:
if (plyr_row < enemy_row + enemy_size && plyr_ row
+ plyr_size > enemy_row && plyr_col < enemy_col + enemy_size
&& plyr_size + plyr_col > enemy_col)
{
lives--;
}
The lives do go down, but a lot of lives are taken away while the player is making contact with the enemy. In other words, during contact, the lives drop really fast and I just want to remove one for each time they collide, how can I accomplish that?
You have to use a flag to remember, if a collision is currently happening or not. Something like:
int in_collision = 0; // global flag, initialized to 0 once at start
...
if (plyr_row < enemy_row + enemy_size &&
plyr_row + plyr_size > enemy_row &&
plyr_col < enemy_col + enemy_size &&
plyr_size + plyr_col > enemy_col) {
if (!in_collision) {
in_collision = 1;
lives--;
}
} else {
in_collision = 0;
}
Now, the running collision must stop before another life will be removed on the following collision.
The simplest solution is to maintain a flag IN_COLLISION. You want to remove a life when there is a collision and IN_COLLISION is false.
Then it's a matter of toggling it to true at the first collision detection and then to false when you are not colliding anymore.

User input for a counter

I'm totally new to this and I'm trying to learn by doing. I have some basic skills in c and I've been making small, simple apps for about 2 weeks all by studying other apps and trying to understand how they are built.
I'm trying to make a counter now that is supposed to count people going in or out.
I got everything to work perfectly except for letting the user define the maximum amount. I would like to have the user type in the max amount in the text field and then press a button called start which would give the variable max the value typed in the text box.
Can I get some help with that?
Pseudo code:
mx = Max from User;
nrPersons = 0;
While (Userinput is not ExitProgramm) do
{
if (0 < nrPersons < mx) // allwoed range
{
run your programm that works
}
else
{
if (nrPersons > mx) => take action
if (nrPersons < 0) => take action
}
}

multiple instances of the same object spaced out using a loop is only creating one

I had a hard time trying to word my question properly, so i'm sorry if it seems confusing. Also i'm using the flixel library in flash builder. It may not be that important butcause probably anyone that knows a little more than me or even a little AS3 could probably see what i'm doing wrong.
Anyway, what i'm trying to do is basically create 10 instances of this square object I made. all I have to do is pass it an x an y coordinate to place it and it works. so ive tested if i just do:
var testsquare:Bgsq;
testsquare = new Bgsq(0,0);
add(testsquare);
it works fine and adds a square at 0,0 just like i told it to, but i want to add 10 of them then move the next one that's created 25 px to the right (because each square is 25px)
my problem is that I only ever see 1 square, like it's only making 1 instance of it still.
anyone possibly have an idea what I could be doing wrong?
var counter:int = 0;
var bgsqa:Array = new Array;
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x += 25;
add(bgsqa[counter]);
counter++;
}
There's a lot you're doing wrong here.
First off, you're using a pseudo-iterator (counter) to access array elements through a loop instead of, well, using the iterator (ibgs).
Second, I don't see anything in the array (bgsqa) you're iterating through. It's no wonder you're having problems. Here's what you should do.
var bgsqa:Array = [];
for(var i:int=0;i<10;i++)
{
var bgsq:Bgsq = new Bgsq(i * 25, 0);
add(bgsq);
bgsqa.push(bgsq);
}
That should probably do it if your post is accurate.
for (var ibgs:int = 0; ibgs < 10; ibgs++)
{
bgsqa[counter] = new Bgsq(0,0);
bgsqa[counter].x = counter * 25;
add(bgsqa[counter]);
counter++;
}
They start at 0, so applying += is simply adding 25 to 0. This should do the trick.

Querying real time data from an SQL database sudden latency problem

We are testing an application that is supposed to display real time data for multiple users on a 1 second basis. New data of 128 rows is inserted each one second by the server application into an SQL datatbase then it has to be queried by all users along with another old referential 128 rows.
We tested the query time and it didn't exceed 30 milliseonds; also the interface function that invokes the query didn't take more than 50 milliseconds with processing the data and all
We developed a testing application that creates a thread and an SQL connection per each user. The user issues 7 queries each 1 second. Everything starts fine, and no user takes more than 300 milliseconds for the 7 data series ( queries ). However, after 10 minutes, the latency exceeds 1 second and keeps on increasing. We don't know if the problem is from the SQL server 2008 handling multiple requests at the same time, and how to overcome such a problem.
Here's our testing client if it might help. Note that the client and server are made on the same 8 CPU machine with 8 GB RAM. Now we're questioning whether the database might not be the optimal solution for us.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Number of threads");
int threads = int.Parse(Console.ReadLine());
ArrayList l = new ArrayList();
for (int i = 0; i < threads; i++)
{
User u = new User();
Thread th = new Thread(u.Start);
th.IsBackground = true;
th.Start();
l.Add(u);
l.Add(th);
}
Thread.CurrentThread.Join();
GC.KeepAlive(l);
}
}
class User
{
BusinessServer client ; // the data base interface dll
public static int usernumber =0 ;
static TextWriter log;
public User()
{
client = new BusinessServer(); // creates an SQL connection in the constructor
Interlocked.Increment(ref usernumber);
}
public static void SetLog(int processnumber)
{
log = TextWriter.Synchronized(new StreamWriter(processnumber + ".txt"));
}
public void Start()
{
Dictionary<short, symbolStruct> companiesdic = client.getSymbolData();
short [] symbolids=companiesdic.Keys.ToArray();
Stopwatch sw = new Stopwatch();
while (true)
{
int current;
sw.Start();
current = client.getMaxCurrentBarTime();
for (int j = 0; j < 7; j++)
{
client.getValueAverage(dataType.mv, symbolids,
action.Add, actionType.Buy,
calculationType.type1,
weightType.freeFloatingShares, null, 10, current, functionBehaviour.difference); // this is the function that has the queries
}
sw.Stop();
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss") + "\t" + sw.ElapsedMilliseconds);
if (sw.ElapsedMilliseconds > 1000)
{
Console.WriteLine("warning");
}
sw.Reset();
long diff = 0;//(1000 - sw.ElapsedMilliseconds);
long sleep = diff > 0 ? diff : 1000;
Thread.Sleep((int)sleep);
}
}
}
Warning: this answer is based on knowledge of MSSQL 2000 - not sure if it is still correct.
If you do a lot of inserts, the indexes will eventually get out of date and the server will automatically switch to table scans until the indexes are rebuilt. Some of this is done automatically, but you may want to force reindexing periodically if this kind of performance is critical.
I would suspect the query itself. While it may not take much time on an empty database, as the amount of data grows it may require more and more time depending on how the look up is done. Have you examined the query plan to make sure that it is doing index lookups instead of table scans to find the data? If not, perhaps introducing some indexes would help.

Resources