Skip to content

Commit

Permalink
report when players are playing
Browse files Browse the repository at this point in the history
  • Loading branch information
cflavio committed Feb 14, 2018
1 parent deeb0f9 commit 6ace358
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 20 deletions.
32 changes: 26 additions & 6 deletions menu/multiplayer/button.py
@@ -1,5 +1,6 @@
from direct.gui.DirectGuiGlobals import ENTER, EXIT
from direct.gui.DirectGuiGlobals import ENTER, EXIT, NORMAL, DISABLED
from direct.gui.DirectLabel import DirectLabel
from direct.gui.DirectButton import DirectButton
from panda3d.core import TextNode
from yyagl.engine.gui.imgbtn import ImgBtn
from yyagl.gameobject import GameObject
Expand All @@ -23,6 +24,12 @@ def __init__(self, parent, owner, menu_args, img_path, msg_btn_x, cb, usr,
extraArgs=[usr], **menu_args.imgbtn_args)
self.btn.bind(ENTER, self.on_enter)
self.btn.bind(EXIT, self.on_exit)
self.tooltip_btn = DirectButton(
parent=parent, scale=.024, pos=(msg_btn_x, 1, .01),
frameColor=(1, 1, 1, 0), frameSize=(-1, 1, -1, 1), command=None,
**menu_args.imgbtn_args)
self.tooltip_btn.bind(ENTER, self.on_enter)
self.tooltip_btn.bind(EXIT, self.on_exit)
self.on_create()
self.tooltip = DirectLabel(
text=tooltip, pos=self.btn.get_pos() + self.tooltip_offset,
Expand All @@ -31,17 +38,30 @@ def __init__(self, parent, owner, menu_args, img_path, msg_btn_x, cb, usr,
self.tooltip.set_bin('gui-popup', 10)
self.tooltip.hide()

def on_create(self): self.btn.hide()
def on_create(self):
self.btn.hide()
self.tooltip_btn.hide()

def is_hidden(self): return self.btn.is_hidden()

def show(self): return self.btn.show()
def show(self):
if self.btn['state'] == DISABLED:
self.tooltip_btn.show()
else:
self.tooltip_btn.hide()
return self.btn.show()

def hide(self): return self.btn.hide()
def hide(self):
self.tooltip_btn.hide()
return self.btn.hide()

def enable(self): return self.btn.enable()
def enable(self):
self.tooltip_btn.hide()
return self.btn.enable()

def disable(self): return self.btn.disable()
def disable(self):
self.tooltip_btn.show()
return self.btn.disable()

def on_enter(self, pos):
self.owner.on_enter(pos)
Expand Down
4 changes: 2 additions & 2 deletions menu/multiplayer/forms.py
Expand Up @@ -102,14 +102,14 @@ def enable_invite_btn(self, enable=True): pass

class UserFrmList(UserFrm):

def __init__(self, name, name_full, is_supporter, is_friend, pos, parent,
def __init__(self, name, name_full, is_supporter, is_friend, is_in_yorg, is_playing, pos, parent,
menu_args):
UserFrm.__init__(
self, name, name_full, is_supporter, pos, parent, menu_args)
lab_args = menu_args.label_args
lab_args['scale'] = .046
lab_args['text_fg'] = self.menu_args.text_normal
self.__enable_invite_btn = True
self.__enable_invite_btn = is_in_yorg and not is_playing
self.invite_btn = MPBtn(
self.frm, self, menu_args, 'assets/images/gui/invite.txo',
.65, self.on_invite, name_full, _("isn't playing yorg"))
Expand Down
4 changes: 2 additions & 2 deletions menu/multiplayer/matchfrm.py
Expand Up @@ -77,13 +77,13 @@ def on_presence_unavailable_room(self, msg):
@property
def users_names(self):
clean = lambda lab: lab.replace('\1smaller\1', '').replace('\2', '')
return [clean(frm.lab['text']) for frm in self.forms]
return [clean(frm.lab.lab['text']) for frm in self.forms]

def set_frm_pos(self, frm, i):
row, col = i % 4, i / 4
x = .1 + 1.24 * col
y = .38 - .08 * row
frm.frm.set_pos(x, 1, y)
frm.frm.set_pos((x, 1, y))

def on_declined(self, msg):
usr = str(JID(msg['from']).bare)
Expand Down
34 changes: 33 additions & 1 deletion menu/multiplayer/multiplayerfrm.py
Expand Up @@ -48,6 +48,7 @@ def __init__(self, menu_args):
self.eng.xmpp.attach(self.on_cancel_invite)
self.eng.xmpp.attach(self.on_ip_address)
self.eng.xmpp.attach(self.on_yorg_init)
self.eng.xmpp.attach(self.on_is_playing)

def create_match_frm(self, room):
self.match_frm = MatchFrm(self.menu_args)
Expand Down Expand Up @@ -104,6 +105,12 @@ def on_yorg_init(self, msg):
usr.is_in_yorg = True
self.users_frm.on_users()

def on_is_playing(self, msg):
self.eng.log('is playing ' + str(msg['from']))
usr = [user for user in self.eng.xmpp.users if user.name == str(msg['from'].bare)][0]
usr.is_playing = msg['body'] == '1'
self.users_frm.on_users()

def on_presence_available(self, msg):
self.users_frm.on_users()

Expand All @@ -118,7 +125,8 @@ def on_presence_unavailable_room(self, msg):
if str(msg['muc']['nick']) == self.users_frm.in_match_room:
self.exit_dlg = ExitDialog(self.menu_args, msg)
self.exit_dlg.attach(self.on_exit_dlg)
if str(msg['muc']['nick']) == self.eng.xmpp.client.boundjid.bare:
if str(msg['muc']['nick']) == self.eng.xmpp.client.boundjid.bare and \
self.match_frm and msg['muc']['room'] == self.match_frm.room:
self.removed_dlg = RemovedDialog(self.menu_args, msg)
self.removed_dlg.attach(self.on_remove_dlg)

Expand Down Expand Up @@ -163,6 +171,14 @@ def on_room_back(self):
self.users_frm.invited_users = []
self.users_frm.in_match_room = None
self.users_frm.room_name = None
for usr_name in ['ya2_yorg@jabb3r.org'] + \
[_usr.name_full for _usr in self.eng.xmpp.users if _usr.is_in_yorg]:
self.eng.xmpp.client.send_message(
mfrom=self.eng.xmpp.client.boundjid.full,
mto=usr_name,
mtype='ya2_yorg',
msubject='is_playing',
mbody='0')
self.on_users()

def on_msg(self, msg):
Expand All @@ -182,6 +198,14 @@ def on_groupchat_msg(self, msg):
def on_invite_chat(self, msg):
self.invite_dlg = InviteDialog(self.menu_args, msg)
self.invite_dlg.attach(self.on_invite_answer)
for usr_name in ['ya2_yorg@jabb3r.org'] + \
[_usr.name_full for _usr in self.eng.xmpp.users if _usr.is_in_yorg]:
self.eng.xmpp.client.send_message(
mfrom=self.eng.xmpp.client.boundjid.full,
mto=usr_name,
mtype='ya2_yorg',
msubject='is_playing',
mbody='1')

def on_invite_answer(self, msg, val):
self.invite_dlg.detach(self.on_invite_answer)
Expand Down Expand Up @@ -222,6 +246,14 @@ def on_invite_answer(self, msg, val):
msubject='declined',
mtype='ya2_yorg',
mbody=str(msg['body']))
for usr_name in ['ya2_yorg@jabb3r.org'] + \
[_usr.name_full for _usr in self.eng.xmpp.users if _usr.is_in_yorg]:
self.eng.xmpp.client.send_message(
mfrom=self.eng.xmpp.client.boundjid.full,
mto=usr_name,
mtype='ya2_yorg',
msubject='is_playing',
mbody='1')

def on_declined(self, msg):
self.eng.log('on declined')
Expand Down
30 changes: 24 additions & 6 deletions menu/multiplayer/usersfrm.py
Expand Up @@ -83,22 +83,22 @@ def on_users(self):
if self.conn_lab:
self.conn_lab.destroy()
bare_users = [self.trunc(user.name, 20)
for user in self.eng.xmpp.users]
for user in self.eng.xmpp.users_nodup]
for lab in self.labels[:]:
_lab = lab.lab.lab['text'].replace('\1smaller\1', '').replace('\2', '')
if _lab not in bare_users:
if _lab not in self.eng.xmpp.client.client_roster.keys():
lab.destroy()
self.labels.remove(lab)
nusers = len(self.eng.xmpp.users)
nusers = len(self.eng.xmpp.users_nodup)
invite_btn = len(self.invited_users) < 8
invite_btn = invite_btn and not self.in_match_room
top = .08 * nusers + .08
self.frm['canvasSize'] = (-.02, .76, 0, top)
label_users = [lab.lab.lab['text'] for lab in self.labels]
clean = lambda n: n.replace('\1smaller\1', '').replace('\2', '')
label_users = map(clean, label_users)
for i, user in enumerate(self.eng.xmpp.users):
for i, user in enumerate(self.eng.xmpp.users_nodup):
usr_inv = invite_btn and user.is_in_yorg
if self.trunc(user.name, 20) not in label_users:
if self.eng.xmpp.client.boundjid.bare != user.name:
Expand All @@ -107,6 +107,8 @@ def on_users(self):
user,
user.is_supporter,
self.eng.xmpp.is_friend(user.name),
user.is_in_yorg,
user.is_playing,
(0, 1, top - .08 - .08 * i),
self.frm.getCanvas(),
self.menu_args)
Expand All @@ -123,12 +125,20 @@ def on_users(self):
lab.attach(self.on_friend)
lab.attach(self.on_unfriend)
lab.attach(self.on_add_chat)
for i, user in enumerate(self.eng.xmpp.users):
for i, user in enumerate(self.eng.xmpp.users_nodup):
clean = lambda n: n.replace('\1smaller\1', '').replace('\2', '')
lab = [lab for lab in self.labels
if clean(lab.lab.lab['text']) == self.trunc(user.name, 20)][0]
lab.enable_invite_btn(
usr_inv and user.name not in self.invited_users)
enb_val = usr_inv and user.name not in self.invited_users and user.is_in_yorg and not user.is_playing
if hasattr(lab, 'invite_btn'):
inv_btn = lab.invite_btn
if enb_val: inv_btn.tooltip['text'] = _('invite the user to a match')
elif len(self.invited_users) == 8: inv_btn.tooltip['text'] = _("you can't invite more players")
elif self.in_match_room: inv_btn.tooltip['text'] = _("you're already in a match")
elif not user.is_in_yorg: inv_btn.tooltip['text'] = _("the user isn't playing yorg")
elif user.name in self.invited_users: inv_btn.tooltip['text'] = _("you've already invited this user")
elif user.is_playing: inv_btn.tooltip['text'] = _("the user is already playing a match")
lab.enable_invite_btn(enb_val)
lab.frm.set_z(top - .08 - .08 * i)
lab.lab.set_supporter(user.is_supporter)

