home · contact · privacy
Move socket configuration into command line options.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 24 Nov 2020 22:57:15 +0000 (23:57 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 24 Nov 2020 22:57:15 +0000 (23:57 +0100)
plomrogue/config.py [new file with mode: 0644]
rogue_chat.py
rogue_chat_curses.py
rogue_chat_curses.sh

diff --git a/plomrogue/config.py b/plomrogue/config.py
new file mode 100644 (file)
index 0000000..fcb74dd
--- /dev/null
@@ -0,0 +1,35 @@
+from plomrogue.io_websocket import PlomWebSocketServer
+from plomrogue.io_tcp import PlomTCPServer, PlomTCPServerSSL
+from plomrogue.errors import ArgError
+
+def parse_command_line_arguments():
+    import argparse
+    parser = argparse.ArgumentParser()
+    parser.add_argument('savefile')
+    parser.add_argument('--server-type', nargs='+', required=True,
+                        choices=['tcp', 'tcp_ssl', 'ws'])
+    parser.add_argument('--port', nargs='+', required=True, type=int)
+    parser.add_argument('--keyfile')
+    parser.add_argument('--certfile')
+    opts = parser.parse_args()
+    if len(opts.server_type) != len(opts.port):
+        raise ArgError('number of ports unequal to number of server types')
+    if 'tcp_ssl' in opts.server_type and not (opts.keyfile or opts.certfile):
+        raise ArgError('need key and cert file for tcp_ssl server')
+    config = {
+        'savefile': opts.savefile,
+        'servers': {},
+        'keyfile': opts.keyfile,
+        'certfile': opts.certfile,
+    }
+    for i in range(len(opts.port)):
+        server_type = opts.server_type[i]
+        if server_type == 'ws':
+            config['servers'][opts.port[i]] = PlomWebSocketServer
+        elif server_type == 'tcp':
+            config['servers'][opts.port[i]] = PlomTCPServer
+        elif server_type == 'tcp_ssl':
+            config['servers'][opts.port[i]] = PlomTCPServerSSL
+    return config
+
+config = parse_command_line_arguments()
index 1f40913eae7b4c11fc7baf3624470e221c72eab8..407dc29381534bec3cda962575c59783f3bead0a 100755 (executable)
@@ -1,7 +1,5 @@
 #!/usr/bin/env python3
 from plomrogue.game import Game
-from plomrogue.io_websocket import PlomWebSocketServer
-from plomrogue.io_tcp import PlomTCPServer
 from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_PING, cmd_THING,
                                 cmd_MAP, cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION,
                                 cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE,
@@ -13,13 +11,9 @@ from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_PING, cmd_THIN
 from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE, Task_PICK_UP,
                              Task_DROP, Task_FLATTEN_SURROUNDINGS)
 from plomrogue.things import Thing_Player, Thing_Item, Thing_Furniture
-import sys
 
-if len(sys.argv) != 2:
-    print('wrong number of arguments, expected one (save file)')
-    exit(1)
-savefile = sys.argv[1]
-game = Game(savefile)
+from plomrogue.config import config
+game = Game(config['savefile'])
 game.register_command(cmd_PING)
 game.register_command(cmd_ALL)
 game.register_command(cmd_LOGIN)
@@ -56,5 +50,6 @@ game.register_thing_type(Thing_Item)
 game.register_thing_type(Thing_Furniture)
 game.read_savefile()
 game.io.start_loop()
-game.io.start_server(8000, PlomWebSocketServer)
-game.io.start_server(5000, PlomTCPServer)
+for port in config['servers']:
+    game.io.start_server(port, config['servers'][port],
+                         config['certfile'], config['keyfile'])
index d22539b31b98e7294a5bf1c9ac97a8e1ce9cdb21..2362d99f8dd29a5030f44d62777c78637e8d4a27 100755 (executable)
@@ -3,6 +3,7 @@ import curses
 import queue
 import threading
 import time
+import sys
 from plomrogue.game import GameBase
 from plomrogue.parser import Parser
 from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex
@@ -900,5 +901,7 @@ class TUI:
                 elif key in self.movement_keys:
                     move_explorer(self.movement_keys[key])
 
-#TUI('localhost:5000')
-TUI('wss://plomlompom.com/rogue_chat/')
+if len(sys.argv) != 2:
+    raise ArgError('wrong number of arguments, need game host')
+host = sys.argv[1]
+TUI(host)
index fa78c01da7fd18025b36ac3e0f9e1fb77f4f1711..5fd7385739b7b473d90cc1ac45f56c0598731381 100755 (executable)
@@ -3,4 +3,4 @@
 python3 -m venv .venv
 . .venv/bin/activate
 pip install -r requirements_client.txt
-./rogue_chat_curses.py
+./rogue_chat_curses.py 'wss://plomlompom.com/rogue_chat/'