-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (87 loc) · 3.29 KB
/
app.py
File metadata and controls
106 lines (87 loc) · 3.29 KB
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
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
import time
import uinput, time
device = uinput.Device([uinput.KEY_Z, uinput.KEY_X])
device.emit_click(uinput.KEY_Z) #
app = Flask(__name__)
socketIO = SocketIO(app)
totalInputs = 0
players = [0,0,0,0]
keymap = [
{"red":uinput.BTN_0, "blue": uinput.BTN_2, "orange":uinput.BTN_3, "green": uinput.BTN_4, "yellow": uinput.BTN_5},
{"red":uinput.BTN_6, "blue": uinput.BTN_7, "orange":uinput.BTN_8, "green": uinput.BTN_9, "yellow": uinput.BTN_DPAD_DOWN},
{"red":uinput.BTN_DPAD_UP, "blue": uinput.BTN_C, "orange":uinput.BTN_DPAD_LEFT, "green": uinput.BTN_START, "yellow": uinput.BTN_X},
{"red":uinput.BTN_A, "blue": uinput.BTN_B, "orange":uinput.BTN_BACK, "green": uinput.BTN_SELECT, "yellow": uinput.BTN_Z},
]
allkeys=[]
[allkeys.extend(list(d.values())) for d in keymap]
device = uinput.Device(allkeys,"WebBuzzPad Gamepad")
time.sleep(1)
@app.route("/")
def home():
disables = ["disabled" if p!=0 else "" for p in players]
return render_template('./index.html', disables=disables)
@socketIO.on("buttonPush")
def buttonPush(json):
global totalInputs, device
totalInputs = totalInputs + 1
k = keymap[json["pNum"] - 1 ][json["colour"]]
print(f"{totalInputs}. Player {json["pNum"]} pushes {json["colour"]}. key={k}")
#keyboard.press(k)
device.emit(k, 1)
time.sleep(0.05)
device.emit(k, 0)
@socketIO.on("newPlayer")
def newplayer(json):
if (not "name" in json or not "pNum" in json):
return
n = json["name"]
pNum = json["pNum"]
if (not isinstance(n, str)):
return
if (not isinstance(pNum, int)):
return
if (pNum > 4 or pNum < 0):
return
#
# Add player
if (players[pNum-1] == 0):
players[pNum-1] = {"name": n, "sid": request.sid, "nudgeTime": time.time()}
print(f"Player {pNum} ({players[pNum-1]["name"]}) joined. Current Players:")
print(str(players))
# Tell everyone about the new player
emit("newPlayer", {"name": n, "pNum": pNum}, broadcast=True)
# Tell client they have joined as the new player.
x = [p["name"] if p!=0 else 0 for p in players]
socketIO.emit("assignPlayer", {"name": n, "pNum": pNum, "playerNames": x}, to=request.sid)
@socketIO.on("myDisconnect")
def disconnect():
for i in range(len(players)):
pl = players[i]
# no player here
if pl==0:
continue
if (pl["sid"]==request.sid):
print(f"Player {i+1} ({pl["name"]}) disconnected.")
players[i]=0
emit("lostPlayer", i+1, broadcast=True)
socketIO.emit("successDisconnect", 0, to=request.sid)
return
@socketIO.on("nudge")
def nudge(targetPNum):
for i in range(len(players)):
pl = players[i]
if pl==0:
continue
if (i == targetPNum):
# Must have been 5 seconds before new nudge.
if (time.time() - pl["nudgeTime"] < 3):
print(f"Tried nudging Player {targetPNum} ({pl["name"]}) but too soon...")
return
pl["nudgeTime"] = time.time()
socketIO.emit("nudgeYou", 0, to=pl["sid"])
print(f"Nudged Player {targetPNum} ({pl["name"]})")
return
if __name__ == "__main__":
socketIO.run(app)