Browse Source

Improve war formatting

JoostSijm 4 years ago
parent
commit
18104cf161
2 changed files with 47 additions and 28 deletions
  1. 4 4
      Pipfile.lock
  2. 43 24
      src/vboo_info_bot/functions.py

+ 4 - 4
Pipfile.lock

@@ -190,7 +190,7 @@
         "rival-regions-wrapper": {
             "editable": true,
             "git": "https://github.com/jjoo914/rival_regions_wrapper",
-            "ref": "68ad07e44be7bfe16b41f17c15c52f6735ba0d96"
+            "ref": "91965797a657c1c45d739e6a0889ec75402bdccc"
         },
         "selenium": {
             "hashes": [
@@ -331,11 +331,11 @@
         },
         "py": {
             "hashes": [
-                "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa",
-                "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0"
+                "sha256:a673fa23d7000440cc885c17dbd34fafcb7d7a6e230b29f6766400de36a33c44",
+                "sha256:f3b3a4c36512a4c4f024041ab51866f11761cc169670204b235f6b20523d4e6b"
             ],
             "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
-            "version": "==1.8.1"
+            "version": "==1.8.2"
         },
         "pyparsing": {
             "hashes": [

+ 43 - 24
src/vboo_info_bot/functions.py

@@ -42,53 +42,72 @@ def abbreviate(string, max_lenght):
 def format_state_region(side):
     """Format state and region name"""
     state = '*[{}](https://m.rivalregions.com/#state/details/{})*'.format(
-        abbreviate(side['state_name'], 4),
+        escape_markdown(abbreviate(side['state_name'], 4), 2),
         side['state_id'],
     )
     region = '[{}](http://m.rivalregions.com/#map/details/{})'.format(
-        side['region_name'],
+        escape_markdown(side['region_name'], 2),
         side['region_id'],
     )
     return '{} {}'.format(state, region)
 
 def roundk(integer):
     """Round down number"""
-    thousand = 1
-    while integer >= 999:
+    thousand = 0
+    decimal = 0
+    while integer >= 999 or integer <= -999:
         thousand += 1
-        decimal = str(integer)[-3:-2]
+        decimal = int(str(integer)[-3:-2])
         integer = int(str(integer)[:-3])
-    return '{}\\.{}{}'.format(integer, decimal, 'k' * thousand)
+    if decimal == 0:
+        return '{}{}'.format(integer, 'k' * thousand)
+    return '{}.{}{}'.format(integer, decimal, 'k' * thousand)
+
 
 
 def telegram_format_war(war):
     """Format war object for article"""
+    row_list = []
     if 'region_name' in war['attack']:
-        title = '{} vs {}'.format(
+        row_list.append('{} vs {}'.format(
             format_state_region(war['attack']),
             format_state_region(war['defend'])
-        )
+        ))
     else:
-        title = '{} vs {}'.format(
+        row_list.append('{} vs {}'.format(
             '*{}*'.format(war['type']),
             format_state_region(war['defend'])
-        )
-    damage = '{} *vs* {} \\= {}'.format(
-        roundk(war['attack']['damage']),
-        roundk(war['defend']['damage']),
-        roundk(war['damage']),
+        ))
+
+    total_damage = war['attack']['damage'] + war['defend']['damage']
+    row_list.append('{} *vs* {} \\= {}'.format(
+        escape_markdown('{} ({:0.2f}%)'.format(
+            roundk(war['attack']['damage']),
+            100 / total_damage * war['attack']['damage']
+        ), 2),
+        escape_markdown('{} ({:0.2f}%)'.format(
+            roundk(war['defend']['damage']),
+            100 / total_damage * war['defend']['damage']
+        ), 2),
+        escape_markdown(roundk(war['damage']), 2),
+    ))
+
+    row_list.append(
+        'finish: {} UTC'.format(escape_markdown(war['finish_date'].strftime('%Y-%m-%d %H:%M'), 2))
     )
 
-    link = '{} \\| {}'.format(
+    if war['time_left']:
+        row_list.append('time left: {}'.format(escape_markdown(str(war['time_left']))))
+    else:
+        row_list.append('max damage [{}](https://m.rivalregions.com/#slide/profile/{}): {}'.format(
+            war['max_hero_name'],
+            war['max_hero_id'],
+            escape_markdown(roundk(war['max_hero_damage']), 2),
+        ))
+
+    row_list.append('{} \\| {}'.format(
         '[mobile](https://m.rivalregions.com/#war/details/{})'.format(war['war_id']),
         '[desktop](http://rivalregions.com/#war/details/{})'.format(war['war_id']),
-    )
+    ))
 
-    formatted_war = '\n'.join([
-        title,
-        damage,
-        'finish: {} UTC'.format(escape_markdown(war['finish_date'].strftime('%Y-%m-%d %H:%M'), 2)),
-        'time left: {}'.format(escape_markdown(str(war['time_left']))),
-        link,
-    ])
-    return formatted_war
+    return '\n'.join(row_list)