-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
626 additions
and
626 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
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
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
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
8 changes: 4 additions & 4 deletions
8
aimmo-game/simulation/communicator.py → aimmo-game/simulation/django_communicator.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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import requests | ||
|
||
|
||
class Communicator(object): | ||
class DjangoCommunicator(object): | ||
""" | ||
This class encapsulates the communication between aimmo-game | ||
and the django server | ||
""" | ||
def __init__(self, api_url, completion_url): | ||
self.api_url = api_url | ||
def __init__(self, django_api_url, completion_url): | ||
self.django_api_url = django_api_url | ||
self.completion_url = completion_url | ||
|
||
def get_game_metadata(self): | ||
return requests.get(self.api_url).json() | ||
return requests.get(self.django_api_url).json() | ||
|
||
def mark_game_complete(self, data=None): | ||
return requests.post(requests.post(self.completion_url, json=data)) |
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 |
---|---|---|
@@ -1,47 +1,69 @@ | ||
import time | ||
import threading | ||
|
||
WORKER_UPDATE_SLEEP_TIME = 10 | ||
from django_communicator import DjangoCommunicator | ||
from simulation_runner import ConcurrentSimulationRunner | ||
from avatar.avatar_manager import AvatarManager | ||
|
||
TURN_TIME = 2 | ||
|
||
|
||
class GameRunner(threading.Thread): | ||
def __init__(self, worker_manager, game_state, communicator): | ||
def __init__(self, worker_manager_class, game_state_generator, django_api_url, port): | ||
super(GameRunner, self).__init__() | ||
self.worker_manager = worker_manager | ||
self.game_state = game_state | ||
self.communicator = communicator | ||
self.worker_manager = worker_manager_class(port=port) | ||
self.game_state = game_state_generator(AvatarManager()) | ||
self.communicator = DjangoCommunicator(django_api_url=django_api_url, | ||
completion_url=django_api_url + 'complete/') | ||
self.subscriber = None | ||
self.simulation_runner = ConcurrentSimulationRunner(communicator=self.communicator, | ||
game_state=self.game_state) | ||
self._end_turn_callback = lambda: None | ||
|
||
def set_end_turn_callback(self, callback_method): | ||
self._end_turn_callback = callback_method | ||
|
||
def get_users_to_add(self, game_metadata): | ||
def player_is_new(_player): | ||
return _player['id'] not in self.worker_manager.avatar_id_to_worker.keys() | ||
return _player['id'] not in self.worker_manager.player_id_to_worker.keys() | ||
|
||
return [player['id'] for player in game_metadata['users'] if player_is_new(player)] | ||
|
||
def get_users_to_delete(self, game_metadata): | ||
def player_in_worker_manager_but_not_metadata(pid): | ||
return pid not in [player['id'] for player in game_metadata['users']] | ||
|
||
return [player_id for player_id in self.worker_manager.avatar_id_to_worker.keys() | ||
return [player_id for player_id in self.worker_manager.player_id_to_worker.keys() | ||
if player_in_worker_manager_but_not_metadata(player_id)] | ||
|
||
def update_main_user(self, game_metadata): | ||
self.game_state.main_avatar_id = game_metadata['main_avatar'] | ||
|
||
def update(self): | ||
def update_workers(self): | ||
game_metadata = self.communicator.get_game_metadata()['main'] | ||
|
||
users_to_add = self.get_users_to_add(game_metadata) | ||
users_to_delete = self.get_users_to_delete(game_metadata) | ||
|
||
worker_urls = self.worker_manager.add_workers(users_to_add) | ||
self.worker_manager.add_workers(users_to_add) | ||
self.worker_manager.delete_workers(users_to_delete) | ||
self.game_state.add_avatars(users_to_add, worker_urls) | ||
self.game_state.add_avatars(users_to_add) | ||
self.game_state.delete_avatars(users_to_delete) | ||
self.worker_manager.update_worker_codes(game_metadata['users']) | ||
|
||
self.update_main_user(game_metadata) | ||
self.worker_manager.fetch_all_worker_data(self.game_state.get_serialised_game_states_for_workers()) | ||
|
||
def update_simulation(self, player_id_to_serialised_actions): | ||
self.simulation_runner.run_single_turn(player_id_to_serialised_actions) | ||
self._end_turn_callback() | ||
|
||
def update(self): | ||
self.update_workers() | ||
self.update_simulation(self.worker_manager.get_player_id_to_serialised_actions()) | ||
self.worker_manager.clear_logs() | ||
|
||
def run(self): | ||
while True: | ||
self.update() | ||
time.sleep(WORKER_UPDATE_SLEEP_TIME) | ||
time.sleep(TURN_TIME) |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.