Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (41 loc) · 1.41 KB
/
Copy pathmain.py
File metadata and controls
52 lines (41 loc) · 1.41 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
# This starts the 1830 Game Daemon.
# By Jawaad Mahmood (ideas@jawaadmahmood.com)
from typing import List
import os
import time
from app.base import Player, Move
from app.state import Game
def ongoing_game(pipe_filen="/tmp/mypipe"):
"""Conveys messages from a file pipe to the running daemon process"""
pipe_path = pipe_filen
if not os.path.exists(pipe_path):
os.mkfifo(pipe_path)
# Open the fifo. We need to open in non-blocking mode or it will stalls until
# someone opens it for writting
pipe_fd = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)
continue_game = True
with os.fdopen(pipe_fd) as pipe:
while continue_game:
msg = pipe.read()
if msg:
yield Move.fromMessage(msg)
print("Waiting for command.")
time.sleep(0.5)
continue_game = yield
def main(players: List[Player], saved_game=None):
"""
Game Daemon
:param saved_game:
:param players:
:param state:
:return:
"""
game = Game.initialize(players, saved_game)
game.setPlayerOrder() # Rah.
for move in ongoing_game("/tmp/mypipe"):
if game.isValidMove(move) and game.isValidPlayer(move.player) and game.performedMove(move):
game.saveState()
game.notifyPlayers()
game.setPlayerOrder()
# Keep waiting for input if the game is ongoing.
move.send(game.isOngoing())