Roborally I

Having had not much time for weekend projects in a while the urge to look at new thing to do has overgrown the lack of motiviation.

Thus: next stop – Roborally

We started plaing that game again during the christmas holiday period and got slightly addicted to it again, but as we all cam together from various locations (whereever love and job drove different parts of the family), there is no way to continue a game when not at home.

So at least for the basics (ideally a working prototype), I have started to look into digital versions of the game. Sadly there is little to be found – some old programms more than 10 years old, coded in Java and other languages I consider not useful for the purpose.

First battleplan:

  1. Analysis
  2. Basic implementation (cards, internal simulation, no graphical output)
  3. full implementation (including visualization of the board)
  4. advanced implementation (including the option to control multiple robots and implement the special cards)

1 and 2 should be easy to achieve, 3 and 4 will depend on time and whether or not I am willing to get myself into JS again 😉

If I am going to implement it, a basic design will be based on:

  • a webserver running the game (php and/or python)
  • clients connecting via browser (up to 8 players)

That would allow us to play a game online, turn based, regardless of the distance.

Telegram SMS gateway/bridge II

Having had the script running for a while, some points for improvement emerged:

  1. multi-sim support (I got two prepaid cards, that might be cancelled if not being used every now and then and it might be clever, to receive the warning messages 😉 )
  2. unicode – there are some messages coming in, that kill the script for encoding problem (need to look into telepot exception handling)
  3. storage (why not keeping messages copies in the database?)
  4. longer messages – I ceceived one from t-mobile which again killed the script

so, for a starter lets have a look at Nr 1 and 2:

I have created a dual-model environment following this post: smstools forum

To ensure the umts-sticks will have the correct (fixed) device name I modified the udev-rules as described here: debian handbook on udev

short version: I created udev-rules to create links to the devices as modem1, modem2…

 

root@tefrod:/home/greaman# cat /etc/udev/rules.d/99-usb-serial.rules
SUBSYSTEM=="tty",ACTION=="add", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1436", SYMLINK+="modem1"
SUBSYSTEM=="tty",ACTION=="add", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1001", SYMLINK+="modem2"

and of course modified the script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import telepot
import re
import string
import random
import os

class smsbot(telepot.Bot):

    def __init__ (self, uid, token, spooldir):
        super(smsbot, self).__init__(token)
        self._mode = 0
        self._recipient = ""
        self._active = 1
        self._user = uid
        self._spooldir = spooldir
        self.message_loop(self.handle)
        self._queues = {1:"GSM1",2:"GSM2"}
        self._modem  = 1

    def handle (self, msg):
        chat_id = msg['chat']['id']
        command = msg['text']
        if chat_id != self._user:
            self.sendMessage( chat_id, "unauthorized user")
            return 0
        res = re.match(r'^\\(\w+)\s?([\w\d]*)$', command, flags=0)
        if res:
            if      res.group(1) == "status":
                self.status()
            elif    res.group(1) == "setrcpt":
                self._recipient = res.group(2)
                self.sendMessage( chat_id, "recipient set to: %s" % self._recipient)
            elif    res.group(1) == "open":
                if self._recipient != "":
                    self._mode = 1
                    self.sendMessage( chat_id, "relay active")
                else:
                    self.sendMessage( chat_id, "set recipient first!")
            elif    res.group(1) == "close":
                self.sendMessage( chat_id, "relay deactived")
                self._mode = 0
            elif    res.group(1) == "mdmselect":
                self._modem = int(res.group(2))
                self.sendMessage( chat_id, "modem set")
            elif    res.group(1) == "terminate":
                self._active = 0
            else:
                self.sendMessage( chat_id, "unrecognized command!")
        else:
            if self._mode == 1 and self._recipient != "":
                self.sendsm ( command )
            else:
                self.sendMessage( chat_id, "relay not enabled")

    def id_generator(self, size=8, chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

    def status (self):
        self.sendMessage( self._user , "recipient: %s, relay status: %d" % (self._recipient, self._active))

    def sendsm (self, msg):
        # add function to handle longer messages
        output = "To: %s \n\n%s" % (self._recipient, msg)
        outlst = []
        if self._modem == 0:
          for key in self._queues:
            outlst.append(self._queues[key])
        else:
          outlst.append(self._queues[self._modem])
        for rcpt in outlst:
          filename = os.path.join (self._spooldir, rcpt, self.id_generator())
          fh = open (filename, "w")
          fh.write (output)
          fh.close()

    def recvsm (self ):
        for x in os.listdir(self._spooldir+"incoming/"):
            f = open(self._spooldir+"incoming/"+x, 'r',encoding='ISO-8859-15')
            cont = []
            for line in f:
                cont.append(line)
            self.sendMessage(self._user, cont[0]+"\n"+cont[3]+"\n"+cont[12])
            f.close()
            os.remove(self._spooldir+"incoming/"+x)


    def checkerr ( self ):
        for x in os.listdir(self._spooldir+"failed/"):
            self.sendMessage(self._user, "FAILED:")
            f = open(self._spooldir+"failed/"+x, 'r')
            ln=0
            for line in f:
                self.sendMessage(self._user, str(ln)+line)
                ln=ln+1
            f.close()
            os.remove(self._spooldir+"failed/"+x)

    def run (self ):
        while self._active == 1:
            self.recvsm ()
            self.checkerr ()
            time.sleep(5)


mybot = smsbot (000000, "#token here", "/var/spool/sms/")
mybot.run()

so the system will now receive short messages from two simcard (or more), send messages via those sims individually or simultaniously (just set modem to 0).

Having done that… I will look into the other topics… 😉