192 lines
5.4 KiB
Python
Executable File
192 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import string
|
|
import sys
|
|
import os
|
|
import time
|
|
import asyncio
|
|
from Tomato import Tomato
|
|
from Cucumber import Cucumber
|
|
from Plant import Plant
|
|
from Trac import Trac
|
|
|
|
string.ascii_letters = 'oOpPx'
|
|
string.ascii_numbers = '01'
|
|
OKGREEN = '\033[92m'
|
|
OKBLUE = '\033[94m'
|
|
OKRED = '\033[91m'
|
|
ENDC = '\033[0m'
|
|
|
|
|
|
def initialize_field():
|
|
field = []
|
|
for i in range(11):
|
|
row = []
|
|
for j in range(7):
|
|
if i == 0 or i == 10 or j == 0 \
|
|
or j == 3 or j == 6:
|
|
row.append(None)
|
|
elif j < 3:
|
|
row.append(Cucumber())
|
|
else:
|
|
row.append(Tomato())
|
|
field.append(row)
|
|
return field
|
|
|
|
|
|
def print_field(field, tractor):
|
|
if sys.platform == "win32":
|
|
os.system("cls")
|
|
else:
|
|
os.system("clear")
|
|
print(tractor.get_position())
|
|
sys.stdout.write(OKBLUE)
|
|
sys.stdout.write("$")
|
|
sys.stdout.write(" ")
|
|
sys.stdout.write("\n")
|
|
|
|
for x, row in enumerate(field):
|
|
for y, i in enumerate(row):
|
|
if not i:
|
|
if tractor.get_position()[0] == x \
|
|
and tractor.get_position()[1] == y:
|
|
sys.stdout.write(OKBLUE)
|
|
sys.stdout.write(tractor.get_symbol())
|
|
sys.stdout.write(" ")
|
|
else:
|
|
sys.stdout.write(" ")
|
|
else:
|
|
symbol = i.get_symbol()
|
|
if symbol[1] == "green":
|
|
sys.stdout.write(OKGREEN)
|
|
sys.stdout.write(symbol[0])
|
|
sys.stdout.write(" ")
|
|
else:
|
|
sys.stdout.write(OKRED)
|
|
sys.stdout.write(symbol[0])
|
|
sys.stdout.write(" ")
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
def update_state(field):
|
|
for row in field:
|
|
for i in row:
|
|
if i:
|
|
i.tick(1)
|
|
|
|
def try_move(field, tractor):
|
|
x = tractor.get_position()[0]
|
|
y = tractor.get_position()[1]
|
|
rotation = tractor.get_rotation()
|
|
if rotation == 'N':
|
|
x -= 1
|
|
elif rotation == 'S':
|
|
x += 1
|
|
elif rotation == 'W':
|
|
y -= 1
|
|
else:
|
|
y += 1
|
|
if x >= 0 and x < len(field) \
|
|
and y >= 0 and y < len(field[0]) \
|
|
and not field[x][y]:
|
|
# print(str(x) + " " + str(y))
|
|
return True
|
|
return False
|
|
|
|
def test_cords(cord, len1, len2):
|
|
if cord[0] > -1 and cord[0] < len1 \
|
|
and cord[1] > -1 and cord[1] < len2:
|
|
return True
|
|
return False
|
|
|
|
|
|
def look_at_plats(field, location):
|
|
wsp = [
|
|
(location[0] + 1, location[1]),
|
|
(location[0] - 1, location[1]),
|
|
(location[0], location[1] - 1),
|
|
(location[0], location[1] + 1)
|
|
]
|
|
len1 = len(field)
|
|
len2 = len(field[0])
|
|
wsp = [l if test_cords(l, len1, len2) else None for l in wsp]
|
|
plants = []
|
|
for i in wsp:
|
|
if i and field[i[0]][i[1]]:
|
|
plants.append(i)
|
|
return plants
|
|
|
|
|
|
def send_stats(field, location):
|
|
x = location[0]
|
|
y = location[1]
|
|
stats = field[x][y].get_stats()
|
|
str_stats = str(stats["ttl"]) + " " + str(stats["is_alive"]) + " " + str(stats["hydration"]) + " " \
|
|
+ str(stats["soil_level"]) + " " + str(stats["ready"]) + " " + str(stats["name"]) + "\n"
|
|
return str_stats
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
field = initialize_field()
|
|
tractor = Trac('N', (0,0))
|
|
|
|
async def handle_echo(reader, writer):
|
|
data = await reader.readline()
|
|
message = data.decode().split()
|
|
|
|
if message[0] == "rotate":
|
|
tractor.set_rotation(message[1])
|
|
writer.write("OK\n".encode())
|
|
elif message[0] == "try":
|
|
if try_move(field, tractor):
|
|
writer.write("OK\n".encode())
|
|
else:
|
|
writer.write("FAIL\n".encode())
|
|
elif message[0] == "move":
|
|
tractor.move()
|
|
writer.write(("OK\n").encode())
|
|
elif message[0] == "look_at_plants":
|
|
plants = look_at_plats(field, (int(message[1]), int(message[2])))
|
|
plants_str = ""
|
|
for i in plants:
|
|
plants_str += (" " + str(i[0]) + " " + str(i[1]))
|
|
writer.write((plants_str + "\n").encode())
|
|
elif message[0] == "stats":
|
|
x = int(message[1])
|
|
y = int(message[2])
|
|
stats = send_stats(field, (x, y)).encode()
|
|
writer.write(stats)
|
|
elif message[0] == "inc_soil":
|
|
x = int(message[1])
|
|
y = int(message[2])
|
|
field[x][y].increase_soillevel(field[x][y].max_soil_lvl - field[x][y].soil_level)
|
|
elif message[0] == "inc_hydration":
|
|
x = int(message[1])
|
|
y = int(message[2])
|
|
field[x][y].increase_hydration(field[x][y].max_hydration_lvl - field[x][y].hydration)
|
|
elif message[0] == "dec_ttl":
|
|
x = int(message[1])
|
|
y = int(message[2])
|
|
field[x][y].decrease_ttl(0)
|
|
|
|
|
|
print_field(field, tractor)
|
|
# print(tractor.get_position().get_x())
|
|
# print(tractor.get_position().get_y())
|
|
# print(tractor.get_rotation())
|
|
update_state(field)
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
async def main():
|
|
server = await asyncio.start_server(
|
|
handle_echo, '127.0.0.1', 8887)
|
|
|
|
addr = server.sockets[0].getsockname()
|
|
|
|
async with server:
|
|
await server.serve_forever()
|
|
|
|
asyncio.run(main())
|