zapsaber/zapsaber.py

92 lines
2.5 KiB
Python
Executable File

import asyncio
import json
import logging
from pyshock import PiShock
from datapuller import (
LiveData,
MapData,
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Events:
combo_A = 100
combo_B = 300
combo_A_break = ['vibrate', 0.5, 20]
combo_B_break = ['shock', 0.3, 5]
fail = ['shock', 1, 10]
playing = False
combo_level = None
map_failed = False
def __init__(self, config, shocker):
self.combo_A = config['comboA']
self.combo_B = config['comboB']
self.combo_A_break = config['comboABreak']
self.combo_B_break = config['comboBBreak']
self.fail = config['fail']
self.shocker = shocker
def update_combo(self, message):
combo = message['Combo']
health = message['PlayerHealth']
if not self.map_failed and health == 0:
logger.info('Failed the map!')
self.shocker.act(*self.fail)
self.map_failed = True
self.combo_level = None
if combo > self.combo_A:
self.combo_level = 'A'
if combo > self.combo_B:
self.combo_level = 'B'
def break_combo(self, message):
if self.combo_level == 'A':
logger.info('Broke combo A!')
self.shocker.act(*self.combo_A_break)
if self.combo_level == 'B':
logger.info('Broke combo B!')
self.shocker.act(*self.combo_B_break)
self.combo_level = None
def tick(self, message):
logger.info('Tick')
if self.playing and message['TimeElapsed'] == 0:
logger.info('Stopping map...')
self.map_failed = False
self.combo = 0
self.playing = False
elif not self.playing and message['TimeElapsed'] != 0:
logger.info('Starting map...')
self.playing = True
async def main(config):
logger.info('Initializing...')
shocker = PiShock(config['username'], config['apiKey'], config['code'], name='ZapSaber')
#shocker.beep()
events = Events(config, shocker)
livedata = LiveData()
livedata.on('ScoreChange', events.update_combo)
livedata.on('NoteMissed', events.break_combo)
livedata.on('TimerElapsed', events.tick)
await livedata.listen()
await mapdata.listen()
shocker.vibrate()
if __name__ == '__main__':
with open('config.json') as config_file:
config = json.load(config_file)
asyncio.run(main(config))