Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Update levels.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick2bad4u committed Apr 17, 2024
1 parent b57a31d commit ec52ce9
Showing 1 changed file with 120 additions and 50 deletions.
170 changes: 120 additions & 50 deletions levels.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,112 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from utils import *

level_bbox = 975, 143, 1060, 198
level_bbox = (975, 143, 1060, 198)
font_color = (254, 254, 254)
single_digit_diff = 21
second_digit_diff = 43

bar_bbox = 1082, 148, 1656, 149
bar_bbox = (1082, 148, 1656, 149)
bar_colors = ((158, 158, 177), (128, 128, 153), (30, 60, 126))

rewards_bar_bbox = 786, 331, 1365, 332
rewards_bar_bbox = (786, 331, 1365, 332)
rewards_bar_colors = ((35, 120), (190, 255), (40, 165))

locked_pixel = 1163, 875
locked_pixel = (1163, 875)
locked_color = (110, 200, 211)

first_digit_dict = {
"1": ((10, 17), (15, 45), (17, 4), (20, 15), (27, 45), (29, 4), (19, 32)),
"2": ((5, 11), (6, 5), (9, 3), (30, 4), (34, 45), (36, 12)),
"3": ((3, 37), (7, 10), (8, 3), (9, 23), (33, 6), (36, 34)),
"4": ((2, 25), (2, 34), (19, 45), (30, 45), (32, 3), (37, 27)),
"5": ((4, 37), (5, 26), (7, 4), (23, 20), (34, 4), (36, 32)),
"6": ((2, 28), (3, 37), (16, 3), (25, 46), (29, 3), (37, 27)),
"7": ((4, 3), (4, 11), (5, 45), (19, 45), (36, 3), (36, 12)),
"8": ((2, 38), (5, 12), (19, 27), (20, 3), (36, 11), (37, 35)),
"9": ((10, 45), (15, 3), (15, 30), (19, 23), (22, 28), (24, 45)),
"0": ((1, 30), (9, 6), (15, 13), (23, 46), (24, 34), (37, 17)),
}

second_digit_dict = dict(
(
(key, tuple(((x + second_digit_diff, y) for x, y in value)))
for key, value in first_digit_dict.items()
)
)
'1': (
(10, 17),
(15, 45),
(17, 4),
(20, 15),
(27, 45),
(29, 4),
(19, 32),
),
'2': (
(5, 11),
(6, 5),
(9, 3),
(30, 4),
(34, 45),
(36, 12),
),
'3': (
(3, 37),
(7, 10),
(8, 3),
(9, 23),
(33, 6),
(36, 34),
),
'4': (
(2, 25),
(2, 34),
(19, 45),
(30, 45),
(32, 3),
(37, 27),
),
'5': (
(4, 37),
(5, 26),
(7, 4),
(23, 20),
(34, 4),
(36, 32),
),
'6': (
(2, 28),
(3, 37),
(16, 3),
(25, 46),
(29, 3),
(37, 27),
),
'7': (
(4, 3),
(4, 11),
(5, 45),
(19, 45),
(36, 3),
(36, 12),
),
'8': (
(2, 38),
(5, 12),
(19, 27),
(20, 3),
(36, 11),
(37, 35),
),
'9': (
(10, 45),
(15, 3),
(15, 30),
(19, 23),
(22, 28),
(24, 45),
),
'0': (
(1, 30),
(9, 6),
(15, 13),
(23, 46),
(24, 34),
(37, 17),
),
}

single_digit_dict = dict(
(
(key, tuple(((x + single_digit_diff, y) for x, y in value)))
for key, value in first_digit_dict.items()
)
)
second_digit_dict = dict((key, tuple((x + second_digit_diff, y) for (x,
y) in value)) for (key, value) in
first_digit_dict.items())

single_digit_dict = dict((key, tuple((x + single_digit_diff, y) for (x,
y) in value)) for (key, value) in
first_digit_dict.items())

level_hundred_conditions = (
(0, 9),
Expand All @@ -51,10 +118,11 @@
(62, 37),
(84, 9),
(84, 40),
)
)

levels_xp = [
0, # infinite xp if lvl 0 (undefined)
levels_xp = [ # infinite xp if lvl 0 (undefined)
# infinite xp if lvl 100 (max lvl)
0,
210,
368,
455,
Expand Down Expand Up @@ -154,8 +222,8 @@
24592,
24917,
25263,
float("inf"), # infinite xp if lvl 100 (max lvl)
]
float('inf'),
]

gold_levels = list(range(7, 20, 2)) + list(range(21, 101))

Expand All @@ -169,39 +237,43 @@ def get_duration_gold(duration):


class LevelNotDetected(Exception):

pass


class LevelDefiner:

def __init__(self, brawlhalla):
self.brawlhalla = brawlhalla

def _get_single_digit_level(self, image):
return int(self.get_single_digit(image))

def _get_double_digit_level(self, image):
return int(self.get_first_digit(image) + self.get_second_digit(image))
return int(self.get_first_digit(image)
+ self.get_second_digit(image))

@staticmethod
def _get_level_hundred(image):
if all(image.getpixel(pos) == font_color for pos in level_hundred_conditions):
if all(image.getpixel(pos) == font_color for pos in
level_hundred_conditions):
return 100
raise TypeError

def get_level(self):
screenshot = self.brawlhalla.make_screenshot()
level = screenshot.crop(level_bbox)
for f in [
self._get_single_digit_level,
self._get_double_digit_level,
self._get_level_hundred,
]:
for f in [self._get_single_digit_level,
self._get_double_digit_level,
self._get_level_hundred]:
try:

# noinspection PyArgumentList

return f(level)
except TypeError:
pass
logger.error("level_error")
logger.error('level_error')
raise LevelNotDetected

def get_percentage(self):
Expand All @@ -221,21 +293,18 @@ def get_reward_percentage(self):
bar = screenshot.crop(rewards_bar_bbox)
for i in range(bar.width):
pixel = bar.getpixel((i, 0))
if all(
rewards_bar_colors[i][1] > pixel[i] > rewards_bar_colors[i][0]
for i in range(len(pixel))
):
if all(rewards_bar_colors[i][1] > pixel[i]
> rewards_bar_colors[i][0] for i in
range(len(pixel))):
count += 1
perc = count / bar.width
return perc

def get_xp(self, level, reward=False):
if level == 100:
return 0
return int(
(self.get_reward_percentage() if reward else self.get_percentage())
* levels_xp[level]
)
return int(((self.get_reward_percentage() if reward else self.get_percentage()))
* levels_xp[level])

def get_unlocked(self):
screenshot = self.brawlhalla.make_screenshot()
Expand All @@ -244,7 +313,8 @@ def get_unlocked(self):
@staticmethod
def get_digit(conditions, image):
for digit in conditions:
if all(image.getpixel(pos) == font_color for pos in conditions[digit]):
if all(image.getpixel(pos) == font_color for pos in
conditions[digit]):
return digit

def get_first_digit(self, image):
Expand Down

0 comments on commit ec52ce9

Please sign in to comment.