remote: added player management to game class
This commit is contained in:
parent
0960c2e020
commit
cdb96bab01
|
@ -6,7 +6,7 @@ import random
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, device, gameName, gameChannel, announcechannel, announcemac):
|
def __init__(self, device, gameName, gameChannel, announcechannel, announcemac, maxplayer=0):
|
||||||
self.gameName = gameName
|
self.gameName = gameName
|
||||||
self.channel = gameChannel
|
self.channel = gameChannel
|
||||||
self.gamemac = [int(random.random()*254) for x in range(1,6)]
|
self.gamemac = [int(random.random()*254) for x in range(1,6)]
|
||||||
|
@ -25,6 +25,49 @@ class Game:
|
||||||
|
|
||||||
self.sendAnnounce()
|
self.sendAnnounce()
|
||||||
|
|
||||||
|
self.maxplayer = maxplayer
|
||||||
|
self.players = {}
|
||||||
|
self.callbacks = []
|
||||||
|
self.queue = Queue.Queue()
|
||||||
|
self.bridge.registerQueue(self.queue)
|
||||||
|
self.bridge.registerCallback(self.receivedPacket)
|
||||||
|
self.checkPlayers()
|
||||||
|
|
||||||
|
def checkPlayers(self):
|
||||||
|
if self.maxplayer > 0:
|
||||||
|
toremove = []
|
||||||
|
for player in self.players:
|
||||||
|
self.players[player]-=1
|
||||||
|
if self.players[player] == 0:
|
||||||
|
toremove.append(player)
|
||||||
|
for player in toremove:
|
||||||
|
print "removing player", player
|
||||||
|
del self.players[player]
|
||||||
|
for callback in self.callbacks:
|
||||||
|
callback("removed", player)
|
||||||
|
self.timer = threading.Timer(1, self.checkPlayers)
|
||||||
|
self.timer.start()
|
||||||
|
|
||||||
|
def receivedPacket(self, packet):
|
||||||
|
if self.maxplayer == 0:
|
||||||
|
return
|
||||||
|
if isinstance(packet, packets.Join):
|
||||||
|
# flags = 1: join ok
|
||||||
|
# flags = 0: join not ok
|
||||||
|
flags = 0
|
||||||
|
if len(self.players) < self.maxplayer:
|
||||||
|
flags = 1
|
||||||
|
self.players[packet.id] = 10
|
||||||
|
for callback in self.callbacks:
|
||||||
|
callback("added", packet.id)
|
||||||
|
|
||||||
|
ack = packets.Ack(packet.id, packet.ctr, flags)
|
||||||
|
qp = bridge.QueuePacket(
|
||||||
|
self.channel, self.playermac, False, ack)
|
||||||
|
self.bridge.putInQueue(self.queue, qp)
|
||||||
|
elif packet.id in self.players:
|
||||||
|
self.players[packet.id] = 10
|
||||||
|
|
||||||
def sendAnnounce(self):
|
def sendAnnounce(self):
|
||||||
aq = bridge.QueuePacket(self.announcechannel,
|
aq = bridge.QueuePacket(self.announcechannel,
|
||||||
self.announcemac, False, self.announce)
|
self.announcemac, False, self.announce)
|
||||||
|
@ -32,3 +75,6 @@ class Game:
|
||||||
self.announcetimer = threading.Timer(1, self.sendAnnounce)
|
self.announcetimer = threading.Timer(1, self.sendAnnounce)
|
||||||
self.announcetimer.start()
|
self.announcetimer.start()
|
||||||
|
|
||||||
|
def registerPlayerCallback(self, callback):
|
||||||
|
if not callback in self.callbacks:
|
||||||
|
self.callbacks.append(callback)
|
||||||
|
|
|
@ -4,24 +4,9 @@ import r0ketrem0te.packets
|
||||||
import time
|
import time
|
||||||
import Queue
|
import Queue
|
||||||
|
|
||||||
maxplayer = 2
|
|
||||||
players = {}
|
|
||||||
|
|
||||||
def receivedPacket(packet):
|
def receivedPacket(packet):
|
||||||
if isinstance(packet, r0ketrem0te.packets.Join):
|
pass
|
||||||
# flags = 1: join ok
|
game = r0ketrem0te.game.Game('/dev/ttyACM0', "testgame", 83, 81, (1,2,3,2,1), 2)
|
||||||
# flags = 0: join not ok
|
|
||||||
flags = 0
|
|
||||||
if len(players) < maxplayer:
|
|
||||||
flags = 1
|
|
||||||
players[packet.id] = 10
|
|
||||||
ack = r0ketrem0te.packets.Ack(packet.id, packet.ctr, flags)
|
|
||||||
qp = r0ketrem0te.bridge.QueuePacket(game.channel, game.playermac, False, ack)
|
|
||||||
game.bridge.putInQueue(queue, qp)
|
|
||||||
elif packet.id in players:
|
|
||||||
players[packet.id] = 10
|
|
||||||
|
|
||||||
game = r0ketrem0te.game.Game('/dev/ttyACM0', "testgame", 83, 81, (1,2,3,2,1))
|
|
||||||
|
|
||||||
queue = Queue.Queue()
|
queue = Queue.Queue()
|
||||||
game.bridge.registerQueue(queue)
|
game.bridge.registerQueue(queue)
|
||||||
|
@ -29,11 +14,3 @@ game.bridge.registerCallback(receivedPacket)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
toremove = []
|
|
||||||
for player in players:
|
|
||||||
players[player]-=1
|
|
||||||
if players[player] == 0:
|
|
||||||
toremove.append(player)
|
|
||||||
for player in toremove:
|
|
||||||
print "removing player", player
|
|
||||||
del players[player]
|
|
||||||
|
|
Loading…
Reference in New Issue