-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathplayers.py
More file actions
executable file
·87 lines (68 loc) · 2.03 KB
/
players.py
File metadata and controls
executable file
·87 lines (68 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
import asyncio
import sys
import math
import random
import traceback
import argparse
class Player:
# les 4 directions
directions = ('N', 'E', 'S', 'W')
def __init__(self, name, period, cycles):
self.name = name
self.period = period
# durer indéfiniment
self.cycles = cycles
def __repr__(self):
return f"{self.name}: {self.cycles}x{self.period}s"
async def run(self):
counter = 0
while counter < self.cycles:
counter += 1
duration = self.period * random.random()
direction = random.choice(self.directions)
print(f"{direction} {self.name}")
await asyncio.sleep(duration)
class Players:
def __init__(self, *players):
self.players = list(players)
def __repr__(self):
return f"[Players {' + '.join(repr(p) for p in self.players)}]"
async def run(self):
jobs = [
player.run() for player in self.players
]
return await asyncio.gather(*jobs)
##########
predefined = {
# circa 3s
1: Players(Player('john', .8, 3),
Player('mary', .4, 7),
),
2: Players(Player('bill', .5, 5),
Player('jane', .7, 4),
),
# circa 6s
3: Players(Player('augustin', .8, 8),
Player('randalph', .6, 10),
),
4: Players(Player('bertrand', .5, 12),
Player('juliette', .7, 8),
),
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument(default=1, type=int, dest='groupnb', nargs='?',
choices=(1, 2, 3, 4),
help="select predefined group 1 or 2")
args = parser.parse_args()
players = predefined[args.groupnb]
try:
asyncio.run((players.run()))
sys.exit(0)
except Exception as e:
print(f"EMERGENCY {type(e)}, {e}")
sys.stderr.write(traceback.format_exc())
sys.exit(1)
if __name__ == '__main__':
main()