davidramiro / yeelight-valve-gsi

💡 Changing light scenes on Yeelight smart lights for different scenarios in Valve games using GSI
Other
0 stars 0 forks source link

Fixed Ammo Warning and TeamWin #2

Open djtomcat opened 5 years ago

djtomcat commented 5 years ago

After some troubleshooting , I (maybe) finished your script now it has also :

If "bomb is planted" timer wont interrupt by low ammo / lose health

from http.server import BaseHTTPRequestHandler, HTTPServer
from yeelight import *
import sys
import time
import json
import configparser

starttime=time.time()

alarm_flow = [
    HSVTransition(0, 100, duration=250, brightness=100),
    HSVTransition(0, 100, duration=250, brightness=60),
]
ammo_alarm_flow = [
    RGBTransition(0, 0, 255, duration=900, brightness=10),
    RGBTransition(0, 127, 255, duration=100, brightness=100),
]
bomb_flow = [
    RGBTransition(255, 0, 0, duration=900, brightness=100),
    RGBTransition(255, 127, 0, duration=100, brightness=100),
]

bulbs = []

class MyServer(HTTPServer):
    # prepare checked items for http server
    def init_state(self):
        self.round_phase = None
        self.round_win_team = None
        self.bomb_state = None
        self.player_health = None
        self.ammo_status = None

class MyRequestHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        # receive payload from game
        length = int(self.headers['Content-Length'])
        body = self.rfile.read(length).decode('utf-8')
        self.parse_payload(json.loads(body))
        self.send_header('Content-type', 'text/html')
        self.send_response(200)
        self.end_headers()

    def parse_payload(self, payload):
        round_phase = self.get_round_phase(payload)
        round_win_team = self.get_round_win_team(payload)
        bomb_state = self.get_bomb_state(payload)
        player_health = self.get_player_health(payload)
        ammo_status = self.get_ammo_status(payload)

        # compare and check if payload changed

        if round_win_team != self.server.round_win_team:
            self.server.round_win_team = round_win_team
            print('winner Team: %s' % round_win_team)

        if round_phase != self.server.round_phase:
            self.server.round_phase = round_phase
            print('new round phase: %s' % round_phase)

            if 'over' in round_phase:
                change_light(255, 0, 0)
            if round_win_team == 'CT':
                change_light(0, 127, 255)
            if round_win_team == 'T':
                change_light(255, 127, 0)

            if 'live' in round_phase:
                change_light(0, 255, 0)
            if 'freezetime' in round_phase:
                change_light(255, 255, 255)

        if bomb_state != self.server.bomb_state:
            self.server.bomb_state = bomb_state
            print('changed bomb status: %s' % bomb_state)
            if 'planted' in bomb_state:
                police()
            if 'defused' in bomb_state:
                change_light(0, 127, 255)
            if 'exploded' in bomb_state:
                change_light(255, 127, 0)

        if player_health != self.server.player_health:
            self.server.player_health = player_health
            print('player health: %s' % player_health)

            hp_g = int(round(player_health / 100 * 255))
            hp_r = int(round((255 * (1 - player_health / 100))))
            change_light(hp_r, hp_g, 0)
            if player_health <= 10:
                alarm()
                time.sleep(0.8)

            if bomb_state == 'planted':
                police()
            if bomb_state == 'defused':
                change_light(0, 127, 255)
            if bomb_state == 'exploded':
                change_light(255, 127, 0)

        if ammo_status != self.server.ammo_status:
            self.server.ammo_status = ammo_status
            print('ammo status: %s' % ammo_status)
            if ammo_status == 1:
                       ammo_alarm()

            if ammo_status == None or ammo_status == 0:
                       for bulbn in (bulbs):
                            if bulbn != '':
                                    bulb = Bulb(bulbn)
                                    bulb.stop_flow()

            if bomb_state == 'planted':
                police()
            if bomb_state == 'defused':
                change_light(0, 127, 255)
            if bomb_state == 'exploded':
                change_light(255, 127, 0)

    def get_round_phase(self, payload):
        if use_phase == True:
            if 'round' in payload and 'phase' in payload['round']:
                return payload['round']['phase']
            else:
                return None

    def get_round_win_team(self, payload):
        if use_phase == True:
            if 'round' in payload and 'win_team' in payload['round']:
                return payload['round']['win_team']
            else:
                return None

    def get_bomb_state(self, payload):
        if use_bomb == True:
            if 'round' in payload and 'bomb' in payload['round']:
                return payload['round']['bomb']
            else:
                return None

    def get_player_health(self, payload):
        if use_health == True:
            if 'player' in payload and 'state' in payload['player'] and 'health' in payload['player']['state']:
                return payload['player']['state']['health']
            else:
                return None

    def get_ammo_status(self, payload): # if ammo under 40 percent send 1 else 0
        if use_ammo == True:
            if 'player' in payload and 'weapons' in payload['player']:
                if 'weapon_1' in payload['player']['weapons'] and 'state' in payload['player']['weapons']['weapon_1']:
                    if payload['player']['weapons']['weapon_1']['state'] == 'active':
                        if 'ammo_clip' in payload['player']['weapons']['weapon_1'] and 'ammo_clip_max' in payload['player']['weapons']['weapon_1']:
                            if payload['player']['weapons']['weapon_1']['ammo_clip'] < (((payload['player']['weapons']['weapon_1']['ammo_clip_max']) * 40) / 100):
                                return 1;
                            else:
                                return 0;
                if 'weapon_2' in payload['player']['weapons'] and 'state' in payload['player']['weapons']['weapon_2']:
                    if payload['player']['weapons']['weapon_2']['state'] == 'active':
                        if 'ammo_clip' in payload['player']['weapons']['weapon_2'] and 'ammo_clip_max' in payload['player']['weapons']['weapon_2']:
                            if payload['player']['weapons']['weapon_2']['ammo_clip'] < (((payload['player']['weapons']['weapon_2']['ammo_clip_max']) * 40) / 100):
                                return 1;
                            else:
                                return 0;
                if 'weapon_3' in payload['player']['weapons'] and 'state' in payload['player']['weapons']['weapon_3']:
                    if payload['player']['weapons']['weapon_3']['state'] == 'active':
                        if 'ammo_clip' in payload['player']['weapons']['weapon_3'] and 'ammo_clip_max' in payload['player']['weapons']['weapon_3']:
                            if payload['player']['weapons']['weapon_3']['ammo_clip'] < (((payload['player']['weapons']['weapon_3']['ammo_clip_max']) * 40) / 100):
                                return 1;
                            else:
                                return 0;
                if 'weapon_4' in payload['player']['weapons'] and 'state' in payload['player']['weapons']['weapon_4']:
                    if payload['player']['weapons']['weapon_4']['state'] == 'active':
                        if 'ammo_clip' in payload['player']['weapons']['weapon_4'] and 'ammo_clip_max' in payload['player']['weapons']['weapon_4']:
                            if payload['player']['weapons']['weapon_4']['ammo_clip'] < (((payload['player']['weapons']['weapon_4']['ammo_clip_max']) * 40) / 100):
                                return 1;
                            else:
                                return 0;

                else:
                    return None
            else:
                return None

    def log_message(self, format, *args):
        return

def change_light(r, g, b):
    # loop through bulbs with new rgb value
    for bulbn in (bulbs):
        if bulbn != '':
            bulb = Bulb(bulbn)
            bulb.set_rgb(r, g, b)

def alarm():
    for bulbn in (bulbs):
        if bulbn != '':
            bulb = Bulb(bulbn)
            bulb.start_flow(Flow(2, Flow.actions.recover, alarm_flow))

def ammo_alarm():
    for bulbn in (bulbs):
        if bulbn != '':
            bulb = Bulb(bulbn)
            bulb.start_flow(Flow(0, Flow.actions.recover, ammo_alarm_flow))            

