Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This app will create a virtual pet simulation game where users can adopt, care for, and interact with a digital creature.
// We'll use React for the frontend, SQLite for data persistence, and custom emoji combinations for visuals.
/** @jsxImportSource https://esm.sh/react */
import React, { useEffect, useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
const EMOTIONS = {
happy: "😊",
sad: "😒",
angry: "😠",
hungry: "🍽️",
sleepy: "😴",
};
const FOODS = ["🍎", "🍌", "πŸ₯•", "πŸ–", "🍣"];
function App() {
const [pet, setPet] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetchPet();
}, []);
const fetchPet = async () => {
const response = await fetch("/pet");
const data = await response.json();
setPet(data);
setLoading(false);
};
const interact = async (action) => {
const response = await fetch("/interact", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action }),
});
const data = await response.json();
setPet(data);
};
if (loading) return <div>Loading your pet...</div>;
if (!pet) return <div>Error loading pet</div>;
return (
<div className="container">
<h1>Virtual Pet Simulator</h1>
<div className="pet-container">
<div className="pet">
{pet.species === "cat" ? "🐱" : "🐢"}
{EMOTIONS[pet.emotion]}
</div>
<div>Name: {pet.name}</div>
<div>Health: {pet.health}</div>
<div>Happiness: {pet.happiness}</div>
<div>Last fed: {new Date(pet.lastFed).toLocaleString()}</div>
</div>
<div className="actions">
<button onClick={() => interact("pet")}>Pet</button>
<button onClick={() => interact("play")}>Play</button>
<button onClick={() => interact("sleep")}>Sleep</button>
<div className="food-options">
{FOODS.map((food, index) => (
<button key={index} onClick={() => interact(`feed_${index}`)}>
Feed {food}
</button>
))}
</div>
</div>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">
View Source
</a>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {
client();
}
async function server(request: Request): Promise<Response> {
const { sqlite } = await import("https://esm.town/v/stevekrouse/sqlite");
const SCHEMA_VERSION = 1;
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS ${KEY}_pets_${SCHEMA_VERSION} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
species TEXT NOT NULL,
health INTEGER NOT NULL,
happiness INTEGER NOT NULL,
emotion TEXT NOT NULL,
lastFed DATETIME NOT NULL
muhammad_owais_warsi-virtualpetsim.web.val.run
September 5, 2024