This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
104 lines (92 loc) · 2.54 KB
/
client.py
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
from parser import *
from classes import *
from time import sleep
import signal
import threading
import netifaces as ni
from sys import exit
def signal_handler(signal, frame):
print("Shutting down..")
send("DISCONNECT " + player.name)
sleep(1) # shutdown grace time
sock.close()
exit(0)
signal.signal(signal.SIGINT, signal_handler)
MSG_LEN = 4096
players = {}
def receive():
return sock.recv(MSG_LEN).decode()
def receiveFromServer():
global players
while True:
msg = receive().strip().split()
print("received: ", msg)
if not msg:
print("Disconnected from server. Shutting down..")
sock.close()
exit(2)
token = msg.pop(0)
if token == "CONNECT":
p = msg.pop(0)
players[p] = Player(p)
print("added player", p)
elif token == "DISCONNECT":
p = msg.pop(0)
del players[p]
print("removed player", p)
elif token == "MOVE":
p = msg.pop(0)
d = msg.pop(0)
move(p, d)
elif token == "SET":
obj = msg.pop(0)
msg.pop(0) # skip 'IN'
room = msg.pop(0)
msg.pop(0) # skip 'TO'
status = msg.pop(0)
if status == "OPEN":
rooms[room].objects[obj].trigger("open")
elif status == "BREAK":
rooms[room].objects[obj].trigger("break")
elif token == "DECREASE": #seperate for attack on player and attack on entitie?
msg.pop(0) # skip 'HEALTH'
msg.pop(0) # skip 'OF'
p = msg.pop(0)
msg.pop(0) # skip 'BY'
amount = msg.pop(0)
if p.lower() in ["wizard", "dark lord", "goblin"]:
enemy.lose_health(amount)
else:
players[p].lose_health(amount)
# elif token == "COPY":==>UNKNOWN USE
# item = msg.pop(0)
# msg.pop(0) # skip 'FROM'
# p = msg.pop(0)
# player.take_item(players[p].inventory[item])
else:
print("Unkown action in receiveFromServer()")
break
print("Textlive - a multiplayer text-adventure")
print("---------------------------------------")
# PLAYER NAME
while player.name == "":
player.name = input("Please enter the name of your Character: ").strip()
# HOST ADDRESS
host = input("Enter Host Address: ")
if host == "": # use local address if none is specified
addrs = ni.ifaddresses('wlan0')
if ni.AF_INET in addrs.keys():
host = addrs[ni.AF_INET].pop(0)['addr']
else:
host = "127.0.0.1"
# INIT SOCKET
sock.connect((host, 9555))
send("CONNECT " + player.name)
# CREATE THREAD FOR SERVER COMMUNICATION
threading.Thread(target=receiveFromServer, daemon=True).start()
print("---------------------------------------\n")
print(player.room.desc)
print("What do you do?")
while True:
readCmd()
pOperation()