Initialize the network knowledge base CSV file with headers.
(self)
| 1483 | loaded_data = raw_data |
| 1484 | except json.JSONDecodeError: |
| 1485 | logger.warning("Gamification file is corrupted; starting with defaults") |
| 1486 | except Exception as exc: |
| 1487 | logger.warning(f"Unable to load gamification file: {exc}") |
| 1488 | |
| 1489 | self.gamification_data = {**default_data, **loaded_data} |
| 1490 | if not isinstance(self.gamification_data.get("mac_points"), dict): |
| 1491 | self.gamification_data["mac_points"] = {} |
| 1492 | if not isinstance(self.gamification_data.get("lifetime_counts"), dict): |
| 1493 | self.gamification_data["lifetime_counts"] = {} |
| 1494 | |
| 1495 | self._update_gamification_state() |
| 1496 | |
| 1497 | def save_gamification_data(self): |
| 1498 | """Persist gamification progress to disk.""" |
| 1499 | try: |
| 1500 | os.makedirs(os.path.dirname(self.gamification_file), exist_ok=True) |
| 1501 | data_to_save = dict(self.gamification_data) |
| 1502 | data_to_save["total_points"] = int(self.gamification_data.get("total_points", 0) or 0) |
| 1503 | data_to_save["level"] = int(self.gamification_data.get("level", 1) or 1) |
| 1504 | with open(self.gamification_file, 'w', encoding='utf-8') as fp: |
| 1505 | json.dump(data_to_save, fp, indent=4) |
| 1506 | except Exception as exc: |
| 1507 | logger.error(f"Failed to save gamification data: {exc}") |
| 1508 | |
| 1509 | def calculate_level(self, total_points: int) -> int: |
| 1510 | """Calculate the level from total points using a slower progression curve.""" |
| 1511 | if total_points < 0: |
| 1512 | total_points = 0 |
| 1513 | return max(1, 1 + total_points // max(self.points_per_level, 1)) |
| 1514 | |
| 1515 | def _update_gamification_state(self): |
| 1516 | """Synchronize in-memory level/points from gamification data.""" |
| 1517 | total_points = int(self.gamification_data.get("total_points", 0) or 0) |
| 1518 | self.coinnbr = total_points |
| 1519 | self.levelnbr = self.calculate_level(total_points) |
| 1520 | self.gamification_data["level"] = self.levelnbr |
| 1521 |
no test coverage detected