aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/server
diff options
context:
space:
mode:
authornotoraptor <stevenbocco@gmail.com>2019-07-20 15:35:34 -0400
committerPhilip Paquette <pcpaquette@gmail.com>2019-07-21 15:45:33 -0400
commit8b52f299150f834b676d4dde353e5f12cdbe4012 (patch)
tree7fe14e3536e384607f87bb6c28ccfe5010f6dc5e /diplomacy/server
parent11af6bd80e1bc3f14dd66fc6508a9e7daf063a88 (diff)
Fixed synchronization issues
- Added __enter__, __exit__, and .current_state() to game object - set_orders throws an exception is the server phase is not the same as the client phase - Returning only waiting dummy powers to bot
Diffstat (limited to 'diplomacy/server')
-rw-r--r--diplomacy/server/request_managers.py6
-rw-r--r--diplomacy/server/server.py24
2 files changed, 16 insertions, 14 deletions
diff --git a/diplomacy/server/request_managers.py b/diplomacy/server/request_managers.py
index 59c0e88..ff93977 100644
--- a/diplomacy/server/request_managers.py
+++ b/diplomacy/server/request_managers.py
@@ -71,6 +71,9 @@ def on_clear_orders(server, request, connection_handler):
"""
level = verify_request(server, request, connection_handler, observer_role=False)
assert_game_not_finished(level.game)
+ if not request.phase or request.phase != level.game.current_short_phase:
+ raise exceptions.ResponseException(
+ 'Invalid order phase, received %s, server phase is %s' % (request.phase, level.game.current_short_phase))
level.game.clear_orders(level.power_name)
Notifier(server, ignore_addresses=[request.address_in_game]).notify_cleared_orders(level.game, level.power_name)
@@ -988,6 +991,9 @@ def on_set_orders(server, request, connection_handler):
"""
level = verify_request(server, request, connection_handler, observer_role=False, require_power=True)
assert_game_not_finished(level.game)
+ if not request.phase or request.phase != level.game.current_short_phase:
+ raise exceptions.ResponseException(
+ 'Invalid order phase, received %s, server phase is %s' % (request.phase, level.game.current_short_phase))
power = level.game.get_power(level.power_name)
previous_wait = power.wait
power.clear_orders()
diff --git a/diplomacy/server/server.py b/diplomacy/server/server.py
index 120e893..06648c1 100644
--- a/diplomacy/server/server.py
+++ b/diplomacy/server/server.py
@@ -53,7 +53,7 @@ import os
from random import randint
import socket
import signal
-from typing import Dict, Set
+from typing import Dict, Set, List
import tornado
import tornado.web
@@ -236,8 +236,8 @@ class Server():
# Each game also stores tokens connected (player tokens, observer tokens, omniscient tokens).
self.games = {} # type: Dict[str, ServerGame]
- # Dictionary mapping game IDs to dummy power names.
- self.games_with_dummy_powers = {} # type: dict{str, set}
+ # Dictionary mapping game ID to list of power names.
+ self.games_with_dummy_powers = {} # type: Dict[str, List[str]]
# Dictionary mapping a game ID present in games_with_dummy_powers, to
# a couple of associated bot token and time when bot token was associated to this game ID.
@@ -532,7 +532,7 @@ class Server():
:param server_game: server game to check
:type server_game: ServerGame
"""
- updated = False
+ dummy_power_names = []
if server_game.is_game_active or server_game.is_game_paused:
dummy_power_names = server_game.get_dummy_unordered_power_names()
if dummy_power_names:
@@ -542,22 +542,17 @@ class Server():
# then we also update bot time in registry of dummy powers associated to bot tokens.
bot_token, _ = self.dispatched_dummy_powers.get(server_game.game_id, (None, None))
self.dispatched_dummy_powers[server_game.game_id] = (bot_token, common.timestamp_microseconds())
- updated = True
- if not updated:
- # Registry not updated for this game, meaning that there is no
- # dummy powers waiting for orders or 'no wait' for this game.
+ if not dummy_power_names:
+ # No waiting dummy powers for this game, or game is not playable (canceled, completed, or forming).
self.games_with_dummy_powers.pop(server_game.game_id, None)
- # We remove game from registry of dummy powers associated to bot tokens only if game is terminated.
- # Otherwise, game will remain associated to a previous bot token, until bot failed to order powers.
- if server_game.is_game_completed or server_game.is_game_canceled:
- self.dispatched_dummy_powers.pop(server_game.game_id, None)
+ self.dispatched_dummy_powers.pop(server_game.game_id, None)
def get_dummy_waiting_power_names(self, buffer_size, bot_token):
""" Return names of dummy powers waiting for orders for current loaded games.
This query is allowed only for bot tokens.
:param buffer_size: maximum number of powers queried.
:param bot_token: bot token
- :return: a dictionary mapping game IDs to lists of power names.
+ :return: a dictionary mapping each game ID to a list of power names.
"""
if self.users.get_name(bot_token) != constants.PRIVATE_BOT_USERNAME:
raise exceptions.ResponseException('Invalid bot token %s' % bot_token)
@@ -568,7 +563,8 @@ class Server():
if registered_token is not None:
time_elapsed_seconds = (common.timestamp_microseconds() - registered_time) / 1000000
if time_elapsed_seconds > constants.PRIVATE_BOT_TIMEOUT_SECONDS or registered_token == bot_token:
- # This game still has dummy powers but time allocated to previous bot token is over.
+ # This game still has dummy powers but, either time allocated to previous bot token is over,
+ # or bot dedicated to this game is asking for current dummy powers of this game.
# Forget previous bot token.
registered_token = None
if registered_token is None: