price_script_maker.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # coding=utf-8
  2. import os
  3. from sdk.tool.ad_ecpm_config_txt import AdEcpmConfigTxt
  4. _AdmobCountryRenameDic = {
  5. "圣文森特岛": "圣文森特和格林纳丁斯",
  6. "香港特别行政区": "香港",
  7. "安道尔共和国": "安道尔",
  8. "科特迪瓦共和国": "科特迪瓦",
  9. "吉尔吉斯坦": "吉尔吉斯斯坦",
  10. "蒙特塞拉特岛": "蒙特塞拉特",
  11. "百慕大群岛": "百慕大",
  12. "安圭拉岛": "安圭拉",
  13. }
  14. _AdmobSkipCountryList = {
  15. "刚果",
  16. "朝鲜",
  17. "古巴",
  18. "南斯拉夫",
  19. "扎伊尔",
  20. }
  21. _FacebookCountryRenameDic = {
  22. "圣文森特岛": "圣文森特和格林纳丁斯",
  23. "吉尔吉斯坦": "吉尔吉斯斯坦",
  24. "阿拉伯联合酋长国": "阿联酋",
  25. "香港特别行政区": "中国香港",
  26. "捷克": "捷克共和国",
  27. "百慕大群岛": "百慕大",
  28. "安道尔共和国": "安道尔",
  29. "刚果": "刚果共和国",
  30. "蒙特塞拉特岛": "蒙塞拉特岛",
  31. "安圭拉岛": "安圭拉",
  32. }
  33. class GAME_STORE:
  34. GOOGLE_PLAY, IOS = range(2)
  35. @staticmethod
  36. def global_price_mul(game_store):
  37. return {
  38. GAME_STORE.GOOGLE_PLAY: 1,
  39. GAME_STORE.IOS: 1.7
  40. }[game_store]
  41. @staticmethod
  42. def platform_from_string(game_store_str):
  43. return {
  44. "android": GAME_STORE.GOOGLE_PLAY,
  45. "ios": GAME_STORE.IOS
  46. }[game_store_str]
  47. class SCRIPT_PLATFORM(object):
  48. ADMOB, FACEBOOK = range(2)
  49. @staticmethod
  50. def script_template_name(type):
  51. return {
  52. SCRIPT_PLATFORM.ADMOB: "Template_Android_Admob_Banner.js",
  53. SCRIPT_PLATFORM.FACEBOOK: "Template_Android_FB_Interstitial.js",
  54. }[type]
  55. @staticmethod
  56. def all_platforms():
  57. return [SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK]
  58. class SCRIPT_AD_TYPE(object):
  59. BANNER, INTERSTITIAL = range(2)
  60. @staticmethod
  61. def global_base_price(type, index):
  62. return {
  63. SCRIPT_AD_TYPE.BANNER: [0.3, 0.6, 1, 1.6],
  64. SCRIPT_AD_TYPE.INTERSTITIAL: [1.05, 2.1, 2.8, 3.5, 5.6, 7, 8.4, 11.2]
  65. }[type][index]
  66. @staticmethod
  67. def script_file_name(type, index):
  68. """ 写入的文件名
  69. :param type: SCRIPT_AD_TYPE
  70. :param index: int
  71. :return: string
  72. """
  73. return {
  74. SCRIPT_AD_TYPE.BANNER:[
  75. "AM_B1_lv2.js",
  76. "AM_B2_lv1.js",
  77. "AM_B2_lv2.js",
  78. "AM_B2_lv3.js"
  79. ],
  80. SCRIPT_AD_TYPE.INTERSTITIAL: [
  81. "AM_I1_lv2.js",
  82. "FB_I1_lv1.js",
  83. "AM_I2_lv1.js",
  84. "FB_I1_lv2.js",
  85. "AM_I2_lv2.js",
  86. "FB_I2_lv1.js",
  87. "AM_I2_lv3.js",
  88. "FB_I2_lv2.js"
  89. ]
  90. }[type][index]
  91. @staticmethod
  92. def script_file_platform(type, index):
  93. return {
  94. SCRIPT_AD_TYPE.BANNER:[
  95. SCRIPT_PLATFORM.ADMOB,
  96. SCRIPT_PLATFORM.ADMOB,
  97. SCRIPT_PLATFORM.ADMOB,
  98. SCRIPT_PLATFORM.ADMOB
  99. ],
  100. SCRIPT_AD_TYPE.INTERSTITIAL: [
  101. SCRIPT_PLATFORM.ADMOB,
  102. SCRIPT_PLATFORM.FACEBOOK,
  103. SCRIPT_PLATFORM.ADMOB,
  104. SCRIPT_PLATFORM.FACEBOOK,
  105. SCRIPT_PLATFORM.ADMOB,
  106. SCRIPT_PLATFORM.FACEBOOK,
  107. SCRIPT_PLATFORM.ADMOB,
  108. SCRIPT_PLATFORM.FACEBOOK
  109. ]
  110. }[type][index]
  111. def _gen_script(script_platform, global_price, country_lines, write_to_file):
  112. """ 生成脚本配置文件
  113. :param script_platform: _SCRIPT_PLATFORM
  114. :param global_price: number
  115. :param country_lines: string 国家array字符口中
  116. :param write_to_file: string 写入文件
  117. """
  118. template_file_name = SCRIPT_PLATFORM.script_template_name(script_platform)
  119. current_path = os.path.dirname(os.path.realpath(__file__)) # 当前文件路径
  120. template_file_path = os.path.join(current_path, "script_templates", template_file_name)
  121. with open(template_file_path) as f:
  122. template_str = f.read()
  123. template_str = template_str.replace("$ZG_country_configs_ZG$", country_lines)
  124. template_str = template_str.replace("$ZG_global_price_ZG$", "{:.2f}".format(global_price))
  125. with open(write_to_file, "w") as ff:
  126. ff.write(template_str)
  127. def _gen_script_array_string(price_country_dic):
  128. """ 生成脚本价格配置字符串
  129. :param price_country_dic: {2.13: ['美国', '法国']}
  130. :return: string arr["2.13"] = Array("美国", "法国");
  131. """
  132. ret_str = ""
  133. for price in price_country_dic:
  134. arr_country = price_country_dic[price]
  135. country_str = '","'.join(arr_country)
  136. country_str = '"{}"'.format(country_str)
  137. ret_str = '{}\tarr["{:.2f}"] = Array({});\n'.format(ret_str, price, country_str)
  138. return ret_str
  139. def _replace_country_names(country_name_dic, script_platform):
  140. """按需替换国家名字
  141. 脚本中的国家名字,每个平台都有不一样的名字
  142. :param country_name_dic: {'美国':2.13, '日本':2.13, '法国':1.23}
  143. :param script_platform: _SCRIPT_PLATFORM
  144. :return:
  145. """
  146. if script_platform == SCRIPT_PLATFORM.ADMOB:
  147. for country_name in _AdmobCountryRenameDic:
  148. if country_name_dic.has_key(country_name):
  149. price = country_name_dic[country_name]
  150. country_name_dic.__delitem__(country_name)
  151. country_name_dic[_AdmobCountryRenameDic[country_name]] = price
  152. for country_name in _AdmobSkipCountryList:
  153. if country_name_dic.has_key(country_name):
  154. country_name_dic.__delitem__(country_name)
  155. if script_platform == SCRIPT_PLATFORM.FACEBOOK:
  156. for country_name in _FacebookCountryRenameDic:
  157. if country_name_dic.has_key(country_name):
  158. price = country_name_dic[country_name]
  159. country_name_dic.__delitem__(country_name)
  160. country_name_dic[_FacebookCountryRenameDic[country_name]] = price
  161. return country_name_dic
  162. def _country_dic_2_price_dic(country_name_dic):
  163. """ 国家名字典 转 价格字典
  164. :param country_name_dic: {'美国':2.13, '日本':2.13, '法国':1.23}
  165. :return: {2.13: ['美国', '日本'], 1.23: ['法国']}
  166. """
  167. price_dic = {}
  168. for country_name in country_name_dic:
  169. price = country_name_dic[country_name]
  170. price_country_name_arr = []
  171. if price_dic.has_key(price):
  172. price_country_name_arr = price_dic[price]
  173. price_country_name_arr.append(country_name)
  174. price_dic[price] = price_country_name_arr
  175. return price_dic
  176. class PriceScriptMaker:
  177. """ 脚本生成
  178. 参数列表:
  179. """
  180. def __init__(self, game_store):
  181. self.game_store = game_store
  182. self.banner_txt = AdEcpmConfigTxt()
  183. self.inter_txt = AdEcpmConfigTxt()
  184. def read(self, banner_txt_path, inter_txt_path):
  185. if banner_txt_path is None:
  186. self.banner_txt = None
  187. else:
  188. self.banner_txt.read(banner_txt_path, is_banner=True)
  189. if inter_txt_path is None:
  190. self.inter_txt = None
  191. else:
  192. self.inter_txt.read(inter_txt_path)
  193. def _gen_script(self, script_ad_type, txt, write_to_dir):
  194. game_store_mul = GAME_STORE.global_price_mul(self.game_store)
  195. country_name_index_arr = txt.get_country_name_index_arr()
  196. for index in range(0, len(country_name_index_arr)):
  197. # 每个序号对应的 platform 都是不一样的
  198. script_platform = SCRIPT_AD_TYPE.script_file_platform(script_ad_type, index)
  199. # 国家名字根据平台需要重新替换
  200. country_name_price_dic = country_name_index_arr[index]
  201. country_name_price_dic = _replace_country_names(country_name_price_dic, script_platform)
  202. # 按平台重新计算全球底价
  203. base_global_price = SCRIPT_AD_TYPE.global_base_price(script_ad_type, index)
  204. base_global_price *= game_store_mul
  205. # 按平台需要重新计算价格
  206. # for country_name in country_name_price_dic:
  207. # price = country_name_price_dic[country_name]
  208. # country_name_price_dic[country_name] = price * game_store_mul
  209. # 转换价格
  210. price_dic = _country_dic_2_price_dic(country_name_price_dic)
  211. # 生成脚本需要的代码
  212. script_array_string = _gen_script_array_string(price_dic)
  213. # 生成脚本并写入文件
  214. write_to_path = os.path.join(write_to_dir, SCRIPT_AD_TYPE.script_file_name(script_ad_type, index))
  215. _gen_script(script_platform, base_global_price, script_array_string, write_to_path)
  216. def gen_scripts(self, write_to_dir):
  217. if self.banner_txt is not None:
  218. self._gen_script(SCRIPT_AD_TYPE.BANNER, self.banner_txt, write_to_dir)
  219. if self.inter_txt is not None:
  220. self._gen_script(SCRIPT_AD_TYPE.INTERSTITIAL, self.inter_txt, write_to_dir)
  221. if __name__ == '__main__':
  222. write_to_dir = "/Users/zhuge/Temp/20201224/test/scripts"
  223. # _gen_script(SCRIPT_PLATFORM.ADMOB, 2.13, "wo shi yi hang", "/Users/zhuge/Temp/20201224/test/scripts/test.js")
  224. game_store = GAME_STORE.IOS
  225. maker = PriceScriptMaker(game_store)
  226. maker.read("/Users/zhuge/Temp/20201224/test/FunnyFruitSplash-连消-Android_banner_summary.txt", "/Users/zhuge/Temp/20201224/test/wood_inter_20200813_22_summary.txt")
  227. maker.gen_scripts(write_to_dir)
  228. # print maker._genScript(7, "admob", 1, 1.8, "/Users/zhuge/Temp/20201224/wood_3d_1010/t.js")
  229. # maker.genScriptFiles_interstitial(GAME_STORE.GOOGLE_PLAY, "/Users/zhuge/Temp/20201224/wood_3d_1010/scripts")