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.
91 lines
2.8 KiB
91 lines
2.8 KiB
import {SyncableStatePB} from "../proto/state";
|
|
import {SyncedState} from "../state/SyncedState";
|
|
import {HexagonOrientation, HexCell, HexLayout, HexMap, LineParity} from "../state/HexMap";
|
|
import {UserStatePB} from "../proto/user";
|
|
import {
|
|
HexCellPB,
|
|
HexMapPB,
|
|
HexMapPB_Layout,
|
|
HexMapPB_Layout_LineParity,
|
|
HexMapPB_Layout_Orientation
|
|
} from "../proto/map";
|
|
import {UserState} from "../state/UserState";
|
|
import {unpackHexColor} from "../util/ColorUtils";
|
|
import {encode} from "base64-arraybuffer";
|
|
import {StorageCoordinatesPB} from "../proto/coords";
|
|
import {StorageCoordinates} from "../state/Coordinates";
|
|
|
|
export function storageCoordsFromPb(coords: StorageCoordinatesPB): StorageCoordinates {
|
|
return {
|
|
line: coords.line,
|
|
cell: coords.cell,
|
|
};
|
|
}
|
|
|
|
function orientationFromPb(orientation: HexMapPB_Layout_Orientation): HexagonOrientation {
|
|
switch (orientation) {
|
|
case HexMapPB_Layout_Orientation.FLAT_TOP:
|
|
return HexagonOrientation.FLAT_TOP
|
|
case HexMapPB_Layout_Orientation.POINTY_TOP:
|
|
return HexagonOrientation.POINTY_TOP
|
|
case HexMapPB_Layout_Orientation.UNKNOWN_ORIENTATION:
|
|
case HexMapPB_Layout_Orientation.UNRECOGNIZED:
|
|
throw Error("unknown orientation")
|
|
}
|
|
}
|
|
|
|
function lineParityFromPb(parity: HexMapPB_Layout_LineParity): LineParity {
|
|
switch (parity) {
|
|
case HexMapPB_Layout_LineParity.EVEN:
|
|
return LineParity.EVEN
|
|
case HexMapPB_Layout_LineParity.ODD:
|
|
return LineParity.ODD
|
|
case HexMapPB_Layout_LineParity.UNKNOWN_LINE:
|
|
case HexMapPB_Layout_LineParity.UNRECOGNIZED:
|
|
throw Error("unknown line parity")
|
|
}
|
|
}
|
|
|
|
function layoutFromPb(layout: HexMapPB_Layout): HexLayout {
|
|
return {
|
|
orientation: orientationFromPb(layout.orientation),
|
|
indentedLines: lineParityFromPb(layout.indentedLines),
|
|
}
|
|
}
|
|
|
|
function cellFromPb(cell: HexCellPB): HexCell {
|
|
return {
|
|
color: unpackHexColor(cell.color)
|
|
}
|
|
}
|
|
|
|
function mapFromPb(map: HexMapPB): HexMap {
|
|
if (!map.layout || !map.layer) {
|
|
throw Error("HexMapPB did not have layout and layer");
|
|
}
|
|
return {
|
|
xid: encode(map.xid),
|
|
lines: map.lines,
|
|
cellsPerLine: map.cellsPerLine,
|
|
layout: layoutFromPb(map.layout),
|
|
layer: map.layer.lines.map((line): HexCell[] => {
|
|
return line.cells.map(cellFromPb)
|
|
}),
|
|
};
|
|
}
|
|
|
|
function userFromPb(user: UserStatePB): UserState {
|
|
return {
|
|
activeColor: unpackHexColor(user.color)
|
|
}
|
|
}
|
|
|
|
export function stateFromPb(state: SyncableStatePB): SyncedState {
|
|
if (!state.map || !state.user) {
|
|
throw Error("SyncableStatePB did not have map and user")
|
|
}
|
|
return {
|
|
map: mapFromPb(state.map),
|
|
user: userFromPb(state.user),
|
|
}
|
|
} |