def police():
    for bulbn in (bulbs):
        if bulbn != '':
            bulb = Bulb(bulbn)
            # pulsate 40 times for 1 second each to show bomb timer
            bulb.start_flow(Flow(40, Flow.actions.recover, bomb_flow))

def main():
    print('Welcome to yeelight-valve-gsi by davidramiro')
    print('Reading config...')
    config = configparser.ConfigParser()
    config.read('config.ini')
    bulb_count = int(config.get('general', 'Lamp Count'))
    for n in range(1, (bulb_count + 1)):
        bulbs.append(config.get(str(n), 'ip'))
    global use_phase, use_bomb, use_health, use_ammo
    use_phase = config.getboolean('csgo settings', 'round phase colors')
    use_bomb = config.getboolean('csgo settings', 'c4 status colors')
    use_health = config.getboolean('csgo settings', 'health colors')
    use_ammo = config.getboolean('csgo settings', 'ammo colors')
    print('Initializing...')
    for bulbn in bulbs:
        if bulbn != '':
            print('Initializing Yeelight at %s' % bulbn)
            bulb = Bulb(bulbn)
            bulb.turn_on()
            # turn on music mode on the yeelights for better latency
            bulb.start_music()
            bulb.set_rgb(255, 172, 68)
            bulb.set_brightness(100)
    # start up the listening server
    server = MyServer(('localhost', 3000), MyRequestHandler)
    server.init_state()
    print(time.asctime(), '-', 'yeelight-valve-gsi is running - CTRL+C to stop')
    try:
        server.serve_forever()
    except (KeyboardInterrupt, SystemExit):
        pass
    server.server_close()
    # turn off music mode
    for bulbn in bulbs:
        if bulbn != '':
            bulb = Bulb(bulbn)
            bulb.stop_music
            bulb.set_rgb(255, 172, 68)
            bulb.set_brightness(1)
    print(time.asctime(), '-', 'Listener stopped. Thanks for using yeelight-valve-gsi!')

main()
davidramiro commented 5 years ago

Thanks a lot! Since I don't play this game much nowadays, I haven't really looked into this script any further.

Seems to work well from a first look! Also, great idea to skip some actions when the bomb timer is active, didn't think of that.

Do you want to open a pull request on this or should I just commit this myself with you as the co-author?

djtomcat commented 5 years ago

Hey David,

sorry that I answer sooo late, but had lots of work to do and restaurated some old laptops for reselling and even burned one ^^

Well there are some more problems and errors sometimes after the round / match is over, but the script works further

Just try it in deathmatch or offline 😃 if you want you can add me as CO 😃 would be nice 😊 (My SteamTag is also DJTOMCAT 😊 )

Well what I don’t get to work is while the bomb timer is ticking (red / yellow flashing) ammo would be nice to get a little blue blink then, but don’t know how to program it, my knowledge is on novice level 😊

Greetings

Björn

Von: David Ramiro notifications@github.com Gesendet: Dienstag, 21. Mai 2019 11:31 An: davidramiro/yeelight-valve-gsi yeelight-valve-gsi@noreply.github.com Cc: djtomcat djtomcat@chief-rocker.de; Author author@noreply.github.com Betreff: Re: [davidramiro/yeelight-valve-gsi] Fixed Ammo Warning and TeamWin (#2)

Thanks a lot! Since I don't play this game much nowadays, I haven't really looked into this script any further.

Seems to work well from a first look! Also, great idea to skip some actions when the bomb timer is active, didn't think of that.

Do you want to open a pull request on this or should I just commit this myself with you as the co-author?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/davidramiro/yeelight-valve-gsi/issues/2?email_source=notifications&email_token=AALVY45EXJMJKR4GC3Z3PUTPWO6LXA5CNFSM4HNREUMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV3KMUQ#issuecomment-494315090 , or mute the thread https://github.com/notifications/unsubscribe-auth/AALVY46H57DPAHV32AAXOEDPWO6LXANCNFSM4HNREUMA . https://github.com/notifications/beacon/AALVY44GTF56F7WFFSWPHPTPWO6LXA5CNFSM4HNREUMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV3KMUQ.gif