Kwissle
= socket.io + tornado + tornadio + mongodb

A real-time general knowledge battle game

Kwissle elevator pitch

Demo!

The titans

MongoKit

@register
class FlashMessage(BaseDocument):
    __collection__ = 'flash_messages'
    structure = {
      'user': User,
      'title': unicode,
      'text': unicode,
      'read': bool,
    }
    default_values = {
      'read': False,
      'text': u'',
    }
    required_fields = ['user', 'title']

MongoKit (2)

"ORM" (aka. ODM)

user_settings = self.db.UserSettings.one(_search)
if create_if_necessary and not user_settings:
    user_settings = self.db.UserSettings()
    user_settings.user = user
    user_settings.save()
return user_settings

Querying

for played_question in (self.db.PlayedQuestion.collection
                        .find({'question.$id': question._id,
                               'user.$id': {'$nin': bot_ids}})
                        .sort('add_date', 1)):

MongoLite (bonus slide!)

"MongoLite is a fork of MongoKit by the same author. It aims to come back to simplicity by stripping a lot of features and replacing them with best practices."

Tornado

Tornado code

@route('/login/')
class LoginHandler(tornado.web.RequestHandler):

    def get(self, errors=None):
        form = forms.LoginForm()
        self.render('login.html', form=form, errors=errors)

    def post(self):
        form = forms.LoginForm(data=request.arguments)
        if form.is_valid():
            user = self._login(form.data)
            self.set_secure_cookie('user', user.id)
            self.redirect('/')
        else:
            self.get(errors=form.errors)

tornadio

tornadio code

online = set()  # imagine this is Redis

class Client(tornadio.SocketConnection):
    def on_open(self, request, **__):
        self.send('Welcome!')
        self.name = request.cookies['name']
        online.add(self)

    def on_message(self, message):
        self.send({'you': message['shout'] + '!'})
        for clientin in online:
            if client is not self:
                each.send({self.name: message['shout']})

    def on_close(self):
        online.remove(self)

socket.io

socket.io code

// for socket.io 0.6
socket = new io.Socket(null, {port:8888, rememberTransport: false});

socket.on('connect', function() {
    console.log('Gloovy baby!');
});

socket.on('message', function(msg) {
    $('<strong>', {text: msg.user_name}).appendTo($('#log'));
    $('<span>', {text: msg.shout}).appendTo($('#log'));
    $('<br>').appendTo($('#log'));
});

socket.connect();

Lessons learned

More lessons learned (bonus slide!)

Kwissle's future

Kwissle's future version 2

The End

Challenge your friends and help out with more questions

http://kwissle.com