new new
This commit is contained in:
parent
052616c318
commit
c56f9f7a4b
@ -1,7 +1,7 @@
|
|||||||
package archive
|
package archive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -9,7 +9,7 @@ import (
|
|||||||
"../config"
|
"../config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pln = fmt.Println
|
var pln = log.Println
|
||||||
|
|
||||||
var Archive_map map[uint32]config.Track_t
|
var Archive_map map[uint32]config.Track_t
|
||||||
var Archive_map_keys []uint32
|
var Archive_map_keys []uint32
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package playlist
|
package playlist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -11,7 +11,7 @@ import (
|
|||||||
"../config"
|
"../config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pln = fmt.Println
|
var pln = log.Println
|
||||||
|
|
||||||
type Playlist struct {
|
type Playlist struct {
|
||||||
NAME string
|
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
|
// https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
|
||||||
|
|
||||||
func (p *Playlist) Pop() error {
|
func (p *Playlist) Pop() error {
|
||||||
@ -157,20 +165,54 @@ func (p *Playlist) Insert(track_id uint32, at_index int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Playlist) Delete(track_id uint32) error {
|
func (p *Playlist) index(track_id uint32) int {
|
||||||
|
|
||||||
k := -1
|
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 }
|
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 {
|
if k < 0 {
|
||||||
return errors.New("Invalid track ID")
|
return errors.New("Invalid track ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
p.LIST = append(p.LIST[:k-1], p.LIST[k+1:]...)
|
slice_head := p.LIST[:k]
|
||||||
return nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
5
serve.go
5
serve.go
@ -13,7 +13,10 @@ var pln = fmt.Println
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.Loadconfig("config.ini")
|
config.Loadconfig("config.ini")
|
||||||
p, _ := playlist.MakeRandom("YOYO", 10)
|
p, err := playlist.MakeRandom("YOYO", 10)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
p.Pop()
|
p.Pop()
|
||||||
www.Init(p)
|
www.Init(p)
|
||||||
log.Fatal(http.ListenAndServe(":8718", nil))
|
log.Fatal(http.ListenAndServe(":8718", nil))
|
||||||
|
|||||||
@ -48,7 +48,7 @@
|
|||||||
function on_update_operation(id, index) {
|
function on_update_operation(id, index) {
|
||||||
if(sock) {
|
if(sock) {
|
||||||
console.log("sending")
|
console.log("sending")
|
||||||
sock.send(JSON.stringify({"op": "update", "id": id, "index": index}));
|
sock.send(JSON.stringify({"op": "move", "id": id, "index": index}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
17
www/www.go
17
www/www.go
@ -2,16 +2,17 @@ package www
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
// "golang.org/x/net/websocket"
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"../playlist"
|
"../playlist"
|
||||||
)
|
)
|
||||||
|
|
||||||
// todo: hub - https://stackoverflow.com/questions/31532652/go-websocket-send-all-clients-a-message
|
// 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
|
var pln = log.Println
|
||||||
|
|
||||||
@ -26,7 +27,8 @@ var upgrader = websocket.Upgrader{
|
|||||||
WriteBufferSize: 512,
|
WriteBufferSize: 512,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(p *playlist.Playlist) {
|
func Init(playlist *playlist.Playlist) {
|
||||||
|
p = playlist
|
||||||
pp = p.Pretty()
|
pp = p.Pretty()
|
||||||
http.HandleFunc("/pp", pp_handler)
|
http.HandleFunc("/pp", pp_handler)
|
||||||
http.HandleFunc("/ppop", pp_operations)
|
http.HandleFunc("/ppop", pp_operations)
|
||||||
@ -59,6 +61,15 @@ func readop(c *websocket.Conn) {
|
|||||||
pln(err)
|
pln(err)
|
||||||
return //connection lost?
|
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.OP)
|
||||||
pln(opdata.ID)
|
pln(opdata.ID)
|
||||||
pln(opdata.INDEX)
|
pln(opdata.INDEX)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user