Expand All @@ -150,6 +160,14 @@ def on_invite(self, usr):
cfg.set_values(values)
self.eng.xmpp.client.plugin['xep_0045'].configureRoom(self.room_name, cfg)
self.eng.log('created room ' + self.room_name)
for usr_name in ['ya2_yorg@jabb3r.org'] + \
[_usr.name_full for _usr in self.eng.xmpp.users if _usr.is_in_yorg]:
self.eng.xmpp.client.send_message(
mfrom=self.eng.xmpp.client.boundjid.full,
mto=usr_name,
mtype='ya2_yorg',
msubject='is_playing',
mbody='1')
public_addr = load(urlopen('http://httpbin.org/ip'))['origin']
sock = socket(AF_INET, SOCK_DGRAM)
try:
Expand Down
2 changes: 0 additions & 2 deletions project.py
Expand Up @@ -3,8 +3,6 @@
# TODO LIST =====

# 0.9 (network)
# users connected both in-game and with a chat client
# disable invite button for in-room and invited players (less alpha)
# manage protocol acks
# server's address == client's one -> use the local one
# do a rapid start for network
Expand Down
2 changes: 1 addition & 1 deletion yyagl
Submodule yyagl updated 1 files
+21 −4 engine/network/xmpp.py

0 comments on commit 6ace358

Please sign in to comment.