gui.py 7.3 KB
Newer Older
Agrigor's avatar
Agrigor committed
1
#!/usr/bin/python3
Agrigor's avatar
Agrigor committed
2
3
4
5
6
7
8

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


import sys
9
#import psutil
Agrigor's avatar
Agrigor committed
10
11
import threading
import socket
12
import time
Agrigor's avatar
Agrigor committed
13
14
15
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
Agrigor's avatar
Agrigor committed
16
17
18
19
20
21
22

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PORT = 50000
IP = "127.0.0.1"
23
PLAYERS_MAX = 4
Agrigor's avatar
Agrigor committed
24
25
26
27
28
29
30
31
32
33

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


class Comunication:
    def __init__(self, ip, port):
        self.__ip = ip
        self.__port = port
34
        self.socketCreated = False
Agrigor's avatar
Agrigor committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

    def check_ip(self):
        try:
            socket.inet_aton(self.__ip)
            return True
        except OSError:
            return False

    def create_client_socket(self):
        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.__socket.bind(("", self.__port))

    def receive(self, bytes):
        return self.__socket.recvfrom(bytes)

    def cleanup(self):
        self.__socket.close()

53

Agrigor's avatar
Agrigor committed
54
class Player:
55
56
57
    def __init__(self):
        self.points = 0
        self.name = ""
Agrigor's avatar
Agrigor committed
58
59


Agrigor's avatar
Agrigor committed
60
61
62
63
64
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Agrigor's avatar
Agrigor committed
65
66
class GUI:
    def __init__(self):
67
68
69
70
71
        self.players = [Player() for i in range(PLAYERS_MAX)]
        self.__app = QApplication(sys.argv)
        self.__start = StartGUI(self.players)
        self.__app.exec_()
        #self.kill_proc_tree(os.getpid())
Agrigor's avatar
Agrigor committed
72

73
74
75
76
    #def kill_proc_tree(pid, including_parent=True):
        #parent = psutil.Process(pid)
        #if including_parent:
            #parent.kill()
Agrigor's avatar
Agrigor committed
77

78
79
80


class StartGUI:
81
    def __init__(self, players):
82
83
84
        self.__UI_Start = loadUi("GUIs/start.ui")

        self.players = players
85
86
87
88
89
        self.__player_name_entered = [False] * PLAYERS_MAX
        self.__playerNameInputs = [self.__UI_Start.I_NamePlayer1,
                                   self.__UI_Start.I_NamePlayer2,
                                   self.__UI_Start.I_NamePlayer3,
                                   self.__UI_Start.I_NamePlayer4]
90
91
92

        self.__UI_Start.connect(self.__UI_Start.I_Connect, SIGNAL("clicked()"), self.connect)
        self.__UI_Start.connect(self.__UI_Start.I_Start, SIGNAL("clicked()"), self.start)
Agrigor's avatar
Agrigor committed
93
94
95
96
97
        self.__UI_Start.connect(self.__UI_Start.I_NumberOfPlayers, SIGNAL("valueChanged(int)"), self.show_names_input_field)
        self.__UI_Start.connect(self.__UI_Start.I_NamePlayer1, SIGNAL("textChanged(QString)"), self.name_player_changed)
        self.__UI_Start.connect(self.__UI_Start.I_NamePlayer2, SIGNAL("textChanged(QString)"), self.name_player_changed)
        self.__UI_Start.connect(self.__UI_Start.I_NamePlayer3, SIGNAL("textChanged(QString)"), self.name_player_changed)
        self.__UI_Start.connect(self.__UI_Start.I_NamePlayer4, SIGNAL("textChanged(QString)"), self.name_player_changed)
98
99
100

        self.__UI_Start.show()

101
102
103
104
105
106
107
108
109
110
    def connect(self):
        self.socket = Comunication(str(self.__UI_Start.I_IPAdress.text()), PORT)
        if self.socket.check_ip() and not self.socket.socketCreated:
            self.socket.create_client_socket()
            self.socket.socketCreated = True
            self.__UI_Start.I_Connect.setDisabled(True)
            self.__UI_Start.I_IPAdress.setDisabled(True)
            self.show_msg("Connected")
            self.__UI_Start.I_NumberOfPlayers.setDisabled(False)
            self.__UI_Start.I_NamePlayer1.setDisabled(False)
