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