Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding generator script to make dynamic maps #28

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ demo_planet.html
demo_edsm.html
js/ed3dplanet.js
json_samples/eddb.json
json_samples/edsm.json
json_samples/edsm.json
json_samples/factions.json
.vscode
94 changes: 94 additions & 0 deletions demo_factions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>Milky Way</title>
<link href="http://fonts.googleapis.com/css?family=Orbitron:400" rel="stylesheet" type="text/css" />
<!-- Custom CSS -->
<style type="text/css">
#systemDetails,
#hud {
width: 450px;
}

h1 {
position: fixed;
top: 2%;
left: 0;
width: 100%;
font-size: 1.3rem;
text-align: center;
color: #ccc;
font-family: "Orbitron";
}

h1 a {
color: inherit;
}

#edmap {
position: fixed;
top: 10%;
right: 0;
width: 100%;
height: 80%;
}

#debug {
position: fixed;
bottom: 5%;
right: 0;
width: 100%;
text-align: center;
color: #aaa;
font-family: "Helvetica";
font-size: 0.8rem;
}
</style>

</head>

<body>

<h1>Milky Way</h1>

<div id="edmap"></div>

<div id="debug"></div>



<!-- jQuery -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<!-- Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r75/three.min.js"></script>

<!-- Launch ED3Dmap -->
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script src="js/ed3dmap.js"></script>
<script type="text/javascript">

var d = new Date()
d.setDate(d.getDate() - 1);
var queryDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':00:00';

Ed3d.init({
container: 'edmap',
jsonPath: "json_samples/factions.json",
withHudPanel: true,
withOptionsPanel: true,
hudMultipleSelect: true,
effectScaleSystem: [10, 800],
startAnim: true,
showGalaxyInfos: true,
systemColor: '#FF0000'
});

</script>

</body>

</html>
100 changes: 100 additions & 0 deletions json_samples/generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import sys
import time
import json
import aiohttp
import asyncio
import aiofiles

# Lock for file writing
file_lock = asyncio.Lock()

async def clear_json(filename='json_samples/factions.json'):
async with aiofiles.open(filename, 'w') as file:
Government = {
"1": {
"name": "Communist Systems",
"color": "CC0000"
},
"2": {
"name": "Federal Systems",
"color": "0000EF"
}
}
file_data = {"categories": {"Government": Government}, "systems": []}
await file.write(json.dumps(file_data, indent=4))

async def write_json(new_data, filename='json_samples/factions.json'):
async with file_lock:
async with aiofiles.open(filename, 'r+') as file:
file_data = json.loads(await file.read())
file_data["systems"].append(new_data)
await file.seek(0)
await file.write(json.dumps(file_data, indent=4))

async def getEDBGSData(session, url, pageNumber):
async with session.get(url + str(pageNumber)) as response:
if response.status == 200:
data = await response.json()
rawSystemData = data['docs']
for rawSystemDataArray in rawSystemData:
systemName = rawSystemDataArray['name']
coords = {
"x": rawSystemDataArray['x'],
"y": rawSystemDataArray['y'],
"z": rawSystemDataArray['z']
}
government = rawSystemDataArray['government']
allegiance = rawSystemDataArray['allegiance']
if government == "$government_communism;":
cat = [1]
elif allegiance == "federation":
cat = [2]
else:
continue

systemDict = {"name": systemName, "coords": coords, "cat": cat}
await write_json(systemDict)
if data['hasNextPage']:
nextPage = data['nextPage']
pageNumber += 1
await getEDBGSData(session, url, pageNumber)
return True
else:
return False

async def process_allegiance(url, allegiance):
print(f"{allegiance} SYSTEMS STARTED AT {time.strftime('%X')}")
start_time = time.time()

async with aiohttp.ClientSession() as session:
page_number = 1
await getEDBGSData(session, url, page_number)

elapsed_time = time.time() - start_time
minutes, seconds = divmod(elapsed_time, 60)
print(f"{allegiance} SYSTEMS COMPLETED AT {time.strftime('%X')} (Duration: {int(minutes):02d} minutes {seconds:.2f} seconds)", flush=True)

