Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions server/player_and_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, game, id, name=None):

def dict(self):
cards = {}
for k, v in self.cards.iteritems():
for k, v in self.cards.items():
cards[k] = array_dict(v)
return {
'id': self.id,
Expand Down Expand Up @@ -213,7 +213,7 @@ def player_from_dict(obj, game):
self.uuid = obj['uuid']
self.reserved = [card_from_dict(x) for x in obj['reserved']]
self.gems = obj['gems']
for k, v in obj['cards'].iteritems():
for k, v in obj['cards'].items():
self.cards[k] = [card_from_dict(x) for x in v]
self.nobles = [noble_from_dict(x) for x in v]
return self
Expand Down Expand Up @@ -267,7 +267,7 @@ def __init__(self, c, p, w=0, u=0, g=0, r=0, b=0):

def __str__(self):
result = "{0} card worth {1}, costing ".format(COLOR_DICT[self.color], self.points)
costs = ["{0} {1}".format(v, COLOR_DICT[k]) for k, v in self.cost.iteritems() if v > 0]
costs = ["{0} {1}".format(v, COLOR_DICT[k]) for k, v in self.cost.items() if v > 0]
return result + ', '.join(costs)

def dict(self):
Expand Down Expand Up @@ -297,7 +297,7 @@ def array_dict(cards):

def shuffle_deck(deck):
n = len(deck)
for i in xrange(n):
for i in range(n):
j = random.randint(i, n-1)
x = deck[j]
deck[j] = deck[i]
Expand All @@ -318,7 +318,7 @@ def __init__(self, nid, p, w=0, u=0, g=0, r=0, b=0):

def __str__(self):
result = "noble worth {0}, seeking ".format(self.points)
costs = ["{0} {1}".format(v, COLOR_DICT[k]) for k, v in self.requirement.iteritems() if v > 0]
costs = ["{0} {1}".format(v, COLOR_DICT[k]) for k, v in self.requirement.items() if v > 0]
return result + ', '.join(costs)

def dict(self):
Expand Down Expand Up @@ -507,9 +507,9 @@ def dict(self, player_id=None):
def private_dict(self):
cards = {}
decks = {}
for k, v in self.cards.iteritems():
for k, v in self.cards.items():
cards[k] = array_dict(v)
for k, v in self.decks.iteritems():
for k, v in self.decks.items():
decks[k] = array_dict(v)

return {
Expand Down Expand Up @@ -648,9 +648,9 @@ def game_from_dict(obj):
self.active_player_index = obj['turn']
self.num_players = obj['num_players']
self.players = [player_from_dict(p, self) if p else None for p in obj['players']]
for k, v in obj['cards'].iteritems():
for k, v in obj['cards'].items():
self.cards[k] = [card_from_dict(c) for c in v]
for k, v in obj['decks'].iteritems():
for k, v in obj['decks'].items():
self.decks[k] = [card_from_dict(c) for c in v]
self.nobles = [noble_from_dict(n) for n in obj['nobles']]
return self
Expand Down
20 changes: 14 additions & 6 deletions server/splendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import json
import os
import platform
import sys

app = Flask(__name__)
Expand Down Expand Up @@ -114,6 +115,8 @@ def spectate_game(self):
return {'id': pid, 'uuid': uuid}

def start_game(self):
if self.game.num_players < 2:
return {'error': 'Need at least 2 players to start'}
if self.game.start_game():
self.has_changed()
self.started = True
Expand All @@ -125,7 +128,7 @@ def game_manager_from_dict(obj):
self = GameManager(obj['uuid'])
self.starter = obj['starter']
self.game = game_from_dict(obj['game'])
for k, v in obj['changed'].iteritems():
for k, v in obj['changed'].items():
self.changed[int(k)] = v
self.chats = obj['chats']
self.ended = obj['ended']
Expand Down Expand Up @@ -196,7 +199,7 @@ def start_game(game, starter):
def suggest_game():
global game_map, words
n = len(words)
idx = random.choice(xrange(n))
idx = random.choice(range(n))
start = idx
while words[idx] in game_map:
idx = (idx + 1) % n
Expand Down Expand Up @@ -264,7 +267,7 @@ def act(game, action, target):
@json_response
def list_games():
delete_games = []
for k, v in game_map.iteritems():
for k, v in game_map.items():
if not v.started and time.time() - v.created > 600:
delete_games.append(k)
elif v.started and time.time() - v.created > 24*60*60:
Expand Down Expand Up @@ -327,7 +330,7 @@ def save_and_exit(number, frame):
global game_map

games = {}
for k, v in game_map.iteritems():
for k, v in game_map.items():
games[k] = v.private_dict()
with open('server/save.json', 'w') as f:
f.write(json.dumps(games))
Expand All @@ -341,10 +344,15 @@ def save_and_exit(number, frame):
try:
with open('server/save.json') as f:
save = json.loads(f.read())
for k, v in save.iteritems():
for k, v in save.items():
game_map[k] = game_manager_from_dict(v)
except IOError:
pass

signal.signal(signal.SIGHUP, save_and_exit)
if platform.system() != 'Windows':
signal.signal(signal.SIGHUP, save_and_exit)
# These usually work on both, but Windows only supports a limited set
signal.signal(signal.SIGINT, save_and_exit) # Handles Ctrl+C
signal.signal(signal.SIGTERM, save_and_exit) # Handles termination

app.run(host='127.0.0.1', port=8000, threaded=True)