-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
134 lines (77 loc) · 3.17 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
# -*- coding: utf-8 -*-
#needed for debug mode to work!
from gevent import monkey
monkey.patch_all()
#do some imports!
from flask import Flask, render_template, request, jsonify, current_app, session
from flask.ext.socketio import SocketIO, emit
import urllib, json
# from Queue import Empty
# # from time import sleep
#get game classes
from threading import Thread
from Queue import Queue
from game import Game, Player, Badguy, Room
#start me up!
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
queue = Queue()
#FLASK
@app.route('/')
def index():
return render_template('main.html')
@socketio.on('connect')
def on_connect():
with app.app_context():
if not current_app.game:
return
session['id'] = current_app.game.id_count
msg = "New Connection: {}.".format(session['id'])
current_app.game.id_count += 1
emit('connection_response', {"msg":msg, "player_chosen_colors_dict":current_app.game.player_chosen_colors_dict}, broadcast=True,)
@socketio.on('keypress_request')
def keypress_func(d):
if session.get('id') is not None:
d['id'] = session['id']
queue.put_nowait(d)
@socketio.on('player_choose_request')
def player_choose_func(d):
msg = "Player {} chooses {}.".format(session['id'],d['col'])
current_app.game.player_to_color_dict[session['id']] = d['col']
player_chosen_colors_dict = {'red':0,'blu':0,'gre':0,'yel':0}
for val in current_app.game.player_to_color_dict.values():
player_chosen_colors_dict[val] += 1
emit('player_choose_response', {"msg":msg, "player_chosen_colors_dict":player_chosen_colors_dict,}, broadcast=True,)
def broadcast_game_state(data):
socketio.emit('get_game_state_response', data)
@socketio.on('start_game_request')
def start_game_func(d):
for key in current_app.game.player_to_color_dict.keys():
current_app.game.add_player(id=key,color=current_app.game.player_to_color_dict[key])
msg = "Starting game."
thread = Thread(target=current_app.game.run)
thread.start()
emit('start_game_response', {"msg":msg, "world_width":current_app.game.world_width, "world_height":current_app.game.world_height}, broadcast=True,)
# @socketio.on('reset_game_request')
# def reset_game_func(d):
# msg = "Resetting game"
# # queue.put_nowait({"type":"STOP"})
# # game = Game(queue, broadcast_game_state)
# # current_app.game = game
# current_app.id_count = 0
# current_app.player_to_color_dict = {}
# current_app.player_chosen_colors_dict = {'red':0,'blu':0,'gre':0,'yel':0}
# emit('connection_response', {"msg":msg, "player_chosen_colors_dict":current_app.player_chosen_colors_dict}, broadcast=True,)
@app.before_first_request
def initialize_game():
game = Game(queue, broadcast_game_state)
current_app.game = game
# current_app.player_chosen_colors_dict = {'red':0,'blu':0,'gre':0,'yel':0}
# current_app.id_count = 0
# current_app.player_to_color_dict = {}
if __name__ == '__main__':
with app.app_context():
current_app.game = None
socketio.run(app, host= '0.0.0.0', port=5000)