-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from imjangkar/refactor-event-handler
Refactor Event Handler
- Loading branch information
Showing
13 changed files
with
440 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from albibong.classes.event_handler.handle_event_character_equipment_changed import ( | ||
handle_event_character_equipment_changed, | ||
) | ||
from albibong.classes.event_handler.handle_event_health import ( | ||
handle_event_health_update, | ||
handle_event_health_updates, | ||
) | ||
from albibong.classes.event_handler.handle_event_new_character import ( | ||
handle_event_new_character, | ||
) | ||
from albibong.classes.event_handler.handle_event_other_grabbed_loot import ( | ||
handle_event_other_grabbed_loot, | ||
) | ||
from albibong.classes.event_handler.handle_event_party import ( | ||
handle_event_party_disbanded, | ||
handle_event_party_joined, | ||
handle_event_party_player_joined, | ||
handle_event_party_player_left, | ||
) | ||
from albibong.classes.event_handler.handle_event_update_fame import ( | ||
handle_event_update_fame, | ||
) | ||
from albibong.classes.event_handler.handle_event_update_re_spec_points import ( | ||
handle_event_update_re_spec_points, | ||
) | ||
from albibong.classes.event_handler.handle_operation_change_cluster import ( | ||
handle_operation_change_cluster, | ||
) | ||
from albibong.classes.event_handler.handle_operation_join import handle_operation_join | ||
from albibong.classes.event_handler.handle_operation_move import handle_operation_move | ||
from albibong.classes.world_data import WorldData | ||
from albibong.resources.EventCode import EventCode | ||
from albibong.resources.OperationCode import OperationCode | ||
|
||
|
||
EVENT_TYPE_PARAMETER = 252 | ||
REQUEST_TYPE_PARAMETER = 253 | ||
RESPONSE_TYPE_PARAMETER = 253 | ||
|
||
|
||
class EventHandler: | ||
def __init__(self): | ||
self.request_handler = {} | ||
self.response_handler = {} | ||
self.event_handler = {} | ||
|
||
# Event Handler | ||
self.event_handler[EventCode.NEW_CHARACTER.value] = handle_event_new_character | ||
self.event_handler[EventCode.HEALTH_UPDATE.value] = handle_event_health_update | ||
self.event_handler[EventCode.HEALTH_UPDATES.value] = handle_event_health_updates | ||
self.event_handler[EventCode.UPDATE_FAME.value] = handle_event_update_fame | ||
self.event_handler[EventCode.UPDATE_RE_SPEC_POINTS.value] = ( | ||
handle_event_update_re_spec_points | ||
) | ||
self.event_handler[EventCode.OTHER_GRABBED_LOOT.value] = ( | ||
handle_event_other_grabbed_loot | ||
) | ||
self.event_handler[EventCode.PARTY_JOINED.value] = handle_event_party_joined | ||
self.event_handler[EventCode.PARTY_DISBANDED.value] = ( | ||
handle_event_party_disbanded | ||
) | ||
self.event_handler[EventCode.PARTY_PLAYER_JOINED.value] = ( | ||
handle_event_party_player_joined | ||
) | ||
self.event_handler[EventCode.PARTY_PLAYER_LEFT.value] = ( | ||
handle_event_party_player_left | ||
) | ||
self.event_handler[EventCode.CHARACTER_EQUIPMENT_CHANGED.value] = ( | ||
handle_event_character_equipment_changed | ||
) | ||
|
||
# Request Handler | ||
self.request_handler[OperationCode.MOVE.value] = handle_operation_move | ||
|
||
# Response Handler | ||
self.response_handler[OperationCode.JOIN.value] = handle_operation_join | ||
self.response_handler[OperationCode.CHANGE_CLUSTER.value] = ( | ||
handle_operation_change_cluster | ||
) | ||
|
||
def on_request(self, world_data: WorldData, parameters): | ||
if REQUEST_TYPE_PARAMETER not in parameters: | ||
return None | ||
|
||
if parameters[REQUEST_TYPE_PARAMETER] not in self.request_handler: | ||
return None | ||
|
||
handler = self.request_handler[parameters[REQUEST_TYPE_PARAMETER]] | ||
return handler(world_data, parameters) | ||
|
||
def on_response(self, world_data: WorldData, parameters): | ||
if RESPONSE_TYPE_PARAMETER not in parameters: | ||
return None | ||
|
||
if parameters[RESPONSE_TYPE_PARAMETER] not in self.response_handler: | ||
return None | ||
|
||
handler = self.response_handler[parameters[RESPONSE_TYPE_PARAMETER]] | ||
return handler(world_data, parameters) | ||
|
||
def on_event(self, world_data: WorldData, parameters): | ||
if EVENT_TYPE_PARAMETER not in parameters: | ||
return None | ||
|
||
if parameters[EVENT_TYPE_PARAMETER] not in self.event_handler: | ||
return None | ||
|
||
handler = self.event_handler[parameters[EVENT_TYPE_PARAMETER]] | ||
return handler(world_data, parameters) |
24 changes: 24 additions & 0 deletions
24
src/albibong/classes/event_handler/handle_event_character_equipment_changed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from albibong.classes.world_data import WorldData | ||
from albibong.threads.websocket_server import send_event | ||
|
||
|
||
def handle_event_character_equipment_changed(world_data: WorldData, parameters): | ||
if 2 in parameters: | ||
if parameters[0] in world_data.char_id_to_username: | ||
if world_data.char_id_to_username[parameters[0]] != "not initialized": | ||
char = world_data.characters[ | ||
world_data.char_id_to_username[parameters[0]] | ||
] | ||
if char.username in world_data.party_members: | ||
char.update_equipment(parameters[2]) | ||
ws_update_character_equipment(world_data) | ||
else: | ||
world_data.change_equipment_log[parameters[0]] = parameters[2] | ||
|
||
|
||
def ws_update_character_equipment(world_data: WorldData): | ||
event = { | ||
"type": "update_dps", | ||
"payload": {"party_members": world_data.serialize_party_members()}, | ||
} | ||
send_event(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from albibong.classes.character import Character | ||
from albibong.classes.world_data import WorldData | ||
from albibong.threads.websocket_server import send_event | ||
|
||
|
||
def handle_event_health_update(world_data: WorldData, parameters): | ||
|
||
if world_data.is_dps_meter_running == False: | ||
return | ||
|
||
if 0 not in parameters or 6 not in parameters or 2 not in parameters: | ||
return | ||
|
||
nominal = parameters[2] | ||
target = parameters[0] | ||
inflictor = parameters[6] | ||
|
||
update_damage_or_heal(world_data, target, inflictor, nominal) | ||
|
||
|
||
def handle_event_health_updates(world_data: WorldData, parameters): | ||
|
||
if world_data.is_dps_meter_running == False: | ||
return | ||
|
||
if 0 not in parameters or 6 not in parameters or 2 not in parameters: | ||
return | ||
|
||
for i in range(len(parameters[2])): | ||
nominal = parameters[2][i] | ||
target = parameters[0] | ||
inflictor = parameters[6][i] | ||
|
||
update_damage_or_heal(world_data, target, inflictor, nominal) | ||
|
||
|
||
def update_damage_or_heal(world_data: WorldData, target, inflictor, nominal): | ||
|
||
if inflictor not in world_data.char_id_to_username: | ||
# character not initialized yet | ||
return | ||
|
||
username = world_data.char_id_to_username[inflictor] | ||
|
||
if username == "not initialized": | ||
# self not initialized | ||
return | ||
|
||
char: Character = world_data.characters[username] | ||
|
||
if nominal < 0: | ||
if target == inflictor: | ||
# suicide | ||
return | ||
char.update_damage_dealt(abs(nominal)) | ||
else: | ||
char.update_heal_dealt(nominal) | ||
|
||
ws_update_damage_heal(world_data) | ||
|
||
|
||
def ws_update_damage_heal(world_data: WorldData): | ||
event = { | ||
"type": "update_dps", | ||
"payload": {"party_members": world_data.serialize_party_members()}, | ||
} | ||
send_event(event) |
41 changes: 41 additions & 0 deletions
41
src/albibong/classes/event_handler/handle_event_new_character.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from albibong.classes.character import Character | ||
from albibong.classes.coords import Coords | ||
from albibong.classes.utils import Utils | ||
from albibong.classes.world_data import WorldData | ||
|
||
|
||
def handle_event_new_character(world_data: WorldData, parameters): | ||
|
||
id = parameters[0] | ||
uuid = parameters[7] | ||
username = parameters[1] | ||
guild = parameters[8] if 8 in parameters else "" | ||
alliance = parameters[49] if 49 in parameters else "" | ||
coords = ( | ||
Coords(parameters[15][0], parameters[15][1]) | ||
if 15 in parameters | ||
else Coords(0, 0) | ||
) | ||
equipments = parameters[38] if 38 in parameters else [] | ||
|
||
# initiate character | ||
if username not in world_data.characters: | ||
char: Character = Character( | ||
id=id, | ||
uuid=Utils.convert_int_arr_to_uuid(uuid), | ||
username=username, | ||
guild=guild, | ||
alliance=alliance, | ||
coords=coords, | ||
) | ||
char.update_equipment(equipments) | ||
world_data.characters[char.username] = char | ||
world_data.char_id_to_username[char.id] = char.username | ||
world_data.char_uuid_to_username[char.uuid] = char.username | ||
|
||
# change map | ||
else: | ||
char: Character = world_data.characters[username] | ||
char.update_equipment(equipments) | ||
char.coords = coords | ||
world_data.convert_id_to_name(old_id=char.id, new_id=id, char=char) |
14 changes: 14 additions & 0 deletions
14
src/albibong/classes/event_handler/handle_event_other_grabbed_loot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from albibong.classes.character import Character | ||
from albibong.classes.world_data import WorldData | ||
|
||
|
||
def handle_event_other_grabbed_loot(world_data: WorldData, parameters): | ||
|
||
# update char data | ||
if 2 in parameters and parameters[2] in world_data.characters: | ||
char: Character = world_data.characters[parameters[2]] | ||
char.update_loot(parameters) | ||
|
||
# update dungeon data | ||
if world_data.current_dungeon and parameters[2] == world_data.me.username: | ||
world_data.current_dungeon.update_loot(parameters) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from uuid import UUID | ||
from albibong.classes.world_data import WorldData | ||
from albibong.threads.websocket_server import send_event | ||
|
||
|
||
def handle_event_party_joined(world_data: WorldData, parameters): | ||
world_data.party_members = set(parameters[5]) | ||
ws_update_party_member(world_data) | ||
|
||
|
||
def handle_event_party_disbanded(world_data: WorldData, parameters): | ||
world_data.party_members = {world_data.me.username} | ||
ws_update_party_member(world_data) | ||
|
||
|
||
def handle_event_party_player_joined(world_data: WorldData, parameters): | ||
world_data.party_members.add(parameters[2]) | ||
ws_update_party_member(world_data) | ||
|
||
|
||
def handle_event_party_player_left(world_data: WorldData, parameters): | ||
uuid = UUID(bytes=bytes(parameters[1])) | ||
|
||
if uuid in world_data.char_uuid_to_username: | ||
name = world_data.char_uuid_to_username[uuid] | ||
|
||
# self out party | ||
if name == world_data.me.username: | ||
world_data.party_members = {world_data.me.username} | ||
ws_update_party_member(world_data) | ||
|
||
|
||
def ws_update_party_member(world_data: WorldData): | ||
event = { | ||
"type": "update_dps", | ||
"payload": {"party_members": world_data.serialize_party_members()}, | ||
} | ||
send_event(event) |
7 changes: 7 additions & 0 deletions
7
src/albibong/classes/event_handler/handle_event_update_fame.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from albibong.classes.world_data import WorldData | ||
|
||
|
||
def handle_event_update_fame(world_data: WorldData, parameters): | ||
world_data.me.update_fame(parameters) | ||
if world_data.current_dungeon: | ||
world_data.current_dungeon.update_fame(parameters) |
11 changes: 11 additions & 0 deletions
11
src/albibong/classes/event_handler/handle_event_update_re_spec_points.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from albibong.classes.world_data import WorldData | ||
|
||
|
||
def handle_event_update_re_spec_points(world_data: WorldData, parameters): | ||
|
||
# update char data | ||
world_data.me.update_re_spec(parameters) | ||
|
||
# update dungeon data | ||
if world_data.current_dungeon: | ||
world_data.current_dungeon.update_re_spec(parameters) |
40 changes: 40 additions & 0 deletions
40
src/albibong/classes/event_handler/handle_operation_change_cluster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from albibong.classes.location import Location | ||
from albibong.classes.world_data import WorldData | ||
from albibong.threads.websocket_server import send_event | ||
|
||
|
||
def handle_operation_change_cluster(world_data: WorldData, parameters): | ||
world_data.change_equipment_log = {} | ||
|
||
if 1 in parameters: | ||
check_map = Location.get_location_from_code(parameters[1]) | ||
map_type_splitted = set(check_map.type.split("_")) | ||
world_data.set_dungeon_status(check_map, map_type_splitted) | ||
|
||
if "ISLAND" in map_type_splitted or "HIDEOUT" in map_type_splitted: | ||
check_map.name = f"{parameters[2]}'s {check_map.name}" | ||
world_data.current_map = check_map | ||
|
||
elif 0 in parameters: | ||
check_map = Location.get_location_from_code(parameters[0]) | ||
map_type_splitted = set(check_map.type.split("_")) | ||
is_dungeon = world_data.set_dungeon_status(check_map, map_type_splitted) | ||
if is_dungeon == False: | ||
world_data.current_map = Location.get_location_from_code(parameters[0]) | ||
|
||
ws_update_location(world_data) | ||
|
||
|
||
def ws_update_location(world_data: WorldData): | ||
event = { | ||
"type": "update_location", | ||
"payload": { | ||
"map": world_data.current_map.name if world_data.current_map else "None", | ||
"dungeon": ( | ||
world_data.current_dungeon.name | ||
if world_data.current_dungeon | ||
else "None" | ||
), | ||
}, | ||
} | ||
send_event(event) |
Oops, something went wrong.