Created
February 8, 2022 10:12
-
-
Save JohannesLoot/88d6e23082179cf0fdfa2dbed897d3b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Re-write this function when we have an endpoint for time, not working with WebGL | |
| public UInt32 GetInternetTimeSeed() | |
| { | |
| // Make a request to a website | |
| HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://lootlocker.io"); | |
| // Save the response | |
| WebResponse response = myHttpWebRequest.GetResponse(); | |
| // Save today's date to a string | |
| string originalDate = response.Headers["date"]; | |
| string dateSeed = ""; | |
| // We now have a string that has the following format: | |
| // Day, XX Month 2022 Hour:Minute:Seconds Timezone | |
| // For example: Wed, 26 Jan 2022 08:57:56 GMT | |
| // We want to have a new word each day, so we do not need the clock or the timezone information | |
| // so let's first remove that from the string | |
| dateSeed = originalDate.Substring(0, originalDate.Length - 13); | |
| // Now we have the following: | |
| // Day, XX Month Year | |
| // What we want to do next is to remove the weekday since that is not needed, we do this by removing 5 characters; | |
| // the weekday in 3 letters, the ',' and the space | |
| dateSeed = dateSeed.Substring(5); | |
| // Now we have the following: | |
| // XX Month Year | |
| // This will give us a new value for each day, | |
| // and if you come back the same day, you will get the same result | |
| // Now we'll swap out the name of the month to a number from our list of months | |
| for (int i = 0; i < months.Length; i++) | |
| { | |
| if (dateSeed.Contains(months[i])) | |
| { | |
| // Replace the current month with the number of the month instead | |
| dateSeed = dateSeed.Replace(months[i], i.ToString()); | |
| break; | |
| } | |
| } | |
| // Now we have: | |
| // 26 Jan 2022 becomes -> 26 0 2022 | |
| // Now we will use regex to remove the whitespaces from this string | |
| dateSeed = Regex.Replace(dateSeed, @"\s", ""); | |
| // Then parse this to an int | |
| UInt32 seed = UInt32.Parse(dateSeed); | |
| // What we now have is only numbers, so for example: | |
| // Wed, 26 Jan 2022 becomes 2602022 | |
| // This is what we'll use for our seed, | |
| // this will give us a different number for every day for all days to come! | |
| // Output to the console: | |
| Debug.Log("Today's seed:" + seed.ToString()); | |
| return seed; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment