Health/Energy consumption and restoration - android

I am new to developing games and apps in android, as much a noob as i am i was wondering how i could implement a health/energy bar.
Such as candy crush, so when you die you lose 1 health and a timer starts for aprox 15 minutes and when that timer ends you recive that 1 health back.
If you lose 5 times you run out of health and cannot play until you have restored atleast 1 health.
I was thinking of tracking it like a score system, so when you press the play button 1 health is removed and you start a timer, not sure how i would implement this thou.
Any help is much appriciated!

Theoretical attempt, implementation up to you.
I would have a SQLite DB where you store your lifes. Initially let's say 5. When a users fails it is substracted. Now this is where you would add or change an entry in a DB or in SharedPrefs.
Your DB would look like:
lifes=>4
restoreTime=>27.02.2014 14:34:22 (that is the time when a new life will be added)
In you game loop just periodically check for if you are already behind that time. If so add lifes by calculating the difference of time divided by the length of your "regen" time and
if(newLifes == 5) delete restore time or set it to infinity or a date in 9999 years or so (so it wont increment in the next gameloop check).
else update restore time to the next time it will add lifes.
You see: Making games is not that trivial ...

Related

How to auto-save a form on Android - Best practices

I am trying to introduce auto-save functionality on one of my Android applications. The idea is that as the user inputs first name, last name and after a fixed interval I would like to save that information to the server before the user hits Next button. The final goal is to have something similar to the draft option in the Gmail app where your email information is automatically saved. So, if there is a timer that runs every 10 seconds, I will pass the information on the screen to the ViewModel and let it deal with the logic of saving the data to the server.
A couple of options I have explored are.
Execute recurring code with a specified interval using Handler.
PeriodicWorkRequest -- however this option has a minimum interval of 15 minutes which is a little too much for my use case.
AlarmManager -- This option runs even if your application is not currently running, In my opinion, this option can be an overkill.
I wanted to know if there are best practices/blogs around this and if anyone I on the wrong path or potential red flags with this approach.
you can make countdown for 10 second, when countdown is down save the data and call the countdown again.
when your activity is destroyed, so stop the countdown

Feasibility of perfectly syncing number counter between app and server?

First time asking a question. I appreciate any thoughts/feedback you may have.
Part of the functionality of an app I am working on requires that an internal counter in the app be in perfect sync with a counter on a remote server.
More specifically, there are 86,400 seconds in a day. Assume on a server I start a counter that ticks from 1 to 86,400 progressing at one tick a second. When it gets to 86,400 it starts over at 1. This then repeats indefinitely.
Part of the functionality of the app is the first time it is launched by the user it will connect to the server and sync a similar running counter within the app to perfectly match the counter on the server. (e.g. at the exact same time that the server counter ticks to perhaps 10,215.....so should the app counter be ticking to 10,215.)
Assume perhaps thousands of users have the app. I need it so that the thousands of individual app users now have 1 - 86,400 counters all running exactly in sync with each other. With the idea that a user only has to have their app perform this server sync a single time. After it's synced, the counters within the apps would then run in unison in perpetuity.
Questions:
1) Is this feasible to do? For the reasons I need this to occur, it literally cannot be off by even a second or two. They all have to match perfectly.
2) Would varying internet and/or phone speed by user be a problem (during the sync process)? Again, it is a one time sync. It does not have to sync on a continuous, real-time basis. Nonetheless, the first sync does have to be perfect.
3) If the answer to the above is that it is doable.....getting greedy here, can I sync it at even faster speeds? Instead of the counter just counting up one tick per second (i.e. 86,400 ticks per day), can the counter tick at perhaps a rate of 1/10 of a second (864,000 ticks per day). At that super fast tick speed, could the perfect sync still be accomplished?
While not the exact reason for needing the above, to give perspective.....imagine I had 100 people sitting in a room download the app. They then open the app which then all perfectly sync to the server so that they are all now running their individual counters in perfect unison. At that point, they lose internet connectivity (yet, the counter continues to run within the app in their phone.) If the app was programmed to start playing a particular song when the internal app counter reached tick X, all 100 phones would start playing the song at the exact same time (when their counters in unison reached tick X).
Sorry for the long question, but wanted to lay out my issue. Thanks everyone!

Displaying time exceeded (Timer) on Android Studio using Firebase?

My question might be bit confusing, but what I basically want to do is,
I want to display the time exceeded for each item in the list when the start button on each item is clicked.
For example, suppose I have a listView of items that has a time and a button
TIME START
TIME START
TIME START
When I click the start button, the TIME textView should start a timer that increments every 1 minute and stops when it reaches a specific minute).
Since I am using the Firebase realtime database as my DB, I was thinking of incrementing 1 to the Time data every 1 minute.
What is a good approach of doing this?
You can update a value in Firebase Realtime Database either on client side or either on server side by using Cloud Functions for Firebase.
You can write a function that updates that particular value every 10 minutes. The benefit of using Cloud Functions rather then the first option is that will still work even if the application is offline. This is the best practice.

How to make your app do something even when you're not doing anything or even using it

This question is vague but I am not sure what to Google for exactly.
But in my app I have a section where you create a list of tasks with various attributes, and a lot of these numbers are summed up and displayed in daily totals visually. But these daily totals should reset every 24 hours (based on the hour of the day the user chooses for the cutoff, e.g. 3 am if someone works late).
Right now: my database can hold all the data by day. Then my daily counters will visually display the numbers by pulling the corresponding data from the database looking for the current day. That's the easy part.
The hard part: I can refresh the counter after the time cutoff if the user rotates the screen or restarts the app because then it'll be looking for items in the database with a new day that won't be found, so everything will be 0 as intended. But what if the user is just staring at the screen as the time cutoff rolls by? How do I make the app refresh the counters once the time hits? What if they're not even using the app at all (either it's minimized in the background or not even active).
Do I need to have some kind of always-running loop in the background that checks the current time against the values in the database? If so, isn't this inefficient if it's always needing to pull values from a database based on time? What's the correct practice for something like this?
You can setup a service and schedule that service to run periodically so that it does whatever job you want it to do
maybe this article can help you.
Alarm manager and services will be ideal for you to implement to do something for your requirement.
Services : It will be running all the time irrespective of your life-cycle of activity.
Alarm manager: Inside services use alarm manager to trigger event to update UI at regular interval.
Finally you can use Local braodcast reciever to update your Activity from services.
You can check implemetation in details below :
Android update activity UI from service

Restricting Users from Gaining Life when changing local device time & date cocos2dx Game

Am developing a cocos2dx game(IOS & android) in Which we provide the user Diamond for every 4 hours.
Since no Server is used,every data are saved locally in CCUserDefault.My problem is ,now the user
able to Gain Diamonds by setting future date & time in local device. I am fully dependent on the
device time , as i can't run my own timer when the game is not in memory .I am stuck here
eg : candy crush saga we can get life when changing date or time.
please guide me in the right direction . Suggestions are welcomed !!
Thanks
You are using an information on which you have no control whatsoever : 'time of day'. You dont control the clock, nor its accuracy, nor its precision. Instead of 'time of day', use 'play time' as your measuring stick. Store a minutesCounter in a secure place, and increment it every minute of active play. When minutesCounter reaches 240, you have a winner, award the goodies, and reset the counter to zero. In you game loop, setup a timer that does this whenever the app is running (not in background).

Categories

Resources