remote: added nick transport
This commit is contained in:
parent
8978eca3fa
commit
d51f5c9caf
|
@ -38,7 +38,7 @@ struct packet{
|
|||
}__attribute__((packed)) text;
|
||||
struct nick{
|
||||
uint8_t flags;
|
||||
uint8_t text[18];
|
||||
uint8_t nick[18];
|
||||
}__attribute__((packed)) nick;
|
||||
struct nickrequest{
|
||||
uint8_t reserved[19];
|
||||
|
@ -124,7 +124,7 @@ void playGame(void)
|
|||
sendButton(button);
|
||||
|
||||
while(1){
|
||||
len = nrf_rcv_pkt_time(30,sizeof(p),(uint8_t*)&p);
|
||||
len = nrf_rcv_pkt_time(32,sizeof(p),(uint8_t*)&p);
|
||||
if(len==sizeof(p)){
|
||||
processPacket(&p);
|
||||
}else{
|
||||
|
@ -256,7 +256,21 @@ uint8_t selectGame()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void processNickRequest( struct nickrequest *nq)
|
||||
{
|
||||
struct packet p;
|
||||
p.len=sizeof(p);
|
||||
p.protocol='G'; // Proto
|
||||
p.command='n';
|
||||
p.id= id;
|
||||
p.ctr= ++ctr;
|
||||
p.c.nick.flags = 0;
|
||||
uint8_t *nick = GLOBAL(nickname);
|
||||
strcpy(p.c.nick.nick, nick);
|
||||
//p.c.nick.nick[0] = 'S';
|
||||
//p.c.nick.nick[1] = 0;
|
||||
nrf_snd_pkt_crc(sizeof(p),(uint8_t*)&p);
|
||||
}
|
||||
|
||||
void processPacket(struct packet *p)
|
||||
{
|
||||
|
@ -265,7 +279,7 @@ void processPacket(struct packet *p)
|
|||
//processText(&(p->c.text));
|
||||
}
|
||||
else if (p->command=='N'){
|
||||
//processNickRequest(&(p->c.nickrequest));
|
||||
processNickRequest(&(p->c.nickrequest));
|
||||
}
|
||||
else if (p->command=='A'){
|
||||
processAnnounce(&(p->c.announce));
|
||||
|
@ -298,17 +312,3 @@ void sendButton(uint8_t button)
|
|||
nrf_snd_pkt_crc(sizeof(p),(uint8_t*)&p);
|
||||
}
|
||||
|
||||
//send join request for game
|
||||
void sendJoin(uint32_t game)
|
||||
{
|
||||
struct packet p;
|
||||
p.len=sizeof(p);
|
||||
p.protocol='G';
|
||||
p.command='J';
|
||||
p.ctr= ++ctr;
|
||||
p.id=id;
|
||||
p.c.join.gameId=game;
|
||||
|
||||
nrf_snd_pkt_crc(sizeof(p),(uint8_t*)&p);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@ class Pong:
|
|||
pygame.mixer.pre_init(22050, -16, 2, 1024)
|
||||
pygame.init()
|
||||
|
||||
self.rem0te = r0ketrem0te.game.Game('/dev/ttyACM0', "pong", 83, 81, (1,2,3,2,1), 2)
|
||||
self.rem0te = r0ketrem0te.game.Game('/dev/ttyACM0', "pong", 83,
|
||||
81, [ord(x) for x in 'REM0T'], 2, True)
|
||||
self.rem0te.registerPlayerCallback(self.playercallback)
|
||||
|
||||
self.player_right = Rem0tePlayer(self.rem0te)
|
||||
|
@ -38,21 +39,24 @@ class Pong:
|
|||
self.start = False
|
||||
self.restart()
|
||||
|
||||
|
||||
def playercallback(self, action, player):
|
||||
if action == 'added':
|
||||
if self.player_left.player == 0:
|
||||
if self.player_left.player == None:
|
||||
self.player_left.player = player
|
||||
elif self.player_right.player == 0:
|
||||
elif self.player_right.player == None:
|
||||
self.player_right.player = player
|
||||
if self.player_left.player and self.player_right.player:
|
||||
self.start = True
|
||||
elif action == 'removed':
|
||||
print 'got remove for', player.nick
|
||||
if self.player_left.player == player:
|
||||
self.player_left.player = 0
|
||||
print 'removing left player'
|
||||
self.player_left.player = None
|
||||
elif self.player_right.player == player:
|
||||
self.player_right.player = 0
|
||||
if self.player_left.player == 0 or self.player_right.player == 0:
|
||||
print 'removing right player'
|
||||
self.player_right.player = None
|
||||
if self.player_left.player == None or self.player_right.player == None:
|
||||
print 'halting game'
|
||||
self.stop = True
|
||||
|
||||
def restart(self):
|
||||
|
@ -77,9 +81,6 @@ class Pong:
|
|||
self.restart()
|
||||
self.start = False
|
||||
self.stop = False
|
||||
if self.stop:
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
|
||||
self.clock.tick(60)
|
||||
now = pygame.time.get_ticks()
|
||||
|
@ -88,10 +89,25 @@ class Pong:
|
|||
print self.clock.get_fps()
|
||||
self.input_state['key'] = pygame.key.get_pressed()
|
||||
self.input_state['mouse'] = pygame.mouse.get_pos()
|
||||
self.game.update()
|
||||
if not self.stop:
|
||||
self.game.update()
|
||||
self.game.draw(self.output_surface)
|
||||
|
||||
|
||||
#~ pygame.surfarray.pixels_alpha(output_surface)[:,::2] = 12
|
||||
self.display_surface.blit(self.output_surface, (0,0))
|
||||
|
||||
font = pygame.font.Font(None, 36)
|
||||
if self.player_left.player:
|
||||
text = font.render(self.player_left.player.nick, 1, (0, 255, 0))
|
||||
textpos = text.get_rect(centerx=self.output_surface.get_width()/4)
|
||||
self.display_surface.blit(text, textpos)
|
||||
|
||||
if self.player_right.player:
|
||||
text = font.render(self.player_right.player.nick, 1, (0, 255, 0))
|
||||
textpos = text.get_rect(centerx=self.output_surface.get_width()/4*3)
|
||||
self.display_surface.blit(text, textpos)
|
||||
|
||||
if self.debug_surface:
|
||||
self.display_surface.blit(self.debug_surface, (0,0))
|
||||
pygame.display.flip()
|
||||
|
|
|
@ -11,10 +11,12 @@ class Rem0tePlayer(object):
|
|||
self.rem0te = rem0te
|
||||
self.rem0te.bridge.registerCallback(self.receivedPacket)
|
||||
self.state = 0
|
||||
self.player = 0
|
||||
self.player = None
|
||||
|
||||
def receivedPacket(self, packet):
|
||||
if packet.id == self.player:
|
||||
if self.player == None:
|
||||
return
|
||||
if packet.id == self.player.id:
|
||||
if isinstance(packet, r0ketrem0te.packets.Button):
|
||||
self.state = packet.button
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import threading
|
|||
import Queue
|
||||
import crcmod
|
||||
import packets
|
||||
import traceback
|
||||
|
||||
class QueuePacket:
|
||||
def __init__(self, channel, mac, acked, packet, callback=None):
|
||||
|
@ -158,6 +159,7 @@ class Bridge:
|
|||
self.setChannel(self.gameChannel)
|
||||
except Exception as e:
|
||||
print e
|
||||
traceback.print_stack()
|
||||
|
||||
def readerThread(self):
|
||||
while True:
|
||||
|
@ -167,20 +169,21 @@ class Bridge:
|
|||
self.newPacket(data)
|
||||
elif command == '2':
|
||||
self.free.release()
|
||||
elif command:
|
||||
while True:
|
||||
pass
|
||||
except Exception as e:
|
||||
print e
|
||||
traceback.print_stack()
|
||||
|
||||
def newPacket(self, data):
|
||||
#print "received:", list(data)
|
||||
print "received:", list(data)
|
||||
crc = self.crc(data[:-2])
|
||||
if data[-2:] == chr(crc>>8) + chr(crc&0xFF):
|
||||
packet = packets.fromMessage(data)
|
||||
print "received:", packet
|
||||
if packet.id in self.ctrs and self.ctrs[packet.id] == packet.ctr:
|
||||
if packet == None:
|
||||
return
|
||||
#if packet.id in self.ctrs and self.ctrs[packet.id] == packet.ctr:
|
||||
# print 'ignoring duplicate'
|
||||
# return
|
||||
if isinstance(packet,packets.Ack):
|
||||
self.ProcessAck(packet)
|
||||
else:
|
||||
|
|
|
@ -5,8 +5,15 @@ import Queue
|
|||
import random
|
||||
import threading
|
||||
|
||||
class Player():
|
||||
def __init__(self, id):
|
||||
self.id = id
|
||||
self.nick = 'anonymous'
|
||||
self.timeout = 10
|
||||
self.active = False
|
||||
|
||||
class Game:
|
||||
def __init__(self, device, gameName, gameChannel, announcechannel, announcemac, maxplayer=0):
|
||||
def __init__(self, device, gameName, gameChannel, announcechannel, announcemac, maxplayer=0, askname=False):
|
||||
self.gameName = gameName
|
||||
self.channel = gameChannel
|
||||
self.gamemac = [int(random.random()*254) for x in range(1,6)]
|
||||
|
@ -22,6 +29,7 @@ class Game:
|
|||
self.bridge.registerQueue(self.announcequeue)
|
||||
self.announcechannel = announcechannel
|
||||
self.announcemac = announcemac
|
||||
self.askname = askname
|
||||
|
||||
self.sendAnnounce()
|
||||
|
||||
|
@ -36,15 +44,23 @@ class Game:
|
|||
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)
|
||||
for id in self.players:
|
||||
player = self.players[id]
|
||||
player.timeout-=1
|
||||
if player.timeout == 0:
|
||||
toremove.append(id)
|
||||
for id in toremove:
|
||||
player = self.players[id]
|
||||
if self.askname:
|
||||
print "removing player", player.nick
|
||||
else:
|
||||
print "removing player", id
|
||||
|
||||
del self.players[id]
|
||||
if player.active:
|
||||
player.active = False
|
||||
for callback in self.callbacks:
|
||||
callback("removed", player)
|
||||
self.timer = threading.Timer(1, self.checkPlayers)
|
||||
self.timer.start()
|
||||
|
||||
|
@ -57,16 +73,34 @@ class Game:
|
|||
flags = 0
|
||||
if len(self.players) < self.maxplayer:
|
||||
flags = 1
|
||||
self.players[packet.id] = 10
|
||||
for callback in self.callbacks:
|
||||
callback("added", packet.id)
|
||||
self.players[packet.id] = Player(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
|
||||
print "player known:", packet.id
|
||||
player = self.players[packet.id]
|
||||
player.timeout = 10
|
||||
if not player.active and isinstance(packet, packets.Button):
|
||||
if self.askname:
|
||||
nickrequest = packets.Nickrequest(packet.id)
|
||||
qp = bridge.QueuePacket(self.channel,
|
||||
self.playermac, False, nickrequest)
|
||||
self.bridge.putInQueue(self.queue, qp)
|
||||
else:
|
||||
player.active = True
|
||||
for callback in self.callbacks:
|
||||
callback("added", player)
|
||||
elif not player.active and isinstance(packet, packets.Nick):
|
||||
if self.askname:
|
||||
player.nick = packet.nick
|
||||
player.active = True
|
||||
for callback in self.callbacks:
|
||||
callback("added", player)
|
||||
else:
|
||||
print "player unknown"
|
||||
|
||||
def sendAnnounce(self):
|
||||
aq = bridge.QueuePacket(self.announcechannel,
|
||||
|
|
|
@ -137,6 +137,11 @@ class Nickrequest(Packet):
|
|||
s = "Nickrequest packet with " + self.headerString()
|
||||
return s
|
||||
|
||||
def toMessage(self):
|
||||
message = Packet.toMessage(self)
|
||||
message += '\x00'*19
|
||||
return message
|
||||
|
||||
class Nick(Packet):
|
||||
def __init__(self, id, flags=None, nick=None):
|
||||
if flags != None and nick != None:
|
||||
|
|
|
@ -6,8 +6,8 @@ import Queue
|
|||
|
||||
def receivedPacket(packet):
|
||||
pass
|
||||
game = r0ketrem0te.game.Game('/dev/ttyACM0', "testgame", 83, 81, (1,2,3,2,1), 2)
|
||||
|
||||
game = r0ketrem0te.game.Game('/dev/ttyACM0', "testgame", 83,
|
||||
81, [ord(x) for x in 'REM0T'], 2, True)
|
||||
queue = Queue.Queue()
|
||||
game.bridge.registerQueue(queue)
|
||||
game.bridge.registerCallback(receivedPacket)
|
||||
|
|
Loading…
Reference in New Issue