github.com/gkstretton/dark/services/goo@v0.0.0-20231114224855-2d1a2074d446/actor/decider/decider_twitch.go (about)

     1  package decider
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/gkstretton/asol-protos/go/machinepb"
     8  	"github.com/gkstretton/dark/services/goo/actor/executor"
     9  	"github.com/gkstretton/dark/services/goo/livechat"
    10  )
    11  
    12  type twitchDecider struct {
    13  	api *livechat.TwitchApi
    14  }
    15  
    16  func NewTwitchDecider(twitchApi *livechat.TwitchApi) Decider {
    17  	return &twitchDecider{api: twitchApi}
    18  }
    19  
    20  func conductVote(msgCh chan *livechat.Message, timeout time.Duration, handler func(*livechat.Message) bool) {
    21  	timeoutCh := time.After(timeout)
    22  	for {
    23  		select {
    24  		case <-timeoutCh:
    25  			return
    26  		case msg := <-msgCh:
    27  			if handler(msg) {
    28  				return
    29  			}
    30  		}
    31  	}
    32  }
    33  
    34  func (d *twitchDecider) DecideCollection(predictedState *machinepb.StateReport) executor.Executor {
    35  	msgCh := d.api.SubscribeChat()
    36  	defer d.api.UnsubscribeChat(msgCh)
    37  
    38  	d.api.Announce("Taking votes on next collection (2-6)", livechat.COLOUR_GREEN)
    39  
    40  	vialNo := 2
    41  
    42  	conductVote(
    43  		msgCh,
    44  		time.Duration(10)*time.Second,
    45  		func(msg *livechat.Message) bool {
    46  			fmt.Println("vote? ", msg)
    47  			switch msg.Message {
    48  			case "2":
    49  				vialNo = 2
    50  			case "3":
    51  				vialNo = 3
    52  			case "4":
    53  				vialNo = 4
    54  			case "5":
    55  				vialNo = 5
    56  				return true
    57  			case "6":
    58  				vialNo = 6
    59  			}
    60  			return false
    61  		},
    62  	)
    63  
    64  	d.api.Say(fmt.Sprintf("settled collection vote on %d", vialNo))
    65  
    66  	return executor.NewCollectionExecutor(vialNo, int(getVialVolume(vialNo)))
    67  }
    68  
    69  func (d *twitchDecider) DecideDispense(predictedState *machinepb.StateReport) executor.Executor {
    70  	//todo: implement this one, test above (special 5)
    71  	e := executor.NewDispenseExecutor(0, 0)
    72  
    73  	type voteStruct struct {
    74  		x float32
    75  		y float32
    76  	}
    77  	voteChan := make(chan voteStruct)
    78  	for vote := range voteChan {
    79  		e.X = vote.x
    80  		e.Y = vote.y
    81  		e.Preempt()
    82  	}
    83  
    84  	return e
    85  }