Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Lynchburg Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lynchburg
Lynchburg Server
Commits
957c1e94
Commit
957c1e94
authored
4 years ago
by
Nils G.
Browse files
Options
Downloads
Patches
Plain Diff
Added /game/roles API
parent
3d7efc7f
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
controllers/game.py
+56
-1
56 additions, 1 deletion
controllers/game.py
modules/room_util.py
+5
-2
5 additions, 2 deletions
modules/room_util.py
views/game/index.html
+1
-0
1 addition, 0 deletions
views/game/index.html
views/include/websocket.html
+7
-1
7 additions, 1 deletion
views/include/websocket.html
with
69 additions
and
4 deletions
controllers/game.py
+
56
−
1
View file @
957c1e94
from
parameter_util
import
JSON_BODY
,
JSON_CONTAINS
from
http_util
import
FAIL
,
CODE_JSON
,
CODE_MISSING
,
CODE_SEMANTIC
,
CODE_GONE
,
CODE_CONFLICT
from
room_util
import
ROOM_GET
from
room_util
import
ROOM_GET
,
PLAYERS_GET
from
websocket_util
import
WEBSOCKET_SEND
import
base64
...
...
@@ -39,3 +39,58 @@ def start():
json_response
=
{
"
status
"
:
"
success
"
}
return
(
response
.
json
(
json_response
))
def
roles
():
parameters
=
JSON_BODY
(
request
)
if
parameters
==
None
:
return
(
FAIL
(
CODE_JSON
))
if
not
JSON_CONTAINS
(
parameters
,
[(
'
room_id
'
,
int
),
(
'
room_pw
'
,
str
),
(
'
user_roles
'
,
dict
)]):
return
(
FAIL
(
CODE_MISSING
))
room_id
=
parameters
[
'
room_id
'
]
room_pw
=
parameters
[
'
room_pw
'
]
user_roles
=
parameters
[
'
user_roles
'
]
room_record
=
ROOM_GET
(
room_id
,
room_pw
)
if
not
room_record
:
return
(
FAIL
(
CODE_SEMANTIC
))
if
room_record
.
closed
:
return
(
FAIL
(
CODE_GONE
))
players
=
PLAYERS_GET
(
room_record
,
True
)
server_rolechange
=
{}
client_rolechange
=
{}
for
player
in
players
:
playerid
=
str
(
player
[
"
id
"
])
if
not
playerid
in
user_roles
:
continue
definition
=
user_roles
[
playerid
]
if
not
isinstance
(
definition
,
dict
):
return
(
FAIL
(
CODE_MISSING
))
if
not
JSON_CONTAINS
(
definition
,
[(
"
role
"
,
str
)]):
return
(
FAIL
(
CODE_MISSING
))
client_rolechange
[
player
[
"
uid
"
]]
=
{
"
role
"
:
definition
[
"
role
"
]}
if
player
[
"
role
"
]
!=
definition
[
"
role
"
]:
server_rolechange
[
player
[
"
uid
"
]]
=
definition
[
"
role
"
]
for
uid
in
server_rolechange
:
db
(
db
.
Player
.
id
==
uid
).
update
(
role
=
server_rolechange
[
uid
])
websocket_response
=
base64
.
standard_b64encode
(
json
.
dumps
(
client_rolechange
).
encode
(
"
ascii
"
)).
decode
(
"
utf-8
"
)
WEBSOCKET_SEND
(
room_record
,
"
role
"
,
websocket_response
)
json_response
=
{
"
status
"
:
"
success
"
}
return
(
response
.
json
(
json_response
))
This diff is collapsed.
Click to expand it.
modules/room_util.py
+
5
−
2
View file @
957c1e94
...
...
@@ -18,13 +18,16 @@ def ROOM_GET_CODE(_code):
return
(
room_record
)
def
PLAYERS_GET
(
_room
):
def
PLAYERS_GET
(
_room
,
_uid
=
False
):
rows
=
current
.
db
(
current
.
db
.
Player
.
room_id
==
_room
.
id
).
select
(
orderby
=
current
.
db
.
Player
.
id
)
players
=
[]
player_id
=
0
for
row
in
rows
:
players
.
append
({
"
id
"
:
player_id
,
"
name
"
:
row
.
name
,
"
role
"
:
row
.
role
})
obj
=
{
"
id
"
:
player_id
,
"
name
"
:
row
.
name
,
"
role
"
:
row
.
role
}
if
_uid
:
obj
[
"
uid
"
]
=
row
.
id
players
.
append
(
obj
)
player_id
=
player_id
+
1
return
(
players
)
This diff is collapsed.
Click to expand it.
views/game/index.html
+
1
−
0
View file @
957c1e94
...
...
@@ -15,6 +15,7 @@
<script
type=
"text/javascript"
>
var
name
=
"
{{=player_record.name}}
"
;
var
playerid
=
"
{{=player_record.id}}
"
;
var
role
=
"
{{=player_record.role}}
"
;
var
roles
=
JSON
.
parse
(
atob
(
'
{{=XML(room_record.roles)}}
'
));
</script>
...
...
This diff is collapsed.
Click to expand it.
views/include/websocket.html
+
7
−
1
View file @
957c1e94
...
...
@@ -20,12 +20,18 @@
switch
(
type
)
{
case
"
roles
"
:
roles
=
JSON
.
parse
(
atob
(
data
));
render
(
name
,
role
,
roles
);
break
;
case
"
role
"
:
role_definition
=
JSON
.
parse
(
atob
(
data
));
if
(
playerid
in
role_definition
)
{
role
=
role_definition
[
playerid
][
"
role
"
];
}
break
;
default
:
set_error
(
"
{{=T('Invalid WebSocket data received')}}: '
"
+
type
+
"
'
"
);
return
;
}
render
(
name
,
role
,
roles
);
};
ws
.
onerror
=
function
(
e
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment