forked from 4KaNE/Discord-Lottery-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_battle_results.py
140 lines (116 loc) · 4 KB
/
output_battle_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import random
# loading table
table_data = {}
try:
with open('./output_battle_table.json', 'r', encoding="utf-8_sig") as fc:
table_data = json.load(fc)
except json.JSONDecodeError as e:
print('JSONDecodeError: ', e)
exit(e)
except FileNotFoundError as e:
print('FileNotFoundError: ', e)
exit(e)
# get my tier
def get_tier(damage):
# 対象Tierの抽出
target_table_data = [x for x in table_data['tiers'] if int(x['min']) <= damage <= int(x['max'])]
x = random.choice(target_table_data)
my_tier = int(x['tier'])
n = random.choice([1, 2, 3])
if n == 1:
enemy_min_tier = int(x['1_min'])
enemy_max_tier = int(x['1_max'])
elif n == 2:
enemy_min_tier = int(x['2_min'])
enemy_max_tier = int(x['2_max'])
else:
enemy_min_tier = int(x['3_min'])
enemy_max_tier = int(x['3_max'])
return my_tier, enemy_min_tier, enemy_max_tier
# get my ship
def get_my_ship(tier):
ships = []
for x in table_data['ships']:
if tier == int(x['tier']):
ships.append(x['name'])
return random.choice(ships)
# get enemy ships
def get_enemy_ships(my_tier, min_tier, max_tier):
# 対象Tierの抽出
target_table_data = [x for x in table_data['ships'] if min_tier <= int(x['tier']) <= max_tier]
enemy_cv_ships_counter = 0
enemy_ships = []
loop_counter = 0
while len(enemy_ships) < 12 and loop_counter < 1000:
loop_counter += 1 # 無限ループ防止用
x = random.choice(target_table_data)
kind = x['kind']
tier = int(x['tier'])
if kind == '空母':
if enemy_cv_ships_counter > 0 or tier != my_tier:
# 当面空母は自分と同Tierの1隻限定
continue
else:
enemy_cv_ships_counter += 1
enemy_ships.append(x)
return enemy_ships
class ShipDamageClass:
def __init__(self):
self.name = ''
self.hp_total = 0
self.hp_remains = 0
self.damage = 0
self.sink = ''
# get damage results
def get_damage_results(damage, enemy_ships):
remains = int(damage)
pers = list(range(1, 100))
target_ships = []
target_ships_hp_total = 0
for ship in enemy_ships:
x = ShipDamageClass()
x.name = ship['name']
x.hp_total = round(int(ship['hp']) + int(ship['hp_add']))
x.hp_remains = x.hp_total
x.damage = 0
target_ships.append(x)
target_ships_hp_total += x.hp_total
if target_ships_hp_total < remains:
return ' 敵艦全滅(' + str(target_ships_hp_total) + ')以上のダメージを叩き出しました。神かよ'
loop_counter = 0
while remains > 0 and loop_counter < 100:
loop_counter += 1 # 無限ループ防止用
x = random.choice(target_ships)
if x.hp_remains < 0:
continue
per_hp = random.choice(pers)
ship_damage = round(x.hp_total * per_hp / 100)
if x.hp_remains < ship_damage:
ship_damage = x.hp_remains
if remains < ship_damage:
ship_damage = remains
x.damage += ship_damage
x.hp_remains -= ship_damage
remains -= ship_damage
n = random.choice(pers)
if n <= 10:
# 攻撃1回につき10%の確率で撃沈
x.sink = '撃沈'
damage_ships = []
for x in target_ships:
if x.damage > 0:
s = x.name + x.sink + '(' + "{:,}".format(x.damage) + ')'
damage_ships.append(s)
return '、'.join(damage_ships)
# output_battle_results
def output_battle_results(damage):
tiers = get_tier(damage)
my_tier = tiers[0]
enemy_min_tier = tiers[1]
enemy_max_tier = tiers[2]
my_ship = get_my_ship(my_tier)
enemy_ships = get_enemy_ships(my_tier, enemy_min_tier, enemy_max_tier)
damage_result = get_damage_results(damage, enemy_ships)
result = 'あなたの使用艦艇は' + my_ship + 'で、戦果は' + damage_result + 'でした。'
return result