From f495a32d55731268db3e8fbd272769e1c6ab1fb3 Mon Sep 17 00:00:00 2001
From: hxp <ale99527@vip.qq.com>
Date: 星期五, 17 十月 2025 14:32:11 +0800
Subject: [PATCH] 16 卡牌服务端(删除红包相关;)
---
ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/FormulaControl.py | 76 ++++++++++++++++++++++++++++++++++++++
1 files changed, 76 insertions(+), 0 deletions(-)
diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/FormulaControl.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/FormulaControl.py
index 79568c9..650611d 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/FormulaControl.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/FormulaControl.py
@@ -33,6 +33,10 @@
#===============================================================================
#---------------------------------------------------------------------
+import traceback
+import random
+import math
+
#---------------------------------------------------------------------
#公式字典名注册
#注册公式字典名方式 'Facl_DistKey_ + KeyName' (前缀名用于区别策划配置txt公式Key)
@@ -74,3 +78,75 @@
AllFormulaDist = {}
#GameWorld.Log('ClearCompileFormulaDist Sucess AllFormulaDist = %s'%(AllFormulaDist))
return
+
+def Eval(formulaKey, formula, paramDict, toInt=True, ceil=False, ndigits=0, playerID=0):
+ """ 动态计算
+ :param formulaKey: 公式编译缓存key
+ :param formula: 公式字符串,如 "int(Atk*10 + MaxHP)"
+ :param paramDict: 参数字典,如 {'Atk': 100, 'MaxHP': 5000}
+ :param toInt: 是否转为整数
+ :param ceil: 是否向上取整
+ :param ndigits: 小数精确位数
+ :return: 计算结果
+ """
+ compileFormula = GetCompileFormula(formulaKey, formula)
+
+ safe_env = {
+ '__builtins__': None,
+ # 基础数学函数
+ 'abs': abs,
+ 'min': min,
+ 'max': max,
+ 'pow': pow,
+ 'round': round,
+ # 类型转换
+ 'int': int,
+ 'long': long,
+ 'float': float,
+ 'bool': bool,
+ # 数学常数
+ #'pi': math.pi,
+ #'e': math.e
+ }
+ safe_env.update(paramDict)
+ try:
+ # 执行计算
+ value = eval(compileFormula, safe_env)
+ if ndigits > 0:
+ value = round(value, ndigits)
+ if ceil:
+ value = math.ceil(value)
+ if toInt:
+ value = int(value)
+ return value
+ except:
+ import GameWorld
+ GameWorld.SendGameErrorEx("FormulaEvalError", "formulaKey:%s, %s, %s" % (formulaKey, paramDict, traceback.format_exc()), playerID)
+ return 0
+
+def test():
+ attrParamDict={
+ 'lineupHaloPer': 0, 'awakeTalentPer': 0, 'breakLVPer': 0, 'breakLVValue': 0, 'bookValue': 0, 'lineupStarAddPer': 0, 'lvValue': 0,
+ 'heroSelfValue': 0, 'awakeTalentValue': 0, 'fetterPer': 0, 'lineupBreakLVAddPer': 0,
+ 'starTalentValue': 30, 'inheritPer': 1, 'equipValue': 269, 'fetterValue': 0, 'lineupLVAddPer': 0, 'heroSelfPer': 0,
+ 'bookPer': 0, 'starTalentPer': 0, 'lineupHaloValue': 0, 'lineupInitAddPer': 0
+ }
+
+ formula = "(lvValue+equipValue+bookValue)*inheritPer+(heroSelfValue+lineupHaloValue+starTalentValue+breakLVValue+awakeTalentValue)+fetterValue"
+ formula = "(lvValue+equipValue+bookValue)*(1+lineupHaloPer+bookPer+lineupInitAddPer+lineupLVAddPer+lineupBreakLVAddPer+lineupStarAddPer)*(inheritPer+fetterPer+starTalentValue+breakLVValue+awakeTalentValue)+heroSelfValue"
+
+ import time
+ doCount = 1
+
+ time1 = time.time()
+ for _ in xrange(doCount):
+ value = Eval("aaa", formula, attrParamDict)
+ print "公式 计算结果: ", value
+ print "use time %s" % (time.time() - time1)
+ return
+
+## 使用示例
+#if __name__ == "__main__":
+# test()
+
+
\ No newline at end of file
--
Gitblit v1.8.0