Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Commit 83e19c6

Browse files
author
Tomek
committed
Use socketio to obtain all loggedin users. Display it on gui.
1 parent 6feb133 commit 83e19c6

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

game/events.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class UserNamespace(BaseNamespace, BroadcastMixin):
1515

1616
def on_join(self, user):
1717
user['gamestate'] = GAME_STATES['AVAILABLE']
18-
for u in self.users:
19-
if u['username'] == user['username']:
20-
self.error("USER IS CONNECTED")
21-
self.users.append(user)
18+
if any(filter(lambda u: u['username'] == user['username'], self.users)):
19+
self.error("user_connected", "User is connected")
20+
else:
21+
self.users.append(user)
22+
self.broadcast_event_not_me('connected', user)
23+
2224
self.emit('join', self.users)
23-
self.broadcast_event_not_me('connected', user)
25+
2426

2527
def recv_disconnect(self):
2628
# Remove nickname from the list.

game/static/js/custom.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
// $(document).ready(function(){
2-
// $('#logout').click(function(){
3-
// $.post('/accounts/logout/').
4-
// done(function(){
5-
// location.reload();
6-
// });
7-
// });
8-
// });
1+
$(document).ready(function(){
2+
var username = $("#username").text();
3+
var usocket = io.connect('/users');
4+
5+
usocket.on('join', function(users){
6+
var tbl_body = "";
7+
for (var i = 0; i < users.length; i++) {
8+
tbl_body += "<tr><td>" + users[i].username + "</td>" + "<td>" + users[i].gamestate + "</td>";
9+
if(users[i].gamestate == 2 ){
10+
tbl_body += "<td></td>";
11+
}
12+
else{
13+
tbl_body += "<td><button>Small button</button></td>";
14+
}
15+
}
16+
$('#playerTable').append(tbl_body);
17+
});
18+
19+
usocket.on('connected', function(user) {
20+
var tbl_body = "";
21+
tbl_body += "<tr><td>" + user.username + "</td>" + "<td>" + user.gamestate + "</td>";
22+
if(user.gamestate == 2 ){
23+
tbl_body += "<td></td>";
24+
} else {
25+
tbl_body += "<td><button>Small button</button></td>";
26+
}
27+
tbl_body += "</tr>";
28+
$('#playerTable').append(tbl_body);
29+
});
30+
31+
32+
usocket.emit('join', {'username':username});
33+
});

memo/templates/base_index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
================================================== -->
5050
<!-- Placed at the end of the document so the pages load faster -->
5151
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
52-
<script src="{{ STATIC_URL }}/js/bootstrap.min.js"></script>
52+
<script src="{{ STATIC_URL }}js/bootstrap.min.js"></script>
5353
{% block js_import %}
5454
{% endblock %}
5555

memo/templates/main_view.html

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,10 @@ <h2>Games in progress</h2>
1919

2020
</div>
2121

22+
<div id="username">{{ user.username }}</div>
2223
{% endblock %}
2324
{% block js_import %}
2425
{% socketio %}
25-
<script src="{{ STATIC_URL }}/js/fabric.js"></script>
26-
<script>
27-
$(document).ready(function(){
28-
//var json = '{"players" : [ {"username":"tomek","gamestate":0},{"username":"rysiek","gamestate":1}, {"username":"kamil","gamestate":2}]}';
29-
30-
var usocket = io.connect('/users');
31-
32-
usocket.on('join', function(users){
33-
var tbl_body = "";
34-
for (var i = 0; i < users.length; i++) {
35-
tbl_body += "<tr><td>" + users[i].username + "</td>" + "<td>" + users[i].gamestate + "</td>";
36-
if(users[i].gamestate == 2 ){
37-
tbl_body += "<td></td>";
38-
}
39-
else{
40-
tbl_body += "<td><button>Small button</button></td>";
41-
}
42-
tbl_body += "</tr>";
43-
}
44-
$('#playerTable').append(tbl_body);
45-
});
46-
47-
usocket.on('connected', function(user) {
48-
var tbl_body = "";
49-
tbl_body += "<tr><td>" + user.username + "</td>" + "<td>" + user.gamestate + "</td></tr>";
50-
if(user.gamestate == 2 ){
51-
tbl_body += "<td></td>";
52-
}
53-
else{
54-
tbl_body += "<td><button>Small button</button></td>";
55-
}
56-
$('#playerTable').append(tbl_body);
57-
});
58-
59-
60-
usocket.emit('join', {'username':'rysiek'});
61-
62-
});
63-
64-
65-
66-
</script>
26+
<script src="{{ STATIC_URL }}js/fabric.js"></script>
27+
<script src="{{ STATIC_URL }}js/custom.js"></script>
6728
{% endblock %}

0 commit comments

Comments
 (0)