CRAW now runs on Windows 7 too - the problem was that Windows 7 has moved some functi...
[craw.git] / script / command.py
blob465d5c63c6298009ba2b761cdfead2a372956493
1 import craw, string, utility, types, flash, packets
3 class command_handler_class:
4 def __init__(self, bind_handler):
5 self.town_portal_handler = None
6 self.skill_handler = None
7 self.bind_handler = bind_handler
8 self.bind_handler.command_handler = self
9 self.bncs_handler = None
10 self.flash_objects = []
12 one_or_more = lambda x: x >= 1
13 two_or_more = lambda x: x >= 2
15 self.command_map = [
16 ('leave', '', 'Leave the current game', 0, craw.leave_game),
17 ('say', '<text>', 'Send a chat message to the game server', one_or_more, self.say),
18 ('bind', '<key> <Python expression>', 'Allows you to bind a key to an arbitrary action', two_or_more, self.bind),
19 ('unbind', '<key>', 'Unbinds a previously bound key', 1, self.unbind),
20 ('bindings', '', 'Lists all the keys currently bound', 0, self.bindings),
21 ('help', '', 'Prints the help for the Python commands', 0, self.print_help),
22 ('players', '', 'Print a list of players', 0, self.print_players),
23 ('skills', '', 'Sets your skills to the values currently specified', 0, self.set_skills),
24 ('flash', '<index> <delay in ms>', 'Flashes a player at the specified rate', 2, self.flash),
25 ('stop', '', 'Stop all flashing threads', 0, self.stop),
26 ('whois', '<player>', 'Whois a player', 1, self.whois),
27 ('hostile', '', 'Declares hostility to all players', 0, self.hostile),
28 ('experience', '', 'Prints your current experience', 0, self.experience),
31 def process_command(self, line):
32 tokens = line.split(' ')
33 if len(tokens) == 0:
34 return False
35 command = tokens[0]
36 argument_tokens = tokens[1 : ]
38 for command_string, arguments, description, argument_count, command_handler in self.command_map:
39 if command_string != command:
40 continue
42 argument_token_count = len(argument_tokens)
44 if type(argument_count) == types.FunctionType:
45 argument_count_match = argument_count(argument_token_count)
46 else:
47 argument_count_match = argument_count == argument_token_count
49 if not argument_count_match:
50 print 'Invalid argument count specified.'
51 print 'Usage: %s %s' % (command, arguments)
52 print 'Description: %s' % description
53 return True
55 command_handler(argument_tokens)
56 return True
58 return False
60 def set_skills(self, arguments):
61 if self.skill_handler != None:
62 self.skill_handler.set_skills()
64 def say(self, arguments):
65 message = string.join(arguments, ' ')
66 print 'Sending chat message "%s" to the server' % message
67 utility.send_chat(message)
69 def bind(self, arguments):
70 key = arguments[0]
71 function = string.join(arguments[1 : ], ' ')
72 self.bind_handler.bind(key, function)
73 print 'Bound key "%s" to action "%s"' % (key, function)
75 def unbind(self, arguments):
76 key = arguments[0]
77 self.bind_handler.unbind(key)
78 print 'Unbound key "%s"' % key
80 def bindings(self, arguments):
81 print 'List of keys currently bound:'
82 self.bind_handler.print_bindings()
84 def print_help(self, arguments):
85 print '\nPython commands:\n'
86 for command_string, arguments, description, argument_count, command_handler in self.command_map:
87 print 'Command: %s %s' % (command_string, arguments)
88 print 'Description: %s\n' % description
90 def print_players(self, arguments):
91 players = craw.get_players()
92 index = 0
93 for player in players:
94 print 'Player index: %d' % index
95 print 'Name: %s' % player.name
96 print 'ID: %08x' % player.id
97 print 'Level: %d' % player.level
98 print 'Character class: %d' % player.character_class
99 print 'Life: %d' % player.life
100 print 'Area: %d' % player.level_id
101 print 'Location: (%d, %d)\n' % (player.x, player.y)
102 print 'Party ID: %08x' % player.party
103 index += 1
105 def flash(self, arguments):
106 try:
107 index = int(arguments[0])
108 delay = int(arguments[1])
109 except:
110 print 'Invalid input'
111 return
113 players = craw.get_players()
114 if index < 0 or index >= len(players):
115 print 'Invalid player index specified'
116 return
118 id = players[index].id
119 print 'Launching a flash thread for id %08x at %d ms' % (id, delay)
120 delay = float(delay) / 1000.0
121 self.flash_objects.append(flash.flash_thread(id, delay))
123 def stop(self, arguments):
124 print 'Stopping all threads'
125 for flash_object in self.flash_objects:
126 flash_object.run_thread = False
127 self.flash_objects = []
129 def whois_callback(self, account):
130 print 'Account: %s' % account
132 def whois(self, arguments):
133 name = arguments[0]
134 print 'Running a whois on %s' % name
135 self.bncs_handler.whois(name, self.whois_callback)
137 def hostile(self, arguments):
138 packets.hostile_players()
140 def experience(self, arguments):
141 experience = craw.get_experience()
142 if experience == None:
143 print 'Unable to retrieve experience'
144 else:
145 print 'Experience: %d' % experience