def act_aiJoin(data): game = dbi.getXbyY('Game', 'id', data['gameId']) if game.state != GAME_WAITING: raise BadFieldException('badGameState') maxPlayersNum = game.map.playersNum if len(game.players) >= maxPlayersNum: raise BadFieldException('tooManyPlayers') maxPriority = max(game.players, key=lambda x: x.priority).priority if len( game.players) else 0 aiCnt = len(filter(lambda x: x.isAI == True, game.players)) sid = getSid() ai = User('AI%d' % sid, None, True) ai.sid = sid ai.gameId = game.id ai.isReady = True ai.priority = maxPriority + 1 ai.inGame = True dbi.add(ai) dbi.flush(ai) game.aiRequiredNum -= 1 readyPlayersNum = dbi.query(User).filter(User.gameId == game.id).filter( User.isReady == True).count() if maxPlayersNum == readyPlayersNum: misc_game.startGame(game, ai, data) return {'result': 'ok', 'sid': ai.sid, 'id': ai.id}
def act_getMessages(data): since = data['since'] records = dbi.query(Message).filter(Message.id > since).order_by(Message.id).all()[-100:] messages = [] for rec in records: messages.append({'id': rec.id , 'text': rec.text, 'time': rec.time, 'username': rec.senderUser.name}) return {'result': 'ok', 'messages': messages}
def act_selectRace(data): user = dbi.getXbyY('User', 'sid', data['sid']) if user.currentTokenBadge: raise BadFieldException('badStage') game = user.game if not (game and user.inGame): raise BadFieldException('notInGame') checkStage(GAME_SELECT_RACE, user) if (user.game.state == misc.GAME_START): user.game.state = misc.GAME_PROCESSING chosenBadge = game.getTokenBadge(data['position']) position = chosenBadge.pos tokenBadges = dbi.query(TokenBadge).order_by(TokenBadge.pos).all() tokenBadges = tokenBadges[:6] price = position if user.coins < price : raise BadFieldException('badMoneyAmount') raceId, specialPowerId = chosenBadge.raceId, chosenBadge.specPowId addUnits = callRaceMethod(raceId, 'turnStartReinforcements') tokensNum = races.racesList[raceId].initialNum + races.specialPowerList[specialPowerId].tokensNum user.coins += chosenBadge.bonusMoney - price user.currentTokenBadge = chosenBadge user.tokensInHand = tokensNum + addUnits chosenBadge.inDecline = False chosenBadge.bonusMoney = 0 chosenBadge.totalTokensNum = tokensNum + addUnits chosenBadge.specPowNum = races.specialPowerList[specialPowerId].bonusNum chosenBadge.pos = None dbi.flush(chosenBadge) updateRacesOnDesk(game, position) dbi.updateHistory(user, GAME_SELECT_RACE, chosenBadge.id) dbi.updateGameHistory(game, data) return {'result': 'ok', 'tokenBadgeId': chosenBadge.id}
def act_getMapList(data): result = {'result': 'ok'} result['maps'] = list() maps = dbi.query(Map).all() for map_ in maps: result['maps'].append(getMapState(map_.id)) return result
def getNextRaceAndPowerFromStack(game, vRace, vSpecialPower): if vRace != None and vSpecialPower != None: race = filter(lambda x: x.name == vRace, races.racesList) if not race: raise BadFieldException('badRace') raceId = races.racesList.index(race[0]) specialPower = filter(lambda x: x.name == vSpecialPower, races.specialPowerList) if not specialPower: raise BadFieldException('badStage') specialPowerId = races.specialPowerList.index(specialPower[0]) else: racesInStack = range(0, misc.RACE_NUM) specialPowersInStack = range(0, misc.SPECIAL_POWER_NUM) tokenBadges = dbi.query(TokenBadge).filter( TokenBadge.gameId == game.id).all() for tokenBadge in tokenBadges: racesInStack.remove(tokenBadge.raceId) specialPowersInStack.remove(tokenBadge.specPowId) if misc.TEST_MODE: raceId = random.choice(racesInStack) specialPowerId = random.choice(specialPowersInStack) else: raceId = racesInStack[generateNextNum(game) % len(racesInStack)] specialPowerId = specialPowersInStack[generateNextNum(game) % len(specialPowersInStack)] return raceId, specialPowerId
def act_selectRace(data): user = dbi.getXbyY('User', 'sid', data['sid']) if user.currentTokenBadge: raise BadFieldException('badStage') game = user.game if not (game and user.inGame): raise BadFieldException('notInGame') checkStage(GAME_SELECT_RACE, user) if (user.game.state == misc.GAME_START): user.game.state = misc.GAME_PROCESSING chosenBadge = game.getTokenBadge(data['position']) position = chosenBadge.pos tokenBadges = dbi.query(TokenBadge).order_by(TokenBadge.pos).all() tokenBadges = tokenBadges[:6] price = position if user.coins < price: raise BadFieldException('badMoneyAmount') raceId, specialPowerId = chosenBadge.raceId, chosenBadge.specPowId addUnits = callRaceMethod(raceId, 'turnStartReinforcements') tokensNum = races.racesList[raceId].initialNum + races.specialPowerList[ specialPowerId].tokensNum user.coins += chosenBadge.bonusMoney - price user.currentTokenBadge = chosenBadge user.tokensInHand = tokensNum + addUnits chosenBadge.inDecline = False chosenBadge.bonusMoney = 0 chosenBadge.totalTokensNum = tokensNum + addUnits chosenBadge.specPowNum = races.specialPowerList[specialPowerId].bonusNum chosenBadge.pos = None dbi.flush(chosenBadge) updateRacesOnDesk(game, position) dbi.updateHistory(user, GAME_SELECT_RACE, chosenBadge.id) dbi.updateGameHistory(game, data) return {'result': 'ok', 'tokenBadgeId': chosenBadge.id}
def act_getGameList(data): result = {'result': 'ok'} games = dbi.query(Game).filter(Game.state != GAME_ENDED).all() result['games'] = list() gameAttrs = ['id', 'name', 'descr', 'state', 'turn', 'activePlayerId', 'mapId'] gameAttrNames = ['gameId', 'gameName', 'gameDescription', 'state', 'turn', 'activePlayerId', 'mapId'] gameAttrs.append('aiRequiredNum') gameAttrNames.append('aiRequiredNum') playerAttrs = ['id', 'name', 'isReady'] playerAttrNames = ['userId', 'username', 'isReady'] for game in games: curGame = dict() for i in range(len(gameAttrs)): if gameAttrs[i] == 'descr': continue curGame[gameAttrNames[i]] = getattr(game, gameAttrs[i]) players = game.players resPlayers = list() priority = 0 for player in players: curPlayer = dict() for i in range(len(playerAttrs)): curPlayer[playerAttrNames[i]] = getattr(player, playerAttrs[i]) resPlayers.append(curPlayer) curGame['players'] = resPlayers curGame['maxPlayersNum'] = game.map.playersNum curGame['turnsNum'] = game.map.turnsNum result['games'].append(curGame) return result
def getVisibleTokenBadges(gameId): rows = dbi.query(TokenBadge).filter(and_(TokenBadge.gameId == gameId, TokenBadge.pos >= 0)).order_by(asc(TokenBadge.pos)) result = list() for tokenBadge in rows[:6]: result.append({ 'tokenBadgeId': tokenBadge.id, 'raceName': races.racesList[tokenBadge.raceId].name, 'specialPowerName': races.specialPowerList[tokenBadge.specPowId].name, 'bonusMoney': tokenBadge.bonusMoney}) return result
def showNextRace(game, lastIndex, vRace = None, vSpecialPower = None): raceId, specPowerId = getNextRaceAndPowerFromStack(game, vRace, vSpecialPower) tokenBadges = dbi.query(TokenBadge).filter(TokenBadge.gameId == game.id).all() tokenBadgesInStack = filter(lambda x: not x.Owner() and not x.inDecline and x.pos > lastIndex, tokenBadges) for tokenBadge in tokenBadgesInStack: tokenBadge.pos -= 1 dbi.flush(tokenBadge) tokBadge = TokenBadge(raceId, specPowerId, game.id) dbi.add(tokBadge) dbi.flush(tokBadge) return races.racesList[raceId].name, races.specialPowerList[specPowerId].name,
def updateRacesOnDesk(game, position): for tokenBadge in filter(lambda x: x.pos < position, game.tokenBadges): tokenBadge.bonusMoney += 1 if len(filter(lambda x: x.pos is not None, game.tokenBadges)) < 6: showNextRace(game, position) else: tokenBadges = dbi.query(TokenBadge).filter(TokenBadge.gameId == game.id).all() tokenBadgesInStack = filter(lambda x: not x.Owner() and not x.inDecline and x.pos > position, tokenBadges) for tokenBadge in tokenBadgesInStack: tokenBadge.pos -= 1 dbi.flush(tokenBadge)
def act_getMessages(data): since = data['since'] records = dbi.query(Message).filter(Message.id > since).order_by( Message.id).all()[-100:] messages = [] for rec in records: messages.append({ 'id': rec.id, 'text': rec.text, 'time': rec.time, 'username': rec.senderUser.name }) return {'result': 'ok', 'messages': messages}
def updateRacesOnDesk(game, position): for tokenBadge in filter(lambda x: x.pos < position, game.tokenBadges): tokenBadge.bonusMoney += 1 if len(filter(lambda x: x.pos is not None, game.tokenBadges)) < 6: return showNextRace(game, position) else: tokenBadges = dbi.query(TokenBadge).filter( TokenBadge.gameId == game.id).all() tokenBadgesInStack = filter( lambda x: not x.Owner() and not x.inDecline and x.pos > position, tokenBadges) for tokenBadge in tokenBadgesInStack: tokenBadge.pos -= 1 dbi.flush(tokenBadge)
def act_aiJoin(data): game = dbi.getXbyY('Game', 'id', data['gameId']) if game.state != GAME_WAITING: raise BadFieldException('badGameState') maxPlayersNum = game.map.playersNum if len(game.players) >= maxPlayersNum: raise BadFieldException('tooManyPlayers') maxPriority = max(game.players, key=lambda x: x.priority).priority if len(game.players) else 0 games = dbi.query(Game).all() aiCnt = reduce(lambda a, b: a + b, map(lambda y : len(filter(lambda x: x.isAI == True, y.players)), games)) ai = User('AI%d' % aiCnt, None, True) ai.sid = getSid() ai.gameId = game.id ai.isReady = True ai.priority = maxPriority + 1 ai.inGame = True dbi.add(ai) dbi.flush(ai) game.aiRequiredNum -= 1 readyPlayersNum = dbi.query(User).filter(User.gameId == game.id).filter(User.isReady==True).count() if maxPlayersNum == readyPlayersNum: misc_game.startGame(game, ai, data) return {'result': 'ok', 'sid' : ai.sid, 'id' : ai.id}
def showNextRace(game, lastIndex, vRace=None, vSpecialPower=None): raceId, specPowerId = getNextRaceAndPowerFromStack(game, vRace, vSpecialPower) tokenBadges = dbi.query(TokenBadge).filter( TokenBadge.gameId == game.id).all() tokenBadgesInStack = filter( lambda x: not x.Owner() and not x.inDecline and x.pos > lastIndex, tokenBadges) for tokenBadge in tokenBadgesInStack: tokenBadge.pos -= 1 dbi.flush(tokenBadge) tokBadge = TokenBadge(raceId, specPowerId, game.id) dbi.add(tokBadge) dbi.flush(tokBadge) return races.racesList[raceId].name, races.specialPowerList[ specPowerId].name,
def getVisibleTokenBadges(gameId): rows = dbi.query(TokenBadge).filter( and_(TokenBadge.gameId == gameId, TokenBadge.pos >= 0)).order_by(asc(TokenBadge.pos)) result = list() for tokenBadge in rows[:6]: result.append({ 'tokenBadgeId': tokenBadge.id, 'raceName': races.racesList[tokenBadge.raceId].name, 'specialPowerName': races.specialPowerList[tokenBadge.specPowId].name, 'bonusMoney': tokenBadge.bonusMoney }) return result
def act_setReadinessStatus(data): user = dbi.getXbyY('User', 'sid', data['sid']) game = user.game if not (game and user.inGame): raise BadFieldException('notInGame') if game.state != GAME_WAITING: raise BadFieldException('badGameState') user.isReady = data['isReady'] dbi.flush(user) maxPlayersNum = game.map.playersNum readyPlayersNum = dbi.query(User).filter(User.game == game).filter( User.isReady == True).count() if maxPlayersNum == readyPlayersNum: misc_game.startGame(game, user, data) dbi.updateGameHistory(user.game, data) return {'result': 'ok'}
def act_setReadinessStatus(data): user = dbi.getXbyY('User', 'sid', data['sid']) game = user.game if not (game and user.inGame): raise BadFieldException('notInGame') if game.state != GAME_WAITING: raise BadFieldException('badGameState') user.isReady = data['isReady'] dbi.flush(user) maxPlayersNum = game.map.playersNum readyPlayersNum = dbi.query(User).filter(User.game==game).filter(User.isReady==True).count() if maxPlayersNum == readyPlayersNum: misc_game.startGame(game, user, data) dbi.updateGameHistory(user.game, data) return {'result': 'ok'}
def act_getGameList(data): result = {'result': 'ok'} games = dbi.query(Game).filter(Game.state != GAME_ENDED).all() result['games'] = list() gameAttrs = [ 'id', 'name', 'descr', 'state', 'turn', 'activePlayerId', 'mapId' ] gameAttrNames = [ 'gameId', 'gameName', 'gameDescription', 'state', 'turn', 'activePlayerId', 'mapId' ] gameAttrs.append('aiRequiredNum') gameAttrNames.append('aiRequiredNum') playerAttrs = ['id', 'name', 'isReady'] playerAttrNames = ['userId', 'username', 'isReady'] for game in games: curGame = dict() for i in range(len(gameAttrs)): if gameAttrs[i] == 'descr': continue curGame[gameAttrNames[i]] = getattr(game, gameAttrs[i]) players = game.players resPlayers = list() priority = 0 for player in players: curPlayer = dict() for i in range(len(playerAttrs)): curPlayer[playerAttrNames[i]] = getattr(player, playerAttrs[i]) resPlayers.append(curPlayer) curGame['players'] = resPlayers curGame['maxPlayersNum'] = game.map.playersNum curGame['turnsNum'] = game.map.turnsNum result['games'].append(curGame) return result
def getNextRaceAndPowerFromStack(game, vRace, vSpecialPower): if vRace != None and vSpecialPower !=None: race = filter(lambda x: x.name == vRace, races.racesList) if not race: raise BadFieldException('badRace') raceId = races.racesList.index(race[0]) specialPower = filter(lambda x: x.name == vSpecialPower, races.specialPowerList) if not specialPower: raise BadFieldException('badStage') specialPowerId = races.specialPowerList.index(specialPower[0]) else: racesInStack = range(0, misc_const.RACE_NUM) specialPowersInStack = range(0, misc_const.SPECIAL_POWER_NUM) tokenBadges = dbi.query(TokenBadge).filter(TokenBadge.gameId == game.id).all() for tokenBadge in tokenBadges: racesInStack.remove(tokenBadge.raceId) specialPowersInStack.remove(tokenBadge.specPowId) if misc_const.TEST_MODE: raceId = random.choice(racesInStack) specialPowerId = random.choice(specialPowersInStack) else: raceId = racesInStack[generateNextNum(game) % len(racesInStack)] specialPowerId = specialPowersInStack[generateNextNum(game) % len(specialPowersInStack)] return raceId, specialPowerId
def conquered(self, region, tokenBadge): if len(dbi.query(WarHistoryEntry).filter(WarHistoryEntry.agressorBadgeId == tokenBadge.id).all()) >= 2: return for region in tokenBadge.regions: region.holeInTheGround = True