220 lines
5.3 KiB
Python
220 lines
5.3 KiB
Python
# coding=utf-8
|
|
from ToolsConfig import *
|
|
|
|
|
|
def GetItemUserData(itemDict, *args):
|
|
"""
|
|
获取物品的userData,若不含有相应的键,则创建
|
|
:param itemDict: 物品字典
|
|
:param args: 键值key
|
|
:return:
|
|
"""
|
|
itemDict['userData'] = itemDict.get('userData', {})
|
|
if itemDict.get('userData') is None:
|
|
itemDict['userData'] = {}
|
|
ver = itemDict['userData']
|
|
for key in args:
|
|
ver[key] = ver.get(key, {})
|
|
ver = ver[key]
|
|
return ver
|
|
|
|
|
|
def FixList(_list, _len, defaultValue):
|
|
"""
|
|
修正列表到目标长度
|
|
:param _list:列表
|
|
:param _len:目标长度
|
|
:param defaultValue:默认值
|
|
:return:
|
|
"""
|
|
if _list.__class__ is not list: _list = []
|
|
if len(_list) == _len: return _list
|
|
_list = _list if isinstance(_list, list) else [copy.deepcopy(defaultValue) for _ in range(_len)]
|
|
del _list[_len:]
|
|
_list.extend(copy.deepcopy(defaultValue) for _ in range(len(_list), _len))
|
|
return _list
|
|
|
|
|
|
def GetPlayerHasAut(playerId):
|
|
"""
|
|
判断玩家是否拥有创造和管理员权限
|
|
:param playerId:
|
|
:return:
|
|
"""
|
|
return serverCompFactory.CreatePlayer(playerId).GetPlayerOperation() == 2 and \
|
|
serverCompFactory.CreateGame(LevelId).GetPlayerGameType(playerId) == 1
|
|
|
|
|
|
def NbtTranslator(nbtDict):
|
|
"""
|
|
将nbt格式的数据转换为python数据
|
|
:param nbtDict:
|
|
:return:
|
|
"""
|
|
if nbtDict is None:
|
|
return {}
|
|
if type(nbtDict) is list:
|
|
return NbtList(nbtDict)
|
|
elif type(nbtDict) is dict and nbtDict.get('__type__', None) is None:
|
|
return NbtDict(nbtDict)
|
|
else:
|
|
return NbtValue(nbtDict)
|
|
|
|
|
|
def NbtValue(nbtDict):
|
|
"""
|
|
处理数Nbt数据
|
|
:param nbtDict:
|
|
:return:
|
|
"""
|
|
if type(nbtDict) is dict:
|
|
valueType = nbtDict.get('__type__', 0)
|
|
return NbtTranslatorFunc[valueType - 1](nbtDict['__value__']) if valueType else 0
|
|
else:
|
|
return nbtDict
|
|
|
|
|
|
def NbtDict(nbtDict):
|
|
"""
|
|
处理字典型Nbt数据
|
|
:param nbtDict:
|
|
:return:
|
|
"""
|
|
value = {}
|
|
for _ in nbtDict.items():
|
|
value[_[0]] = NbtTranslator(_[1])
|
|
return value
|
|
|
|
|
|
def NbtList(nbtList):
|
|
"""
|
|
处理列表型Nbt数据
|
|
:param nbtList:
|
|
:return:
|
|
"""
|
|
value = []
|
|
for _ in nbtList:
|
|
value.append(NbtTranslator(_))
|
|
return value
|
|
|
|
|
|
def TransBool(nbtValue):
|
|
"""
|
|
处理bool数据
|
|
"""
|
|
if nbtValue == 2:
|
|
return None
|
|
return bool(nbtValue)
|
|
|
|
|
|
NbtTranslatorFunc = [
|
|
TransBool, int, int, int, float, int, bytearray, str, list, NbtDict, list
|
|
]
|
|
|
|
|
|
def UpdateFunction(old_func, new_func):
|
|
"""
|
|
热更新函数
|
|
"""
|
|
old_func.__doc__ = new_func.__doc__
|
|
old_func.__dict__ = new_func.__dict__
|
|
old_func.__defaults__ = new_func.__defaults__
|
|
old_func.__code__ = new_func.__code__
|
|
|
|
|
|
mounts = {'s': [], 'c': []}
|
|
|
|
|
|
def Mount(system):
|
|
# type:(Union[clientBaseSystem,serverBaseSystem]) -> Any
|
|
"""
|
|
挂载系统,为非系统类添加属性blueFoxSystem
|
|
服务端只挂载BlueFoxServerSystem,客户端只挂载BlueFoxClientSystem
|
|
"""
|
|
|
|
def _mount(cls):
|
|
if issubclass(system, serverBaseSystem):
|
|
mounts['s'].append(cls)
|
|
elif issubclass(system, clientBaseSystem):
|
|
mounts['c'].append(cls)
|
|
return cls
|
|
|
|
return _mount
|
|
|
|
|
|
def GetLastSubclass(cls, basicClass=None):
|
|
"""
|
|
获取类的最终继承类
|
|
:param cls:
|
|
:param basicClass:
|
|
:return:
|
|
"""
|
|
if basicClass is None:
|
|
basicClass = []
|
|
subclasses = cls.__subclasses__()
|
|
for subclass in subclasses:
|
|
if subclass.__subclasses__():
|
|
GetLastSubclass(subclass, basicClass)
|
|
else:
|
|
basicClass.append(subclass)
|
|
return basicClass
|
|
|
|
|
|
def AddTupleContext(list1, list2):
|
|
# type:(Union[Tuple,List],Union[Tuple,List]) -> Union[Tuple[Any,...],Tuple[int,int,int,int]]
|
|
"""
|
|
将两个相同长度的tuple or list内容彼此相加,并返回新的tuple
|
|
:return:list1 + list2
|
|
"""
|
|
if list2.__len__() == list1.__len__():
|
|
newList = [list1[n] + list2[n] for n in range(list1.__len__())]
|
|
return tuple(newList)
|
|
return tuple([])
|
|
|
|
|
|
def AddTupleContextReList(list1, list2):
|
|
"""
|
|
将两个相同长度的tuple or list内容彼此相加,并返回新的list
|
|
:return:list1 + list2
|
|
"""
|
|
if list2.__len__() == list1.__len__():
|
|
newList = [list1[n] + list2[n] for n in range(list1.__len__())]
|
|
return newList
|
|
return []
|
|
|
|
|
|
def GetDistance(pos1, pos2):
|
|
# type:(Union[List,Tuple],Union[List, Tuple]) -> float
|
|
"""
|
|
计算三维上两点距离
|
|
:return:
|
|
"""
|
|
_sum = 0
|
|
for _ in range(min(len(pos1), len(pos2))): _sum += (pos1[_] - pos2[_]) ** 2
|
|
return _sum ** 0.5
|
|
|
|
|
|
def VecMod(vec):
|
|
# type:(Union[List,Tuple]) -> float
|
|
"""
|
|
计算向量的模
|
|
:param vec:
|
|
:return:
|
|
"""
|
|
_sum = 0
|
|
for _ in vec:
|
|
_sum += _ ** 2
|
|
return _sum ** 0.5
|
|
|
|
|
|
def ReduceTupleContext(list1, list2):
|
|
# type:(Union[Tuple,List],Union[Tuple,List]) -> Union[Tuple[Any,...],Tuple[int,int,int,int]]
|
|
"""
|
|
将两个相同长度的tuple or list内容彼此相减,并返回新的tuple
|
|
:return:list1 - list2
|
|
"""
|
|
if list2.__len__() == list1.__len__():
|
|
newList = [list1[n] - list2[n] for n in range(list1.__len__())]
|
|
return tuple(newList)
|
|
return tuple([])
|