forked from dolev146/Ex4_OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameControl.py
120 lines (109 loc) · 5.03 KB
/
GameControl.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
import json
import math
import random
import settings
from Classes.Agent import Agent
from Classes.Pokemon import Pokemon
from Classes.client import Client
from run_game import Gui
def where_to_put_agents():
taken_list = []
node_list_graph = []
for n in settings.graph.Nodes:
node_list_graph.append(n)
# if amount equals no problem :)
if settings.agents_amount == settings.pokemons_amount:
for i in range(settings.agents_amount):
win_node_id = settings.pokemons[i].win_node_src.id
settings.client.add_agent("{\"id\":" + str(win_node_id) + "}")
# if agents are more than we need to spread the agents
elif settings.agents_amount > settings.pokemons_amount:
counter = 0
for i in range(settings.pokemons_amount):
win_node_id = settings.pokemons[i].win_node_src.id
settings.client.add_agent("{\"id\":" + str(win_node_id) + "}")
taken_list.append(win_node_id)
counter = i
for i in range(counter + 1, settings.agents_amount):
# https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another
# https://stackoverflow.com/questions/69425514/a-random-integer-between-specific-values-excluding-0
l3 = [x for x in node_list_graph if x not in taken_list]
win_node_id = random.choice(l3)
settings.client.add_agent("{\"id\":" + str(win_node_id) + "}")
# if pokemons are more then we just use all the agents to be close to pokemons
# https://stackoverflow.com/questions/2688079/how-to-iterate-over-the-first-n-elements-of-a-list
elif settings.agents_amount < settings.pokemons_amount:
settings.pokemons.sort(key=lambda pok: pok.value, reverse=True)
tempForDebug=settings.pokemons
for pokemon in settings.pokemons[:settings.agents_amount]:
win_node_id = pokemon.win_node_src.id
settings.client.add_agent("{\"id\":" + str(win_node_id) + "}")
def init_connection():
"""
init the connection to the server , and the pygame window
"""
settings.client = Client()
settings.client.start_connection(settings.HOST, settings.PORT)
# login
settings.client.log_in("207867342")
# initializing the code info settings
# print(settings.client.get_info())
info = json.loads(settings.client.get_info())
settings.agents_amount = info['GameServer']["agents"]
# print("agents amount: " + str(settings.agents_amount))
settings.pokemons_amount = info['GameServer']["pokemons"]
settings.game_level = info['GameServer']["game_level"]
settings.moves = info['GameServer']["moves"]
settings.is_logged_in = info['GameServer']["is_logged_in"]
settings.grade = info['GameServer']["grade"]
settings.max_user_level = info['GameServer']["max_user_level"]
settings.info_id = info['GameServer']["id"]
settings.info_graph = info['GameServer']["graph"]
json_graph = settings.client.get_graph()
dict_graph = json.loads(json_graph)
for n in dict_graph["Nodes"]:
if "pos" not in n:
x = None
y = None
z = None
xyz = (x, y, z)
node_id = int(n["id"])
settings.graph.add_node(node_id, xyz)
else:
pos = n["pos"].split(",")
xyz = (float(pos[0]), float(pos[1]), float(pos[2]))
node_id = int(n["id"])
settings.graph.add_node(node_id, xyz)
for e in dict_graph["Edges"]:
settings.graph.add_edge(int(e["src"]), int(e["dest"]), float(e["w"]))
settings.algo.init(settings.graph)
m = 0.0
for edge in settings.graph.Edges.values():
p1 = [settings.graph.Nodes.get(edge.dest).x, settings.graph.Nodes.get(edge.dest).y]
p2 = [settings.graph.Nodes.get(edge.src).x, settings.graph.Nodes.get(edge.src).y]
distance = math.dist(p1, p2)
settings.distance_edges.append({"value": distance, "src": edge.src, "dest": edge.dest})
json_pokemons = settings.client.get_pokemons()
dict_pokemons = json.loads(json_pokemons)
for pok in dict_pokemons["Pokemons"]:
pok = pok["Pokemon"]
pok_obj = Pokemon(value=pok["value"], type=pok["type"], pos=pok["pos"])
pok_obj.set_edge()
settings.pokemons.append(pok_obj)
# the reason this func in before everything is that in order to build the agents list
# we need to set the agents at first to access their information
where_to_put_agents()
json_agents = settings.client.get_agents()
dict_agents = json.loads(json_agents)
for agent in dict_agents['Agents']:
agent = agent["Agent"]
settings.agents.append(
Agent(id=agent["id"], value=agent["value"], src=agent["src"], dest=agent["dest"], speed=agent["speed"],
pos=agent["pos"]))
settings.agentDestNodeList.update({int(agent["id"]): -1})
class GameControl:
def __init__(self):
init_connection()
# update.start_update_process()
Gui()
# start the game only when we finish with gui and establishing connection