[Twisted-Python] Thinking backwards, design ideas
andrew-twisted at puzzling.org
Sat Apr 19 23:16:09 EDT 2003
On Sat, Apr 19, 2003 at 10:46:59PM -0400, dansan wrote:
> Fellow twisters,
> I have finally read all that seemed necesary to get a good feel of
> twisted! and I have finally been able to implement a few 'play'
> I have a project I'd like to write, and I have always tought about it in
> the normal "use a library" fashion (in oppisition to the "use a
> framework"). My idea is to create a program that is a tournament
> manager, it works kind of like this:
> * Accepts connections (there are 2 types, admin, regular).
I'd say there's two types of *users*. There's no need to listen on
different ports, it's just a matter of who they authenticate as.
> * Listen to the admin connection
> - Start tournament at admin's command
> This would create tournament brackets and actually give the
> connected players the "play" command.
> - View current games
> - A bunch of other things :)
> Then when the admin starts the games, the program would spun a new "game
> thread" that would have the game logic, and that would keep track of the
> game (check legality of moves), tell player A to go, tell B what A play,
> tell B to go, decide on the winner, etc.
> But now I find my self trying to think twisted. I am no longer in
> control of the main program loop it seems. I see how I can keep some of
> my static info (having my Protocols register them in the Application).
> What I'm having a hard time with is envisioning how to get my games
> started, and how to keep track of them.
> * Where does my game-logic go?
I think you want a TournamentService, see
http://twistedmatrix.com/documents/howto/pb-cred for details of using
Services in the context of PB, but hopefully you can get something out of it
even if you don't use PB. See doc/examples/pbecho.py for an example of a
simple service. Basically, it's:
from twisted.internet.app import Application
from twisted.cred.service import Service
from twisted.cred.authorizer import DefaultAuthorizer
"""Game logic goes here"""
myApp = Application('Tournament')
auth = DefaultAuthorizer(myApp)
svc = TournamentService('Tournament', myApp, auth)
> * Do my protocols check for valid moves... if so, who keeps track of the
> board? The board is a share resource between 2 players of a short
> amount of time, and there are potentially many games going on at the
> same time.
I'd have a TournamentService keeping tracking of all the games running at
any time in a dict mapping players to a board. So when a player sends a
move, the service looks up the player's board, and tells the board what that
player X wants to play move Y, and the board then accepts that or raises an
exception. It's the board that knows what moves can be played at any given
time; the protocol's job is merely to pass messages to and from the player
on the network.
> * How can I implement timeouts? So that players have to play within
> given time-limits? (like, X seconds per-move kinds of deals?) I
> thougt about starting a timer in my Protocol when a "MAKE A MOVE"
> command is sent to the player, and then checking that time when the
> move is received... but this seems to involve threads?
There's plenty of stuff built into Twisted for doing timeouts already, that
doesn't involve threads or anything icky.
You can use reactor.callLater, as described here:
Depending on what you're doing, you may also find the setTimeout method of
Deferreds to be useful.
> Anyway, I'd really would appreciate your thoughts on this. This
> 'higher-design' stuff is keeping me from getting dirty :)
I hope I've made it clearer for you. Let us know if you're still unsure
[I think someone wrote a tic-tac-toe example, which might help a little
here... does anyone know where that is?]
More information about the Twisted-Python