This commit is contained in:
gauthiier 2019-01-02 19:06:45 +01:00
parent 816c2b593c
commit 5e4f9b8862
5 changed files with 72 additions and 16 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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))

View File

@ -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}));
}
}
</script>

View File

@ -2,16 +2,17 @@ package www
import (
"log"
"strconv"
"html/template"
"net/http"
// "golang.org/x/net/websocket"
"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)