Skip to content

Instantly share code, notes, and snippets.

@caseploeg
Created April 22, 2024 00:43
Show Gist options
  • Save caseploeg/60d6e10a364df274a64aff5245b214b8 to your computer and use it in GitHub Desktop.
Save caseploeg/60d6e10a364df274a64aff5245b214b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import time
import socket
import random
verbose = True
def print_objects_grid(data):
items = data.split('\n')
if verbose:
print(items)
grid_dict = {}
min_x = min_y = float('inf')
max_x = max_y = -float('inf')
objs = set()
px, py = 0,0
for item in items[:-1]:
parts = item.split(', ')
if len(parts) <= 1:
continue
obj_type = parts[0][1:]
x = int(float(parts[1]))
y = int(float(parts[2]))
min_x = min(min_x, x)
max_x = max(max_x, x)
min_y = min(min_y, y)
max_y = max(max_y, y)
shortnames = {'Player1': 'P',
'Brick': 'B'}
if obj_type == 'Player1':
px, py = x, y
grid_dict[(x,y)] = obj_type[0]
objs.add(obj_type)
if (px, py) != (0, 0):
grid_dict[(px,py)] = 'P'
if min_x == float('inf') or max_x == -float('inf'):
return
width = int((max_x - min_x) // 16 + 1)
height = int((max_y - min_y) // 16 + 1)
grid = [['.' for _ in range(width)] for _ in range(height)]
for (x, y), val in grid_dict.items():
grid_x = (x - min_x) // 16
grid_y = (y - min_y) // 16
grid[grid_y][grid_x] = val
for row in grid:
print(''.join(row))
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 5002)
server_socket.bind(server_address)
# Listen for incoming connections
server_socket.listen(1)
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = server_socket.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(10000)
if not data:
continue
data_str = data.decode('utf-8').strip('\x00').split('<break>')
action = data_str[0]
map_state = data_str[1]
state = data_str[2]
if data:
# LOOKUP
if 'Bomb' in action:
print("LOOKUP WIKI:")
print("<p><b>Bombs</b> are single-use equipment items. They are one of the Spelunker's most useful resources. </p><p>Bombs work much like light objects, capable of being dropped or thrown into position, where they explode after a few seconds, destroying dterrain</a> and killing <d>enemies</a>.")
if 'Chest' in state:
print("LOOKUP WIKI:")
print("Chests can be found on every level of the caves, containing varying quantities of treasure. It is opened by holding 'Up' and pressing the action key (Default: X). It can be opened either on the ground or when carried in the hands. The empty chest remains after being looted, and can be carried and thrown. However, as a heavy object, it does not go far and cannot be taken through the level exit with you. The Locked Chest is a special locked chest that always spawns somewhere in the Mines which contains the Udjat Eye.")
print(action)
print(state)
#print('received {!r}'.format(data))
print('================================')
print_objects_grid(map_state)
print('================================')
print()
msg = "from server"
if random.randint(0,10) > 5:
msg = "left"
else:
msg = "right"
msg = "from server"
msg_bytes = msg.encode('utf-8')
connection.sendall(msg_bytes)
finally:
# Clean up the connection
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment