mirror of
https://github.com/MikuLeaks/KianaBH3.git
synced 2025-12-14 13:54:43 +01:00
Initial commit
This commit is contained in:
93
database/__init__.py
Normal file
93
database/__init__.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from __future__ import annotations
|
||||
import pymongo
|
||||
from database.create_db import MongoDBCreate
|
||||
from database.save_data import SaveData
|
||||
from game_server.resource import ResourceManager
|
||||
|
||||
|
||||
class MongoDBConnection:
|
||||
def __init__(self, host="localhost", port=27017, db_name="mikubh3"):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.db_name = db_name
|
||||
self.client = None
|
||||
self.db = None
|
||||
|
||||
def connect(self):
|
||||
try:
|
||||
ResourceManager.instance().load_resources()
|
||||
self.client = pymongo.MongoClient(f"mongodb://{self.host}:{self.port}/")
|
||||
if self.db_name:
|
||||
self.db = self.client[self.db_name]
|
||||
databases = self.client.list_database_names()
|
||||
if self.db_name not in databases:
|
||||
print("Database not found. Will create one")
|
||||
MongoDBCreate(self)
|
||||
print("Connected to MongoDB successfully!")
|
||||
else:
|
||||
print("No database selected.")
|
||||
except pymongo.errors.ConnectionFailure as e:
|
||||
print("Could not connect to MongoDB: %s" % e)
|
||||
|
||||
def close(self):
|
||||
if self.client:
|
||||
self.client.close()
|
||||
print("Connection to MongoDB closed.")
|
||||
|
||||
def get_collection(self, collection_name):
|
||||
if self.db is not None:
|
||||
return self.db[collection_name]
|
||||
else:
|
||||
print("No database selected. Connect to a database first.")
|
||||
return None
|
||||
|
||||
def insert_document(self, collection_name, document):
|
||||
collection = self.get_collection(collection_name)
|
||||
if collection is not None:
|
||||
try:
|
||||
result = collection.insert_one(document)
|
||||
print(f"Document inserted successfully with id:{result.inserted_id}")
|
||||
except Exception as e:
|
||||
print(f"Error inserting document:{e}")
|
||||
|
||||
def find_documents(self, collection_name, query={}):
|
||||
collection = self.get_collection(collection_name)
|
||||
if collection is not None:
|
||||
return collection.find(query)
|
||||
else:
|
||||
print("Collection not found.")
|
||||
return None
|
||||
|
||||
def find_documents_by_key_values(self, collection_name, key_values):
|
||||
collection = self.get_collection(collection_name)
|
||||
if collection is not None:
|
||||
query = {key: value for key, value in key_values.items()}
|
||||
return collection.find(query)
|
||||
else:
|
||||
print("Collection not found.")
|
||||
return None
|
||||
|
||||
def update_document(self, collection_name, filter_query, update_query):
|
||||
collection = self.get_collection(collection_name)
|
||||
if collection is not None:
|
||||
try:
|
||||
result = collection.update_one(filter_query, update_query)
|
||||
print(f"Document updated successfully:{result.modified_count}")
|
||||
except Exception as e:
|
||||
print(f"Error updating document:{e}")
|
||||
|
||||
def delete_document(self, collection_name, filter_query):
|
||||
collection = self.get_collection(collection_name)
|
||||
if collection is not None:
|
||||
try:
|
||||
result = collection.delete_one(filter_query)
|
||||
print(f"Document deleted successfully:{result.deleted_count} document(s) deleted.")
|
||||
except Exception as e:
|
||||
print(f"Error deleting document:{e}")
|
||||
|
||||
def save(self,session,data_type,ids=[0]):
|
||||
save_data = SaveData(self,session,data_type,ids)
|
||||
save_data.save()
|
||||
|
||||
mongo = MongoDBConnection()
|
||||
mongo.connect()
|
||||
169
database/create_db.py
Normal file
169
database/create_db.py
Normal file
@@ -0,0 +1,169 @@
|
||||
import time
|
||||
import json
|
||||
import random
|
||||
from game_server.resource import ResourceManager
|
||||
from game_server.resource.configdb.avatar_data import AvatarData
|
||||
from game_server.resource.configdb.weapon_data import WeaponData
|
||||
from game_server.resource.configdb.stigmata_data import StigmataData
|
||||
from game_server.resource.configdb.material_data import MaterialData
|
||||
from game_server.resource.configdb.elf_astra_mate_data import ElfAstraMateData
|
||||
from game_server.resource.configdb.dress_data import DressData
|
||||
from game_server.game.enum.item_type import MainType
|
||||
|
||||
class MongoDBCreate:
|
||||
def __init__(self, mongo):
|
||||
self.mongo = mongo
|
||||
self.manager = ResourceManager.instance()
|
||||
self.create_db()
|
||||
self.avatars()
|
||||
self.items()
|
||||
self.elfs()
|
||||
|
||||
def create_db(self):
|
||||
player_data = {
|
||||
"UID": 1337,
|
||||
"Name": "Miku",
|
||||
"Level": 88,
|
||||
"Exp": 0,
|
||||
"HCoin": 1337,
|
||||
"Stamina": 80,
|
||||
"Sign": "MikuPS",
|
||||
"HeadPhoto": 161090,
|
||||
"HeadFrame": 200001,
|
||||
"WarshipId": 400004,
|
||||
"AssistantAvatarId": 101,
|
||||
"WarshipAvatar": {
|
||||
"WarshipFirstAvatarId": 101,
|
||||
"WarshipSecondAvatarId": 0
|
||||
},
|
||||
"BirthDate": 0,
|
||||
"CustomAvatarTeamList":{
|
||||
f"{i}":{
|
||||
"TeamId":i,
|
||||
"Name": f"Team {i}",
|
||||
"astraMateId":0,
|
||||
"isUsingAstraMate":False,
|
||||
"elfIdList":[],
|
||||
"AvatarIdLists":[]
|
||||
}
|
||||
for i in range(1,11)
|
||||
}
|
||||
}
|
||||
self.mongo.insert_document("players",player_data)
|
||||
|
||||
def avatars(self):
|
||||
data = []
|
||||
for avatar in self.manager.instance().values(AvatarData):
|
||||
valk = {
|
||||
"AvatarID":avatar.avatarID,
|
||||
"Star":avatar.unlockStar,
|
||||
"Level":80,
|
||||
"Exp":0,
|
||||
"Fragment":0,
|
||||
"TouchGoodFeel":0,
|
||||
"TodayHasAddGoodFeel":0,
|
||||
"DressID":avatar.DefaultDressId,
|
||||
"DressLists":[
|
||||
dress.dressID
|
||||
for dress in self.manager.instance().values(DressData)
|
||||
if avatar.avatarID in dress.avatarIDList
|
||||
],
|
||||
"AvatarArtifact":None,
|
||||
"SubStar":0,
|
||||
"SkillLists": {
|
||||
f"{skillId}": {
|
||||
"SkillId": skillId,
|
||||
"SubSkillLists": {}
|
||||
}
|
||||
for skillId in avatar.skillList
|
||||
},
|
||||
"CreateTime":int(time.time())
|
||||
}
|
||||
data.append(valk)
|
||||
self.mongo.get_collection("avatars").insert_many(data)
|
||||
|
||||
def items(self):
|
||||
last_item = self.mongo.get_collection("items").find_one(sort=[("UniqueID", -1)])
|
||||
unique_id = last_item["UniqueID"] + 1 if last_item else 1
|
||||
|
||||
items_data = []
|
||||
|
||||
for weapon in self.manager.instance().values(WeaponData):
|
||||
if weapon.rarity == weapon.maxRarity:
|
||||
weapon_data = {
|
||||
"UniqueID":unique_id,
|
||||
"ItemID":weapon.ID,
|
||||
"Level":weapon.maxLv,
|
||||
"Exp":0,
|
||||
"IsLocked":False,
|
||||
"IsExtracted":False,
|
||||
"QuantumBranchLists":None,
|
||||
"MainType":MainType.WEAPON.value,
|
||||
"EquipAvatarID":0
|
||||
}
|
||||
items_data.append(weapon_data)
|
||||
unique_id += 1
|
||||
|
||||
for stigmata in self.manager.instance().values(StigmataData):
|
||||
if stigmata.rarity == stigmata.maxRarity:
|
||||
stigmata_data = {
|
||||
"UniqueID":unique_id,
|
||||
"ItemID":stigmata.ID,
|
||||
"Level":stigmata.maxLv,
|
||||
"Exp":0,
|
||||
"SlotNum":0,
|
||||
"RefineValue":0,
|
||||
"PromoteTimes":0,
|
||||
"IsLocked":False,
|
||||
"RuneLists":[],
|
||||
"WaitSelectRuneLists":[],
|
||||
"WaitSelectRuneGroupLists":[],
|
||||
"MainType":MainType.STIGMATA.value,
|
||||
"EquipAvatarID":0
|
||||
}
|
||||
items_data.append(stigmata_data)
|
||||
unique_id += 1
|
||||
|
||||
for material in self.manager.instance().values(MaterialData):
|
||||
material = {
|
||||
"ItemID":material.ID,
|
||||
"ItemNum":99999999 if material.ID == 100 else (999 if material.quantityLimit > 999 else material.quantityLimit),
|
||||
"MainType":MainType.MATERIAL.value,
|
||||
}
|
||||
items_data.append(material)
|
||||
|
||||
for avatar in self.manager.instance().values(AvatarData):
|
||||
avatar_data = {
|
||||
"UniqueID":unique_id,
|
||||
"ItemID":avatar.initialWeapon,
|
||||
"Level":15,
|
||||
"Exp":0,
|
||||
"IsLocked":False,
|
||||
"IsExtracted":False,
|
||||
"QuantumBranchLists":None,
|
||||
"MainType":MainType.WEAPON.value,
|
||||
"EquipAvatarID":avatar.avatarID
|
||||
}
|
||||
items_data.append(avatar_data)
|
||||
unique_id += 1
|
||||
|
||||
self.mongo.get_collection("items").insert_many(items_data)
|
||||
|
||||
def elfs(self):
|
||||
elfs_data = []
|
||||
for elf in self.manager.instance().values(ElfAstraMateData):
|
||||
elf_data = {
|
||||
"ElfId":elf.ElfID,
|
||||
"Level":elf.MaxLevel,
|
||||
"Star":elf.MaxRarity,
|
||||
"Exp":0,
|
||||
"SkillLists":{
|
||||
f"{skill.ElfSkillID}":{
|
||||
"SkillId":skill.ElfSkillID,
|
||||
"Level":skill.MaxLv
|
||||
}
|
||||
for skill in elf.skill_lists
|
||||
}
|
||||
}
|
||||
elfs_data.append(elf_data)
|
||||
self.mongo.get_collection("elfs").insert_many(elfs_data)
|
||||
130
database/save_data.py
Normal file
130
database/save_data.py
Normal file
@@ -0,0 +1,130 @@
|
||||
from game_server.game.enum.data_type import DataType
|
||||
|
||||
class SaveData:
|
||||
def __init__(self, mongo, session, data_type:DataType, ids: list):
|
||||
self.mongo = mongo
|
||||
self.session = session
|
||||
self.data_type = data_type
|
||||
self.ids = ids
|
||||
|
||||
def save(self):
|
||||
data_type_handlers = {
|
||||
DataType.MATERIAL: self._save_material,
|
||||
DataType.WEAPON: self._save_weapon,
|
||||
DataType.STIGMATA: self._save_stigmata,
|
||||
DataType.AVATAR: self._save_avatar,
|
||||
DataType.PLAYER: self._save_player
|
||||
}
|
||||
handler = data_type_handlers.get(self.data_type)
|
||||
if handler:
|
||||
handler()
|
||||
else:
|
||||
raise ValueError(f"Unsupported data type: {self.data_type}")
|
||||
|
||||
def _save_material(self):
|
||||
for id in self.ids:
|
||||
get_item = self.session.player.inventory.material_items.get(id)
|
||||
if get_item:
|
||||
filter = {"ItemID": get_item.item_id}
|
||||
update = {"$set": {"Num": get_item.num}}
|
||||
self.mongo.update_document("items",filter,update)
|
||||
|
||||
def _save_weapon(self):
|
||||
for unique_id in self.ids:
|
||||
if id == 0:
|
||||
continue
|
||||
get_item = self.session.player.inventory.weapon_items.get(unique_id)
|
||||
if get_item:
|
||||
filter = {"UniqueID": unique_id}
|
||||
update = {
|
||||
"$set":
|
||||
{
|
||||
"Level": get_item.level,
|
||||
"Exp": get_item.exp,
|
||||
"IsLocked" : get_item.is_locked,
|
||||
"IsExtracted" : get_item.is_extracted,
|
||||
"EquipAvatarID" : get_item.equip_avatar_id
|
||||
}
|
||||
}
|
||||
self.mongo.update_document("items",filter,update)
|
||||
def _save_stigmata(self):
|
||||
for unique_id in self.ids:
|
||||
if id == 0:
|
||||
continue
|
||||
get_item = self.session.player.inventory.stigmata_items.get(unique_id)
|
||||
if get_item:
|
||||
filter = {"UniqueID": unique_id}
|
||||
update = {
|
||||
"$set":
|
||||
{
|
||||
"Level": get_item.level,
|
||||
"Exp": get_item.exp,
|
||||
"SlotNum": get_item.slot_num,
|
||||
"IsLocked" : get_item.is_locked,
|
||||
"EquipAvatarID" : get_item.equip_avatar_id
|
||||
}
|
||||
}
|
||||
self.mongo.update_document("items",filter,update)
|
||||
|
||||
|
||||
def _save_avatar(self):
|
||||
for id in self.ids:
|
||||
avatar = self.session.player.avatars.get(id)
|
||||
if avatar:
|
||||
filter = {"AvatarID": id}
|
||||
update = {
|
||||
"$set":
|
||||
{
|
||||
"Star": avatar.star,
|
||||
"Fragment": avatar.fragment,
|
||||
"TouchGoodFeel" : avatar.touch_good_feel,
|
||||
"TodayHasAddGoodFeel" : avatar.today_has_add_good_feel,
|
||||
"DressID" : avatar.dress_id,
|
||||
"SkillLists" : {
|
||||
f"{skill_id}":{
|
||||
"SkillId":skill.skill_id,
|
||||
"SubSkillLists":{
|
||||
f"{sub_skill.sub_skill_id}":{
|
||||
"subSkillId":sub_skill.sub_skill_id,
|
||||
"level":sub_skill.level
|
||||
}
|
||||
for sub_id, sub_skill in skill.sub_skill_lists.items()
|
||||
}
|
||||
}
|
||||
for skill_id,skill in avatar.skill_lists.items()
|
||||
},
|
||||
}
|
||||
}
|
||||
self.mongo.update_document("avatars",filter,update)
|
||||
|
||||
def _save_player(self):
|
||||
filter = {"UID": self.session.player.uid}
|
||||
update = {
|
||||
"$set":
|
||||
{
|
||||
"Name": self.session.player.name,
|
||||
"HCoin": self.session.player.hcoin,
|
||||
"Sign": self.session.player.signature,
|
||||
"HeadPhoto": self.session.player.head_photo,
|
||||
"HeadFrame": self.session.player.head_frame,
|
||||
"WarshipId": self.session.player.warship_id,
|
||||
"AssistantAvatarId": self.session.player.assistant_avatar_id,
|
||||
"WarshipAvatar":{
|
||||
"WarshipFirstAvatarId": self.session.player.warship_avatar.warship_first_avatar_id,
|
||||
"WarshipSecondAvatarId": self.session.player.warship_avatar.warship_second_avatar_id
|
||||
},
|
||||
"BirthDate":self.session.player.birth_date,
|
||||
"CustomAvatarTeamList":{
|
||||
f"{team_id}":{
|
||||
"TeamId":team_id,
|
||||
"Name": team.name,
|
||||
"astraMateId":team.astral_mate_id,
|
||||
"isUsingAstraMate":team.is_using_astra_mate,
|
||||
"elfIdList":team.elf_id_list,
|
||||
"AvatarIdLists":team.avatar_id_list
|
||||
}
|
||||
for team_id,team in self.session.player.custom_avatar_team_list.items()
|
||||
}
|
||||
}
|
||||
}
|
||||
self.mongo.update_document("players",filter,update)
|
||||
Reference in New Issue
Block a user