• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sky Warden

Python Twisted question: need help on deferred

4 posts in this topic

Hi everyone. I'm writing a Twisted based daemon for a simple text-based multiplayer game. The game logic goes like this. The client sends a string in a particular format (command.arguments) to the server. The server receives that string, split it, and run the command along with the arguments. I already made it working.

 

One of the command called has to get data from database. I'm using Twistar to make things fancy.  The problem is, I need to return the result of the deferred made by the query. I know it sounds so newbie, but this is my first Twisted program, and I'm still learning.

 

Well, I think the scripts will make things clearer:

 

The server.py (don't worry, it's inside a code tag):

[spoiler]

from twisted.application import internet, service

import protocols, services


udp_port = 10000
multicast_addr = '234.0.0.1'

dbuser = 'testuser'
dbpass = 'password'

main_service = service.MultiService()

game_service = services.GameService(dbuser, dbpass)
game_service.setServiceParent(main_service)

udp_service = internet.MulticastServer(udp_port, protocols.MulticastGameProtocol(multicast_addr, game_service), listenMultiple=True)
udp_service.setServiceParent(main_service)

application = service.Application('Simple Thing')

main_service.setServiceParent(application)

 

[/spoiler]

 

The protocols.py

 

[spoiler]

 

from twisted.internet.protocol import DatagramProtocol


class MulticastGameProtocol(DatagramProtocol):

    def __init__(self, multicast_addr, service):
        self.multicast_addr = multicast_addr
        self.service = service


    def startProtocol(self):
        self.transport.setTTL(5)
        self.transport.joinGroup(self.multicast_addr)

        
    def datagramReceived(self, datagram, address):
        print 'Datagram %s received from %s' % (repr(datagram), repr(address))
        if '.' not in datagram:
            return 'Invalid command format.'

        celcommand_name, arguments = datagram.split('.', 1)
            
        self.celcommandRequestReceived(celcommand_name, arguments, address)

        
    def celcommandRequestReceived(self, celcommand_name, arguments, address):
        command_result = self.doCommand(celcommand_name, arguments)
        
        if command_result is not None:
            self.transport.write(command_result, address)

            
    def doCommand(self, celcommand_name, arguments):
        submitted_command = getattr(self, 'celcommand_%s' % (celcommand_name), None)
        
        if submitted_command is None:
            return 'Invalid command.'
            
        try:
            #return 'COMMAND SUCCESS!!!'
            return repr(submitted_command(arguments))
        except:
            #return 'Command failed.'
            return repr(submitted_command(arguments))
            
    def celcommand_login(self, arguments):
        username, password = arguments.split('.', 1)
        return self.service.login(username, password)

[/spoiler]

 

The services.py

[spoiler]

 

from twisted.application import service
from twisted.python import log
from twisted.internet import defer

from twistar.dbobject import DBObject

class Users(DBObject):
    pass
    
class GameService(service.Service):

    def __init__(self, dbuser, dbpass):
        self.dbuser = dbuser
        self.dbpass = dbpass
        
    def startService(self):
        service.Service.startService(self)
        
        from twisted.enterprise import adbapi
        from twistar.registry import Registry

        Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user=dbuser, passwd=dbpass, db='test')
        self.user = Users()
        
        log.msg('Made connection to database.')
        
    def login(self, username, password):
        self.loginUser = self.user.find(where=['username="%s" AND password_hash="%s"' % (username, password)], limit=1)
        self.loginUser.addCallback(self.checkLogin)
        return self.loginUser
        
    def checkLogin(self, query_result):
        if query_result is None:
            return 'Login failed!'
        else:
            print '%s logged in' % (query_result.username)
            return query_result.username
 

[/spoiler]

 

I'm sure the error is in the login function. I think it's like saying "Wait. My result isn't here yet" by returning a deferred, but it will be the deferred which is sent to the client.

 

Well, there's no complicated logic yet. I started writing this just two days ago. All I want is to send the query_result.username to the client. The query is fine. I can print that thingy on the server.

 

What I want : the login() function returns the result of the query (a list, string or whatever) so I can send it to the client.

What I get : the login() function returns a deferred object.

 

The client is just a simple client which sends 'login.Sky Warden.123'. The arguments will be separated by the '.' as well.

 

I think that's all. Maybe it's just me who don't fully understand deferreds, or it's just a logic hole. Or maybe both. XD

 

Thank you everyone. smile.png

Edited by Sky Warden
0

Share this post


Link to post
Share on other sites

Did you ever figure this out?

 

I tried to follow the logic, but it looks like when you add checkLogin as a callback you don't have an opportunity to specify what the values of the parameters are. Then when checkLogin does get called back, its query_result parameter is not initialized and is set to None. Maybe I missed some subtlety of Python, but I don't see you sending parameter values into checkLogin.

0

Share this post


Link to post
Share on other sites

Did you ever figure this out?

 

I tried to follow the logic, but it looks like when you add checkLogin as a callback you don't have an opportunity to specify what the values of the parameters are. Then when checkLogin does get called back, its query_result parameter is not initialized and is set to None. Maybe I missed some subtlety of Python, but I don't see you sending parameter values into checkLogin.

Thanks for the reply, Steve.

 

As far as I know, the result (in this case, the query result) will be passed to the callback (checkLogin) as the first argument. I think it is correct considering that query_result can be printed out on the server.

 

I think the error is in the login() function. It has to return something. It currently returns a deferred, but somehow, when the deferred fires and we get the result, the login() doesn't seem returning query_result.

 

By the way, do people use this kind of command-line thingy in multiplayer game?

Edited by Sky Warden
0

Share this post


Link to post
Share on other sites

What exactly is the problem? You've said there's a problem and an error, but I don't see you specifying exactly what you can't do, or what error is occurring.

 

If the issue is that checkLogin goes some data but you don't know what to do with it, then there are various approaches. Either have it store a value locally and poll for it from elsewhere, or register a callback which checkLogin can call for you when itself is called, passing in the information it's received.

0

Share this post


Link to post
Share on other sites

What exactly is the problem? You've said there's a problem and an error, but I don't see you specifying exactly what you can't do, or what error is occurring.

 

If the issue is that checkLogin goes some data but you don't know what to do with it, then there are various approaches. Either have it store a value locally and poll for it from elsewhere, or register a callback which checkLogin can call for you when itself is called, passing in the information it's received.

Eh, I haven't specified it? Sorry then.

What I want is the login() to return the result of the query, so the calling function can get it. Like query_result.id. Since login() needs to run query then it's an asynchronous function. That means it returns a deferred. Then it does the query, the deferred fires and then checkLogin runs.

The function login() is called by another function. Simply I want that function (which calls login()) gets the result of the query (string), not a deferred object.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0