ROSE icon indicating copy to clipboard operation
ROSE copied to clipboard

Print game stats to console

Open orilc opened this issue 2 years ago • 2 comments

At the end of the game we would like to see details about the each driver's runs, like how many penguins were collected out of possible number of penguins, number of jumps and breaks that scored points, number of collisions with obstacles.

These details are printed to the console.

Example:

2023-08-10 13:30:59,286 INFO    [game] Stats:
 # |            Name | Score |  Penguins |    Cracks |     Water | Collisions |
   |                 |       | Collected | Collected | Collected |            |
___________________________
 1 |          Group5 |   673 |    6 / 10 |         3 |         2 |          1 |
 2 |          Group4 |   350 |    0 / 10 |         0 |         0 |         13 | (game:123)

orilc avatar Aug 10 '23 10:08 orilc

@sleviim Please review:)

orilc avatar Aug 10 '23 10:08 orilc

2023-08-10 13:30:59,286 INFO    [game] Stats:
 # |            Name | Score |  Penguins |    Cracks |     Water | Collisions |
   |                 |       | Collected | Collected | Collected |            |
___________________________
 1 |          Group5 |   673 |    6 / 10 |         3 |         2 |          1 |
 2 |          Group4 |   350 |    0 / 10 |         0 |         0 |         13 | (game:123)

Thanks for sharing the example. I don't think it is very clear as is, and the code to created is not clear. What if we keep the stats in a dict, and print the dict in simpler yaml format instead of a table?

$ cat stats.py 
import sys
import yaml

stats = {
    "stats": {
        "obstacles": {
            "penguin": 10,
            "crack": 7,
            "water": 4,
        },
        "players": {
            "gropup4": {
                "penguin": 6,
                "crack": 3,
                "water": 2,
                "collision": 1,
            },
            "gropup5": {
                "penguin": 0,
                "crack": 0,
                "water": 0,
                "collision": 13,
            },
        },
    },
}

yaml.dump(stats, sys.stdout, sort_keys=False)

Example output:

$ python3 stats.py 
stats:
  obstacles:
    penguin: 10
    crack: 7
    water: 4
  players:
    gropup4:
      penguin: 6
      crack: 3
      water: 2
      collision: 1
    gropup5:
      penguin: 0
      crack: 0
      water: 0
      collision: 13

The advantage is keeping stats in an easy to use way inside the program, making the output machine readable so it is easy to consume by other programs, and having no code to maintain for formatting the results.

nirs avatar Aug 27 '23 17:08 nirs