-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimulation.py
More file actions
executable file
·86 lines (70 loc) · 2.23 KB
/
Simulation.py
File metadata and controls
executable file
·86 lines (70 loc) · 2.23 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
#!/usr/bin/env python -i
import struct
import socket
import itertools
import json
import collections
"""
A point is a (int, int)-tuple
"""
try:
import getHolodeckEnum
globals().update(dict(
(handler, idx) for idx, handler in enumerate(getHolodeckEnum.findHandlers())))
except ImportError:
Delete = 1
DeleteAll = 2
defaultPort = 30001
class LaserClient(object):
def __init__(self, host="localhost", port=30001):
self.lastSendID = 0
self.host = host
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def __del__(self):
self.socket.close()
def __sendPacket(self, code, instructionID=None, other=None):
packet = other.copy() if other is not None else {}
packet.update({"instruction": code})
if instructionID is not None:
packet.update({"instance": instructionID})
self.sendBytes(json.dumps(packet).encode())
def __packPoints(self, points):
return [{"x": x, "y": y} for x, y in points]
def sendBytes(self, s):
self.socket.sendto(s, (self.host, self.port))
def delete(self, instructionID):
self.__sendPacket(Delete, instructionID)
def deleteAll(self):
self.__sendPacket(DeleteAll)
def instruction(self, code, points=None, instructionID=-1, turkerIds=None):
if points is None:
points = []
if self.lastSendID < instructionID:
self.lastSendID = instructionID + 1
if instructionID == -1:
self.lastSendID += 1
instructionID = self.lastSendID
if turkerIds is None:
turkerIds = []
if not isinstance(turkerIds, collections.Iterable):
turkerIds = [turkerIds]
self.__sendPacket(code, instructionID,
{"points": self.__packPoints(points),
"turkers": turkerIds})
return instructionID
def interact(host, port):
globals()["laser"] = LaserClient(host, port)
print("""Created a UDP connection to %s:%d with object named \"laser\"
Usage:
\tlaser.deleteAll()
\tplayerID = laser.instruction(Player, [(100, 100)])
\tlaser.instruction(Player, [(1000, 1000)], playerID)""" % (host, port))
if __name__ == "__main__":
import sys
if sys.flags.inspect:
host = sys.argv[1] if len(sys.argv) > 1 else "localhost"
port = int(sys.argv[2]) if len(sys.argv) > 2 else defaultPort
interact(host, port)
else:
print("Execute with \"python -i %s [host [port]]\"" % sys.argv[0])