init
This commit is contained in:
39
database/account/account_data.py
Normal file
39
database/account/account_data.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from database.mongodb import get_collection
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
from utils.crypto import generate_combo_token
|
||||
|
||||
class AccountModel(BaseModel):
|
||||
id: int
|
||||
username: str
|
||||
token: str
|
||||
|
||||
account_collection = get_collection("accounts")
|
||||
|
||||
def find_account_by_uid(uid: int) -> Optional[AccountModel]:
|
||||
account_data = account_collection.find_one({"_id": uid})
|
||||
if account_data:
|
||||
account_data["id"] = int(account_data["_id"])
|
||||
del account_data["_id"]
|
||||
return AccountModel(**account_data)
|
||||
return None
|
||||
|
||||
def find_account_by_name(name: str) -> Optional[AccountModel]:
|
||||
account_data = account_collection.find_one({"username": name})
|
||||
if account_data:
|
||||
account_data["id"] = int(account_data["_id"])
|
||||
del account_data["_id"]
|
||||
return AccountModel(**account_data)
|
||||
return None
|
||||
|
||||
def create_new_account(username: str) -> AccountModel:
|
||||
last_account = account_collection.find_one(sort=[("_id", -1)])
|
||||
uid = (last_account["_id"] + 1) if last_account else 1001
|
||||
token = generate_combo_token(str(uid))
|
||||
new_account = {
|
||||
"_id": uid,
|
||||
"username": username,
|
||||
"token": token
|
||||
}
|
||||
account_collection.insert_one(new_account)
|
||||
return AccountModel(id=uid, username=username, token=new_account["token"])
|
||||
142
database/avatar/avatar_data.py
Normal file
142
database/avatar/avatar_data.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from database.mongodb import get_collection
|
||||
from typing import Optional
|
||||
from database.base_database_data import BaseDatabaseData
|
||||
from game_server.resource import ResourceManager
|
||||
from game_server.resource.configdb.avatar_config import AvatarConfig
|
||||
from typing import List
|
||||
from rail_proto.lib import (
|
||||
Avatar,
|
||||
AvatarSkillTree,
|
||||
BattleAvatar,
|
||||
AvatarType,
|
||||
SpBarInfo,
|
||||
AvatarSkillTree,
|
||||
BattleEquipment,
|
||||
EquipRelic
|
||||
)
|
||||
|
||||
avatars_collection = get_collection("avatars")
|
||||
|
||||
class AvatarData(BaseDatabaseData):
|
||||
avatar_id: int
|
||||
level: int = 80
|
||||
exp: int = 0
|
||||
promotion: int = 6
|
||||
rank: int = 6
|
||||
lightcone_id: int = 0
|
||||
relic_ids: dict[str,int] = {}
|
||||
skills: dict[str,int] = {}
|
||||
current_hp: int = 10000
|
||||
current_sp: int = 0
|
||||
|
||||
def add_avatar(self) -> "AvatarData":
|
||||
get_avatar = avatars_collection.find_one({"uid": self.uid, "avatar_id":self.avatar_id})
|
||||
if get_avatar:
|
||||
return False
|
||||
|
||||
avatar = ResourceManager.instance().find_by_index(AvatarConfig, self.avatar_id)
|
||||
if not avatar:
|
||||
return False
|
||||
|
||||
self.avatar_id=avatar.AvatarID
|
||||
|
||||
for skill_id,skill_level in avatar.AvatarSkills.items():
|
||||
self.skills[str(skill_id)] = skill_level
|
||||
|
||||
avatar_data = self.model_dump()
|
||||
avatars_collection.insert_one(avatar_data)
|
||||
return self
|
||||
|
||||
def ToProto(self) -> Avatar:
|
||||
return Avatar(
|
||||
base_avatar_id=self.avatar_id,
|
||||
level=self.level,
|
||||
exp=self.exp,
|
||||
promotion=self.promotion,
|
||||
rank=self.rank,
|
||||
skilltree_list=[
|
||||
AvatarSkillTree(
|
||||
point_id=int(skill_id),
|
||||
level=skill_level
|
||||
)
|
||||
for skill_id,skill_level in self.skills.items()
|
||||
],
|
||||
equipment_unique_id=self.lightcone_id,
|
||||
equip_relic_list=[
|
||||
EquipRelic(
|
||||
relic_unique_id=relic_id,
|
||||
type=int(type)
|
||||
)
|
||||
for type,relic_id in self.relic_ids.items()
|
||||
],
|
||||
has_taken_promotion_reward_list=[1,2,3,4,5,6],
|
||||
)
|
||||
|
||||
def ToBattleProto(self,index,session):
|
||||
get_equipment = None
|
||||
if self.lightcone_id > 0:
|
||||
get_equipment = session.player.inventory_manager.get(self.lightcone_id)
|
||||
|
||||
return BattleAvatar(
|
||||
id=self.avatar_id,
|
||||
index=index,
|
||||
level=self.level,
|
||||
promotion=self.promotion,
|
||||
rank=self.rank,
|
||||
hp=self.current_hp,
|
||||
avatar_type=AvatarType.AVATAR_FORMAL_TYPE.value,
|
||||
sp_bar=SpBarInfo(
|
||||
cur_sp=5000,
|
||||
max_sp=10000
|
||||
),
|
||||
skilltree_list=[
|
||||
AvatarSkillTree(
|
||||
point_id=int(skill_id),
|
||||
level=level
|
||||
)
|
||||
for skill_id,level in self.skills.items()
|
||||
],
|
||||
equipment_list=[
|
||||
BattleEquipment(
|
||||
id=get_equipment.item_id if get_equipment else 0,
|
||||
level=get_equipment.level if get_equipment else 0,
|
||||
promotion=get_equipment.promotion if get_equipment else 0,
|
||||
rank=get_equipment.rank if get_equipment else 0
|
||||
)
|
||||
],
|
||||
relic_list = [
|
||||
session.player.inventory_manager.get(relic_id).RelicBattleProto()
|
||||
for type, relic_id in self.relic_ids.items()
|
||||
if relic_id > 0
|
||||
]
|
||||
)
|
||||
|
||||
def save_avatar(self):
|
||||
avatar_data = self.model_dump()
|
||||
avatar_data["uid"] = self.uid
|
||||
query = {"uid": self.uid, "avatar_id": self.avatar_id}
|
||||
avatars_collection.update_one(query, {"$set": avatar_data})
|
||||
|
||||
|
||||
def find_avatar_by_avatar_id(uid: int, avatar_id: int) -> Optional[AvatarData]:
|
||||
avatar_data = avatars_collection.find_one({"uid": uid, "avatar_id":avatar_id})
|
||||
if avatar_data:
|
||||
del avatar_data["_id"]
|
||||
return AvatarData(**avatar_data)
|
||||
return None
|
||||
|
||||
def get_all_avatars_by_uid(uid: int) -> List[AvatarData]:
|
||||
avatars_data = avatars_collection.find({"uid": uid})
|
||||
result = []
|
||||
for avatar in avatars_data:
|
||||
del avatar["_id"]
|
||||
result.append(AvatarData(**avatar))
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
4
database/base_database_data.py
Normal file
4
database/base_database_data.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class BaseDatabaseData(BaseModel):
|
||||
uid: int = 0
|
||||
159
database/inventory/inventory_data.py
Normal file
159
database/inventory/inventory_data.py
Normal file
@@ -0,0 +1,159 @@
|
||||
from database.mongodb import get_collection
|
||||
from database.base_database_data import BaseDatabaseData
|
||||
from game_server.resource import ResourceManager
|
||||
from game_server.resource.configdb.relic_main_affix_config import RelicMainAffixConfigData
|
||||
from game_server.resource.configdb.equipment_config import EquipmentConfig
|
||||
from game_server.resource.configdb.relic_config import RelicConfigData
|
||||
from game_server.game.items.relic_manager import RelicManager
|
||||
from game_server.game.enums.avatar.relic_type import RelicTypeEnum
|
||||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
from rail_proto.lib import (
|
||||
Equipment,
|
||||
Relic,
|
||||
RelicAffix,
|
||||
BattleRelic,
|
||||
RelicAffix
|
||||
)
|
||||
|
||||
items_collection = get_collection("items")
|
||||
|
||||
class SubAffix(BaseModel):
|
||||
id: int
|
||||
count: int
|
||||
step: int
|
||||
|
||||
class InventoryData(BaseDatabaseData):
|
||||
item_id: int
|
||||
count: int = 1
|
||||
level: int = 1
|
||||
exp: int = 0
|
||||
promotion: int = 0
|
||||
rank: int = 0
|
||||
locked: bool = False
|
||||
discarded: bool = False
|
||||
main_affix: int = 0
|
||||
sub_affix: list[SubAffix] = []
|
||||
equip_avatar: int = 0
|
||||
unique_id: int = 0
|
||||
main_type: int = 0
|
||||
sub_type: int = 0
|
||||
|
||||
def add_lightcone(self):
|
||||
last_item = items_collection.find_one({"uid": self.uid},sort=[("unique_id", -1)])
|
||||
unique_id = last_item["unique_id"] + 1 if last_item else 1
|
||||
equipment = ResourceManager.instance().find_by_index(EquipmentConfig, self.item_id)
|
||||
if not equipment:
|
||||
return None
|
||||
|
||||
self.level = 80
|
||||
self.promotion = 6
|
||||
self.main_type = 1
|
||||
self.unique_id = unique_id
|
||||
|
||||
lightcone_data = self.model_dump()
|
||||
items_collection.insert_one(lightcone_data)
|
||||
return self
|
||||
|
||||
def add_relic(self):
|
||||
last_item = items_collection.find_one({"uid": self.uid}, sort=[("unique_id", -1)])
|
||||
unique_id = last_item["unique_id"] + 1 if last_item else 1
|
||||
|
||||
relic = ResourceManager.instance().find_by_index(RelicConfigData, self.item_id)
|
||||
if not relic:
|
||||
return None
|
||||
|
||||
self.level = 15
|
||||
self.main_type = 2
|
||||
self.sub_type = RelicTypeEnum[relic.Type].value
|
||||
|
||||
relic_manager = RelicManager()
|
||||
main_stat = ""
|
||||
get_main_affix = None
|
||||
|
||||
if self.main_affix > 0:
|
||||
relics = ResourceManager.instance().find_by_index(RelicConfigData, self.item_id)
|
||||
main_affix_group = ResourceManager.instance().find_all_by_index(RelicMainAffixConfigData, relics.MainAffixGroup)
|
||||
main_stat = next(
|
||||
(affix.Property for affix in main_affix_group if affix.AffixID == int(self.main_affix))
|
||||
)
|
||||
elif self.main_affix == 0:
|
||||
get_main_affix = relic_manager.GetRandomRelicMainAffix(self.item_id)
|
||||
self.main_affix = get_main_affix.AffixID
|
||||
|
||||
if not main_stat and get_main_affix:
|
||||
main_stat = get_main_affix.Property
|
||||
|
||||
if not self.sub_affix:
|
||||
self.sub_affix = relic_manager.GetRandomRelicSubAffix(self.item_id, main_stat, 4)
|
||||
|
||||
if self.sub_affix and len(self.sub_affix) < 4:
|
||||
self.sub_affix.extend(relic_manager.GetRandomRelicSubAffix(self.item_id, main_stat, 4 - len(self.sub_affix), self.sub_affix))
|
||||
|
||||
self.unique_id = unique_id
|
||||
|
||||
relic_data = self.model_dump()
|
||||
items_collection.insert_one(relic_data)
|
||||
|
||||
return self
|
||||
|
||||
def ToProto(self):
|
||||
if self.main_type == 1:
|
||||
proto = Equipment(
|
||||
tid=self.item_id,
|
||||
level=self.level,
|
||||
rank=self.rank,
|
||||
promotion=self.promotion,
|
||||
dress_avatar_id=self.equip_avatar,
|
||||
unique_id=self.unique_id,
|
||||
)
|
||||
if self.main_type == 2:
|
||||
proto = Relic(
|
||||
tid=self.item_id,
|
||||
level=self.level,
|
||||
dress_avatar_id=self.equip_avatar,
|
||||
unique_id=self.unique_id,
|
||||
main_affix_id=self.main_affix,
|
||||
sub_affix_list=[
|
||||
RelicAffix(
|
||||
affix_id=affix.id,
|
||||
cnt=affix.count,
|
||||
step=affix.step
|
||||
)
|
||||
for affix in self.sub_affix
|
||||
]
|
||||
)
|
||||
return proto
|
||||
|
||||
def save_item(self):
|
||||
item_data = self.model_dump()
|
||||
item_data["uid"] = self.uid
|
||||
query = {"uid": self.uid, "unique_id": self.unique_id}
|
||||
items_collection.update_one(query, {"$set": item_data})
|
||||
|
||||
def RelicBattleProto(self):
|
||||
return BattleRelic(
|
||||
id=self.item_id,
|
||||
level=self.level,
|
||||
main_affix_id=self.main_affix,
|
||||
sub_affix_list=[
|
||||
RelicAffix(
|
||||
affix_id=affix.id,
|
||||
cnt=affix.count,
|
||||
step=affix.step
|
||||
)
|
||||
for affix in self.sub_affix
|
||||
],
|
||||
unique_id=self.unique_id,
|
||||
type=self.sub_type
|
||||
)
|
||||
|
||||
|
||||
|
||||
def get_all_items_by_uid(uid: int) -> List[InventoryData]:
|
||||
items_data = items_collection.find({"uid": uid})
|
||||
result = []
|
||||
for items in items_data:
|
||||
del items["_id"]
|
||||
result.append(InventoryData(**items))
|
||||
return result
|
||||
67
database/lineup/lineup_data.py
Normal file
67
database/lineup/lineup_data.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from database.mongodb import get_collection
|
||||
from database.base_database_data import BaseDatabaseData
|
||||
from typing import List
|
||||
from rail_proto.lib import (
|
||||
LineupInfo,
|
||||
ExtraLineupType,
|
||||
LineupAvatar,
|
||||
SpBarInfo,
|
||||
AvatarType
|
||||
)
|
||||
|
||||
lineups_collection = get_collection("lineups")
|
||||
|
||||
class LineupData(BaseDatabaseData):
|
||||
name: str = ""
|
||||
index: int = 0
|
||||
mp: int = 5
|
||||
extra_lineup: ExtraLineupType = ExtraLineupType.LINEUP_NONE
|
||||
leader_slot: int = 0
|
||||
avatar_list: list[int] = []
|
||||
|
||||
def ToProto(self) -> LineupInfo:
|
||||
return LineupInfo(
|
||||
name=self.name,
|
||||
index=self.index,
|
||||
mp=self.mp,
|
||||
max_mp=5,
|
||||
extra_lineup_type=self.extra_lineup,
|
||||
leader_slot=self.leader_slot,
|
||||
avatar_list=[
|
||||
LineupAvatar(
|
||||
id=avatar_id,
|
||||
slot=index,
|
||||
hp=10000,
|
||||
sp_bar=SpBarInfo(cur_sp=5000, max_sp=10000),
|
||||
satiety=100,
|
||||
avatar_type=AvatarType.AVATAR_FORMAL_TYPE.value
|
||||
)
|
||||
for index, avatar_id in enumerate(self.avatar_list)
|
||||
]
|
||||
)
|
||||
def add_lineup(self):
|
||||
lineup_data = self.model_dump()
|
||||
lineups_collection.insert_one(lineup_data)
|
||||
return self
|
||||
|
||||
def save_lineup(self):
|
||||
lineup_data = self.model_dump()
|
||||
lineup_data["uid"] = self.uid
|
||||
query = {"uid": self.uid, "index": self.index}
|
||||
existing_lineup = lineups_collection.find_one(query)
|
||||
|
||||
if existing_lineup:
|
||||
lineups_collection.update_one(query, {"$set": lineup_data})
|
||||
else:
|
||||
lineups_collection.insert_one(lineup_data)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_all_lineup_by_uid(uid: int) -> List[LineupData]:
|
||||
lineups_data = lineups_collection.find({"uid": uid})
|
||||
result = []
|
||||
for lineup in lineups_data:
|
||||
del lineup["_id"]
|
||||
result.append(LineupData(**lineup))
|
||||
return result
|
||||
10
database/mongodb.py
Normal file
10
database/mongodb.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from pymongo import MongoClient
|
||||
|
||||
client = MongoClient("mongodb://localhost:27017")
|
||||
|
||||
def get_database():
|
||||
return client["neonsr"]
|
||||
|
||||
def get_collection(collection_name: str):
|
||||
database = client["neonsr"]
|
||||
return database[collection_name]
|
||||
103
database/player/player_data.py
Normal file
103
database/player/player_data.py
Normal file
@@ -0,0 +1,103 @@
|
||||
from database.mongodb import get_collection
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
from database.base_database_data import BaseDatabaseData
|
||||
from rail_proto.lib import (
|
||||
Gender,
|
||||
PlayerBasicInfo,
|
||||
PlayerDetailInfo,
|
||||
PlatformType,
|
||||
PlayerRecordInfo
|
||||
)
|
||||
|
||||
players_collection = get_collection("players")
|
||||
|
||||
class PositionModel(BaseModel):
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
|
||||
class RotationModel(BaseModel):
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
|
||||
class PlayerData(BaseDatabaseData):
|
||||
name: str = ""
|
||||
signature: str = ""
|
||||
birthday: int = 0
|
||||
cur_basic_type: int = 8001
|
||||
head_icon: int = 208001
|
||||
phone_theme: int = 221000
|
||||
chat_bubble: int = 220000
|
||||
current_bgm: int = 210007
|
||||
current_gender: Gender = Gender.GenderMan
|
||||
level: int = 70
|
||||
exp: int = 0
|
||||
world_level: int = 0
|
||||
scoin: int = 0 # Credits
|
||||
hcoin: int = 0 # Jade
|
||||
mcoin: int = 0 # Crystals
|
||||
plane_id: int = 10000
|
||||
floor_id: int = 10000000
|
||||
entry_id: int = 100000104
|
||||
cur_lineup: int = 0
|
||||
pos: Optional[PositionModel] = PositionModel(x=0,y=0,z=0)
|
||||
rot: Optional[RotationModel] = RotationModel(x=0,y=0,z=0)
|
||||
|
||||
def ToProto(self) -> PlayerBasicInfo:
|
||||
return PlayerBasicInfo(
|
||||
nickname=self.name,
|
||||
level=self.level,
|
||||
exp=self.exp,
|
||||
world_level=self.world_level,
|
||||
scoin=self.scoin,
|
||||
hcoin=self.hcoin,
|
||||
mcoin=self.mcoin,
|
||||
stamina=240
|
||||
)
|
||||
|
||||
def ToDetailProto(self) -> PlayerDetailInfo:
|
||||
return PlayerDetailInfo(
|
||||
nickname=self.Name,
|
||||
level=self.Level,
|
||||
signature=self.Signature,
|
||||
is_banned=False,
|
||||
head_icon=self.HeadIcon,
|
||||
platform=PlatformType.PC,
|
||||
uid=self.uid,
|
||||
world_level=self.WorldLevel,
|
||||
record_info=PlayerRecordInfo()
|
||||
)
|
||||
|
||||
def create_player_data(self,name : str):
|
||||
try:
|
||||
self.name = name
|
||||
player_data = self.model_dump()
|
||||
players_collection.insert_one(player_data)
|
||||
return PlayerData(**player_data)
|
||||
except Exception as e:
|
||||
print(f"Error creating player data: {e}")
|
||||
return None
|
||||
|
||||
def save_player_data(self):
|
||||
try:
|
||||
player_data = self.model_dump()
|
||||
player_data["uid"] = self.uid
|
||||
query = {"uid": self.uid}
|
||||
players_collection.update_one(query, {"$set": player_data})
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error menyimpan data pemain: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def find_player_by_uid(uid: int) -> Optional[PlayerData]:
|
||||
player_data = players_collection.find_one({"uid": uid})
|
||||
if player_data:
|
||||
del player_data["_id"]
|
||||
return PlayerData(**player_data)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user