async def main():
await clear_json()
sys.setrecursionlimit(3000)

allegiance_urls = [
("https://elitebgs.app/api/ebgs/v5/systems?allegiance=empire&page=", "IMPERIAL"),
("https://elitebgs.app/api/ebgs/v5/systems?allegiance=federation&page=", "FEDERAL"),
("https://elitebgs.app/api/ebgs/v5/systems?allegiance=alliance&page=", "ALLIED"),
("https://elitebgs.app/api/ebgs/v5/systems?allegiance=independent&page=", "INDEPENDENT"),
]

total_start_time = time.time() # Record the start time for total duration

await asyncio.gather(*(process_allegiance(url, allegiance) for url, allegiance in allegiance_urls))

total_elapsed_time = time.time() - total_start_time
total_minutes, total_seconds = divmod(total_elapsed_time, 60)
print(f"TOTAL DURATION: {int(total_minutes):02d} minutes {total_seconds:.2f} seconds")

sys.setrecursionlimit(1000)

if __name__ == "__main__":
asyncio.run(main())
174 changes: 87 additions & 87 deletions json_samples/routes.json
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
{
"systems": [
{
"name": "Checkpoint 1",
"coords": {
"x": "-6644",
"y": "-21",
"z": "11738"
}
},
{
"name": "Checkpoint 2",
"coords": {
"x": "-10534",
"y": "-21",
"z": "16384"
}
},
{
"name": "Checkpoint 3",
"coords": {
"x": "-4969",
"y": "-21",
"z": "22262"
}
},
{
"name": "Checkpoint 4",
"coords": {
"x": "3553",
"y": "-21",
"z": "8732"
}
},
{
"name": "Checkpoint 5",
"coords": {
"x": "6297",
"y": "-21",
"z": "14626"
}
},
{
"name": "Checkpoint 6",
"coords": {
"x": "18975",
"y": "-21",
"z": "25676"
}
},
{
"name": "Sagittarius A*",
"coords": {
"x": "25",
"y": "-21",
"z": "25900"
}
}
],
"routes": [
{
"title":"1st test route",
"points": [
{
"s":"Test",
"label":"My home",
"coords": {
"x": "25",
"y": "-2100",
"z": "-3900"
}
},
{"s":"Checkpoint 1"},
{"s":"Checkpoint 2","label":"Great discovery here"},
{"s":"Checkpoint 3"},
{"s":"Sagittarius A*","label":"Center of the galaxy"}
]
},
{
"title":"2nd test route",
"points": [
{"s":"Checkpoint 4"},
{"s":"Checkpoint 5"},
{"s":"Checkpoint 6"}
]
}
]
{
"systems": [
{
"name": "Checkpoint 1",
"coords": {
"x": "-6644",
"y": "-21",
"z": "11738"
}
},
{
"name": "Checkpoint 2",
"coords": {
"x": "-10534",
"y": "-21",
"z": "16384"
}
},
{
"name": "Checkpoint 3",
"coords": {
"x": "-4969",
"y": "-21",
"z": "22262"
}
},
{
"name": "Checkpoint 4",
"coords": {
"x": "3553",
"y": "-21",
"z": "8732"
}
},
{
"name": "Checkpoint 5",
"coords": {
"x": "6297",
"y": "-21",
"z": "14626"
}
},
{
"name": "Checkpoint 6",
"coords": {
"x": "18975",
"y": "-21",
"z": "25676"
}
},
{
"name": "Sagittarius A*",
"coords": {
"x": "25",
"y": "-21",
"z": "25900"
}
}
],
"routes": [
{
"title":"1st test route",
"points": [
{
"s":"Test",
"label":"My home",
"coords": {
"x": "25",
"y": "-2100",
"z": "-3900"
}
},
{"s":"Checkpoint 1"},
{"s":"Checkpoint 2","label":"Great discovery here"},
{"s":"Checkpoint 3"},
{"s":"Sagittarius A*","label":"Center of the galaxy"}
]
},
{
"title":"2nd test route",
"points": [
{"s":"Checkpoint 4"},
{"s":"Checkpoint 5"},
{"s":"Checkpoint 6"}
]
}
]
}