From 97e07e41dfbb40f325b85acc12571b9be52a9f68 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sat, 20 Apr 2024 05:27:00 -0400 Subject: [PATCH] Refactor interfaces --- data.go | 16 +++----- main.go | 109 ++++++++++++++----------------------------------- mpp/channel.go | 19 +++++++++ mpp/message.go | 42 +++++++++++++++++++ mpp/user.go | 12 ++++++ mpp/vec.go | 8 ++++ user.go | 1 + 7 files changed, 119 insertions(+), 88 deletions(-) create mode 100644 mpp/channel.go create mode 100644 mpp/message.go create mode 100644 mpp/user.go create mode 100644 mpp/vec.go create mode 100644 user.go diff --git a/data.go b/data.go index 9bc3b60..303346f 100644 --- a/data.go +++ b/data.go @@ -2,12 +2,11 @@ package main import ( "log" - "strconv" badger "github.com/dgraph-io/badger/v4" ) -var db leveldb.DB +var db badger.DB func SetupDb() { db, err := badger.Open(badger.DefaultOptions("./bot2024.db")) @@ -20,14 +19,11 @@ func SetupDb() { } func GetBalance(uid string) (float64, error) { - s, err := db.Get([]byte("userbal~" + uid), nil) - log.Println("here") - bal, _ := strconv.ParseFloat(string(s), 64) - return bal, err + // TODO thing + return 0, nil } -func SetBalance(uid string, bal float64) (error) { - s := strconv.FormatFloat(bal, 'f', -1, 64) - err := db.Put([]byte("userbal~" + uid), []byte(s), nil) - return err +func SetBalance(uid string, bal float64) error { + // TODO other thing + return nil } diff --git a/main.go b/main.go index 311179d..5f1e0b0 100644 --- a/main.go +++ b/main.go @@ -12,67 +12,14 @@ import ( "strings" "time" + "github.com/Hri7566/mpp-client-go/mpp" + "github.com/gorilla/websocket" "github.com/joho/godotenv" ) var addr = flag.String("addr", "mppclone.com:8443", "websocket address") -type User struct { - Uid string `json:"_id"` - Name string `json:"name"` - Color string `json:"color"` -} - -type Participant struct { - User - Id string `json:"id"` -} - -type MppMessage struct { - Type string `json:"m"` -} - -type HiMessage struct { - MppMessage - Timestamp json.Number `json:"t"` - User User `json:"u"` - Motd string `json:"motd"` -} - -type NqMessage struct { - MppMessage - Allowance json.Number `json:"allowance"` - Max json.Number `json:"max"` - MaxHistLen json.Number `json:"maxHistLen"` -} - -type ChatMessage struct { - MppMessage - Message string `json:"a"` - Part Participant `json:"p"` - Timestamp json.Number `json:"t"` -} - -type TMessage struct { - MppMessage - Timestamp json.Number `json:"t"` - Echo json.Number `json:"e"` -} - -type Channel struct { - Id string `json:"_id"` - Id2 string `json:"id"` -} - -type ChMessage struct { - MppMessage - Channel Channel `json:"ch"` - Participants []Participant `json:"ppl"` -} - -type AnyMppMessage []interface{} - var token string func main() { @@ -113,9 +60,9 @@ func StartSocket(address *string) { go func() { for { select { - case <- serverTicker.C: + case <-serverTicker.C: SendTimeMessage(ws) - case <- serverTickerQuit: + case <-serverTickerQuit: serverTicker.Stop() } } @@ -195,18 +142,18 @@ func ReceiveMessage(ws *websocket.Conn, data []byte) { _ = json.Unmarshal(raw, &header) switch header.Type { case "hi": - msg := HiMessage{} + msg := mpp.HiMppMessage{} _ = json.Unmarshal(raw, &msg) ReceiveHiMessage(ws, &msg) case "nq": - msg := NqMessage{} + msg := mpp.NoteQuotaMppMessage{} _ = json.Unmarshal(raw, &msg) ReceiveNqMessage(&msg) case "t": - msg := TMessage{} + msg := mpp.TimeMppMessage{} _ = json.Unmarshal(raw, &msg) case "a": - msg := ChatMessage{} + msg := mpp.ChatMppMessage{} _ = json.Unmarshal(raw, &msg) ReceiveChatMessage(ws, &msg) case "b": @@ -224,7 +171,7 @@ func ReceiveMessage(ws *websocket.Conn, data []byte) { // } } -func ReceiveNqMessage(msg *NqMessage) { +func ReceiveNqMessage(msg *mpp.NoteQuotaMppMessage) { // println(msg.MaxHistLen) } @@ -240,10 +187,10 @@ func ReceiveBMessage(ws *websocket.Conn) { // sentHi = true } -func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) { +func ReceiveHiMessage(ws *websocket.Conn, msg *mpp.HiMppMessage) { println("I am " + msg.User.Name) - chstr := "[{\"m\":\"ch\",\"_id\":\"Sans' Comedy Corner\"}]" + chstr := "[{\"m\":\"ch\",\"_id\":\"βœ§π““π“”π“₯ π“‘π“Έπ“Έπ“Άβœ§\"}]" // println("Sending ch message: " + chstr) err2 := ws.WriteMessage(1, []byte(chstr)) if err2 != nil { @@ -252,13 +199,13 @@ func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) { } type CommandData struct { - ChatMessage + mpp.ChatMppMessage Args []string - Cmd string + Cmd string } type Command struct { - Id string + Id string Callback func(ws *websocket.Conn, data *CommandData) string } @@ -267,19 +214,19 @@ var helpCommands []Command var commands []Command = []Command{ { Id: "help", - Callback: func (ws *websocket.Conn, data *CommandData) string { + Callback: func(ws *websocket.Conn, data *CommandData) string { output := "Commands: " - + for _, cmd := range helpCommands { output += cmd.Id + ", " } - - return strings.Trim(output[:len(output) - 2], " ") + + return strings.Trim(output[:len(output)-2], " ") }, }, { Id: "about", - Callback: func (ws *websocket.Conn, data *CommandData) string { + Callback: func(ws *websocket.Conn, data *CommandData) string { return "written in go version go1.22.2 linux/amd64" }, }, @@ -287,7 +234,7 @@ var commands []Command = []Command{ Id: "me", Callback: func(ws *websocket.Conn, data *CommandData) string { bal, err := GetBalance(data.Part.Uid) - + if err != nil { log.Println(err) return "An error has occurred." @@ -296,11 +243,17 @@ var commands []Command = []Command{ return "Your balance: " + strconv.FormatFloat(bal, 'f', -1, 64) }, }, + { + Id: "nu", + Callback: func(ws *websocket.Conn, data *CommandData) string { + return "I'd just like to interject for a moment. What you're refering to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX." + }, + }, } var botPrefix string = "g" -func ReceiveChatMessage(ws *websocket.Conn, msg *ChatMessage) { +func ReceiveChatMessage(ws *websocket.Conn, msg *mpp.ChatMppMessage) { log.Println(msg.Part.Uid[0:6] + " <" + msg.Part.Name + ">: " + msg.Message) if strings.HasPrefix(msg.Message, botPrefix) { @@ -308,9 +261,9 @@ func ReceiveChatMessage(ws *websocket.Conn, msg *ChatMessage) { cmd := args[0][1:] var data CommandData = CommandData{ - ChatMessage: *msg, - Args: args, - Cmd: cmd, + ChatMppMessage: *msg, + Args: args, + Cmd: cmd, } for _, command := range commands { @@ -338,5 +291,5 @@ func SendTimeMessage(ws *websocket.Conn) { } func SendChat(ws *websocket.Conn, message string) { - ws.WriteMessage(1, []byte("[{\"m\":\"a\",\"message\":\"\u034f" + message + "\"}]")) + ws.WriteMessage(1, []byte("[{\"m\":\"a\",\"message\":\"\u034f"+message+"\"}]")) } diff --git a/mpp/channel.go b/mpp/channel.go new file mode 100644 index 0000000..6993a99 --- /dev/null +++ b/mpp/channel.go @@ -0,0 +1,19 @@ +package mpp + +type MppChannel struct { + Id string `json:"_id"` + Id2 string `json:"id"` + Crown MppCrown `json:"crown"` + Settings MppChannelSettings `json:"settings"` +} + +type MppCrown struct { + ParticipantId string `json:"participantId"` + UserId string `json:"userId"` + StartPos Vector2 `json:"startPos"` + EndPos Vector2 `json:"endPos"` +} + +type MppChannelSettings struct { + // TODO +} diff --git a/mpp/message.go b/mpp/message.go new file mode 100644 index 0000000..798ebe9 --- /dev/null +++ b/mpp/message.go @@ -0,0 +1,42 @@ +package mpp + +import "encoding/json" + +type MppMessage struct { + Type string `json:"m"` +} + +type MppTimeMessage struct { + MppMessage + Timestamp json.Number `json:"t"` +} + +type HiMppMessage struct { + MppTimeMessage + User MppUser `json:"u"` + Motd string `json:"motd"` +} + +type NoteQuotaMppMessage struct { + MppMessage + Allowance json.Number `json:"allowance"` + Max json.Number `json:"max"` + MaxHistLen json.Number `json:"maxHistLen"` +} + +type ChatMppMessage struct { + MppTimeMessage + Message string `json:"a"` + Part MppParticipant `json:"p"` +} + +type TimeMppMessage struct { + MppTimeMessage + Echo json.Number `json:"e"` +} + +type ChannelMppMessage struct { + MppMessage + Channel MppChannel `json:"ch"` + Participants []MppParticipant `json:"ppl"` +} diff --git a/mpp/user.go b/mpp/user.go new file mode 100644 index 0000000..0c77315 --- /dev/null +++ b/mpp/user.go @@ -0,0 +1,12 @@ +package mpp + +type MppUser struct { + Uid string `json:"_id"` + Name string `json:"name"` + Color string `json:"color"` +} + +type MppParticipant struct { + MppUser + Id string `json:"id"` +} diff --git a/mpp/vec.go b/mpp/vec.go new file mode 100644 index 0000000..db86470 --- /dev/null +++ b/mpp/vec.go @@ -0,0 +1,8 @@ +package mpp + +import "encoding/json" + +type Vector2 struct { + X json.Number `json:"x"` + Y json.Number `json:"y"` +} diff --git a/user.go b/user.go new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/user.go @@ -0,0 +1 @@ +