persists pet
This commit is contained in:
@@ -46,3 +46,31 @@ async def comfy_pets_update_inventory(request):
|
||||
print("Error:", e)
|
||||
return web.json_response({ "error": str(e) }, status=400)
|
||||
|
||||
|
||||
@server.PromptServer.instance.routes.get("/comfy-pets/pets")
|
||||
async def comfy_pets_get_pet(request):
|
||||
try:
|
||||
data = persistence.get_current_pet()
|
||||
return web.json_response({
|
||||
"pet": data
|
||||
}, content_type='application/json')
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
return web.json_response({ "error": e }, status=400)
|
||||
|
||||
@server.PromptServer.instance.routes.post("/comfy-pets/pets/age")
|
||||
async def comfy_pets_pets_age(request):
|
||||
try:
|
||||
data = await request.json()
|
||||
age = data.get("age")
|
||||
|
||||
persistence.update_pet_age(age)
|
||||
|
||||
return web.json_response({
|
||||
"message": "Pet age updated successfully"
|
||||
}, content_type='application/json')
|
||||
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
return web.json_response({ "error": str(e) }, status=400)
|
||||
|
||||
|
||||
@@ -74,6 +74,35 @@ export const setUserNewInventory = async (inventory) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const setPetAge = async (age) => {
|
||||
try {
|
||||
const url = '/comfy-pets/pets/age'
|
||||
const data = {
|
||||
age,
|
||||
}
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data), // Convert the data to JSON format
|
||||
}
|
||||
await fetch(url, requestOptions).then((x) => x.json())
|
||||
} catch (e) {
|
||||
throw new Error('Something went wrong')
|
||||
}
|
||||
}
|
||||
|
||||
export const getCurrentPet = async () => {
|
||||
const url = '/comfy-pets/pets'
|
||||
const { pet } = await fetchWithCache(url)
|
||||
let parsedUserData = {
|
||||
...pet,
|
||||
['age']: parseInt(pet.age),
|
||||
}
|
||||
return parsedUserData
|
||||
}
|
||||
|
||||
export async function ping() {
|
||||
const user = await getCurrentUser()
|
||||
const userId = user?.user_id
|
||||
|
||||
+16
-1
@@ -1,5 +1,6 @@
|
||||
//import { GIF } from "../libs/gif.js";
|
||||
import { GameObject } from './core.js'
|
||||
import { getCurrentPet, setPetAge } from '../apiClient.js'
|
||||
|
||||
const BABY_CORGI =
|
||||
'https://comfyui-output.nyc3.cdn.digitaloceanspaces.com/babycorgi-sprite-128x128.png'
|
||||
@@ -84,6 +85,15 @@ export class Pet extends GameObject {
|
||||
'https://comfyui-output.nyc3.cdn.digitaloceanspaces.com/text-bubble.png'
|
||||
|
||||
this.age = 0
|
||||
this._initializePet()
|
||||
}
|
||||
|
||||
async _initializePet() {
|
||||
const petData = await getCurrentPet()
|
||||
|
||||
for (const key in petData) {
|
||||
this[key] = petData[key]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,6 +128,11 @@ export class Pet extends GameObject {
|
||||
})
|
||||
}
|
||||
|
||||
grow() {
|
||||
this.age++
|
||||
setPetAge(this.age)
|
||||
}
|
||||
|
||||
_chooseRandomDirection() {
|
||||
const directions = ['left', 'right', 'idle1', 'idle2']
|
||||
|
||||
@@ -260,7 +275,7 @@ export class Pet extends GameObject {
|
||||
|
||||
// see if objects interact
|
||||
if (this.isTouching(nearestFood)) {
|
||||
this.age++
|
||||
this.grow()
|
||||
|
||||
// Eat food
|
||||
nearestFood.delete()
|
||||
|
||||
@@ -6,6 +6,7 @@ from datetime import datetime
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
data_path = os.path.join(current_dir, ".dat")
|
||||
u_path = os.path.join(data_path, "u")
|
||||
p_path = os.path.join(data_path, "p")
|
||||
|
||||
def set_user_id():
|
||||
if not os.path.exists(data_path):
|
||||
@@ -50,3 +51,23 @@ def update_balance(balance):
|
||||
def update_inventory(inventory):
|
||||
with dbm.open(u_path, 'w') as db:
|
||||
db[b'inventory'] = str(inventory).encode('utf-8')
|
||||
|
||||
def update_pet_age(age):
|
||||
with dbm.open(p_path, 'w') as db:
|
||||
db[b'age'] = str(age).encode('utf-8')
|
||||
|
||||
def get_current_pet():
|
||||
pet = {}
|
||||
if not os.path.exists(data_path):
|
||||
os.makedirs(data_path)
|
||||
with dbm.open(p_path, 'c') as db:
|
||||
# Check if user exists
|
||||
if b'age' not in db:
|
||||
db[b'age'] = str(0).encode('utf-8')
|
||||
|
||||
# return data
|
||||
for key in db.keys():
|
||||
pet[key.decode('utf-8')] = db[key].decode('utf-8')
|
||||
|
||||
return pet
|
||||
|
||||
|
||||
Reference in New Issue
Block a user