replace print with log_debug

This commit is contained in:
andrea
2025-08-02 10:04:30 +02:00
parent 6c3fe6326f
commit cdb69699ab
6 changed files with 48 additions and 43 deletions

View File

@@ -1,4 +1,5 @@
import os as _os import os as _os
from utils import logs as _logs
_MISSING_LOCAL_SETTINGS= """ _MISSING_LOCAL_SETTINGS= """
=============== ERROR =============== =============== ERROR ===============
@@ -20,7 +21,7 @@ _MISSING_LOCAL_SETTINGS= """
try: try:
from local_settings import TOKEN as _token from local_settings import TOKEN as _token
TOKEN= _token TOKEN= _token
except: print(_MISSING_LOCAL_SETTINGS) except: _logs.log_debug(_MISSING_LOCAL_SETTINGS)
try: try:
from local_settings import BOT_PATH as _bot_path from local_settings import BOT_PATH as _bot_path
BOT_PATH= _os.path.expanduser(_bot_path) BOT_PATH= _os.path.expanduser(_bot_path)
@@ -86,13 +87,13 @@ MAP_UTF8_DEATH_PLAYER= '💀'
MAP_UTF8_ITEM= '📦' MAP_UTF8_ITEM= '📦'
MAP_UTF8_LEGEND= f"""*Legenda*: MAP_UTF8_LEGEND= f"""*Legenda*:
\- *{MAP_UTF8_FIELD}*: Cella *libera* per muoversi - *{MAP_UTF8_FIELD}*: Cella *libera* per muoversi
\- *{MAP_UTF8_MOUNTAIN}*: Bordo della mappa, *non raggiungibile* - *{MAP_UTF8_MOUNTAIN}*: Bordo della mappa, *non raggiungibile*
\- *{MAP_UTF8_PLAYER_MALE}*: Posizione di un *giocatore Maschio* - *{MAP_UTF8_PLAYER_MALE}*: Posizione di un *giocatore Maschio*
\- *{MAP_UTF8_PLAYER_FEMALE}*: Posizione di una *giocatorice Femmina* - *{MAP_UTF8_PLAYER_FEMALE}*: Posizione di una *giocatorice Femmina*
\- *{MAP_UTF8_PLAYER_NONBINARY}*: Posizione di un *giocatore non binario* - *{MAP_UTF8_PLAYER_NONBINARY}*: Posizione di un *giocatore non binario*
\- *{MAP_UTF8_DEATH_PLAYER}*: Posizione di un *giocatore morto* - *{MAP_UTF8_DEATH_PLAYER}*: Posizione di un *giocatore morto*
\- *{MAP_UTF8_ITEM}*: Posizione di un *oggetto* \(non ancora implementato\) - *{MAP_UTF8_ITEM}*: Posizione di un *oggetto* (non ancora implementato)
""" """
MAP_IMAGE_FIELD= (0, 255, 0) # green MAP_IMAGE_FIELD= (0, 255, 0) # green
@@ -104,11 +105,11 @@ MAP_IMAGE_DEATH_PLAYER= (160, 160, 160) # grey
MAP_IMAGE_ITEM= (255, 255, 255) # white MAP_IMAGE_ITEM= (255, 255, 255) # white
MAP_IMAGE_LEGEND= r"""*Legenda*: MAP_IMAGE_LEGEND= r"""*Legenda*:
\- *Verde*: Cella *libera* per muoversi - *Verde*: Cella *libera* per muoversi
\- *Nero*: Bordo della mappa, *non raggiungibile* - *Nero*: Bordo della mappa, *non raggiungibile*
\- *Blue*: Posizione di un *giocatore Maschio* - *Blue*: Posizione di un *giocatore Maschio*
\- *Rosso*: Posizione di una *giocatorice Femmina* - *Rosso*: Posizione di una *giocatorice Femmina*
\- *Giallo*: Posizione di un *giocatore non binario* - *Giallo*: Posizione di un *giocatore non binario*
\- *Grigio*: Posizione di un *giocatore morto* - *Grigio*: Posizione di un *giocatore morto*
\- *Bianco*: Posizione di un *oggetto* \(non ancora implementato\) - *Bianco*: Posizione di un *oggetto* (non ancora implementato)
""" """

View File

@@ -1,6 +1,7 @@
import time as _time import time as _time
import random as _rand import random as _rand
import main as _main import main as _main
from utils import logs as _logs
from bot_libs import syms as _syms from bot_libs import syms as _syms
def _debug_data(): def _debug_data():
@@ -15,7 +16,7 @@ def _debug_data():
def _end_game_debug(alive_players, day): def _end_game_debug(alive_players, day):
last_player= alive_players[0] last_player= alive_players[0]
msg= f'{last_player.get_name()} sopravvive e vince dopo {day} lunghi Giorni, conquistando l\'amore eterno di Guarino' msg= f'{last_player.get_name()} sopravvive e vince dopo {day} lunghi Giorni, conquistando l\'amore eterno di Guarino'
print(msg) _logs.log_debug(msg)
return msg return msg
def _random_action(Arena, Player_one): def _random_action(Arena, Player_one):
@@ -25,7 +26,7 @@ def _random_action(Arena, Player_one):
} }
Map= Arena.get_map() Map= Arena.get_map()
avail_actions= Map.get_player_available_actions(Player_one) avail_actions= Map.get_player_available_actions(Player_one)
print(f'{Player_one.get_name()}:{Player_one.get_coordinates()}, avail_actions: {avail_actions}') _logs.log_debug(f'{Player_one.get_name()}:{Player_one.get_coordinates()}, avail_actions: {avail_actions}')
msg= '' msg= ''
if 1 in avail_actions: if 1 in avail_actions:
# XXX maybe in future this action is available only if you are near to another player # XXX maybe in future this action is available only if you are near to another player
@@ -38,7 +39,7 @@ def _random_action(Arena, Player_one):
elif 2 in avail_actions: elif 2 in avail_actions:
Map= Arena.get_map() Map= Arena.get_map()
available_movements= Map.get_player_available_directions(Player_one) available_movements= Map.get_player_available_directions(Player_one)
print(f'{Player_one.get_name()}:{Player_one.get_coordinates()}, avail_movements: {available_movements}') _logs.log_debug(f'{Player_one.get_name()}:{Player_one.get_coordinates()}, avail_movements: {available_movements}')
if not available_movements: if not available_movements:
# XXX probably should skip this action and look for another action # XXX probably should skip this action and look for another action
return f'{Player_one.get_name()} Pensa a Guarino tutto il giorno' return f'{Player_one.get_name()} Pensa a Guarino tutto il giorno'
@@ -46,14 +47,14 @@ def _random_action(Arena, Player_one):
x, y, direction= available_movements[0] x, y, direction= available_movements[0]
Player_one.move(x, y) Player_one.move(x, y)
Map.init_map_matrix() Map.init_map_matrix()
print(Map.get_renderized_map()) _logs.log_debug(Map.get_renderized_map())
msg= f'{Player_one.get_name()} Si muove verso »»» {direction}' msg= f'{Player_one.get_name()} Si muove verso »»» {direction}'
return msg return msg
def play_one_day_debug(Arena): def play_one_day_debug(Arena):
if not Arena.get_players(): return if not Arena.get_players(): return
print(f'Giorno #{Arena.day}') _logs.log_debug(f'Giorno #{Arena.day}')
alive_players= Arena.get_alive_players() alive_players= Arena.get_alive_players()
if len(alive_players) == 1: if len(alive_players) == 1:
day= Arena.day day= Arena.day
@@ -81,13 +82,13 @@ def init_debug_simulation():
Arena= _debug_data() Arena= _debug_data()
while (len(Arena.get_alive_players()) > 1): while (len(Arena.get_alive_players()) > 1):
events= play_one_day_debug(Arena) events= play_one_day_debug(Arena)
print('#################') _logs.log_debug('#################')
print('#################') _logs.log_debug('#################')
print('#################') _logs.log_debug('#################')
print(events) _logs.log_debug(events)
print('#################') _logs.log_debug('#################')
print('#################') _logs.log_debug('#################')
print('#################') _logs.log_debug('#################')
_time.sleep(0.3) _time.sleep(0.3)
def init_debug_loop(): def init_debug_loop():
@@ -105,14 +106,14 @@ def init_debug_loop():
#End of day #End of day
last_player= Arena.get_alive_players()[0] last_player= Arena.get_alive_players()[0]
print(f'{last_player.get_name()} sopravvive e vince dopo {Arena.day} lunghi Giorni, conquistando l\'amore eterno di Guarino') _logs.log_debug(f'{last_player.get_name()} sopravvive e vince dopo {Arena.day} lunghi Giorni, conquistando l\'amore eterno di Guarino')
def debug_random_map(): def debug_random_map():
from entities import map as _map; from entities import map as _map;
from entities import player; from entities import player;
M= _map.BrSimMap(players= [player.BrSimPlayer(i) for i in range(20)]); M= _map.BrSimMap(players= [player.BrSimPlayer(i) for i in range(20)]);
res= M.get_renderized_map() res= M.get_renderized_map()
print(res) _logs.log_debug(res)
if __name__ == '__main__': if __name__ == '__main__':
init_debug() init_debug()

View File

@@ -2,6 +2,7 @@ from entities import player as _player
from entities import event_picker as _events from entities import event_picker as _events
from entities import gamemap as _map from entities import gamemap as _map
from entities.items import weapons as _weapons from entities.items import weapons as _weapons
from utils import logs as _logs
class BrSimArena(): class BrSimArena():
@@ -30,13 +31,13 @@ class BrSimArena():
def next_day(self): def next_day(self):
self.day+= 1 self.day+= 1
print(f'Giorno: {self.day}') _logs.log_debug(f'Giorno: {self.day}')
alive_players_str= ', '.join([p.get_name() for p in self.get_alive_players()]) alive_players_str= ', '.join([p.get_name() for p in self.get_alive_players()])
print(f'Giocatori vivi: {alive_players_str}') _logs.log_debug(f'Giocatori vivi: {alive_players_str}')
death_players= self.get_death_players() death_players= self.get_death_players()
if (death_players): if (death_players):
death_players_str= ', '.join([p.get_name() for p in death_players]) death_players_str= ', '.join([p.get_name() for p in death_players])
print(f'Giocatori morti: {death_players_str}') _logs.log_debug(f'Giocatori morti: {death_players_str}')
def get_alive_players(self): def get_alive_players(self):
res= [] res= []

View File

@@ -6,6 +6,7 @@ from PIL import ImageDraw as _ImageDraw
from utils import logs as _logs from utils import logs as _logs
from bot_libs import syms as _bot_syms from bot_libs import syms as _bot_syms
from entities import resource as _resource from entities import resource as _resource
from utils import logs as _logs
class BrSimMap(): class BrSimMap():
@@ -78,7 +79,7 @@ class BrSimMap():
x= _random.randint(0, self.world_width -1) x= _random.randint(0, self.world_width -1)
y= _random.randint(0, self.world_height -1) y= _random.randint(0, self.world_height -1)
resource= self.get_map_matrix()[y][x] resource= self.get_map_matrix()[y][x]
print(f'{target.get_name()} >>> ({x},{y})') _logs.log_debug(f'{target.get_name()} >>> ({x},{y})')
target.set_coordinates(x, y) target.set_coordinates(x, y)
self.get_map_matrix()[y][x]= target self.get_map_matrix()[y][x]= target
@@ -150,7 +151,7 @@ class BrSimMap():
attack= self.check_near_players(Player) attack= self.check_near_players(Player)
if attack: if attack:
print(f'{Player.get_name()} can attack {[a.get_name() for a in attack]}') _logs.log_debug(f'{Player.get_name()} can attack {[a.get_name() for a in attack]}')
#avail_actions.append(1) #XXX replace with attack action (or maybe other actions on players) #avail_actions.append(1) #XXX replace with attack action (or maybe other actions on players)
avail_actions[1]= attack #XXX replace with attack action (or maybe other actions on players) avail_actions[1]= attack #XXX replace with attack action (or maybe other actions on players)
if self.get_player_available_directions(Player): if self.get_player_available_directions(Player):

View File

@@ -1,6 +1,7 @@
import random as _random import random as _random
import uuid as _uuid import uuid as _uuid
from entities import resource as _resource from entities import resource as _resource
from utils import logs as _logs
class BrSimPlayer(_resource.BrSimResource): class BrSimPlayer(_resource.BrSimResource):
@@ -137,11 +138,11 @@ class BrSimPlayer(_resource.BrSimResource):
self.health= 0 self.health= 0
if self.player_gender_is_male(): if self.player_gender_is_male():
print(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono morto x.x') _logs.log_debug(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono morto x.x')
elif self.player_gender_is_female(): elif self.player_gender_is_female():
print(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono morta x.x') _logs.log_debug(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono morta x.x')
else: else:
print(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono mort* x.x') _logs.log_debug(f'[{self.get_name_and_stats()}]: Guarino, perdonami se sono mort* x.x')
return damage return damage
def attack(self, target): def attack(self, target):
@@ -166,11 +167,11 @@ class BrSimPlayer(_resource.BrSimResource):
def get_item(self, item): def get_item(self, item):
if self.get_inventory_weight() + item.get_weight() >= self.get_max_weight(): if self.get_inventory_weight() + item.get_weight() >= self.get_max_weight():
if self.player_gender_is_male(): if self.player_gender_is_male():
print(f'Sono sovraccarico, {self.get_name_and_stats} non puo\' prendere questo oggetto') _logs.log_debug(f'Sono sovraccarico, {self.get_name_and_stats} non puo\' prendere questo oggetto')
elif self.player_gender_is_female(): elif self.player_gender_is_female():
print(f'Sono sovraccarica, {self.get_name_and_stats} non puo\' prendere questo oggetto') _logs.log_debug(f'Sono sovraccarica, {self.get_name_and_stats} non puo\' prendere questo oggetto')
else: else:
print(f'Sono sovraccaric#, {self.get_name_and_stats} non puo\' prendere questo oggetto') _logs.log_debug(f'Sono sovraccaric#, {self.get_name_and_stats} non puo\' prendere questo oggetto')
return False return False
self.inventory.append(item) self.inventory.append(item)

View File

@@ -19,17 +19,16 @@ def get_log_name():
def _create_file_handler(): def _create_file_handler():
global file_handler global file_handler
print('### create file handler')
current_log_file = get_log_name() current_log_file = get_log_name()
file_handler = _logging.FileHandler(current_log_file, encoding='utf-8') file_handler = _logging.FileHandler(current_log_file, encoding='utf-8')
formatter = _logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') formatter = _logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter) file_handler.setFormatter(formatter)
logger.addHandler(file_handler) logger.addHandler(file_handler)
logger.setLevel(_logging.INFO) #logger.setLevel(_logging.INFO)
logger.setLevel(_logging.DEBUG)
return file_handler return file_handler
def _setup_logging_file(): def _setup_logging_file():
print('### setup logging')
current_log_file = get_log_name() current_log_file = get_log_name()
if not file_handler: return _create_file_handler() if not file_handler: return _create_file_handler()
@@ -40,6 +39,7 @@ def _setup_logging_file():
_create_file_handler() _create_file_handler()
def log_debug(txt): def log_debug(txt):
print(txt)
_setup_logging_file() _setup_logging_file()
logger.debug(txt) logger.debug(txt)