This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
display.py
76 lines (55 loc) · 2.11 KB
/
display.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
#!/usr/bin/python
# Copyright (C) 2010-2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Display and display workers module for gae-last-across-the-finish-line."""
__author__ = '[email protected] (Daniel Hermes)'
# General libraries
import json
import random
# App engine specific libraries
from google.appengine.api import channel
HEX_DIGITS = '0123456789ABCDEF'
def RandomRowColumnOrdering(rows, columns):
"""Generates a random ordering of all row, column pairs.
Uses random.shuffle to shuffle the integers between 0 and
rows*columns - 1 (inclusive) and then maps each of these
integers to a corresponding row, column pair.
Args:
rows: The number of rows in the table
columns: The number of columns in the table
"""
square_indices = range(rows*columns)
random.shuffle(square_indices)
return [divmod(index, columns) for index in square_indices]
def RandHexColor(length=6):
"""Generates a random color using hexadecimal digits.
Args:
length: The number of hex digits in the color. Defaults to 6.
"""
result = [random.choice(HEX_DIGITS) for _ in range(length)]
return '#' + ''.join(result)
def SendColor(row, column, session_id):
"""Generates and sends a color to client over the channel for the session ID.
Args:
row: The row in the table on the client receiving the color
column: The column in the table on the client receiving the color
session_id: The session ID of the client.
"""
color = RandHexColor(length=6)
color_dict = {
'row': row,
'column': column,
'color': color
}
channel.send_message(session_id, json.dumps(color_dict))