Skip to content

Instantly share code, notes, and snippets.

@iamgreaser
Created July 25, 2011 07:33
Show Gist options
  • Save iamgreaser/1103707 to your computer and use it in GitHub Desktop.
Save iamgreaser/1103707 to your computer and use it in GitHub Desktop.
mafia idea with decorators
# Filter base stuff
def recv_identity(f, game, mtype, src, dest, **kwargs):
f(game, mtype, src, dest, **kwargs)
def send_identity(f, game, mtype, src, dests, **kwargs):
for dest in dests:
game.enqueue(f, mtype, src, dest, **kwargs)
class ClassFilter:
def __init__(self, __baseclass):
self.__baseclass = __baseclass
self.__init__ = __baseclass.__init__
def __getitem__(self, idx):
self.__baseclass.getattr(idx)
def __setitem__(self, idx, val):
self.__baseclass.setattr(idx, val)
# Message decorators
def dec_msgmod(fb):
return fb
def dec_msg_filter_recv(fb):
def _filt():
_recv = dest.recv
dest.recv = fb
return _filt
def dec_msg_filter_send(fb):
def _filt():
_send = dest.send
dest.send = fb
return _filt
def dec_msgmod_copresponse(fb):
def _dec1(f):
class _OppositeAlignment(ClassFilter):
copresponse = f
def _filt_mod_insane(game, mtype, src, dest, **kwargs):
if mtype == "cop":
dest = _OppositeAlignment(game)
return _filt_mod_insane
return _dec1
# Messages
def msg_response(game, mtype, src, dest, public, contents):
if public:
dest.send_public_response(contents)
else:
dest.send_response(contents)
def msg_kill(game, mtype, src, dest):
dest.kill()
def copmessage(resp, nick):
if resp:
return "You're sure that %s is the mafia!" % nick
else:
return "Nobody in their right mind would suspect %s is the mafia." % nick
def msg_cop(game, mtype, src, dest):
resp = dest.ismafia()
dest.send(msg_response, game, mtype, dest, [src], public=False, contents=copmessage(resp), copresponse=resp)
@dec_msg_filter_send
def msg_block(game, mtype, src, dest):
pass # ... still all there is to it
@dec_msg_filter_recv
def msg_doctor(game, mtype, src, dest):
if mtype == "kill":
pass # TODO? report that you saved them?
else:
_recv(f, game, mtype, src, dest, **kwargs)
# Message modifiers
@dec_msgmod
def msgmod_quack(game, mtype, src, dest, **kwargs):
pass # yep, that's all
@dec_msgmod_copresponse
def msgmod_insane(self):
return not self.__baseclass.copresponse()
@dec_msgmod_copresponse
def msgmod_naive(self):
self.__baseclass.copresponse()
return False
@dec_msgmod_copresponse
def msgmod_paranoid(self):
self.__baseclass.copresponse()
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment