1 import utility
, craw
, configuration
, packets
, time
, nil
.thread
, threading
, privileges
3 class enchant_handler_class
:
8 self
.lock
= threading
.Lock()
10 def perform_mana_check(self
, cast_count
):
11 self
.mana_per_cast
= self
.enchant_level
+ 24
12 mana_usage
= cast_count
* self
.mana_per_cast
13 current_mana
, maximum_mana
= self
.mana
14 output
= mana_usage
<= current_mana
15 if self
.mana_per_cast
> current_mana
:
16 packets
.send_chat(configuration
.mana_error
% self
.mana
)
18 elif mana_usage
> current_mana
:
19 packets
.send_chat(configuration
.enchant_mana_lack_warning
% self
.mana
)
24 def perform_party_check(self
):
25 is_in_party
= utility
.same_party(self
.player
, self
.my_player
)
27 packets
.send_chat(configuration
.party_error
)
30 def distance_check(self
):
31 my_coordinates
= (self
.my_player
.x
, self
.my_player
.y
)
32 player_coordinates
= (self
.player
.x
, self
.player
.y
)
34 distance
= utility
.distance(my_coordinates
, player_coordinates
)
36 is_in_range
= self
.player
.x
!= 0 and distance
<= configuration
.maximal_enchant_distance
38 packets
.send_chat(configuration
.enchant_range_error
)
41 def perform_pre_cast_check(self
):
42 if not self
.perform_party_check():
44 return self
.distance_check()
46 def process_command(self
, name
, message
):
47 self
.player
= utility
.get_player_data_by_name(name
)
48 self
.my_player
= utility
.get_my_player()
50 if self
.player
.id == self
.my_player
.id:
53 if self
.player
== None or self
.my_player
== None:
56 enchant_level
= craw
.get_skill_level(enchant_handler_class
.enchant_skill
)
57 if enchant_level
== None:
58 print 'Unable to retrieve the enchant skill level'
61 if enchant_level
== 0:
62 print 'Unable to enchant - this character does not even have this skill!'
65 self
.enchant_level
= enchant_level
67 self
.mana
= craw
.get_mana()
69 print 'Unable to retrieve mana'
72 if message
== configuration
.enchant_command
:
73 self
.launch_function(self
.enchant_player
)
75 elif message
== configuration
.enchant_minions_command
:
76 self
.launch_function(self
.enchant_minions
)
78 elif message
== configuration
.enchant_all_command
:
79 self
.launch_function(self
.enchant_all
)
81 elif message
== configuration
.enchant_players_command
:
82 self
.launch_function(self
.enchant_players
)
84 def launch_function(self
, function
):
87 print 'Received an enchant request that was blocked because the thread is still running.'
90 nil
.thread
.create_thread(lambda: self
.function_wrapper(function
))
93 def thread_termination(self
):
98 def function_wrapper(self
, function
):
100 self
.thread_termination()
102 def enchant_player(self
):
103 if not self
.perform_pre_cast_check() or not self
.perform_mana_check(1):
105 self
.enchant(self
.player
.id, 0)
106 packets
.send_chat(configuration
.enchant_confirmation
% self
.mana
)
109 def get_minions(self
):
110 minions
= craw
.get_minions(self
.player
.id)
112 print 'Unable to retrieve the minions of player %s' % self
.player
.name
115 minions
= filter(lambda minion
: not minion
.enchanted
, minions
)
116 return map(lambda minion
: (1, minion
.id), minions
)
118 def process_targets(self
, targets
):
119 if not self
.perform_mana_check(len(targets
)):
122 targets_enchanted
= 0
123 for type, id in targets
:
124 if not self
.enchant(id, type):
126 targets_enchanted
+= 1
128 return targets_enchanted
130 def enchant_minions(self
):
131 if not self
.perform_pre_cast_check():
134 minions
= self
.get_minions()
135 minions_enchanted
= self
.process_targets(minions
)
136 if minions_enchanted
== None:
139 current_mana
, maximum_mana
= self
.mana
140 packets
.send_chat(configuration
.enchant_minions_confirmation
% (minions_enchanted
, current_mana
, maximum_mana
))
143 def enchant_all(self
):
144 if not self
.perform_pre_cast_check():
147 targets
= self
.get_minions()
149 print 'Unable to retrieve minions!'
151 targets
= [(0, self
.player
.id)] + targets
152 targets_enchanted
= self
.process_targets(targets
)
153 if targets_enchanted
== None:
156 minions_enchanted
= targets_enchanted
- 1
158 current_mana
, maximum_mana
= self
.mana
159 packets
.send_chat(configuration
.enchant_all_confirmation
% (minions_enchanted
, current_mana
, maximum_mana
))
162 def enchant_players(self
):
163 players
= utility
.get_party_players()
164 for player
in players
:
166 if not self
.enchant_all():
170 def process_bytes(self
, bytes
):
172 running
= self
.running
178 my_name
= utility
.get_my_name()
179 if my_name
not in configuration
.enchanters
:
182 self
.mana
= craw
.get_mana()
183 if self
.mana
== None:
184 print 'Unable to retrieve the mana values'
187 message
= packets
.parse_message(bytes
)
189 name
, message
= message
190 if privileges
.has_remote_privileges(name
):
191 self
.process_command(name
, message
)
193 def enchant(self
, target
, type):
194 packets
.set_right_skill(enchant_handler_class
.enchant_skill
)
195 packets
.cast_right_skill_at_target(type, target
)
196 current_mana
, maximum_mana
= self
.mana
197 current_mana
-= self
.mana_per_cast
198 self
.mana
= (current_mana
, maximum_mana
)
199 time
.sleep(configuration
.enchant_delay
)
200 return current_mana
>= self
.mana_per_cast