From 8db0e200f8c2d279c68ca01b9ae235ab63c8f642 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 25 Oct 2020 02:48:53 +0100
Subject: [PATCH] Add map writing.

---
 new2/plomrogue/game.py    |  9 +++++++--
 new2/plomrogue/mapping.py | 13 +++++++++++++
 new2/plomrogue/tasks.py   | 13 +++++++++++++
 new2/rogue_chat.html      | 13 +++++++++----
 4 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py
index e8c85da..81bd530 100755
--- a/new2/plomrogue/game.py
+++ b/new2/plomrogue/game.py
@@ -1,4 +1,4 @@
-from plomrogue.tasks import Task_WAIT, Task_MOVE
+from plomrogue.tasks import Task_WAIT, Task_MOVE, Task_WRITE
 from plomrogue.errors import GameError
 from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY
 from plomrogue.io import GameIO
@@ -36,7 +36,8 @@ class Game(GameBase):
         self.changed = True
         self.io = GameIO(self)
         self.tasks = {'WAIT': Task_WAIT,
-                      'MOVE': Task_MOVE}
+                      'MOVE': Task_MOVE,
+                      'WRITE': Task_WRITE}
         self.map_geometry = MapGeometrySquare(YX(24, 40))
         self.commands = {'QUERY': cmd_QUERY,
                          'ALL': cmd_ALL,
@@ -47,8 +48,12 @@ class Game(GameBase):
         self.map = Map(self.map_geometry.size)
 
     def get_string_options(self, string_option_type):
+        import string
         if string_option_type == 'direction':
             return self.map_geometry.get_directions()
+        if string_option_type == 'char':
+            return [c for c in
+                    string.digits + string.ascii_letters + string.punctuation]
         return None
 
     def send_gamestate(self, connection_id=None):
diff --git a/new2/plomrogue/mapping.py b/new2/plomrogue/mapping.py
index 0a96058..cd0c23d 100644
--- a/new2/plomrogue/mapping.py
+++ b/new2/plomrogue/mapping.py
@@ -63,6 +63,19 @@ class Map():
         self.size = map_size
         self.terrain = '.' * self.size_i
 
+    def __getitem__(self, yx):
+        return self.terrain[self.get_position_index(yx)]
+
+    def __setitem__(self, yx, c):
+        pos_i = self.get_position_index(yx)
+        if type(c) == str:
+            self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
+        else:
+            self.terrain[pos_i] = c
+
     @property
     def size_i(self):
         return self.size.y * self.size.x
+
+    def get_position_index(self, yx):
+        return yx.y * self.size.x + yx.x
diff --git a/new2/plomrogue/tasks.py b/new2/plomrogue/tasks.py
index 0108416..fffb36a 100644
--- a/new2/plomrogue/tasks.py
+++ b/new2/plomrogue/tasks.py
@@ -25,6 +25,7 @@ class Task_WAIT(Task):
 
 
 class Task_MOVE(Task):
+    todo = 1
     argtypes = 'string:direction'
 
     def get_move_target(self):
@@ -38,3 +39,15 @@ class Task_MOVE(Task):
 
     def do(self):
         self.thing.position = self.get_move_target()
+
+
+
+class Task_WRITE(Task):
+    todo = 1
+    argtypes = 'string:char'
+
+    def check(self):
+        pass
+
+    def do(self):
+        self.thing.game.map[self.thing.position] = self.args[0]
diff --git a/new2/rogue_chat.html b/new2/rogue_chat.html
index f1146cb..5af97da 100644
--- a/new2/rogue_chat.html
+++ b/new2/rogue_chat.html
@@ -143,7 +143,9 @@ let game = {
 
 let chat = {
   input_line:"",
-  history: ["contain whitespace, escape them with \\.",
+  history: ["visible ASCII char in the input prompt.",
+	    "To write on the map, enter on a single",
+	    "contain whitespace, escape them with \\.",
 	    "Use double quotes for strings that",
 	    "Use arrow keys to move your avatar.",
 	    "  QUERY USER TEXT - send TEXT to USER",
@@ -169,8 +171,12 @@ document.addEventListener('keydown', (event) => {
     chat.input_line = chat.input_line.slice(0, -1);
     tui.draw_input_line();
   } else if (event.key === 'Enter') {
-    websocket.send(chat.input_line);
-    chat.input_line = ''
+    if (chat.input_line.length === 1) {
+      websocket.send("TASK:WRITE " + chat.input_line);
+    } else {
+      websocket.send(chat.input_line);
+    }
+    chat.input_line = '';
     tui.draw_input_line();
   } else if (event.key === 'ArrowLeft') {
     websocket.send('TASK:MOVE LEFT');
@@ -181,7 +187,6 @@ document.addEventListener('keydown', (event) => {
   } else if (event.key === 'ArrowDown') {
     websocket.send('TASK:MOVE DOWN');
   };
-  console.log(event.key);
 }, false);
 
 let websocket = new WebSocket(websocket_location);
-- 
2.30.2