tests, load materials start

This commit is contained in:
thefish 2022-10-15 01:25:10 +03:00
parent 09bfa44c78
commit def43265de
7 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,26 @@
{
"material_flags": [
{
"metal": {
"conducts_elictricity": true,
"blocks_liquid": true,
"acid_resistant": true,
"blocks_gas": true,
"flammable": false,
"conducts_heat": true,
"radiates": true
}
},
{
"wood": {
"conducts_elictricity": false,
"blocks_liquid": true,
"acid_resistant": false,
"blocks_gas": true,
"flammable": true,
"conducts_heat": false,
"radiates": false
}
}
]
}

View File

@ -0,0 +1,28 @@
{
"materials": [
{
"steel": {
"name": "steel",
"material_flags": {
"$ref": "#/material_flags/metal"
},
"density": "7800",
"fracture_toughness": "30",
"melting_point": "1400",
"boiling_point": "3200"
}
},
{
"iron": {
"name": "iron",
"material_flags": {
"$ref": "#/material_flags/metal"
},
"density": "7800",
"fracture_toughness": "12",
"melting_point": "1400",
"boiling_point": "3200"
}
}
]
}

View File

@ -0,0 +1,15 @@
{
"material_flags": [
{
"acme-fiber": {
"conducts_elictricity": false,
"blocks_liquid": true,
"acid_resistant": true,
"blocks_gas": true,
"flammable": false,
"conducts_heat": false,
"radiates": false
}
}
]
}

View File

@ -0,0 +1,16 @@
{
"materials": [
{
"oakwood": {
"name": "testoakwood",
"material_flags": {
"$ref": "#/material_flags/wood"
},
"density": "700",
"fracture_toughness": "4.5",
"melting_point": "600",
"boiling_point": null
}
}
]
}

View File

@ -84,6 +84,7 @@ type BodyPart struct {
LayerMiddle MedicalMaterial
LayerInner MedicalMaterial
Size DimensionItemSize
Joints []Joint
Contains []InnerOrgan
Exposes []OuterOrgan

View File

@ -2,9 +2,13 @@ package itemprops
import (
"encoding/json"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"
)
type IpTestSuite struct {
@ -173,3 +177,32 @@ func (suite *IpTestSuite) TestMaterialSerialization() {
string(bytes),
)
}
func (suite *IpTestSuite) TestMaterialDeserialization() {
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
mm, err := NewMaterialMap("../../..", logger)
suite.NoError(err)
metalMaterialFlags := MaterialFlags{
ConductsElictricity: true,
BlocksLiquid: true,
AcidResistant: true,
BlocksGas: true,
Flammable: false,
ConductsHeat: true,
Radiates: true,
}
teststeel := Material{
Name: "teststeel",
Flags: metalMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(7800)},
FractureToughness: DimensionFractureToughness{decimal.NewFromInt(30)},
MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(1400), true}},
BoilingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(3200), true}},
}
_ = teststeel
_ = mm
}

View File

@ -1,6 +1,17 @@
package itemprops
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Material struct {
Id string `json:"id"`
Name string `json:"name"`
Flags MaterialFlags `json:"material_flags"`
Density DimensionItemDensity `json:"density"`
@ -9,6 +20,8 @@ type Material struct {
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
}
func (m Material) Unmarshal() {}
type MaterialFlags struct {
ConductsElictricity bool `json:"conducts_elictricity"`
BlocksLiquid bool `json:"blocks_liquid"`
@ -18,3 +31,79 @@ type MaterialFlags struct {
ConductsHeat bool `json:"conducts_heat"`
Radiates bool `json:"radiates"`
}
type MaterialMap map[string]Material
type tt map[string]MaterialMap
func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) {
mm := &MaterialMap{}
tmp := make(map[string][]interface{})
flags := make(map[string]*MaterialFlags)
err := filepath.Walk(path+"/assets/materials",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".json") {
splt := strings.Split(path, "/")
logger.Info().Msgf("loading %s %d", splt[len(splt)-1], info.Size())
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
ttmp := make(map[string][]interface{})
err = json.Unmarshal(bytes, &ttmp)
if err != nil {
return err
}
for idx, _ := range ttmp {
tmp[idx] = append(tmp[idx], ttmp[idx]...)
}
}
return nil
})
_ = flags
if lst, ok := tmp["material_flags"]; ok {
for _, item := range lst {
ttt := item.(map[string]interface{})
_ = ttt
for clause, item2 := range ttt {
bts, err := json.Marshal(item2)
if err != nil {
return nil, fmt.Errorf("Could not marshal back:%w", err)
}
flags[clause] = &MaterialFlags{}
err = json.Unmarshal(bts, flags[clause])
if err != nil {
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
}
}
}
}
logger.Info().Msgf("loaded %d material flag sets", len(flags))
if lst, ok := tmp["materials"]; ok {
for _, item := range lst {
ttt := item.(map[string]interface{})
_ = ttt
for clause, item2 := range ttt {
bts, err := json.Marshal(item2)
if err != nil {
return nil, fmt.Errorf("Could not marshal back:%w", err)
}
flags[clause] = &MaterialFlags{}
err = json.Unmarshal(bts, flags[clause])
if err != nil {
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
}
}
}
}
return mm, err
}