def load_next_card(data): session = globalVars.Session() gameID = data['gameID'] game = GameModel.getGameById(gameID, session) round = game.getCurrentRound() turn = round.getCurrentTurn(session) players = game.getAllPlayers() moderator = turn.getModerator() teller = turn.getTeller() cardData = turn.loadCard() cardData['showCard'] = True emit('load_card', cardData, room=globalVars.socketIOClients[teller.email], namespace='/io/card') emit('load_card', cardData, room=globalVars.socketIOClients[moderator.email], namespace='/io/card') if (cardData['card']['isPhrase'] == 1): isPhrase = "True" else: isPhrase = "False" for player in players: emit('is_phrase', {'isPhrase': isPhrase}, room=globalVars.socketIOClients[player.email], namespace='/io/game') session.commit() session.close()
def request_games(): print("I am requesting!") session = globalVars.Session() games = GameModel.getAllGames(session, filter=GAME_READY) gDict = dict() for game in games: maxPlayersPerTeam = game.maxPlayersPerTeam otDict = dict() tGame = dict() tGame['id'] = game.id tGame['gameID'] = game.id tGame['name'] = game.name tGame['teams'] = list() for team in game.teams: playerCount = len(team.players) tData = { 'name': team.name, 'id': team.id, 'visible': True, 'playerCount': playerCount, 'maxPlayers': maxPlayersPerTeam } if (game.maxPlayersPerTeam <= len(team.players)): tData['disableTeamJoin'] = True otDict[team.id] = tData tGame['teams'] = otDict gDict[game.id] = tGame print_item(gDict, "List of games") emit('game_list', gDict) session.close()
def setup_turn_views(turnID, skipRoll=False): session = globalVars.Session() turn = TurnModel.getTurnById(turnID, session) moderator = turn.getModerator() teller = turn.getTeller() observers = turn.getObservers() guessers = turn.getGuessers() if (skipRoll): globalVars.socketio.emit('swap_view', {'swapView': 'teller'}, room=globalVars.socketIOClients[teller.email], namespace='/io/view') else: globalVars.socketio.emit('swap_view', {'swapView': 'tellerrolldie'}, room=globalVars.socketIOClients[teller.email], namespace='/io/view') globalVars.socketio.emit('swap_view', {'swapView': 'moderator'}, room=globalVars.socketIOClients[moderator.email], namespace='/io/view') for guesser in guessers: globalVars.socketio.emit( 'swap_view', {'swapView': 'gameplayerturn'}, room=globalVars.socketIOClients[guesser.email], namespace='/io/view') for observer in observers: globalVars.socketio.emit( 'swap_view', {'swapView': 'gameplayerturn'}, room=globalVars.socketIOClients[observer.email], namespace='/io/view') session.commit() session.close()
def validate_game_start(data): session = globalVars.Session() gameID = data['gameID'] game = GameModel.getGameById(gameID, session) initiator = game.initiator initiatorEmail = initiator.email # gData = {'id':'name':} ogData = dict() gData = {'id': game.id, 'gameID': game.id, 'teams': dict()} for team in game.teams: tData = { 'name': team.name, 'id': team.id, 'visible': True, 'playerCount': team.numPlayers(), 'maxPlayers': game.maxPlayersPerTeam, 'disableTeamJoin': team.teamFull() } gData['teams'][team.id] = tData ogData[game.id] = gData emit('players_on_team', ogData, broadcast=True) print("HERE and game is", game.readyToStart()) if (game.readyToStart()): emit('show_game_start_button_enabled', room=globalVars.socketIOClients[initiatorEmail], namespace='/io/view') emit('show_game_start_button_enabled1', namespace='/io/view') else: emit('show_game_start_button_disabled', room=globalVars.socketIOClients[initiatorEmail], namespace='/io/view') session.close()
def join_team(data): # Join player to team. Check the game session = globalVars.Session() print_item(data, "Join Team: ") gameID = data['gameID'] teamID = data['teamID'] playerEmail = data['player'] game = GameModel.getGameById(gameID, session) initiator = game.initiator player = PlayerModel.findPlayerByEmail(session, playerEmail) gData = {'id': game.id, 'gameID': game.id, 'teams': dict()} for team in game.teams: # check if all teams have requisite 2 players. if (team.id == teamID): team.addPlayer(player) gData['teams'][team.id] = {'id': team.id, 'name': team.name} emit('swap_view', {'swapView': 'gameplayerwait'}, namespace='/io/view') emit('joined_team', gData, namespace='/io/game') session.commit() session.close() break validate_game_start(data)
def playerLogin(data): print("PLAYER CONNECTED:", data) session = globalVars.Session() player = PlayerModel.findPlayerByEmail(session, data['email']) if (player): globalVars.socketIOClients[request.sid] = data['email'] globalVars.socketIOClients[player.id] = request.sid globalVars.socketIOClients[data[ 'email']] = request.sid # storing request.namespace might be a good idea here else: feedback = PlayerModel.isValidPlayer(data) if not feedback['valid']: errorMessage = feedback['message'] return player = PlayerModel(data['username'], data['email'], 3) session.add(player) session.commit() globalVars.socketIOClients[request.sid] = data['email'] globalVars.socketIOClients[player.id] = request.sid globalVars.socketIOClients[data['email']] = request.sid playerLogin = { 'username': player.nickname, 'email': player.email, 'playerID': player.id, } emit( 'player_logged_in', playerLogin) # storing request.namespace might be a good idea here session.commit() session.close()
def award_penalty(data): session = globalVars.Session() print_item(data, "TAKE POINT RECEIVED") turnID = data['turnID'] turn = TurnModel.getTurnById(turnID, session) turn.penaliseTeam() session.commit() session.close() load_next_card(data)
def award_point(data): session = globalVars.Session() turnID = data['turnID'] teamID = data['teamID'] turn = TurnModel.getTurnById(turnID, session) turn.awardTeamByID(teamID) session.commit() session.close() load_next_card(data)
def skip_card(data): session = globalVars.Session() print_item(data, "SKIP RECEIVED") turnID = data['turnID'] turn = TurnModel.getTurnById(turnID, session) turn.skip() session.commit() session.close() load_next_card(data)
def resume_timer(data): session = globalVars.Session() print_item(data, "RESUMING TIMER") gameID = data['gameID'] game = GameModel.getGameById(gameID, session) round = game.getCurrentRound() turn = round.getCurrentTurn(session) turnID = turn.id timer = globalVars.turnTimers[turnID] timer.resume() session.close()
def start_game(data): session = globalVars.Session() print_item(data, 'data item to start_game') gameID = data['gameID'] game = GameModel.getGameById(gameID, session) # Puts the game in started state game.setStateStart() session.commit() emit('game_started', {'gameID': game.id}, broadcast=True) session.close() start_new_turn(data)
def start_new_turn(data): session = globalVars.Session() gameID = data['gameID'] game = GameModel.getGameById(gameID, session) session.commit() turn = game.createTurn() turnID = turn.id session.commit() session.close() setup_turn_roles(gameID) setup_turn_views(turnID)
def isGameOver(self): """ - checks if the game is over, - the game is over when one of the team has reached the points to win and all teams have played equal number of rounds :return: True if the game is over, False otherwise :rtype: bool """ session = globalVars.Session() if not (self.hasAtLeastOneRound(session)): return False elif not (self.teamsHaveEqualTurns()): return False return self.teamHasReachedThreshold() or self.reachedMaxRounds()
def turn_complete(data): session = globalVars.Session() # TODO: notify clients of turn completion # TODO: sleep for a second. # TODO: start next turn! gameID = data['gameID'] game = GameModel.getGameById(gameID, session) players = game.getAllPlayers() round = game.getCurrentRound() turn = round.getCurrentTurn(session) teamScoreData = turn.getAllTeamScores() if (game.isGameOver()): for player in players: globalVars.socketio.emit( 'report_score', teamScoreData, room=globalVars.socketIOClients[player.email], namespace='/io/game') globalVars.socketio.emit( 'swap_view', {'swapView': 'endgame'}, room=globalVars.socketIOClients[player.email], namespace='/io/view') session.commit() session.close() return waitDuration = 2 for player in players: globalVars.socketio.emit('turn_finished', {'turnID': turn.id}, room=globalVars.socketIOClients[player.email], namespace='/io/game') globalVars.socketio.emit('report_score', teamScoreData, room=globalVars.socketIOClients[player.email], namespace='/io/game') globalVars.socketio.emit('swap_view', {'swapView': 'waitforturn'}, room=globalVars.socketIOClients[player.email], namespace='/io/view') timer = globalVars.turnTimers[turn.id] print(timer) del globalVars.turnTimers[turn.id] globalVars.socketio.sleep(waitDuration) start_new_turn(data) session.commit() session.close()
def roll_wheel_test(data): session = globalVars.Session() gameID = data['gameID'] duration = data['duration'] print_item(data, "Rolling wheel: ") game = GameModel.getGameById(gameID, session) round = game.getCurrentRound() turn = round.getCurrentTurn(session) gameChanger = turn.setGameChangerTest(data['changerID']) rollWheel = { 'gameID': gameID, 'rollID': gameChanger.gameChangerId, 'description': gameChanger.description, 'name': gameChanger.name } emit('my_roll_result_test', rollWheel) globalVars.socketio.sleep(duration + 1.5) emit('enable_start_turn_button') session.commit() session.close()
def getQuizletCards(login='******', client_id='SN77uEA94G', endpoint='258934949'): # Getting the cards from quizlet. session = globalVars.Session() quizletClient = quizlet.QuizletClient(client_id=client_id, login=login) data = quizletClient.sets.endpoint.get(endpoint) count = 0 for term in data['terms']: count += 1 definitions = term['definition'].split('\n') buzzword = term['term'].strip() buzzword.split() card = CardModel(buzzword=buzzword, forbidden_words=json.dumps(definitions[:-1]), source=definitions[-1], source_page=' '.join(definitions[-1].split()[-2:]), is_phrase=(len(buzzword.split()) > 1), quizletEndpoint=endpoint) session.add(card) session.commit() session.close()
def roll_wheel(data): session = globalVars.Session() gameID = data['gameID'] duration = data['duration'] print_item(data, "Rolling wheel: ") game = GameModel.getGameById(gameID, session) # round = game.getCurrentRound() round = RoundModel.getLastRound(session, game.id) turn = TurnModel.getLastTurn(session, round.id) gameChanger = turn.setGameChanger() rollWheel = { 'gameID': gameID, 'rollID': gameChanger.gameChangerId, 'description': gameChanger.description, 'name': gameChanger.name } print_item(rollWheel, "ROLL RESULT WAS") emit('my_roll_result', rollWheel) globalVars.socketio.sleep(duration + 1.5) emit('enable_start_turn_button') session.commit() session.close()
def init_game(data): # TODO: gets passed if the game is valid and tbe user has pressed the init game button # time to build the game in the db and tell the creator's view to switch # ... # print("Email:======================" + globalVars.socketIOClients[request.sid].email) # if not PlayerModel.is_logged_in(): # print ("You are not logged in") # else: # initiator = PlayerModel.find_player_by_id(session, 1) # print ("The new game: " + str(data)) session = globalVars.Session() # feedback is a dictionary with 'valid' : boolean, 'message' : error message feedback = GameModel.isValidGame(session, data) if not feedback['valid']: # inform the creator of the game error errorMessage = feedback['message'] return initiator = PlayerModel.findPlayerByEmail( session, globalVars.socketIOClients[request.sid]) print_item(data, "GAME TO INIT:") gameArgs = { k: v for (k, v) in data.items() if k in [ 'name', 'turnDuration', 'numberOfTeams', 'maxPlayersPerTeam', 'pointsToWin', 'skipPenaltyAfter', 'maxRoundsPerGame', 'withGameChangers' ] } gameArgs['initiator'] = initiator # TODO REMOVE THIS #######################V # gameArgs['turnDuration'] = 5 # TODO REMOVE THIS ####################### game = GameModel(**gameArgs) initiatorTeamName = data['initiatorTeam']['name'] maxPlayersPerTeam = game.maxPlayersPerTeam session.add(game) session.commit() gDict = dict() otData = dict() returnTeams = [] for idx, teamObj in enumerate(data['teamData']): tData = dict() teamName = teamObj['name'] team = TeamModel(name=teamName, maxPlayersPerTeam=maxPlayersPerTeam) game.addTeam(team) session.commit() if (teamName == initiatorTeamName): team.addPlayer(initiator) tData['name'] = teamObj['name'] tData['id'] = team.id tData['teamID'] = team.id tData['visible'] = True tData['playerCount'] = len(team.players) tData['maxPlayers'] = game.maxPlayersPerTeam otData[team.id] = tData gDict[game.id] = { 'name': game.name, 'id': game.id, 'gameID': game.id, 'teams': otData } viewData = {'swapView': 'gameinitiatorwait'} emit('created_game', gDict, broadcast=True) emit('created_your_game', gDict[game.id]) emit('swap_view', viewData, namespace="/io/view") session.commit() session.close()
from flask_socketio import SocketIO import globalVars from models import * environment = 'dev' app = create_app(app_name="buzzwords", environment=environment, configFile={ 'dev_config': 'settings', 'prod_config': '' }, config={}) globalVars.socketio = SocketIO(app) globalVars.Session, globalVars.engine = create_db_connection(app, rebuilddb=True) session = globalVars.Session() if (environment): getQuizletCards() for i in range(50): email = "member" + str(i + 1) + "@bsu.edu" if PlayerModel.emailExists(session, email): print("the entered email already exists") else: member1 = PlayerModel("Member " + str(i + 1), email, 3) session.add(member1) session.flush() session.commit() initiator = PlayerModel.findPlayerById(session, 1) #print ("Email: " + initiator.email)
def setup_turn_roles(gameID, gameChanger=-1): session = globalVars.Session() game = GameModel.getGameById(gameID, session) players = game.getAllPlayers() round = game.getCurrentRound() turn = round.getCurrentTurn(session) moderator = turn.getModerator() teller = turn.getTeller() observers = turn.getObservers() guessers = turn.getGuessers() teamOnDeck = turn.team teams = [] if (not (gameChanger == -1) and gameChanger.gameChangerId == ALL_GUESSERS): for team in game.teams: tDict = dict() tDict['name'] = team.name tDict['id'] = team.id tDict['teamID'] = team.id if (team.id == teamOnDeck.id): tDict['onDeck'] = True else: tDict['onDeck'] = False teams.append(tDict) else: tDict = dict() tDict['name'] = teamOnDeck.name tDict['id'] = teamOnDeck.id tDict['teamID'] = teamOnDeck.id tDict['onDeck'] = True teams.append(tDict) turnRolesData = { 'teller': { 'email': teller.email, 'nickname': teller.nickname }, 'moderator': { 'email': moderator.email, 'nickname': moderator.nickname }, 'observers': [{ 'email': observer.email, 'nickname': observer.nickname } for observer in observers], 'guessers': [{ 'email': guesser.email, 'nickname': guesser.nickname } for guesser in guessers] } players = game.getAllPlayers() turnData = { 'roles': turnRolesData, 'team': { 'name': teamOnDeck.name, 'id': teamOnDeck.id, 'teamID': teamOnDeck.id, 'turnID': turn.id }, 'teams': teams, } for player in players: globalVars.socketio.emit('turn_data', turnData, room=globalVars.socketIOClients[player.email], namespace='/io/game') for observer in observers: globalVars.socketio.emit( 'turn_role_assignment', {'role': 'observer'}, room=globalVars.socketIOClients[observer.email], namespace='/io/game') for guesser in guessers: globalVars.socketio.emit( 'turn_role_assignment', {'role': 'guesser'}, room=globalVars.socketIOClients[guesser.email], namespace='/io/game')
def start_turn(data): session = globalVars.Session() gameID = data['gameID'] game = GameModel.getGameById(gameID, session) round = game.getCurrentRound() turn = round.getCurrentTurn(session) moderator = turn.getModerator() teller = turn.getTeller() gameChanger = turn.getGameChanger() rollWheel = { 'gameID': gameID, 'rollID': gameChanger.gameChangerId, 'description': gameChanger.description, 'name': gameChanger.name } players = game.getAllPlayers() if (gameChanger.gameChangerId == ALL_GUESSERS): setup_turn_roles(gameID, gameChanger) if (gameChanger.gameChangerId == ROUND_KILLER): teamScoreData = turn.getAllTeamScores() if (game.isGameOver()): for player in players: emit('report_score', teamScoreData, room=globalVars.socketIOClients[player.email], namespace='/io/game') emit('swap_view', {'swapView': 'endgame'}, room=globalVars.socketIOClients[player.email], namespace='/io/view') session.commit() session.close() return start_new_turn(data) session.commit() session.close() return for player in players: emit('roll_result', rollWheel, room=globalVars.socketIOClients[player.email], namespace='/io/game') emit('turn_started', {'turnID': turn.id}, room=globalVars.socketIOClients[player.email], namespace='/io/game') cardData = turn.loadCard() print_item(cardData, "CURRENT CARD IS") cardData['showCard'] = True emit('swap_view', {'swapView': 'teller'}, room=globalVars.socketIOClients[teller.email], namespace='/io/view') globalVars.socketio.sleep(1) emit('load_card', cardData, room=globalVars.socketIOClients[teller.email], namespace='/io/card') emit('load_card', cardData, room=globalVars.socketIOClients[moderator.email], namespace='/io/card') if (cardData['card']['isPhrase'] == 1): isPhrase = "True" else: isPhrase = "False" for player in players: emit('is_phrase', {'isPhrase': isPhrase}, room=globalVars.socketIOClients[player.email], namespace='/io/game') turn.startTimer() session.commit() session.close()