You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
hexmap/server/state/map.pbconv.go

142 lines
2.7 KiB

package state
import (
"github.com/rs/xid"
"math"
)
func (x HexMapPB_Layout_Orientation) ToGo() HexOrientation {
switch x {
case HexMapPB_Layout_POINTY_TOP:
return PointyTop
case HexMapPB_Layout_FLAT_TOP:
return FlatTop
default:
return UnknownOrientation
}
}
func (o HexOrientation) ToPB() HexMapPB_Layout_Orientation {
switch o {
case PointyTop:
return HexMapPB_Layout_POINTY_TOP
case FlatTop:
return HexMapPB_Layout_FLAT_TOP
default:
return HexMapPB_Layout_UNKNOWN_ORIENTATION
}
}
func (x HexMapPB_Layout_LineParity) ToGo() LineParity {
switch x {
case HexMapPB_Layout_EVEN:
return EvenLines
case HexMapPB_Layout_ODD:
return OddLines
default:
return UnknownParity
}
}
func (o LineParity) ToPB() HexMapPB_Layout_LineParity {
switch o {
case OddLines:
return HexMapPB_Layout_ODD
case EvenLines:
return HexMapPB_Layout_EVEN
default:
return HexMapPB_Layout_UNKNOWN_LINE
}
}
func (x *HexMapPB_Layout) ToGo() Layout {
return Layout{
Orientation: x.Orientation.ToGo(),
IndentedLines: x.IndentedLines.ToGo(),
}
}
func (l Layout) ToPB() *HexMapPB_Layout {
return &HexMapPB_Layout{
Orientation: l.Orientation.ToPB(),
IndentedLines: l.IndentedLines.ToPB(),
}
}
func (x *HexCellPB) ToGo() HexCell {
return HexCell{
Color: ColorFromInt(x.Color),
}
}
func (h HexCell) ToPB() *HexCellPB {
return &HexCellPB{
Color: h.Color.ToInt(),
}
}
func (x *HexLinePB) ToGo() HexLine {
r := make(HexLine, len(x.Cells))
for index, cell := range x.Cells {
r[index] = cell.ToGo()
}
return r
}
func (l HexLine) ToPB() *HexLinePB {
cells := make([]*HexCellPB, len(l))
for index, cell := range l {
cells[index] = cell.ToPB()
}
return &HexLinePB{
Cells: cells,
}
}
func (x *HexLayerPB) ToGo() HexLayer {
r := make(HexLayer, len(x.Lines))
for index, line := range x.Lines {
r[index] = line.ToGo()
}
return r
}
func (l HexLayer) ToPB() *HexLayerPB {
lines := make([]*HexLinePB, len(l))
for index, line := range l {
lines[index] = line.ToPB()
}
return &HexLayerPB{
Lines: lines,
}
}
func (x *HexMapPB) ToGo() (HexMap, error) {
pbId, err := xid.FromBytes(x.Xid)
if err != nil {
return HexMap{}, err
}
if x.Lines > math.MaxUint8 {
return HexMap{}, ErrOutOfBounds
}
if x.CellsPerLine > math.MaxUint8 {
return HexMap{}, ErrOutOfBounds
}
return HexMap{
XID: pbId,
Lines: uint8(x.Lines),
CellsPerLine: uint8(x.CellsPerLine),
Layout: x.Layout.ToGo(),
Layer: x.Layer.ToGo(),
}, nil
}
func (m HexMap) ToPB() *HexMapPB {
return &HexMapPB{
Xid: m.XID.Bytes(),
Lines: uint32(m.Lines),
CellsPerLine: uint32(m.CellsPerLine),
Layout: m.Layout.ToPB(),
Layer: m.Layer.ToPB(),
}
}