Added sound handling. Added create new room and get room list to client and server

This commit is contained in:
Manuel Cortez 2020-05-18 04:59:26 -05:00
parent 6a195bc0c4
commit 8e628e5b38
7 changed files with 160 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import wx
import client import client
import output import output
import gui import gui
import sound
from pubsub import pub from pubsub import pub
# Client instance. # Client instance.
@ -19,9 +20,12 @@ class controller(object):
self.window = window self.window = window
self.connect_events() self.connect_events()
self.window.Show() self.window.Show()
self.next_action = ""
def connect_events(self): def connect_events(self):
self.window.chat.Bind(wx.EVT_CHAR_HOOK, self.on_process) self.window.chat.Bind(wx.EVT_CHAR_HOOK, self.on_process)
self.window.list.Bind(wx.EVT_LISTBOX_DCLICK, self.on_process_listbox_click)
self.window.list.Bind(wx.EVT_CHAR_HOOK, self.on_process_listbox)
pub.subscribe(self.response, "response") pub.subscribe(self.response, "response")
pub.subscribe(self.ask_login, "ask_login") pub.subscribe(self.ask_login, "ask_login")
pub.subscribe(self.disconnected, "disconnected") pub.subscribe(self.disconnected, "disconnected")
@ -32,6 +36,23 @@ class controller(object):
self.send_message() self.send_message()
event.Skip() event.Skip()
def on_process_listbox_click(self, event):
selected_option = self.window.menu_items[self.window.list.GetSelection()][0]
if selected_option == "create_room":
data = dict(action="create_room")
c.send_data(0, data)
elif selected_option == "join_room":
data = dict(action="request_room_list")
c.send_data(0, data)
if event != None:
event.Skip()
def on_process_listbox(self, event):
key = event.GetKeyCode()
if key == wx.WXK_RETURN:
self.on_process_listbox_click(None)
event.Skip()
def send_message(self): def send_message(self):
global c global c
message = self.window.chat.GetValue() message = self.window.chat.GetValue()
@ -48,6 +69,7 @@ class controller(object):
getattr(self, "cmd_"+command)(data) getattr(self, "cmd_"+command)(data)
def cmd_connected(self, data): def cmd_connected(self, data):
self.window.enable_app()
connected = data.get("nickname") connected = data.get("nickname")
msg = "{} has entered this platform".format(connected) msg = "{} has entered this platform".format(connected)
self.window.add_message(msg) self.window.add_message(msg)
@ -57,8 +79,19 @@ class controller(object):
nickname = data.get("nickname") nickname = data.get("nickname")
msg = "{0}: {1}".format(nickname, msg) msg = "{0}: {1}".format(nickname, msg)
output.speak(msg) output.speak(msg)
sound.sound.play("chat.ogg")
self.window.add_message(msg) self.window.add_message(msg)
def cmd_create_room(self, data):
self.window.list.Clear()
msg = "{} Has created a room.".format(data.get("nickname"))
self.window.add_message(msg)
output.speak(msg)
def cmd_room_list(self, data):
print(data)
def ask_login(self): def ask_login(self):
global c global c
data = dict(action="login", nickname=self.username) data = dict(action="login", nickname=self.username)
@ -71,6 +104,7 @@ class controller(object):
def setup(): def setup():
global c, t global c, t
output.setup() output.setup()
sound.setup()
app = wx.App() app = wx.App()
d = gui.loginDialog() d = gui.loginDialog()
f = gui.appFrame() f = gui.appFrame()

View File

@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
import os
from platform_utils import paths as paths_
def app_path():
return paths_.app_path()
def locale_path():
return os.path.join(app_path(), "locales")
def sound_path():
return os.path.join(app_path(), "sounds")

View File

@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
import threading
import os
import subprocess
import paths
import sound_lib
import output
from sound_lib import recording, stream
from sound_lib import output, input
def recode_audio(filename, quality=10):
subprocess.call(r'"%s" --downmix -q %r "%s"' % (os.path.join(paths.app_path(), 'oggenc2.exe'), quality, filename))
def get_recording(filename):
val = recording.WaveRecording(filename=filename)
return val
class RepeatingTimer(threading.Thread):
"""Call a function after a specified number of seconds, it will then repeat again after the specified number of seconds
Note: If the function provided takes time to execute, this time is NOT taken from the next wait period
t = RepeatingTimer(30.0, f, args=[], kwargs={})
t.start()
t.cancel() # stop the timer's actions
"""
def __init__(self, interval, function, daemon=True, *args, **kwargs):
threading.Thread.__init__(self)
self.daemon = daemon
self.interval = float(interval)
self.function = function
self.args = args
self.kwargs = kwargs
self.finished = threading.Event()
def cancel(self):
"""Stop the timer if it hasn't finished yet"""
self.finished.set()
stop = cancel
def run(self):
while not self.finished.is_set():
self.finished.wait(self.interval)
if not self.finished.is_set(): #In case someone has canceled while waiting
try:
self.function(*self.args, **self.kwargs)
except:
print("Execution failed. Function: %r args: %r and kwargs: %r" % (self.function, self.args, self.kwargs))
class soundSystem(object):
def __init__(self):
""" Sound Player."""
# Set the output and input default devices.
try:
self.output = output.Output()
self.input = input.Input()
except:
pass
self.files = []
self.cleaner = RepeatingTimer(60, self.clear_list)
self.cleaner.start()
def clear_list(self):
if len(self.files) == 0: return
try:
for i in range(0, len(self.files)):
if self.files[i].is_playing == False:
self.files[i].free()
self.files.pop(i)
except IndexError:
pass
def play(self, sound, argument=False):
sound_object = stream.FileStream(file="%s/%s" % (paths.sound_path(), sound))
self.files.append(sound_object)
sound_object.play()
sound = None
def setup():
global sound
sound = soundSystem()

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,5 @@
import string
import random
from enetcomponents import server from enetcomponents import server
class channel(server.channel): class channel(server.channel):
@ -29,6 +31,25 @@ class channel(server.channel):
if channel.room == self.room: if channel.room == self.room:
channel.send_data(0, data) channel.send_data(0, data)
def cmd_create_room(self, data):
code = ''.join(random.choices(string.ascii_uppercase +string.digits, k = 10))
existing = True
while existing:
existing = self.server.room_exists(code)
if existing == True:
code = ''.join(random.choices(string.ascii_uppercase +string.digits, k = 10))
self.server.rooms.append(code)
data.update(nickname=self.nickname)
self.room = code
self.send_data(0, data)
def cmd_request_room_list(self, data):
rooms = dict()
for room in self.server.rooms:
players = self.server.get_players_in_room(room)
rooms[room]= players
self.send_data(0, dict(action="room_list", rooms=rooms))
class server(server.server): class server(server.server):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -45,6 +66,15 @@ class server(server.server):
del self.peers[channel] del self.peers[channel]
break break
def room_exists(self, room_id):
for i in self.rooms:
if room_id == i:
return True
return False
def get_players_in_room(self, room_id):
return [player.nickname for player in self.peers if player.room == room_id]
if __name__ == "__main__": if __name__ == "__main__":
print("Starting chat server...") print("Starting chat server...")
s = server() s = server()