12 changed files with 193 additions and 39 deletions
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
package eventsub |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
|
||||
"github.com/gorilla/websocket" |
||||
) |
||||
|
||||
type WelcomeMessage struct { |
||||
Metadata struct { |
||||
MessageType string `json:"message_type"` |
||||
MessageID string `json:"message_id"` |
||||
} `json:"metadata"` |
||||
Payload struct { |
||||
Session struct { |
||||
ID string `json:"id"` |
||||
Status string `json:"status"` |
||||
} `json:"session"` |
||||
} `json:"payload"` |
||||
} |
||||
|
||||
func Connect() (*websocket.Conn, error) { |
||||
conn, _, err := websocket.DefaultDialer.Dial("wss://eventsub.wss.twitch.tv/ws", nil) |
||||
return conn, err |
||||
} |
||||
|
||||
func HandleMessages(conn *websocket.Conn) { |
||||
for { |
||||
_, message, err := conn.ReadMessage() |
||||
if err != nil { |
||||
fmt.Printf("Read error: %v", err) |
||||
return |
||||
} |
||||
|
||||
var msg WelcomeMessage |
||||
if err := json.Unmarshal(message, &msg); err != nil { |
||||
fmt.Printf("JSON error: %v", err) |
||||
continue |
||||
} |
||||
|
||||
switch msg.Metadata.MessageType { |
||||
case "session_welcome": |
||||
sessionID := msg.Payload.Session.ID |
||||
fmt.Printf("Session ID: %s", sessionID) |
||||
// Use this session ID to create subscriptions
|
||||
case "notification": |
||||
// Handle actual events here
|
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
export class StateMachine { |
||||
constructor(owner) { |
||||
this.owner = owner; |
||||
this.state = null; |
||||
} |
||||
|
||||
get() { |
||||
return this.state; |
||||
} |
||||
|
||||
set(next) { |
||||
if (next === this.state) return; |
||||
|
||||
const prev = this.state; |
||||
|
||||
if (prev && typeof prev.exit === 'function') { |
||||
prev.exit(this.owner, next); |
||||
} |
||||
|
||||
this.state = next || null; |
||||
|
||||
if (this.state && typeof this.state.enter === 'function') { |
||||
this.state.enter(this.owner, prev); |
||||
} |
||||
} |
||||
update(dt) { |
||||
if (this.state && typeof this.state.update === 'function') { |
||||
this.state?.update?.(this.owner, dt); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
import { Idle } from "./idle.js" |
||||
|
||||
export const Airborne = { |
||||
enter(p) { |
||||
p.onGround = false; |
||||
}, |
||||
update(p, dt) { |
||||
p.vy += p.gravity * dt; |
||||
p.y += p.vy * dt; |
||||
|
||||
const landed = p.y >= p.groundY; |
||||
|
||||
if (landed) { |
||||
p.y = p.canvas.height - 60; |
||||
p.vy = 0; |
||||
p.statemachine.set(Idle) |
||||
} |
||||
}, |
||||
exit() { } |
||||
} |
||||
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
import { Walk } from './walk.js' |
||||
import { Airborne } from './airborne.js' |
||||
export const Idle = { |
||||
enter(p) { |
||||
p.vx = 0; |
||||
}, |
||||
update(p) { |
||||
if (p.keys.a || p.keys.d) { |
||||
p.statemachine.set(Walk); |
||||
if (!p.onground) { |
||||
p.statemachine.set(Airborne); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
export const Walk = { |
||||
update(p) { |
||||
p.vx = (p.keys.d - p.keys.a) * p.speed; |
||||
if (!p.keys.a && !p.keys.d) { |
||||
p.statemachine.set(Idle); |
||||
if (!p.onGround) { |
||||
p.statemachine.set(Airborne); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -1 +0,0 @@
@@ -1 +0,0 @@
|
||||
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 |
||||
Binary file not shown.
Loading…
Reference in new issue