Wrote a fix for the Python path detection issue, uses multiple registry sources now
[craw.git] / script / bind.py
blobba8fc91a1fb4a61708836f133d857fee056eb00f
1 import os, utility, packets, nil.file, keyboard_configuration, craw, player_killer, random
3 def get_tp_handler():
4 return current_handler.command_handler.town_portal_handler
6 def say(message):
7 packets.send_chat(message)
9 def tp():
10 get_tp_handler().cast_town_portal()
12 def town_tp():
13 get_tp_handler().town_tp()
15 def tppk():
16 get_tp_handler().tppk()
18 def reveal_act():
19 if craw.reveal_act():
20 pass
21 else:
22 print 'Failed to reveal the current act!'
24 def attack():
25 current_handler.player_killer_handler.attack()
27 def ground_attack():
28 current_handler.player_killer_handler.ground_attack()
30 def invite_all():
31 print 'Inviting all players'
32 my_player = utility.get_my_player()
33 players = craw.get_players()
34 for player in players:
35 if my_player.id == player.id:
36 continue
37 packets.invite_player(player.id)
39 def say_file(file):
40 lines = nil.file.read_lines(file)
41 if lines == None:
42 print 'Unable to read %s'
43 elif len(lines) >= 25:
44 print '%s has too many lies' % file
45 else:
46 print 'Reading %s and sending it to the chat' % file
47 for line in lines:
48 packets.send_chat(line)
50 def random_quote(directory):
51 files = os.listdir(directory)
52 source = files[random.randint(0, len(files) - 1)]
53 say_file(os.path.join(directory, source))
55 current_handler = None
57 class bind_handler_class:
58 def __init__(self):
59 self.bindings = keyboard_configuration.bindings
60 self.player_killer_handler = None
61 craw.set_keyboard_handler(self.process_key)
63 def bind(self, key, action):
64 self.bindings[key] = action
65 self.serialise_bindings()
67 def unbind(self, key):
68 self.bindings.pop(key)
69 self.serialise_bindings()
71 def print_bindings(self):
72 for key in self.bindings:
73 print '"%s": %s' % (key, self.bindings[key])
75 def escape(self, input):
76 return input.replace("'", "\\'")
78 def serialise_bindings(self):
79 data = '#This file was generated automatically, do not edit it at runtime\n\n'
80 data += 'bindings = {\n'
81 for key in self.bindings:
82 key = self.escape(key)
83 command = self.escape(self.bindings[key])
84 data += "\t'%s': '%s',\n" % (key, command)
85 data += '}'
87 data = data.replace(',\n}', '\n}')
89 path = os.path.join(utility.get_configuration_directory(), 'keyboard_configuration.py')
91 nil.file.write_file(path, data)
93 def process_key(self, key):
94 key_char = chr(key)
96 try:
97 global current_handler
98 current_handler = self
99 code = self.bindings[key_char]
100 exec(code)
101 except KeyError:
102 pass