Add ship display

This commit is contained in:
Aelita4 2024-04-15 13:46:34 +02:00
parent 5bb3940088
commit 24ed526cb6
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
6 changed files with 239 additions and 0 deletions

57
src/lib/data/ships.json Normal file
View File

@ -0,0 +1,57 @@
[
{
"id": "transporter",
"capacity": {
"solid": 10000,
"liquid": 10000,
"gas": 10000
},
"requirements": {
"buildings": [
{
"id": "shipyard",
"level": 1
}
],
"research": [],
"resources": {
"iron": 1000,
"gold": 500
}
},
"time": 40,
"structure": {
"hitpoints": 1000,
"defense": 10,
"attack": 0
},
"speed": 10
}, {
"id": "fighter",
"capacity": {
"solid": 100,
"liquid": 100,
"gas": 100
},
"requirements": {
"buildings": [
{
"id": "shipyard",
"level": 1
}
],
"research": [],
"resources": {
"iron": 500,
"gold": 200
}
},
"time": 15,
"structure": {
"hitpoints": 800,
"defense": 20,
"attack": 20
},
"speed": 12
}
]

View File

@ -7,6 +7,7 @@ import { ObjectId } from 'mongodb';
import { hash } from 'argon2' import { hash } from 'argon2'
import { createInitialResources } from '../utils/resourceManager'; import { createInitialResources } from '../utils/resourceManager';
import type Research from '../../types/Research'; import type Research from '../../types/Research';
import type Ship from '../../types/Ship';
export const getAllUsers = async () => { export const getAllUsers = async () => {
const users = await Users(); const users = await Users();
@ -118,3 +119,30 @@ export const createOrUpgradeResearch = async (username: string, research: Resear
); );
} }
} }
export const getUserShips = async (user: User): Promise<Array<Ship>> => {
const ships: Array<Ship> = [];
if (user?.ships !== undefined) {
user?.ships.forEach((ship: Ship) => {
ships.push(ship);
});
}
return ships;
}
export const createOrUpgradeShip = async (user: User, ship: Ship) => {
const users = await Users();
const result = await users.updateOne(
{ username: user.username, "ships.id": ship.id },
{ $set: { "ships.$.amount": ship.amount } }
);
if (result.modifiedCount === 0) {
await users.updateOne({ username: user.username },
{ $push: { ships: ship } }
);
}
}

View File

@ -0,0 +1,12 @@
{
"Label": {
"transporter": {
"name": "Transporter",
"description": "Transporter can move resources between planets."
},
"fighter": {
"name": "Fighter",
"description": "Fighter can attack enemy ships and planets."
}
}
}

136
src/pages/game/ships.astro Normal file
View File

@ -0,0 +1,136 @@
---
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
import ships from '../../lib/data/ships.json';
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
const username = Astro.cookies.get('username')?.value ?? "";
if(loggedToken === null || username === "") return Astro.redirect('/logout');
const checkUser = await getUserByAccessToken(loggedToken);
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const langShips = await getLocales(locale, 'ships');
const playerShips = checkUser.ships;
console.log(playerShips)
---
<Layout title="Ships">
<NavBar loggedIn="true" active="ships" />
<ResourceBar />
{ships.map(ship => (
<div class="ship-card">
<div class="ship-title">
<b>({playerShips.find((s => s.id === ship.id))?.amount ?? 0})</b> <i>{ship.id}</i>
</div>
</div>
))}
</Layout>
<style>
* {
color: white;
}
main {
margin: auto;
padding: 1rem;
width: 800px;
max-width: calc(100% - 2rem);
color: white;
font-size: 20px;
line-height: 1.6;
}
.astro-a {
position: absolute;
top: -32px;
left: 50%;
transform: translatex(-50%);
width: 220px;
height: auto;
z-index: -1;
}
h3 {
font-size: 2rem;
font-weight: 700;
line-height: 1;
text-align: center;
margin-bottom: 1em;
}
.text-gradient {
background-image: var(--accent-gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 400%;
background-position: 0%;
}
.instructions {
margin-bottom: 2rem;
border: 1px solid rgba(var(--accent-light), 25%);
background: linear-gradient(rgba(var(--accent-dark), 66%), rgba(var(--accent-dark), 33%));
padding: 1.5rem;
border-radius: 8px;
}
.instructions code {
font-size: 0.8em;
font-weight: bold;
background: rgba(var(--accent-light), 12%);
color: rgb(var(--accent-light));
border-radius: 4px;
padding: 0.3em 0.4em;
}
.instructions strong {
color: rgb(var(--accent-light));
}
.link-card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(24ch, 1fr));
gap: 2rem;
padding: 0;
}
.a-button {
text-decoration: none;
color: green;
height: fit-content;
}
.a-button:hover {
color: lime;
}
</style>
<script>
const allButtons = document.getElementsByClassName("a-button");
for(const researchButton of allButtons) {
researchButton.addEventListener("click", async () => {
const id = researchButton.id.split("_")[1];
console.log(id);
await fetch('/api/research/performResearch', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
research: id
})
});
});
}
</script>

4
src/types/Ship.ts Normal file
View File

@ -0,0 +1,4 @@
export default interface Ship {
id: string;
amount: number;
}

View File

@ -2,6 +2,7 @@ import type { ObjectId } from "mongodb";
import type Building from "./Building"; import type Building from "./Building";
import type DBResource from "./DBResource"; import type DBResource from "./DBResource";
import type Research from "./Research"; import type Research from "./Research";
import type Ship from "./Ship";
export default interface User { export default interface User {
_id: ObjectId; _id: ObjectId;
@ -12,6 +13,7 @@ export default interface User {
resources: Array<DBResource>; resources: Array<DBResource>;
buildings: Array<Building>; buildings: Array<Building>;
research: Array<Research>; research: Array<Research>;
ships: Array<Ship>
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
} }