http connector first impl
This commit is contained in:
@@ -6,6 +6,9 @@ import (
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
// Version returns semantic version of the Client
|
||||
Version() string
|
||||
|
||||
// Run initializes necessary UI layout an settings, and activates client's main window.
|
||||
// This is a blocking operation until client's main window is closed.
|
||||
Run() error
|
||||
@@ -24,7 +27,7 @@ func (i GameID) String() string {
|
||||
}
|
||||
|
||||
type State struct {
|
||||
// TODO: store user login key
|
||||
// TODO: store user's login key
|
||||
GameState []GameState `json:"gameState"`
|
||||
ActiveGameID GameID `json:"activeGameId"`
|
||||
}
|
||||
@@ -40,8 +43,3 @@ type GameData struct {
|
||||
Report report.Report `json:"report"`
|
||||
Order *order.Order `json:"order,omitempty"`
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
// TODO: use fyne.Storage for initializing and storing data
|
||||
StoragePath string
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
module galaxy/storage
|
||||
|
||||
go 1.26.0
|
||||
@@ -0,0 +1,74 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"galaxy/model/client"
|
||||
"galaxy/model/order"
|
||||
"galaxy/model/report"
|
||||
"galaxy/util"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
UIStorage
|
||||
Exists(string) (bool, error)
|
||||
ReadFile(string) ([]byte, error)
|
||||
WriteFile(string, []byte) error
|
||||
}
|
||||
|
||||
// UIStorage manages Client's data local storing and retrieval.
|
||||
// It performs all I/O operations asynchronously to avoid UI main thread blocking.
|
||||
type UIStorage interface {
|
||||
// StateExists check asynchronously for previously saved [model.State] exists on the filesystem.
|
||||
// Passed callback func will will accept false and non-nil error in case of I/O or decoding errors occuried,
|
||||
// otherwise bool parameter will indicate existence of previously stores state
|
||||
StateExists(func(bool, error))
|
||||
|
||||
// LoadState loads Client's [model.State] from filesystem data asynchronously.
|
||||
// Passed callback func will accept non-nil error in case of I/O or decoding errors occuried,
|
||||
// otherwise callback func accepts loaded [model.State].
|
||||
LoadState(func(client.State, error))
|
||||
|
||||
// SaveState stores Client's state at the filesystem asynchronously.
|
||||
// I/O or encoding error may occur, it that case callback func will be called with non-nil error.
|
||||
SaveState(client.State, func(error))
|
||||
|
||||
// LoadReport loads a [report.Report] for a given [model.GameID] and turn number from filesystem asynchronously.
|
||||
// Passed callback func will will accept non-nil error in case of I/O or decoding errors occuried,
|
||||
// otherwise callback func accepts loaded [report.Report].
|
||||
LoadReport(client.GameID, uint, func(report.Report, error))
|
||||
|
||||
// PutReport stores given [report.Report] for a given [model.GameID] and turn number at the filesystem asynchronously.
|
||||
// I/O or encoding error may occur, it that case callback func will be called with non-nil error.
|
||||
PutReport(client.GameID, uint, report.Report, func(error))
|
||||
|
||||
// LoadOrder loads a [order.Order] for a given [model.GameID] and turn number from filesystem asynchronously.
|
||||
// Passed callback func will will accept non-nil error in case of I/O or decoding errors occuried,
|
||||
// otherwise callback func accepts loaded [order.Order].
|
||||
LoadOrder(client.GameID, uint, func(order.Order, error))
|
||||
|
||||
// PutOrder stores given [order.Order] for a given [model.GameID] and turn number at the filesystem asynchronously.
|
||||
// I/O or encoding error may occur, it that case callback func will be called with non-nil error.
|
||||
PutOrder(client.GameID, uint, order.Order, func(error))
|
||||
}
|
||||
|
||||
type storage struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func NewStorage(path string) (*storage, error) {
|
||||
if ok, err := util.PathExists(path, true); err != nil {
|
||||
return nil, fmt.Errorf("new storage: check path %q exists: %w", path, err)
|
||||
} else if !ok {
|
||||
return nil, fmt.Errorf("new storage: path %q does not exists", path)
|
||||
}
|
||||
if ok, err := util.Writable(path); err != nil {
|
||||
return nil, fmt.Errorf("new storage: check path %q writable: %w", path, err)
|
||||
} else if !ok {
|
||||
return nil, fmt.Errorf("new storage: path %q is not writable", path)
|
||||
}
|
||||
s := &storage{
|
||||
path: path,
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
Reference in New Issue
Block a user