Asciifarm/asciifarm/client/main.py

66 lines
1.7 KiB
Python
Raw Normal View History

2017-10-15 21:30:31 +00:00
#! /usr/bin/python3
2019-09-17 22:32:35 +00:00
2017-10-26 17:05:49 +00:00
import json
2019-09-17 22:32:35 +00:00
import sys
2019-09-17 22:32:35 +00:00
import termios
import tty
import signal
2019-09-18 10:07:58 +00:00
#import os
2019-09-17 22:32:35 +00:00
from .connection import Connection
from .gameclient import Client
2019-09-18 10:19:15 +00:00
from .display import Display
from .parseargs import parse_args
2019-09-17 22:32:35 +00:00
from ratuil.screen import Screen
2017-10-24 16:36:22 +00:00
def main(argv=None):
(name, socketType, address, keybindings, characters, colours, logfile) = parse_args(argv)
2017-10-24 16:36:22 +00:00
connection = Connection(socketType)
try:
connection.connect(address)
except ConnectionRefusedError:
print("ERROR: Could not connect to server.\nAre you sure that the server is running and that you're connecting to the right address?", file=sys.stderr)
return
error = None
closeMessage = None
2017-10-24 16:36:22 +00:00
2019-09-17 22:32:35 +00:00
#os.environ.setdefault("ESCDELAY", "25")
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
2017-10-24 16:36:22 +00:00
2019-09-16 20:12:29 +00:00
try:
2019-09-17 22:32:35 +00:00
tty.setraw(sys.stdin)
Screen.default.hide_cursor()
2019-09-16 20:12:29 +00:00
2019-09-17 22:32:35 +00:00
display = Display(characters)
client = Client(display, name, connection, keybindings, logfile)
signal.signal(signal.SIGWINCH, client.onSigwinch)
try:
client.start()
except KeyboardInterrupt:
client.close("^C caught, goodbye")
except Exception as e:
# throw the execption outside ncurses
# so the cleanup can happen first
error = e
closeMessage = client.closeMessage
2019-09-16 20:12:29 +00:00
finally:
2019-09-17 22:32:35 +00:00
## Set everything back to normal
termios.tcsetattr(fd, termios.TCSADRAIN, oldterm)
Screen.default.finalize()
2019-09-16 20:12:29 +00:00
if error is not None:
raise error
if closeMessage:
print(closeMessage, file=sys.stderr)
2017-10-24 16:36:22 +00:00