Skip to content

Commit 2a246ca

Browse files
author
KirillMysnik
committed
Made PlayerBasedFeaturePage class and everything related to it more flexible
1 parent 64fc32d commit 2a246ca

File tree

5 files changed

+111
-84
lines changed

5 files changed

+111
-84
lines changed

flask-motd/motdplayer_applications/admin/core.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@
88
# =============================================================================
99
# >> CONSTANTS
1010
# =============================================================================
11-
MAX_USERID_ABS_VALUE = 100000000
12-
MAX_PLAYERS_NUMBER = 256
11+
MAX_PLAYER_ID_LENGTH = 20
12+
MAX_PLAYERS_NUMBER = 1
1313

1414

1515
# =============================================================================
1616
# >> FUNCTIONS
1717
# =============================================================================
18-
def filter_userids(userids):
19-
new_userids = []
20-
for userid in userids:
21-
if not isinstance(userid, int):
18+
def filter_ids(player_ids):
19+
new_player_ids = []
20+
for player_id in player_ids:
21+
if not (isinstance(player_id, str) or isinstance(player_id, int)):
2222
return None
2323

24-
if not (-MAX_USERID_ABS_VALUE <= userid <= MAX_USERID_ABS_VALUE):
24+
if len(str(player_id).encode('utf-8')) > MAX_PLAYER_ID_LENGTH:
2525
return None
2626

27-
new_userids.append(userid)
27+
new_player_ids.append(player_id)
2828

29-
if len(userids) > MAX_PLAYERS_NUMBER:
29+
if len(new_player_ids) > MAX_PLAYERS_NUMBER:
3030
return None
3131

32-
return new_userids
32+
return new_player_ids
3333

3434

3535
# =============================================================================
@@ -56,16 +56,16 @@ def new_callback(ex_data_func, data):
5656
return callback(ex_data_func, data)
5757

5858
if data['action'] == "execute":
59-
if 'player_userids' not in data:
59+
if 'player_ids' not in data:
6060
return
6161

62-
player_userids = filter_userids(data['player_userids'])
63-
if player_userids is None:
62+
player_ids = filter_ids(data['player_ids'])
63+
if player_ids is None:
6464
return
6565

6666
return ex_data_func({
6767
'action': "execute",
68-
'player_userids': player_userids,
68+
'player_ids': player_ids,
6969
})
7070

7171
if data['action'] == "get-players":
@@ -84,16 +84,16 @@ def new_callback(data):
8484
return callback(data)
8585

8686
if data['action'] == "execute":
87-
if 'player_userids' not in data:
87+
if 'player_ids' not in data:
8888
return
8989

90-
player_userids = filter_userids(data['player_userids'])
91-
if player_userids is None:
90+
player_ids = filter_ids(data['player_ids'])
91+
if player_ids is None:
9292
return
9393

9494
return {
9595
'action': "execute",
96-
'player_userids': player_userids,
96+
'player_ids': player_ids,
9797
}
9898

9999
if data['action'] == "get-players":

flask-motd/static/admin/core/css/player_based_feature_page.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
animation-duration: 1s;
3535
}
3636

