Jump to content

Daily Recent function


DerekBogie

Recommended Posts

You guys have helped me tremendously and I have one more should be simply task.

 

My game will display 6 treasure chests around the map.. When you open a treasure chest It will add 1 to the localStorage.chest variable to keep track of how many treasure chests you have opened. after you open 10 total chests the chests will all disapear until the 4 hour timer resets the variable. This way they cant refresh the page numerous times and keep getting treasure chests. I curently have this set up to keep track of the hours

	d = new Date();    playerChestTimer = d.getHours();

Can you help me to write an automatic function that will reset the localStorage.chests variable back to 0 every 4 hours real time? I figured if i write,

if (playerChestTimer == 5) {localStorage.chests = 0;}

then the variable will constantly reset at that specified hour until the next hour. My other main concern is the variable not resetting if the player isnt on the game. I figured that this method will only reset that variable if the player is actively on the game at that specified time.

Link to comment
Share on other sites

What you should store in the localstorage variable is the current date when they open a treasure chest.

localStorage.lastOpened = new Date();

When they try to open another chest, compare the current time with the time that the last treasure chest was opened. If it's been 4 hours or more, then allow the treasure chest to open.

// 14400000 milliseconds is 4 hours (1000ms * 60s * 60m * 4h)if((new Date()).getTime() - localStorage.lastOpened.getTime() >= 14400000) {  // Open treasure chest}
Link to comment
Share on other sites

Ive been working with this for roughly an hour now and it works great except i can only get it to work with one chest. I have to be able to make this system work with a totla of 10 chests. How can i restructure it to do so? basically i am trying to work it like this. I put the,

localStorage.lastOpened = new Date();

into the open treasure chest function. Also with this I add 1 to the localStorage.chests variable. I used your code like this

if((new Date()).getTime() - localStorage.lastOpened.getTime() >= 14400000) {localStorage.chests = 0;}

im using that on the body Onload function along with

function onLoadFunction() {		 if((new Date()).getTime() - localStorage.lastOpened.getTime() >= 14400000) {		 localStorage.chests = 0;	 }	if (localStorage.chests <= 9) {		var tempArea = Math.floor((Math.random() * 6) + 1);		if (tempArea == 1) {		document.getElementById("chestSetOne").style.zIndex = "10";		}		else if (tempArea == 2) {		document.getElementById("chestSetTwo").style.zIndex = "10";		}		else if (tempArea == 3) {		document.getElementById("chestSeThree").style.zIndex = "10";		}		else if (tempArea == 4) {		document.getElementById("chestSetFour").style.zIndex = "10";		}		else if (tempArea == 5) {		document.getElementById("chestSetFive").style.zIndex = "10";		}		else if (tempArea == 6) {		document.getElementById("chestSetSix").style.zIndex = "10";		}	}	else {		document.getElementById("chestSetOne").style.zIndex = "-50";		document.getElementById("chestSetTwo").style.zIndex = "-50";		document.getElementById("chestSetThree").style.zIndex = "-50";		document.getElementById("chestSetFour").style.zIndex = "-50";		document.getElementById("chestSetFive").style.zIndex = "-50";		document.getElementById("chestSetSix").style.zIndex = "-50";	}}

How can i make it fit together

Edited by DerekBogie
Link to comment
Share on other sites

I figured it out using your method.. it seems to do the job as needed. Basically here it is in a nutshell

When you open a chest it checks this in the function;

 d = new Date(); playerChestTimer = d.getHours();	if (playerChestTimer >= 19) {     localStorage.lastOpened = 0;     }else {     localStorage.lastOpened = playerChestTimer;     } 

So if the live Hourly Timer get over the 19th hour it will simply make the lastOpened variable to 0.

 

Then the onLoad function for the page checked both variables in this format

if (playerChestTimer >= (localStorage.lastOpened + 4) && playerChestTimer <= 19) {	localStorage.chests = 0;	 }

It seemed to work on the mockup really well and we will just have to see how it works tomorrow. Ingolme your idea worked beautifully with only one chest but with this i can have multiple chests on the same function. Thank you for your help it made it possible.

Edited by DerekBogie
Link to comment
Share on other sites

I would not use getHours. Let's say they start at 11:00pm, and then at 12:00 am they come back. Only one hour has passed, but your object is now comparing 23 to 0. 0 is less than 23, so it's going to allow the user to open the chests again.

 

In my example I used getTime, which returns a timestamp referring to the exact date that something occurred. Future dates always have a higher value, so you can know for sure when a date is further ahead in time than another.

Link to comment
Share on other sites

I understand exactly what you mean, thank you. I was able to recode it to getHours and used it as a localStorage variable to keep track. Due to my game running two direct Iframes to play the game that was the only way that it would work to update the other iframe.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...