Agrigor's avatar
Agrigor committed
111
        else:
112
113
            self.show_msg("No valid IP-address")
            self.socket.socketCreated = False
114

Agrigor's avatar
Agrigor committed
115
    def show_names_input_field(self, number):
Agrigor's avatar
Agrigor committed
116
        number -= 1
117
118
119
        for i in range(len(self.__playerNameInputs)):
            if i <= number and self.socket.socketCreated:
                self.__playerNameInputs[i].setDisabled(False)
Agrigor's avatar
Agrigor committed
120
            else:
121
                self.__playerNameInputs[i].setDisabled(True)
Agrigor's avatar
Agrigor committed
122
        if number >= 1:
123
            self.show_msg(str(number + 1) + " Players")
Agrigor's avatar
Agrigor committed
124
        else:
125
            self.show_msg("1 Player")
Agrigor's avatar
Agrigor committed
126

127
128
129
130
131
132
133
134
135
136
137
    def name_player_changed(self, name):
        for i in range(len(self.__playerNameInputs)):
            if i + 1 > int(self.__UI_Start.O_NumberOfPlayers.text()) or len(self.__playerNameInputs[i].text()) > 1:
                self.__player_name_entered[i] = True
            else:
                self.__player_name_entered[i] = False
        if all(self.__player_name_entered):
            self.__UI_Start.I_Start.setDisabled(False)
        else:
            self.__UI_Start.I_Start.setDisabled(True)

Agrigor's avatar
Agrigor committed
138
    def start(self):
139
140
141
142
143
        namesOfPlayers = [str(self.__UI_Start.I_NamePlayer1.text()),
                          str(self.__UI_Start.I_NamePlayer2.text()),
                          str(self.__UI_Start.I_NamePlayer3.text()),
                          str(self.__UI_Start.I_NamePlayer4.text())]
        for i in range(0, int(self.__UI_Start.O_NumberOfPlayers.text())):
144
145
146
            self.players[i].name = namesOfPlayers[i]
        self.main = MainGUI(self.socket, self.players)
        self.__UI_Start.close()
Agrigor's avatar
Agrigor committed
147

148
149
150
    def show_msg(self, msg):
        self.__UI_Start.O_Msg.setText(msg)

151
    # TODO Include this method if window is closed by user
Agrigor's avatar
Agrigor committed
152
    def cleanup(self):
153
154
        if self.socket.socketCreated:
            self.socket.cleanup()
Agrigor's avatar
Agrigor committed
155
156


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class MainGUI:
    def __init__(self, socket, players):
        self.UI_Main = loadUi("GUIs/main.ui")

        self.socket = socket
        self.players = players

        self.UI_Main.connect(self.UI_Main.Button, SIGNAL("clicked()"), self.button)
        self.UI_Main.connect(self.UI_Main.Button_2, SIGNAL("clicked()"), self.button_2)
        self.UI_Main.show()

    def button_2(self):
        self.stop_event.set()

    def button(self):
        self.stop_event = threading.Event()
        self.c_thread = threading.Thread(target=self.listen_communication, args=(self.stop_event,))
        self.c_thread.start()

    def listen_communication(self, stop_event):
        while True and not stop_event.isSet():
            daten, addr = self.socket.receive(1024)
            self.show_msg(addr[0] + " -> " + daten.decode())
            time.sleep(0.1)

    def show_msg(self, msg):
        self.UI_Main.O_Msg.setText(msg)

    # TODO Include this method if window is closed by user
    def cleanup(self):
        if self.socket.socketCreated:
            self.socket.cleanup()

Agrigor's avatar
Agrigor committed
190
191
192
193
194
195
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# Start Client
Agrigor's avatar
Agrigor committed
196
client = GUI()
Agrigor's avatar
Agrigor committed
197

198

Agrigor's avatar
Agrigor committed
199
200
201
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~