37-
.player-table-userid {
37+
.player-table-id {
3838
float: left;
39-
width: 80px;
39+
width: 200px;
4040
text-align: center;
4141
font-family: monospace;
4242
font-size: 9pt;
4343
background-color: rgba(255, 255, 255, .1);
4444
}
4545

46-
.player-table-line:hover .player-table-userid {
46+
.player-table-line:hover .player-table-id {
4747
background-color: rgba(255, 255, 255, .33);
4848
}
4949

flask-motd/static/admin/core/js/player_based_feature_page.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@ var PLUGIN = function () {
66
var PlayerBasedFeaturePage = function (tableNode) {
77
var playerBasedFeaturePage = this;
88

9-
var PlayerEntry = function (userid, name) {
9+
var PlayerEntry = function (id, name) {
1010
var playerEntry = this;
1111

12-
this.userid = userid;
12+
this.id = id;
1313
this.name = name;
1414

1515
var lineNode;
1616
this.create = function (node) {
1717
lineNode = node.appendChild(document.createElement('div'));
1818
lineNode.classList.add('player-table-line');
1919
var cellNode = lineNode.appendChild(document.createElement('div'));
20-
cellNode.appendChild(document.createTextNode(playerEntry.userid));
21-
cellNode.classList.add('player-table-userid');
20+
cellNode.appendChild(document.createTextNode(playerEntry.id));
21+
cellNode.classList.add('player-table-id');
2222
cellNode = lineNode.appendChild(document.createElement('div'));
2323
cellNode.appendChild(document.createTextNode(playerEntry.name));
2424
cellNode.classList.add('player-table-name');
2525

2626
lineNode.style.animationName = "player-table-row-add";
2727

2828
lineNode.addEventListener('click', function (e) {
29-
if (playerEntry.userid > -1)
30-
executeOnPlayer(playerEntry.userid);
29+
if (playerEntry.id)
30+
executeOnPlayer(playerEntry.id);
3131
});
3232
};
3333
this.destroy = function () {
34-
playerEntry.userid = -1;
34+
playerEntry.id = undefined;
3535
lineNode.style.animationName = "player-table-row-remove";
3636
setTimeout(function () {
3737
if (lineNode)
@@ -40,7 +40,7 @@ var PLUGIN = function () {
4040
}, ANIMATION_DURATION);
4141
};
4242
this.destroyNoDelay = function () {
43-
playerEntry.userid = -1;
43+
playerEntry.id = -1;
4444
lineNode.parentNode.removeChild(lineNode);
4545
lineNode = undefined;
4646
};
@@ -56,16 +56,16 @@ var PLUGIN = function () {
5656
});
5757
playerEntries = [];
5858
};
59-
var addPlayer = function (userid, name) {
60-
removeUserid(userid);
61-
var playerEntry = new PlayerEntry(userid, name);
59+
var addPlayer = function (id, name) {
60+
removeId(id);
61+
var playerEntry = new PlayerEntry(id, name);
6262
playerEntry.create(tableNode);
6363
playerEntries.push(playerEntry);
6464
};
65-
var removeUserid = function (userid) {
65+
var removeId = function (id) {
6666
var invalidEntries = [];
6767
playerEntries.forEach(function (val, i, arr) {
68-
if (val.userid == userid)
68+
if (val.id == id)
6969
invalidEntries.push(val);
7070
});
7171
invalidEntries.forEach(function (val, i, arr) {
@@ -84,10 +84,10 @@ var PLUGIN = function () {
8484
}, function (data) {
8585
switch (data['action']) {
8686
case 'add-player':
87-
addPlayer(data['player']['userid'], data['player']['name']);
87+
addPlayer(data['player']['id'], data['player']['name']);
8888
break;
89-
case 'remove-userid':
90-
removeUserid(data['userid']);
89+
case 'remove-id':
90+
removeId(data['id']);
9191
break;
9292
}
9393
if (wsMessageCallback)
@@ -112,7 +112,7 @@ var PLUGIN = function () {
112112
}, function (data) {
113113
clearPlayers();
114114
data['players'].forEach(function (val, i, arr) {
115-
var playerEntry = new PlayerEntry(val.userid, val.name);
115+
var playerEntry = new PlayerEntry(val.id, val.name);
116116
playerEntry.create(tableNode);
117117
playerEntries.push(playerEntry);
118118
});
@@ -129,17 +129,17 @@ var PLUGIN = function () {
129129
}
130130
};
131131

132-
var executeOnPlayer = function (userid) {
132+
var executeOnPlayer = function (id) {
133133
switch (mode) {
134134
case 'ajax':
135135
MOTDPlayer.post({
136136
action: 'execute',
137-
player_userids: [userid, ],
137+
player_ids: [id, ],
138138
}, function (data) {
139139
if (data['status'] == "ok") ; // TODO: Display success popup
140140
clearPlayers();
141141
data['players'].forEach(function (val, i, arr) {
142-
var playerEntry = new PlayerEntry(userid, name);
142+
var playerEntry = new PlayerEntry(id, name);
143143
playerEntry.create(tableNode);
144144
playerEntries.push(playerEntry);
145145
});
@@ -151,7 +151,7 @@ var PLUGIN = function () {
151151
case 'ws':
152152
MOTDPlayer.sendWSData({
153153
action: 'execute',
154-
player_userids: [userid, ],
154+
player_ids: [id, ],
155155
});
156156
break;
157157
}

0 commit comments

Comments
 (0)