51c565a0 by Barry Ruffner

Added better colors and bug fixes

1 parent 9b22a762
......@@ -16,6 +16,7 @@ global_aliases = {
'social': 'emote'
}
class CommandHandler(object):
def parse(self, id, cmd, params, mud, players):
......@@ -53,7 +54,7 @@ class CommandHandler(object):
command_text = f.read()
exec(command_text, globals(), ldict)
del command_text
if ldict['next_command'] != None:
if ldict['next_command'] is not None:
locals()['tokens'] = []
tokens = []
with open('commands/{}.txt'.format(ldict['next_command']), 'r', encoding='utf-8') as f:
......@@ -62,8 +63,11 @@ class CommandHandler(object):
return True
except Exception as e:
print('Something happened...')
import sys
if 'esp' not in sys.platform:
import traceback
traceback.print_exc()
else:
print(e)
mud.send_message(id, "Unknown command '{}'".format(cmd))
return False
......
......@@ -6,7 +6,8 @@ def look(id, mud, players, tokens):
if len(tokens) > 0 and tokens[0] == 'at':
del tokens[0]
if len(tokens) == 0:
mud.send_message(id, room_data.get('description'))
mud.send_message(id, room_data.get('title'), color=['bold', 'green'])
mud.send_message(id, room_data.get('description'), color='green')
else:
subject = tokens[0]
items = room_data.get('look_items')
......@@ -43,14 +44,14 @@ def look(id, mud, players, tokens):
# send player a message containing the list of players in the room
if len(playershere) > 0:
mud.send_message(id, "Players here: {}".format(", ".join(playershere)))
mud.send_message(id, "%boldPlayers here:%reset {}".format(", ".join(playershere)))
# send player a message containing the list of exits from this room
mud.send_message(id, "Exits are: {}".format(", ".join(room_data.get('exits'))))
mud.send_message(id, "%boldObvious Exits:%reset {}".format(", ".join(room_data.get('exits'))))
# send player a message containing the list of exits from this room
if len(room_data.get('inventory')) > 0:
mud.send_message(id, "Items here: {}".format(", ".join(room_data.get('inventory').keys())))
mud.send_message(id, "%boldItems here:%reset {}".format(", ".join(room_data.get('inventory').keys())))
# send player a message containing the list of players in the room
for mon_name, monster in room_monsters.items():
......
......@@ -6,30 +6,22 @@ MudServer author: Mark Frimston - mfrimston@gmail.com
Micropython port and expansion author: Barry Ruffner - barryruffner@gmail.com
"""
from time import sleep
from math import floor
from sys import platform
from mudserver import MudServer
from utils import load_object_from_file, save_object_to_file
if 'esp' in platform:
from gc import collect, mem_free
from machine import Pin
if 'esp' in platform:
collect()
from time import sleep
if 'esp' in platform:
collect()
# import the MUD server class
from mudserver import MudServer
if 'esp' in platform:
collect()
from utils import load_object_from_file, save_object_to_file
if 'esp' in platform:
collect()
from math import floor
if 'esp' in platform:
collect()
import micropython
micropython.mem_info(1)
print('STARTING MUD\r\n\r\n\r\n')
# Setup button so when pressed the mud goes in to wifi hotspot mode on 192.168.4.1
if 'esp' in platform:
flash_button = Pin(0, Pin.IN, Pin.PULL_UP)
......@@ -39,6 +31,7 @@ players = {}
# start the server
globals()['mud'] = MudServer()
def show_prompt(pid):
if "prompt" not in players[pid]:
players[pid]["prompt"] = "> "
......@@ -61,7 +54,7 @@ while True:
if flash_button.value() == 0:
break
# pause for 1/5 of a second on each loop, so that we don't constantly
sleep(0.001)
sleep(0.002)
if 'esp' in platform:
tick += 0.001
else:
......@@ -125,11 +118,11 @@ while True:
for pid, pl in players.items():
# send each player a message to tell them about the diconnected
# player
if players[pid]["name"] != None:
if players[pid]["name"] is not None:
mud.send_message(pid, "{} quit the game".format(players[pid]["name"]))
# remove the player's entry in the player dictionary
if players[id]["name"] != None:
if players[id]["name"] is not None:
save_object_to_file(players[id], "players/{}.json".format(players[id]["name"]))
del(players[id])
......@@ -165,13 +158,11 @@ while True:
# go through all the players in the game
for pid, pl in players.items():
# send each player a message to tell them about the new player
mud.send_message(pid, "{} entered the game".format(
players[id]["name"]))
mud.send_message(pid, "{} entered the game".format(players[id]["name"]))
# send the new player a welcome message
mud.send_message(id, "\r\n\r\nWelcome to the game, {}. ".format(
players[id]["name"])
+ "\r\nType 'help' for a list of commands. Have fun!\r\n\r\n")
mud.send_message(id, "\r\n\r\nWelcome to the game, {}. ".format(players[id]["name"]) +
"\r\nType 'help' for a list of commands. Have fun!\r\n\r\n")
# send the new player the description of their current room
......@@ -184,7 +175,6 @@ while True:
cmd_handler = CommandHandler()
handler_results = cmd_handler.parse(id, command, params, mud, players)
show_prompt(id)
# Start WIFI Setup
......
......@@ -5,7 +5,7 @@
"mpr": 0.25,
"star": 0.4,
"spawn": {
"hp": "2d2",
"hp": "2d16",
"mp": "10",
"sta": "10"
},
......
......@@ -12,7 +12,9 @@ import socket
import select
import time
import sys
from utils import get_color
from utils import get_color, get_color_list, multiple_replace
class MudServer(object):
"""A basic server for text-based Multi-User Dungeon (MUD) games.
......@@ -188,8 +190,9 @@ class MudServer(object):
"""
# we make sure to put a newline on the end so the client receives the
# message on its own line
message = multiple_replace(message, get_color_list())
chunks, chunk_size = len(message), 80 #len(x)/4
lines = [ message[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
lines = [message[i:i + chunk_size] for i in range(0, chunks, chunk_size)]
if color:
if isinstance(color, list):
colors = ''.join([get_color(c) for c in color])
......@@ -197,7 +200,7 @@ class MudServer(object):
else:
self._attempt_send(to, get_color(color) + '\r\n'.join(lines) + line_ending + get_color('reset'))
else:
self._attempt_send(to, '\r\n'.join(lines) + line_ending)
self._attempt_send(to, '\r\n'.join(lines) + line_ending + get_color('reset'))
def shutdown(self):
"""Closes down the server, disconnecting all clients and
......@@ -233,6 +236,10 @@ class MudServer(object):
# If there is a connection problem with the client (e.g. they have
# disconnected) a socket error will be raised
except Exception as e:
if 'esp' not in sys.platform:
import traceback
traceback.print_exc()
else:
sys.print_exception(e)
self._handle_disconnect(clid)
......@@ -334,6 +341,10 @@ class MudServer(object):
# if there is a problem reading from the socket (e.g. the client
# has disconnected) a socket error will be raised
except Exception as e:
if 'esp' not in sys.platform:
import traceback
traceback.print_exc()
else:
sys.print_exception(e)
self._handle_disconnect(id)
......
{"name": "test", "room": "Room001", "inventory": {"candle": 1}, "prompt": "h %hp m %mp s %st> ", "aliases": {}, "hp": 100, "mp": 10.0, "sta": 10.200000000000031, "aa": "1d2", "mpr": 0.25, "star": 0.4, "maxhp": 953, "maxmp": 10, "maxsta": 10, "weapon": "sword", "sp": {"fireball": {"cost": 5, "dmg": "2d4", "desc": "A fireball blasts from your fingertips striking the target"}}, "at": {"kick": {"cost": 5, "dmg": "2d4", "desc": "You unleash a powerful kick"}}}
\ No newline at end of file
{"name": "test", "room": "Room001", "inventory": {"candle": 1}, "prompt": "h %hp m %mp s %st> ", "aliases": {}, "hp": 74, "mp": 5.0, "sta": 1.2000000000000361, "aa": "1d2", "mpr": 0.25, "star": 0.4, "maxhp": 953, "maxmp": 10, "maxsta": 10, "weapon": "sword", "sp": {"fireball": {"cost": 5, "dmg": "2d4", "desc": "A fireball blasts from your fingertips striking the target"}}, "at": {"kick": {"cost": 5, "dmg": "2d4", "desc": "You unleash a powerful kick"}}}
\ No newline at end of file
......
{"cricket": {"max": 1, "active": [{"mp": 10, "maxhp": 3, "hp": 3, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
\ No newline at end of file
{"cricket": {"max": 1, "active": [{"mp": 10, "maxhp": 15, "hp": 15, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
\ No newline at end of file
......
{"cricket": {"max": 2, "active": [{"mp": 10, "maxhp": 3, "hp": 3, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}, {"mp": 10, "maxhp": 3, "hp": 3, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
\ No newline at end of file
{"cricket": {"max": 2, "active": [{"mp": 10, "maxhp": 20, "hp": 20, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}, {"mp": 10, "maxhp": 23, "hp": 23, "sta": 10, "maxmp": 10, "target": "", "action": "attack", "maxsta": 10}]}}
\ No newline at end of file
......
import json
from urandom import getrandbits
import sys
import re
if 'esp' in sys.platform:
from urandom import getrandbits
else:
from random import getrandbits
def get_color(name):
CODES = {'resetall': 0, 'bold': 1, 'underline': 4,
codes = {'resetall': 0, 'bold': 1, 'underline': 4,
'blink': 5, 'reverse': 7, 'boldoff': 22,
'blinkoff': 25, 'underlineoff': 24, 'reverseoff': 27,
'reset': 0, 'black': 30, 'red': 31, 'green': 32,
'yellow': 33, 'blue': 34, 'magenta': 35, 'cyan': 36,
'white':37}
if name not in CODES:
'white': 37}
def get_color_list():
res = {}
for c in codes:
res['%' + c] = c
return res
def get_color(name):
if name not in codes:
return ''
return '\x1b[{}m'.format(CODES.get(name, 0))
return '\x1b[{}m'.format(codes.get(name, 0))
def multiple_replace(text, adict):
rx = re.compile('|'.join(map(re.escape, adict)))
def one_xlat(match):
return get_color(adict[match.group(0)])
return rx.sub(one_xlat, text)
def save_object_to_file(obj, filename):
with open(filename, 'w', encoding='utf-8') as f:
f.write(json.dumps(obj))
def load_object_from_file(filename):
try:
with open(filename, 'r', encoding='utf-8') as f:
......@@ -27,6 +50,7 @@ def load_object_from_file(filename):
print(e)
return None
def randrange(start, stop=None):
if start == 1:
return 0
......@@ -45,6 +69,7 @@ def randrange(start, stop=None):
break
return r + start
def get_att(d):
att = 0
if 'd' in d:
......@@ -54,12 +79,16 @@ def get_att(d):
else:
att = int(d)
return att
def calc_att(mud, pid, attacks, bank, attack=None):
v_att = []
att = 0
if attack:
colors = ['bold', 'yellow']
v_att.append(attack)
else:
colors = ['bold', 'magenta']
for attack in attacks:
if attack['cost'] < bank:
v_att.append(attack)
......@@ -67,10 +96,7 @@ def calc_att(mud, pid, attacks, bank, attack=None):
if len(v_att) > 0:
attack = v_att[randrange(len(v_att))]
att = get_att(attack['dmg'])
if attack:
mud.send_message(pid, "%s for %d" % (attack['desc'], att,), color=['bold', 'yellow'])
else:
mud.send_message(pid, "%s for %d" % (attack['desc'], att,), color=['bold', 'magenta'])
mud.send_message(pid, "%s for %d" % (attack['desc'], att,), color=colors)
bank -= attack['cost']
return att, bank
......