-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
218 lines (162 loc) · 6.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from flask import Flask, render_template, request, send_file
# from flask.ext.sqlalchemy import SQLAlchemy
import logging
from logging import Formatter, FileHandler
from forms import *
import os
from flask_socketio import SocketIO, send, join_room, leave_room, emit
from game import Game, STATE
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
app.config.from_object('config')
socketio = SocketIO(app)
current_game = None
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
def topic_callback(topic):
socketio.emit('topic', {'topic': topic}, broadcast=True)
print "Emitted topic: " + str(topic)
def pick_target_callback(other_cards_for_players):
socketio.emit('pick_target', other_cards_for_players, broadcast=True)
print "Emitted pick_target: " + str(other_cards_for_players)
def after_guess_callback(target_card):
socketio.emit('after_guess', target_card, broadcast=True)
print "Emitted after_guess: " + str(target_card)
@socketio.on('start_command')
def start(data):
global current_game
current_game = Game(num_players=3, send_topic_callback=topic_callback,
send_pick_target_callback=pick_target_callback,
send_after_guess_callback=after_guess_callback)
if current_game.begin():
to_send = {'starting_player': current_game.current_target_player}
for index, player in enumerate(current_game.players):
print "Index: " + str(index)
to_send[index] = player.deck[0:6]
emit('start', to_send, broadcast=True)
print "Game started!"
print "Game start status %s" % current_game.started
@socketio.on('start_swap_command')
def start(data):
global current_game
current_game = Game(num_players=3, send_topic_callback=topic_callback,
send_pick_target_callback=pick_target_callback,
send_after_guess_callback=after_guess_callback,
swap_cards=True)
if current_game.begin():
to_send = {'starting_player': current_game.current_target_player}
for index, player in enumerate(current_game.players):
print "Index: " + str(index)
to_send[index] = player.deck[0:6]
emit('start', to_send, broadcast=True)
print "Game started!"
print "Game start status %s" % current_game.started
@socketio.on('next_round')
def on_nr(data):
print "Trying to start next round!"
if current_game.start_next_round():
to_send = {'starting_player': current_game.current_target_player}
for index, player in enumerate(current_game.players):
print "Index: " + str(index)
to_send[index] = player.deck[0:6]
emit('start', to_send, broadcast=True)
print "next round started!"
@socketio.on('deck_card_clicked')
def deck_clicked(data):
player_id = data["playerid"]
card_id = data["cardid"]
print "card id: %s clicked by %s" % (str(card_id), str(player_id))
current_game.notify_deck_card_clicked(player_id, card_id)
@socketio.on('other_card_clicked')
def other_clicked(data):
player_id = data["playerid"]
card_id = data["cardid"]
print "card id: %s clicked by %s" % (str(card_id), str(player_id))
current_game.notify_other_card_clicked(player_id, card_id)
@socketio.on('new_topic')
def newtopic(data):
player_id = data["playerid"]
topic_text = data["topictext"]
print "Topic: %s submitted by %s" % (str(topic_text), str(player_id))
current_game.notify_new_topic(player_id, topic_text)
@socketio.on('join')
def on_join(data):
print "Player joined: " + str(data['playerid'])
# We need to send the player his stuff if the game has started
global current_game
if current_game is not None:
# Step 1: Send them their cards
player_id = data["playerid"]
cards = current_game.players[player_id].deck[0:6]
out_dict = {'starting_player': current_game.current_target_player, player_id: cards}
emit('start', out_dict)
print "Emitted starting info on rejoining player %s: %s" % (player_id, str(out_dict))
# Step 2: If the topic has already been chosen - send out topic
if current_game.state > STATE.WAIT_TOPIC:
emit('topic', {'topic': current_game.topic})
print "Emitted current topic on player %s rejoin!", player_id
if current_game.state >= STATE.ALL_CHOOSE_TARGET:
# Step 3: if it's the target choosing stage: have to send
# the other player cards to the player
other_cards_for_players = current_game.resolve_chosen_cards_for_players()
emit('pick_target', other_cards_for_players)
if current_game.state == STATE.WAITING_FOR_NEXT_ROUND:
# Step 4: if that round is 'done',send results
guessed = current_game.resolve_guessed_cards_for_players()
emit('after_guess', guessed)
@socketio.on('disconnect')
def on_leave():
print "Client left!"
# === APP ROUTES ===
@app.route('/<int:playerid>')
def player(playerid):
return render_template('pages/player.html', playerid=str(playerid))
@app.route('/<int:playerid>/play/<int:cardnum>')
def play(playerid, cardnum):
return cardnum
@app.route('/<int:playerid>/choose/<int:cardnum>')
def choose(playerid, cardnum):
return cardnum
@app.route('/')
def home():
return render_template('pages/home.html')
@app.route('/img/<int:img_num>')
def get_image(img_num):
filename = "static/img/" + str(img_num) + ".jpg"
return send_file(filename, mimetype='image/jpg')
# Error handlers.
@app.errorhandler(500)
def internal_error(error):
# db_session.rollback()
return render_template('errors/500.html'), 500
@app.errorhandler(404)
def not_found_error(error):
return render_template('errors/404.html'), 404
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#
# Default port:
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", debug=False)
# Or specify port manually:
'''
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
'''