From c56f9f7a4b86c7dddd2dd3a871fa66ea59e92f97 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Wed, 2 Jan 2019 19:06:45 +0100 Subject: [PATCH] new new --- archive/archive.go | 6 ++--- playlist/playlist.go | 56 ++++++++++++++++++++++++++++++++++++------ serve.go | 5 +++- www/tmpl/playlist.html | 2 +- www/www.go | 19 +++++++++++--- 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/archive/archive.go b/archive/archive.go index 88a4625..dc216af 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -1,7 +1,7 @@ package archive import ( - "fmt" + "log" "strings" "io/ioutil" "path/filepath" @@ -9,7 +9,7 @@ import ( "../config" ) -var pln = fmt.Println +var pln = log.Println var Archive_map map[uint32]config.Track_t var Archive_map_keys []uint32 @@ -24,7 +24,7 @@ func Build() error { Archive_map = make(map[uint32]config.Track_t) - album_dirs, err := ioutil.ReadDir(config.Xcfg.Archive.PATH) + album_dirs, err := ioutil.ReadDir(config.Xcfg.Archive.PATH) if err != nil { return err } diff --git a/playlist/playlist.go b/playlist/playlist.go index 8fa386f..fa5f75c 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -1,7 +1,7 @@ package playlist import ( - "fmt" + "log" "time" "errors" "strconv" @@ -11,7 +11,7 @@ import ( "../config" ) -var pln = fmt.Println +var pln = log.Println type Playlist struct { NAME string @@ -105,6 +105,14 @@ func (pp *PrettyPlaylist) Print() { } } +func print(slice []uint32) { + for i, r := range slice { + pln(" " + strconv.Itoa(i) + " - " + archive.Archive_map[r].NAME) + } + +} + +// https://github.com/golang/go/wiki/SliceTricks // https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang func (p *Playlist) Pop() error { @@ -157,20 +165,54 @@ func (p *Playlist) Insert(track_id uint32, at_index int) error { return nil } -func (p *Playlist) Delete(track_id uint32) error { - +func (p *Playlist) index(track_id uint32) int { k := -1 - for i := 0; i <= len(p.LIST); i++ { + for i := 0; i < len(p.LIST); i++ { if p.LIST[i] == track_id { k = i } } + return k +} +func (p *Playlist) Delete(track_id uint32) error { + k := p.index(track_id); + if k < 0 { + return errors.New("Invalid track ID") + } + p.LIST = append(p.LIST[:k-1], p.LIST[k+1:]...) + return nil +} + +func (p *Playlist) Move(track_id uint32, at_index int) error { + if at_index > p.MAX { + return errors.New("Invalid insert index") + } + k := p.index(track_id); if k < 0 { return errors.New("Invalid track ID") } - p.LIST = append(p.LIST[:k-1], p.LIST[k+1:]...) - return nil + slice_head := p.LIST[:k] + slice_move := p.LIST[k+1:at_index+1] + slice_tail := make([]uint32, len(p.LIST[at_index+1:])) + copy(slice_tail, p.LIST[at_index+1:]) + // slice_move = append(slice_move, track_id) + + pln("from_index: " + strconv.Itoa(k)) + pln("at_index: " + strconv.Itoa(at_index)) + pln("------") + pln("slice head: ") + print(slice_head) + pln("slice move: ") + print(slice_move) + pln("** ") + pln("slice tail: ") + print(slice_tail) + pln("...........................") + + p.LIST = append(slice_head, append(append(slice_move, track_id), slice_tail...)...) + + return nil } diff --git a/serve.go b/serve.go index 94bcb64..7aefb72 100644 --- a/serve.go +++ b/serve.go @@ -13,7 +13,10 @@ var pln = fmt.Println func main() { config.Loadconfig("config.ini") - p, _ := playlist.MakeRandom("YOYO", 10) + p, err := playlist.MakeRandom("YOYO", 10) + if err != nil { + panic(err) + } p.Pop() www.Init(p) log.Fatal(http.ListenAndServe(":8718", nil)) diff --git a/www/tmpl/playlist.html b/www/tmpl/playlist.html index 6918922..fdd5597 100644 --- a/www/tmpl/playlist.html +++ b/www/tmpl/playlist.html @@ -48,7 +48,7 @@ function on_update_operation(id, index) { if(sock) { console.log("sending") - sock.send(JSON.stringify({"op": "update", "id": id, "index": index})); + sock.send(JSON.stringify({"op": "move", "id": id, "index": index})); } } diff --git a/www/www.go b/www/www.go index 5d89dc0..385362b 100644 --- a/www/www.go +++ b/www/www.go @@ -2,16 +2,17 @@ package www import ( "log" + "strconv" "html/template" - "net/http" - // "golang.org/x/net/websocket" + "net/http" "github.com/gorilla/websocket" "../playlist" ) // todo: hub - https://stackoverflow.com/questions/31532652/go-websocket-send-all-clients-a-message -var pp *playlist.PrettyPlaylist +var pp *playlist.PrettyPlaylist +var p *playlist.Playlist var pln = log.Println @@ -26,7 +27,8 @@ var upgrader = websocket.Upgrader{ WriteBufferSize: 512, } -func Init(p *playlist.Playlist) { +func Init(playlist *playlist.Playlist) { + p = playlist pp = p.Pretty() http.HandleFunc("/pp", pp_handler) http.HandleFunc("/ppop", pp_operations) @@ -59,6 +61,15 @@ func readop(c *websocket.Conn) { pln(err) return //connection lost? } + + if opdata.OP == "move" { + if id, err := strconv.ParseUint(opdata.ID, 10, 32); err == nil { + p.Move(uint32(id), opdata.INDEX) + pp = p.Pretty() + pp.Print() + } + } + pln(opdata.OP) pln(opdata.ID) pln(opdata.INDEX)