home · contact · privacy
Split LOGIN into NICK and LOGIN.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 10 Nov 2020 00:15:35 +0000 (01:15 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 10 Nov 2020 00:15:35 +0000 (01:15 +0100)
plomrogue/commands.py
rogue_chat.py
rogue_chat_curses.py
rogue_chat_nocanvas_monochrome.html

index a80731da48336af7db5dbd9359a00ad56a4ad05f..c2484bf5d228ea1407e63caab79be1000554d3e0 100644 (file)
@@ -11,28 +11,35 @@ def cmd_ALL(game, msg, connection_id):
     game.io.send('CHAT ' + quote(t.nickname + ': ' + msg))
 cmd_ALL.argtypes = 'string'
 
-# TOOD split into two commands
 def cmd_LOGIN(game, nick, connection_id):
     for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
         raise GameError('name already in use')
     if connection_id in game.sessions:
-        t_id = game.sessions[connection_id]
-        t = game.get_thing(t_id, False)
-        old_nick = t.nickname
-        t.nickname = nick
-        game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
-    else:
-        t = game.thing_types['player'](game)
-        t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
-        game.things += [t]  # TODO refactor into Thing.__init__?
-        game.sessions[connection_id] = t.id_
-        game.io.send('LOGIN_OK', connection_id)
-        t.nickname = nick
-        game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
-        game.io.send('PLAYER_ID %s' % t.id_, connection_id)
+        raise GameError('cannot log in twice')
+    t = game.thing_types['player'](game)
+    t.position = YX(game.map.size.y // 2, game.map.size.x // 2)
+    game.things += [t]  # TODO refactor into Thing.__init__?
+    game.sessions[connection_id] = t.id_
+    game.io.send('LOGIN_OK', connection_id)
+    t.nickname = nick
+    game.io.send('CHAT ' + quote(t.nickname + ' entered the map.'))
+    game.io.send('PLAYER_ID %s' % t.id_, connection_id)
     game.changed = True
 cmd_LOGIN.argtypes = 'string'
 
+def cmd_NICK(game, nick, connection_id):
+    for t in [t for t in game.things if t.type_ == 'player' and t.nickname == nick]:
+        raise GameError('name already in use')
+    if not connection_id in game.sessions:
+        raise GameError('can only rename when already logged in')
+    t_id = game.sessions[connection_id]
+    t = game.get_thing(t_id, False)
+    old_nick = t.nickname
+    t.nickname = nick
+    game.io.send('CHAT ' + quote(old_nick + ' renamed themselves to ' + nick))
+    game.changed = True
+cmd_NICK.argtypes = 'string'
+
 def cmd_GET_GAMESTATE(game, connection_id):
     game.send_gamestate(connection_id)
 cmd_GET_GAMESTATE.argtypes = ''
index 026fcdf2be073689378385fb7d3d2aba7ef643d5..0b1ea23197e0042049cbce0215c71ac783ab1adf 100755 (executable)
@@ -2,8 +2,8 @@
 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_QUERY, cmd_PING, cmd_MAP,
-                                cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION,
+from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_QUERY, cmd_PING,
+                                cmd_MAP, cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION,
                                 cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE)
 from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE,
                              Task_FLATTEN_SURROUNDINGS)
@@ -16,6 +16,7 @@ savefile = sys.argv[1]
 game = Game(savefile)
 game.register_command(cmd_PING)
 game.register_command(cmd_LOGIN)
+game.register_command(cmd_NICK)
 game.register_command(cmd_QUERY)
 game.register_command(cmd_TURN)
 game.register_command(cmd_MAP)
index a6d1ca7a81e9f8632bd82d2bb9e482da8529aae8..01e6dc3627140f72b93d4d67762af7e8b750e275 100755 (executable)
@@ -549,7 +549,7 @@ class TUI:
                     elif self.input_.startswith('/nick'):
                         tokens = self.input_.split(maxsplit=1)
                         if len(tokens) == 2:
-                            self.send('LOGIN ' + quote(tokens[1]))
+                            self.send('NICK ' + quote(tokens[1]))
                         else:
                             self.log_msg('? need login name')
                     elif self.input_.startswith('/msg'):
index 307bf2619ba02c8f580fb10bf1dd21232d601012..c01f7ab6a4d2c9676c674790f84c71543153529c 100644 (file)
@@ -716,9 +716,9 @@ tui.inputEl.addEventListener('keydown', (event) => {
                     tui.log_help();
                 } else if (tokens[0].slice(1) == 'nick') {
                     if (tokens.length > 1) {
-                        server.send(['LOGIN', tokens[1]]);
+                        server.send(['NICK', tokens[1]]);
                     } else {
-                        tui.log_msg('? need login name');
+                        tui.log_msg('? need new name');
                     }
                 } else if (tokens[0].slice(1) == 'msg') {
                     if (tokens.length > 2) {