-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
162 lines (121 loc) · 5.4 KB
/
game.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
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
158
159
160
161
162
import re
import time
UNKNOWN_COMMAND_EMPTY = "Unknown command: . Please check that the command exists and that you have permission to use it."
RE_INFO = "^\[....-..-.. ..:..:.. INFO] .*$"
RE_INFO_SERVER_STARTED = "^\[....-..-.. ..:..:.. INFO] Server started.$"
RE_INFO_PLAYER_CONNECTED = "^\[....-..-.. ..:..:.. INFO] Player connected: (.*), xuid: .*$"
RE_INFO_PLAYER_DISCONNECTED = "^\[....-..-.. ..:..:.. INFO] Player disconnected: (.*), xuid: .*$"
RE_FOUND_PLAYERS = "Found (.*)"
NO_TARGETS = "No targets matched selector"
BLOCK_ERROR = "Cannot place block outside of the world"
players = []
class Event:
def __init__(self, eventType, eventData = {}):
self.type = eventType
self.data = eventData
class Player:
def __init__(self, name):
self.name = name
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def toCommandString(self):
return "{} {} {}".format(self.x, self.y, self.z)
def toSelectorString(self, radius = 1):
return "[x={},y={},z={},r={}]".format(self.x, self.y, self.z, radius)
class Volume:
def __init__(self, x, y, z, dx, dy, dz):
self.x = x
self.y = y
self.z = z
self.dx = dx
self.dy = dy
self.dz = dz
def toCommandString(self):
return "{} {} {} {} {} {}".format(self.x, self.y, self.z, self.dx, self.dy, self.dz)
def toSelectorString(self):
return "[x={},y={},z={},dx={},dy={},dz={}]".format(self.x, self.y, self.z, self.dx, self.dy, self.dz)
class GameInterface:
def __init__(self, cli):
self.cli = cli
self.eventStdout = []
def sendCommand(self, command, readOutput = True):
self.cli.poll()
self.cli.readstdout()
self.cli.writestdin(command + "\n")
self.cli.writestdin("\n") # Send a blank command so that we know when command output is complete
if readOutput:
output = ""
lastMessage = ""
timeout = time.time() + 1
while timeout != 0:
self.cli.poll()
lastMessage = self.cli.readstdout()
for line in lastMessage.splitlines():
if line == UNKNOWN_COMMAND_EMPTY: # Test to see if command output is complete
return output.rstrip()
elif re.compile(RE_INFO).match(line): # Skip INFO lines, add them to the event queue stdout only
self.eventStdout.append(line)
continue
output += line + "\n"
if time.time() > timeout:
break
else:
# Clear stdout
self.cli.poll()
self.cli.readstdout()
def captureEvents(self):
events = []
self.cli.poll()
stdout = self.cli.readstdout()
for line in stdout.splitlines():
if re.compile(RE_INFO).match(line):
self.eventStdout.append(line)
for line in self.eventStdout:
if re.compile(RE_INFO_SERVER_STARTED).match(line):
events.append(Event("serverStart"))
if re.compile(RE_INFO_PLAYER_CONNECTED).match(line):
playerName = re.compile(RE_INFO_PLAYER_CONNECTED).match(line).group(1)
events.append(Event("playerJoin", {
"name": playerName
}))
players.append(Player(playerName))
if re.compile(RE_INFO_PLAYER_DISCONNECTED).match(line):
playerName = re.compile(RE_INFO_PLAYER_DISCONNECTED).match(line).group(1)
events.append(Event("playerLeave", {
"name": playerName
}))
for i in range(0, len(players)):
if players[i].name == playerName:
players.pop(i)
self.eventStdout = []
return events
def sendChatMessage(self, message, targetPlayers = None):
if targetPlayers == None:
targetPlayers = "@a"
if isinstance(targetPlayers, str):
self.sendCommand("tellraw @a {{\"rawtext\": [{{\"text\": \"{}\"}}]}}".format(message.replace("\"", "\\\"")), False)
else:
for player in targetPlayers:
self.sendCommand("tellraw \"{}\" {{\"rawtext\": [{{\"text\": \"{}\"}}]}}".format(player.name, message.replace("\"", "\\\"")), False)
def observeVolume(self, volume):
playersInVolume = []
playerResult = self.sendCommand("testfor @e" + volume.toSelectorString())
if playerResult != NO_TARGETS and playerResult != BLOCK_ERROR and re.compile(RE_FOUND_PLAYERS).match(str(playerResult)) != None:
playerList = re.compile(RE_FOUND_PLAYERS).match(str(playerResult)).group(1).split(", ")
for player in players:
if player.name in playerList:
playersInVolume.append(player)
return {
"players": playersInVolume
}
def kick(self, message, targetPlayers = None):
if targetPlayers == None:
targetPlayers = "@a"
if isinstance(targetPlayers, str):
self.sendCommand("kick @a {}".format(message), False)
else:
for player in targetPlayers:
self.sendCommand("kick \"{}\" {}".format(player.name, message), False)