Compare commits
2 commits
8a3da4c5fe
...
d55c680ec4
| Author | SHA1 | Date | |
|---|---|---|---|
| d55c680ec4 | |||
| 03fa525a00 |
6 changed files with 69 additions and 37 deletions
14
Taskfile.yml
14
Taskfile.yml
|
|
@ -84,3 +84,17 @@ tasks:
|
|||
- '{{.BIN}}/cool-down'
|
||||
cmds:
|
||||
- go install ./cmd/cool-down
|
||||
|
||||
install-ic-merge:
|
||||
desc: Installs the ic-merge command
|
||||
source:
|
||||
- go.mod
|
||||
- go.sum
|
||||
- cmd/ic-merge/**/*.go
|
||||
- cli/context/*.go
|
||||
- cli/err/*.go
|
||||
- infinitecraft/**/*.go
|
||||
generates:
|
||||
- '{{.BIN}}/ic-merge'
|
||||
cmds:
|
||||
- go install ./cmd/ic-merge
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
c "codeberg.org/danjones000/utils/cli/context"
|
||||
|
|
@ -26,9 +26,9 @@ func main() {
|
|||
g, err := m.Merge(ctx)
|
||||
e.HandleErr(err)
|
||||
|
||||
jsout := json.NewEncoder(os.Stdout)
|
||||
jsout.SetIndent("", "\t")
|
||||
err = jsout.Encode(g)
|
||||
var out string
|
||||
out, err = m.Write(ctx, g)
|
||||
e.HandleErr(err)
|
||||
// */
|
||||
|
||||
fmt.Printf("Saved to %s\n", out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
package infinitecraft
|
||||
|
||||
type Game struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Created uint64 `json:"created"`
|
||||
Updated uint64 `json:"updated"`
|
||||
Instances []any `json:"instances" db:"-"`
|
||||
Items []Item `json:"items" db:"-"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Created int64 `json:"created"`
|
||||
Updated int64 `json:"updated"`
|
||||
Instances []Instance `json:"instances" db:"-"`
|
||||
Items []Item `json:"items" db:"-"`
|
||||
}
|
||||
|
|
|
|||
7
infinitecraft/instance.go
Normal file
7
infinitecraft/instance.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package infinitecraft
|
||||
|
||||
type Instance struct {
|
||||
ItemID int32 `json:"itemId"`
|
||||
X uint16 `json:"x"`
|
||||
Y uint16 `json:"y"`
|
||||
}
|
||||
|
|
@ -139,11 +139,11 @@ func (m *Merger) getGameDetails(ctx context.Context, g *Game) error {
|
|||
return m.db.GetContext(ctx, g, `SELECT name, version FROM games ORDER BY id ASC LIMIT 1`)
|
||||
}
|
||||
|
||||
func (m *Merger) getGameCreated(ctx context.Context, date *uint64) error {
|
||||
func (m *Merger) getGameCreated(ctx context.Context, date *int64) error {
|
||||
return m.db.GetContext(ctx, date, `SELECT MIN(created) FROM games`)
|
||||
}
|
||||
|
||||
func (m *Merger) getGameUpdated(ctx context.Context, date *uint64) error {
|
||||
func (m *Merger) getGameUpdated(ctx context.Context, date *int64) error {
|
||||
return m.db.GetContext(ctx, date, `SELECT MAX(updated) FROM games`)
|
||||
}
|
||||
|
||||
|
|
@ -221,30 +221,6 @@ func (m *Merger) Merge(ctx context.Context) (g Game, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
type GameItem struct {
|
||||
Game string `db:"game"`
|
||||
GameID int64 `db:"game_id"`
|
||||
Text string `db:"name"`
|
||||
Emoji string
|
||||
Id int64 `db:"orig_id"`
|
||||
}
|
||||
|
||||
func (m *Merger) GameItems(ctx context.Context) (gits []GameItem, err error) {
|
||||
err = m.db.SelectContext(ctx, &gits, `
|
||||
SELECT
|
||||
g.name AS game,
|
||||
g.id AS game_id,
|
||||
it.name,
|
||||
it.emoji,
|
||||
it.id - 1 AS orig_id
|
||||
FROM games_items gi
|
||||
JOIN items it ON gi.item_id = it.id
|
||||
JOIN games g ON gi.game_id = g.id
|
||||
ORDER BY gi.id ASC
|
||||
`)
|
||||
return
|
||||
}
|
||||
|
||||
func (m *Merger) insertGames(ctx context.Context) (mp map[int64]Game, err error) {
|
||||
mp = make(map[int64]Game, len(m.games))
|
||||
var res sql.Result
|
||||
|
|
|
|||
35
infinitecraft/write.go
Normal file
35
infinitecraft/write.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package infinitecraft
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *Merger) Write(ctx context.Context, g Game) (out string, err error) {
|
||||
base := fmt.Sprintf("%s - %s", time.Now().Local().Format("2006-01-02T15_04_05"), g.Name)
|
||||
out = base + ".ic"
|
||||
|
||||
var f *os.File
|
||||
if f, err = os.Create(out); err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
gz := gzip.NewWriter(f)
|
||||
defer gz.Close()
|
||||
gz.Name = base
|
||||
gz.ModTime = time.Unix(g.Updated/1000, g.Updated%1000)
|
||||
gz.Comment = "merge by ic-merge"
|
||||
|
||||
if g.Instances == nil {
|
||||
g.Instances = make([]Instance, 0)
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(gz)
|
||||
err = enc.Encode(g)
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue