/
client.py
93 lines (81 loc) · 2.28 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
"""
client.py
Connect to a world, get the blocks
Print the entire world anytime it changes
if the user makes a change, send to the server
"""
import socket
import sys
import threading
import time
import world
host = ''
username = ''
port = 12345
c = None
class Connection(object):
"""Manages server connection socket"""
def __init__(self, username):
self.username = username
self.s = self.connect()
def connect(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((host, port))
s.send('connect:' + self.username)
return s
def send(self, data):
self.s.send(data)
def get_data(self):
while True:
try:
data = self.s.recv(4096)
print 'got: ' + data
return data
except socket.error:
print 'connection refused'
time.sleep(5)
print 'done sleeping'
self.s = self.connect()
def connection(username, terminal=False):
global c
c = Connection(username)
def update_world():
"""sync blocks"""
while True:
data = c.get_data()
cmd, args = data.split(':', 1)
if cmd == 'world':
world.reset(args)
elif cmd == 'update':
world.update(args)
elif cmd == 'move':
world.update_player(args)
else:
print 'bad server data'
a = threading.Thread(target=update_world)
a.daemon = True
a.start()
while terminal:
command = raw_input('command: ')
if command == 'save':
c.send('save')
elif command == 'load':
c.send('load')
else:
try:
world.update(command)
c.send('update:' + command)
except world.BlockFormatError:
print 'bad input'
world.show()
def blockupdate(name, pos):
# update the world and send to the server
x = world.make_string(name, pos)
#world.update(x)
c.send('update:' + x)
def playerupdate(name, player):
x = world.make_string_player(name, player)
c.send('move:' + x)
if __name__ == '__main__':
host = raw_input('host: ')
connection(raw_input('name: '), True)