1 | from typing import Dict, List, Any, Tuple
|
---|
2 | from gui.framsutils.FramsSocket import FramsSocket
|
---|
3 | from gui.framsutils.creature import Creature
|
---|
4 | from gui.framsutils.framsProperty import Property
|
---|
5 | from gui.framsutils.FramsInterface import FramsInterface, TreeNode, InterfaceType
|
---|
6 |
|
---|
7 | class SocketInterface(FramsInterface):
|
---|
8 | DEBUG_ADD_FIRST_CREATURE = False
|
---|
9 | DEBUG_FIRST_CREATURE = "X"
|
---|
10 |
|
---|
11 | def __init__(self) -> None:
|
---|
12 | self.interfaceType = InterfaceType.SOCKET
|
---|
13 | self.frams: FramsSocket = FramsSocket()
|
---|
14 |
|
---|
15 | def connect(self, address: str, port: int):
|
---|
16 | self.frams.initConnection(address, port)
|
---|
17 | if self.DEBUG_ADD_FIRST_CREATURE:
|
---|
18 | self.frams.writeCreatureFromString(0, self.DEBUG_FIRST_CREATURE)
|
---|
19 | self.frams.sendRequest("call /simulator/genepools/groups/0 add {}".format(self.DEBUG_FIRST_CREATURE))
|
---|
20 |
|
---|
21 | def disconnect(self):
|
---|
22 | self.frams.closeConnection()
|
---|
23 |
|
---|
24 | def start(self):
|
---|
25 | self.frams.start()
|
---|
26 |
|
---|
27 | def getError(self):
|
---|
28 | return None
|
---|
29 |
|
---|
30 | def stop(self):
|
---|
31 | self.frams.stop()
|
---|
32 |
|
---|
33 | def step(self):
|
---|
34 | self.frams.step()
|
---|
35 |
|
---|
36 | def listTree(self, path="/") -> List[Property]:
|
---|
37 | return self.frams.info(path)
|
---|
38 |
|
---|
39 | def listTreeList(self, path) -> List[Property]:
|
---|
40 | return self.frams.infoList(path)
|
---|
41 |
|
---|
42 | def makeInfoTree(self) -> TreeNode:
|
---|
43 | root = Property("", "/", "o")
|
---|
44 | childs = self.listTree()
|
---|
45 | tree = self._makeTree(None, root, childs)
|
---|
46 | tree.node.p["id"] = "/"
|
---|
47 | return tree
|
---|
48 |
|
---|
49 | def _makeTree(self, parent: TreeNode, node: Property, childs: List[Property]) -> TreeNode:
|
---|
50 | treeNode = TreeNode(node, None, parent)
|
---|
51 | p = parent
|
---|
52 | path = treeNode.node.p["id"]
|
---|
53 | while p:
|
---|
54 | if p.node.p["id"] != '/':
|
---|
55 | path = p.node.p["id"] + "/" + path
|
---|
56 | p = p.parent
|
---|
57 |
|
---|
58 | for child in childs:
|
---|
59 | if child.p["id"] and child.p["name"] and child.p["type"]:
|
---|
60 | if child.p["type"][0] == "o":
|
---|
61 | nodeChilds = self.listTree(path + "/" + child.p["id"])
|
---|
62 | c = self._makeTree(treeNode, child, nodeChilds)
|
---|
63 | treeNode.addChild(c)
|
---|
64 | elif child.p["type"][0] == "l" and child.p["id"] == "groups":
|
---|
65 | nodeChilds = self.listTree(path + "/" + child.p["id"])
|
---|
66 | c = self._makeTreeFromList(treeNode, child, nodeChilds)
|
---|
67 | treeNode.addChild(c)
|
---|
68 | return treeNode
|
---|
69 |
|
---|
70 | def _makeTreeFromList(self, parent: TreeNode, node: Property, childs: List[Property]) -> TreeNode:
|
---|
71 | treeNode = TreeNode(node, None, parent)
|
---|
72 | p = parent
|
---|
73 | path: str = " " + treeNode.node.p["id"]
|
---|
74 | while p:
|
---|
75 | if p.node.p["id"] != '/' and p.parent and p.parent.node.p["id"] != "/":
|
---|
76 | path = "/" + p.node.p["id"] + path
|
---|
77 | p = p.parent
|
---|
78 |
|
---|
79 | groupsNo = int(self.listTreeList(path)[0].p["groups"])
|
---|
80 | path = path.replace(' ', '/') + '/'
|
---|
81 | childs = [Property("", str(x), "o") for x in range(groupsNo)]
|
---|
82 |
|
---|
83 | for child in childs:
|
---|
84 | child.p["id"] = child.p["name"]
|
---|
85 | if child.p["type"][0] == "o":
|
---|
86 | nodeChilds = self.listTree(path + child.p["id"])
|
---|
87 | c = self._makeTree(treeNode, child, nodeChilds)
|
---|
88 | treeNode.addChild(c)
|
---|
89 |
|
---|
90 | return treeNode
|
---|
91 |
|
---|
92 | def readCreatures(self, colors = True) -> List[Creature]:
|
---|
93 | return self.frams.readCreatures("+", "+", colors)
|
---|
94 |
|
---|
95 | def readGenePoolsGroups(self) -> Dict[str, int]:
|
---|
96 | return self.frams.readGenePoolsGroups()
|
---|
97 |
|
---|
98 | def readGenePools(self, props: List[str]) -> Dict[str, List[Dict[str, Any]]]:
|
---|
99 | return self.frams.readGenePools(props)
|
---|
100 |
|
---|
101 | def readPopulationsGroups(self) -> Dict[str, int]:
|
---|
102 | return self.frams.readPopulationsGroups()
|
---|
103 |
|
---|
104 | def readPopulations(self, props: List[str]) -> Dict[str, List[Dict[str, Any]]]:
|
---|
105 | return self.frams.readPopulations(props)
|
---|
106 |
|
---|
107 | def readCreatureDetails(self, group: int, index: str):
|
---|
108 | return self.frams.readCreatureInfo(group, index)
|
---|
109 |
|
---|
110 | def readGenotypeDetails(self, group: int, index: str):
|
---|
111 | return self.frams.readGenotypeInfo(group, index)
|
---|
112 |
|
---|
113 | def readParameterDetails(self, prop: str):
|
---|
114 | return self.frams.readPropertyInfo(prop)
|
---|
115 |
|
---|
116 | def writeCreatureDetail(self, creatureNo: str, prop: str, value: str):
|
---|
117 | return self.frams.writeCreatureInfo(creatureNo, prop, value)
|
---|
118 |
|
---|
119 | def writeGenotypeDetail(self, genotypeNo: str, prop: str, value: str):
|
---|
120 | return self.frams.writeGenotypeInfo(genotypeNo, prop, value)
|
---|
121 |
|
---|
122 | def writeParameterDetail(self, path: str, prop: str, value: str):
|
---|
123 | return self.frams.writePropertyInfo(path, prop, value)
|
---|
124 |
|
---|
125 | def registerRunningChangeEventCallback(self, callback):
|
---|
126 | self.frams.refreshControlButtonsCallback = callback
|
---|
127 |
|
---|
128 | def registerTreeviewRefreshEventCallback(self, callback):
|
---|
129 | self.frams.refreshTreeviewCallback = callback
|
---|
130 |
|
---|
131 | def getMotd(self) -> str:
|
---|
132 | try:
|
---|
133 | tmp = self.frams.infoList("/ motd")[0].p["motd"]
|
---|
134 | except:
|
---|
135 | tmp = "Not connected"
|
---|
136 | return tmp
|
---|
137 |
|
---|
138 | def loadFile(self, path: str) -> None:
|
---|
139 | self.frams.loadFile(path)
|
---|
140 |
|
---|
141 | def importFile(self, path: str, options: int) -> None:
|
---|
142 | self.frams.importFile(path, options)
|
---|
143 |
|
---|
144 | def saveFile(self, path: str, options: int) -> None:
|
---|
145 | self.frams.saveFile(path, options)
|
---|
146 |
|
---|
147 | def getWorldType(self) -> int:
|
---|
148 | return self.frams.getWorldType()
|
---|
149 |
|
---|
150 | def getWorldSize(self) -> float:
|
---|
151 | return self.frams.getWorldSize()
|
---|
152 |
|
---|
153 | def getWorldWaterLevel(self) -> float:
|
---|
154 | return self.frams.getWorldWaterLevel()
|
---|
155 |
|
---|
156 | def getWorldBoundaries(self) -> int:
|
---|
157 | return self.frams.getWorldBoundaries()
|
---|
158 |
|
---|
159 | def getWorldMap(self) -> str:
|
---|
160 | return self.frams.getWorldMap()
|
---|
161 |
|
---|
162 | def getSimtype(self) -> int:
|
---|
163 | return self.frams.getSimtype()
|
---|
164 |
|
---|
165 | def getFPSDefinitions(self) -> List[Tuple[int, int]]:
|
---|
166 | return [(0.5, 1),
|
---|
167 | (2, 1),
|
---|
168 | (5, 1),
|
---|
169 | (10, 1),
|
---|
170 | (25, 1),
|
---|
171 | (50, 1),
|
---|
172 | (-1, 1)] |
---|