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

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


import sys
import threading
import socket
Agrigor's avatar
Agrigor committed
11
12
13
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *
Agrigor's avatar
Agrigor committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


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


PORT = 50000
IP = "127.0.0.1"

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


class Comunication:
    def __init__(self, ip, port):
        self.__ip = ip
        self.__port = port

    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()

Agrigor's avatar
Agrigor committed
51
52
53
54
class Player:
    def __init__(self, Name):
        self.Points = 0
        self.Name = Name
Agrigor's avatar
Agrigor committed
55
56
57
58
59
60

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Agrigor's avatar
Agrigor committed
61
62
63
64
class GUI:
    def __init__(self):
        self.app = QApplication(sys.argv)
        self.UI_Start = loadUi("GUIs/start.ui")
Agrigor's avatar
Agrigor committed
65

Agrigor's avatar
Agrigor committed
66
        self.socketCreated = False
Agrigor's avatar
Agrigor committed
67
        self.players = []
Agrigor's avatar
Agrigor committed
68
69

        self.UI_Start.connect(self.UI_Start.I_Connect, SIGNAL("clicked()"), self.connect)
Agrigor's avatar
Agrigor committed
70
71
        self.UI_Start.connect(self.UI_Start.I_Start, SIGNAL("clicked()"), self.start)
        self.UI_Start.connect(self.UI_Start.I_NumberOfPlayers, SIGNAL("valueChanged(int)"), self.show_names_input)
Agrigor's avatar
Agrigor committed
72
73
74
75
76

        self.UI_Start.show()
        self.app.exec_()
        self.app.aboutToQuit(self.cleanup())

Agrigor's avatar
Agrigor committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    def show_names_input(self, number):
        number -= 1
        playerInputs = [self.UI_Start.I_NamePlayer1,
                        self.UI_Start.I_NamePlayer2,
                        self.UI_Start.I_NamePlayer3,
                        self.UI_Start.I_NamePlayer4]
        for i in range (len(playerInputs)):
            if i <= number:
                playerInputs[i].setDisabled(False)
            else:
                playerInputs[i].setDisabled(True)

    def start(self):
        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())):
            self.players.append(Player(namesOfPlayers[i]))

Agrigor's avatar
Agrigor committed
97
98
99
100
101
102
    def connect(self):
        self.ip = str(self.UI_Start.I_IPAdress.text())
        self.UDP = Comunication(self.ip, PORT)
        if self.UDP.check_ip():
            self.UDP.create_client_socket()
            self.socketCreated = True
Agrigor's avatar
Agrigor committed
103
            self.UI_Start.I_Start.setDisabled(False)
Agrigor's avatar
Agrigor committed
104
105
106
107
        else:
            print("First argument was no valid IP-address")
            self.socketCreated = False
            sys.exit(1)
Agrigor's avatar
Agrigor committed
108
109

    def cleanup(self):
Agrigor's avatar
Agrigor committed
110
111
112
        if self.socketCreated:
            self.UDP.cleanup()
        print("Bye Bye")
Agrigor's avatar
Agrigor committed
113
114
115
116
117
118
119
120
121
        sys.exit(0)


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# Start Client
Agrigor's avatar
Agrigor committed
122
client = GUI()
Agrigor's avatar
Agrigor committed
123
124
125
126

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~