-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
157 lines (125 loc) · 4.91 KB
/
client.py
File metadata and controls
157 lines (125 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#PORTFOLIO 1 - s341848 - Izen Asmar Nasar
#TCP client
import socket,random,threading,sys,time,argparse
bots=["ash","misty","brock"]
# parser that checks if necessary arguments are provided
argParser = argparse.ArgumentParser(description='Connect to the chat room. Example: client.py localhost 4242 Steven')
argParser.add_argument('IP', type=str, help='IP address of the server the client is connecting to.')
argParser.add_argument('port', type=int, help='The port the client is connecting to (Integers only).')
argParser.add_argument('name', type=str, help='The name of the client. Connect as a host or a bot(Ash, Misty or Brock)')
args = argParser.parse_args()
IP = args.IP
port = args.port
name = args.name
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect((IP, port))
# list of good and bad actions
good_things=["sing", "play", "walk", "read", "train", "sleep"]
bad_things=["shout", "fight", "hack", "bicker", "complain", "steal"]
# all bot functions
def ash(verb):
if verb in good_things:
altAnswers = [
"{}: Did you say {}? That'd be amazing!".format(name,verb + "ing"),
"{}: I think {} sounds great!".format(name,verb + "ing"),
"{}: Finally we can start {}!".format(name,verb + "ing")
]
return random.choice(altAnswers)
elif verb in bad_things:
altAnswers2 = [
"{}: {}? You must be out of your mind!".format(name,verb + "ing"),
"{}: I'm not a fan of {}, sorry.".format(name,verb + "ing"),
"{}: Yeah I don't know about {}...".format(name,verb+"ing")
]
return random.choice(altAnswers2)
else:
return "{}: Sorry, but that's just boring..".format(name)
def misty(verb):
altAnswers3 = [
"{}: {}...? You can't be serious..".format(name,verb + "ing"),
"{}: {} sounds really boring..".format(name,verb + "ing"),
"{}: I've got better things to do than {}..".format(name,verb+"ing")
]
if verb in bad_things or verb in good_things:
return random.choice(altAnswers3)
else:
altAnswers4 = [
"{}: Nope. Not doing that.".format(name),
"{}: You really expect me to do that?".format(name),
"{}: I don't want to do that.".format(name)
]
return random.choice(altAnswers4)
def brock(verb):
if verb in bad_things:
altAnswers5 = [
"{}: {}?? I am not mentally prepared for that yet.".format(name,verb+"ing"),
"{}: You know how I feel about {}...".format(name,verb + "ing"),
"{}: Ooh {}? Meh..".format(name,verb+"ing")
]
return random.choice(altAnswers5)
elif verb in good_things:
altAnswers6 = [
"{}: {}?? Sure, why not?".format(name,verb+"ing"),
"{}: {} it is then.".format(name,verb + "ing"),
"{}: I mean.. I guess {} isn't that bad.".format(name,verb+"ing")
]
return random.choice(altAnswers6)
else:
return "{}: I am uncertain...".format(name)
# handles the messages received from other clients
def clientReceive():
while True:
msg = clientSocket.recv(1024).decode('utf-8')
if msg == "name?":
clientSocket.send(name.encode('utf-8'))
else:
if ":" in msg:
msgSplit = msg.split(": ")
if msgSplit[0] not in bots:
v=""
i=0
while i<len(bad_things):
if bad_things[i] in msg.lower():
v=bad_things[i]
if good_things[i] in msg.lower():
v=good_things[i]
i+=1
botmsg=""
if name.lower() == "ash":
botmsg=ash(v)
elif name.lower() == "misty":
botmsg=misty(v)
elif name.lower() == "brock":
botmsg=brock(v)
print(msg)
clientSend(botmsg)
else:
time.sleep(0.5)
print(msg)
else:
print(msg)
def clientSend(msg):
print(msg)
clientSocket.send(msg.encode('utf-8'))
# forwards a host-client's message
def clientMessager():
while True:
try:
msg = f'{name}: {input()}'
split = msg.split(": ")
if(split[1].isspace() or split[1] == ""):
print("Can't send an empty string. Please write something!")
continue
else:
time.sleep(0.4)
print(msg)
clientSocket.send(msg.encode('utf-8'))
except:
print("\nYou have disconnected from the chat room\n")
sys.exit()
break
receive_thread = threading.Thread(target=clientReceive)
receive_thread.start()
if name not in bots:
send_thread = threading.Thread(target=clientMessager)
send_thread.start()