home · contact · privacy
New client: Add log reading, log window.
[plomrogue] / client_prototype.py
index ab91e9ab4f8f2373fde7e187127177a2d4b61b0b..5e9f16809c4556ee986bf708ae82091ada58a068 100644 (file)
@@ -1,4 +1,5 @@
 import curses
+import math
 import os
 import signal
 import time
@@ -184,10 +185,22 @@ def read_worldstate():
     if read_anew:
         cursed_main.redraw = True
         world_data["turn"] = int(turn_string)
+        world_data["lifepoints"] = int(worldstate_file.readline())
+        world_data["satiation"] = int(worldstate_file.readline())
     worldstate_file.close()
 read_worldstate.last_checked_mtime = -1
 
 
+def read_message_queue():
+    while (len(message_queue["messages"]) > 1
+        or (len(message_queue["messages"]) == 0
+            and not message_queue["open_end"])):
+        message = message_queue["messages"].pop(0)
+        if message[0:4] == "LOG ":
+            world_data["log"] += [message[4:]]
+            cursed_main.redraw = True
+
+
 def cursed_main(stdscr):
 
     def ping_test():
@@ -240,17 +253,43 @@ def cursed_main(stdscr):
         ping_test()
         read_into_message_queue()
         read_worldstate()
+        read_message_queue()
+
+
+def win_foo():
+    winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
+    size = [4, 3]
+    offset = [0, 0]
+    return offset, size, winmap
 
 
-def foo():
-    #winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
-    #size = [4, 3]
-    winmap = str(world_data["turn"]).rjust(10)
-    size = [1, 10]
+def win_info():
+    winmap = "T: " + str(world_data["turn"]) \
+        + " H: " + str(world_data["lifepoints"]) \
+        + " S: " + str(world_data["satiation"])
+    size = [1, len(winmap)]
     offset = [0, 0]
     return offset, size, winmap
 
 
+def win_log():
+    win_size = next(win["size"] for win in windows if win["func"] == win_log)
+    offset = [0, 0]
+    winmap = ""
+    number_of_lines = 0
+    for line in world_data["log"]:
+        number_of_lines += math.ceil(len(line) / win_size[1])
+        padding_size = win_size[1] - (len(line) % win_size[1])
+        winmap += line + (padding_size * " ")
+    if number_of_lines < win_size[0]:
+        winmap = (" " * win_size[1] * (win_size[0] - number_of_lines)) + winmap
+        number_of_lines = win_size[0]
+    elif number_of_lines > win_size[0]:
+        offset[0] = number_of_lines - win_size[0]
+    winmap_size = [number_of_lines, win_size[1]]
+    return offset, winmap_size, winmap
+
+
 def command_quit():
     io["file_out"].write("QUIT\n")
     io["file_out"].flush()
@@ -258,11 +297,11 @@ def command_quit():
 
 
 windows = [
-    {"config": [1, 33], "func": foo},
-    {"config": [-7, 33], "func": foo},
-    {"config": [4, 16], "func": foo},
-    {"config": [4, 16], "func": foo},
-    {"config": [0, -34], "func": foo}
+    {"config": [1, 33], "func": win_info},
+    {"config": [-7, 33], "func": win_log},
+    {"config": [4, 16], "func": win_foo},
+    {"config": [4, 16], "func": win_foo},
+    {"config": [0, -34], "func": win_foo}
 ]
 io = {
     "path_out": "server/in",
@@ -277,6 +316,9 @@ message_queue = {
     "messages": []
 }
 world_data = {
+    "lifepoints": -1,
+    "log": [],
+    "satiation": -1,
     "turn": -1
 }
 sep_size = 1  # Width of inter-window borders and title bars.