fix flags loading

This commit is contained in:
thefish 2022-10-18 06:37:11 +03:00
parent def43265de
commit f52e235799
2 changed files with 251 additions and 243 deletions

View File

@ -1,18 +1,18 @@
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"
"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 {
suite.Suite
suite.Suite
}
// метод для установки тестовых данных для всего набора, запускается до всего прочего
@ -30,179 +30,180 @@ func (suite *IpTestSuite) SetupTest() {}
func (suite *IpTestSuite) TearDownTest() {}
func TestApi(t *testing.T) {
tests := new(IpTestSuite)
suite.Run(t, tests)
tests := new(IpTestSuite)
suite.Run(t, tests)
}
func (suite *IpTestSuite) TestMaterialWeightAndVolume() {
// плотность https://tekkos.ru/katalog/poleznaya-informatsiya/tablica-plotnosti-stali-kg-m3.html
// ударная вязкость https://nposanef.ru/DOCUMENTS/PB-03-605-03/PB-03-605-03_Tab-2.6.pdf
// температура плавления http://zaozmi.ru/polezno/temperatura_plavleniya_metallov.html
// температура кипения http://temperatures.ru/pages/temperatura_plavleniya_i_kipeniya
// Пределы прочности некоторых материалов https://sevparitet.ru/raznoe/koefficient-uprugosti-tablica.html
// плотность https://tekkos.ru/katalog/poleznaya-informatsiya/tablica-plotnosti-stali-kg-m3.html
// ударная вязкость https://nposanef.ru/DOCUMENTS/PB-03-605-03/PB-03-605-03_Tab-2.6.pdf
// температура плавления http://zaozmi.ru/polezno/temperatura_plavleniya_metallov.html
// температура кипения http://temperatures.ru/pages/temperatura_plavleniya_i_kipeniya
// Пределы прочности некоторых материалов https://sevparitet.ru/raznoe/koefficient-uprugosti-tablica.html
metalMaterialFlags := MaterialFlags{
ConductsElictricity: true,
BlocksLiquid: true,
AcidResistant: true,
BlocksGas: true,
Flammable: false,
ConductsHeat: true,
Radiates: true,
}
metalMaterialFlags := MaterialFlags{
ConductsElictricity: true,
BlocksLiquid: true,
AcidResistant: true,
BlocksGas: true,
Flammable: false,
ConductsHeat: true,
Radiates: true,
}
woodMaterialFlags := MaterialFlags{
ConductsElictricity: false,
BlocksLiquid: true,
AcidResistant: false,
BlocksGas: true,
Flammable: true,
ConductsHeat: false,
Radiates: false,
}
woodMaterialFlags := MaterialFlags{
ConductsElictricity: false,
BlocksLiquid: true,
AcidResistant: false,
BlocksGas: true,
Flammable: true,
ConductsHeat: false,
Radiates: false,
}
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 := 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}},
}
testOakWood := Material{
Name: "testoakwood",
Flags: woodMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(700)},
FractureToughness: DimensionFractureToughness{decimal.NewFromFloat(4.5)},
MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(600), true}}, //загорается при 600 град Цельсия
}
testOakWood := Material{
Name: "testoakwood",
Flags: woodMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(700)},
FractureToughness: DimensionFractureToughness{decimal.NewFromFloat(4.5)},
MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(600), true}}, //загорается при 600 град Цельсия
}
testCube := ItemPhysics{
Material: teststeel,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(65)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.1),
Height: decimal.NewFromFloat(0.1),
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.1)),
Thickness: decimal.NullDecimal{},
},
DimensionItemTemperature: DimensionItemTemperature{decimal.NewFromInt(20)},
}
testCube := ItemPhysics{
Material: teststeel,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(65)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.1),
Height: decimal.NewFromFloat(0.1),
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.1)),
Thickness: decimal.NullDecimal{},
},
DimensionItemTemperature: DimensionItemTemperature{decimal.NewFromInt(20)},
}
suite.Equal(decimal.NewFromFloat(7.8).String(), testCube.Weight().String())
suite.Equal(decimal.NewFromFloat(0.001).String(), testCube.Volume().String())
suite.Equal(decimal.NewFromFloat(7.8).String(), testCube.Weight().String())
suite.Equal(decimal.NewFromFloat(0.001).String(), testCube.Volume().String())
testOakCube := ItemPhysics{
Material: testOakWood,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(4)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.1),
Height: decimal.NewFromFloat(0.1),
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.1)),
Thickness: decimal.NullDecimal{},
},
DimensionItemTemperature: DimensionItemTemperature{decimal.NewFromInt(20)},
}
testOakCube := ItemPhysics{
Material: testOakWood,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(4)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.1),
Height: decimal.NewFromFloat(0.1),
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.1)),
Thickness: decimal.NullDecimal{},
},
DimensionItemTemperature: DimensionItemTemperature{decimal.NewFromInt(20)},
}
//oakwood is ~10 times lighter than steel
suite.Equal(decimal.NewFromFloat(0.7).String(), testOakCube.Weight().String())
suite.Equal(decimal.NewFromFloat(0.001).String(), testOakCube.Volume().String())
//oakwood is ~10 times lighter than steel
suite.Equal(decimal.NewFromFloat(0.7).String(), testOakCube.Weight().String())
suite.Equal(decimal.NewFromFloat(0.001).String(), testOakCube.Volume().String())
testCuirass := ItemPhysics{
Material: teststeel,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(55)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.5), //60 cm wide
Height: decimal.NewFromFloat(0.8), //80 cm high
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.4)), //50 cm deep
Thickness: decimal.NewNullDecimal(decimal.NewFromFloat(0.001)), // 1mm thick
},
DimensionItemTemperature: DimensionItemTemperature{},
}
testCuirass := ItemPhysics{
Material: teststeel,
DimensionItemRigidity: DimensionItemRigidity{decimal.NewFromInt(55)},
DimensionItemSize: DimensionItemSize{
Width: decimal.NewFromFloat(0.5), //60 cm wide
Height: decimal.NewFromFloat(0.8), //80 cm high
Depth: decimal.NewNullDecimal(decimal.NewFromFloat(0.4)), //50 cm deep
Thickness: decimal.NewNullDecimal(decimal.NewFromFloat(0.001)), // 1mm thick
},
DimensionItemTemperature: DimensionItemTemperature{},
}
//12.1992 kg HEAVY ARMOR IS HEAVY
suite.Equal(decimal.NewFromFloat(12.1992).String(), testCuirass.Weight().String())
//0.001564 m3 of steel
suite.Equal(decimal.NewFromFloat(0.001564).String(), testCuirass.Volume().String())
//12.1992 kg HEAVY ARMOR IS HEAVY
suite.Equal(decimal.NewFromFloat(12.1992).String(), testCuirass.Weight().String())
//0.001564 m3 of steel
suite.Equal(decimal.NewFromFloat(0.001564).String(), testCuirass.Volume().String())
}
func (suite *IpTestSuite) TestMaterialSerialization() {
metalMaterialFlags := MaterialFlags{
ConductsElictricity: true,
BlocksLiquid: true,
AcidResistant: true,
BlocksGas: true,
Flammable: false,
ConductsHeat: true,
Radiates: true,
}
metalMaterialFlags := MaterialFlags{
ConductsElictricity: true,
BlocksLiquid: true,
AcidResistant: true,
BlocksGas: true,
Flammable: false,
ConductsHeat: true,
Radiates: true,
}
woodMaterialFlags := MaterialFlags{
ConductsElictricity: false,
BlocksLiquid: true,
AcidResistant: false,
BlocksGas: true,
Flammable: true,
ConductsHeat: false,
Radiates: false,
}
woodMaterialFlags := MaterialFlags{
ConductsElictricity: false,
BlocksLiquid: true,
AcidResistant: false,
BlocksGas: true,
Flammable: true,
ConductsHeat: false,
Radiates: false,
}
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 := 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}},
}
testOakWood := Material{
Name: "testoakwood",
Flags: woodMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(700)},
FractureToughness: DimensionFractureToughness{decimal.NewFromFloat(4.5)},
MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(600), true}}, //загорается при 600 град Цельсия
}
bytes, err := json.Marshal(teststeel)
suite.NoError(err)
suite.Equal(
`{"name":"teststeel","material_flags":{"conducts_elictricity":true,"blocks_liquid":true,"acid_resistant":true,"blocks_gas":true,"flammable":false,"conducts_heat":true,"radiates":true},"density":"7800","fracture_toughness":"30","melting_point":"1400","boiling_point":"3200"}`,
string(bytes),
)
testOakWood := Material{
Name: "testoakwood",
Flags: woodMaterialFlags,
Density: DimensionItemDensity{decimal.NewFromInt(700)},
FractureToughness: DimensionFractureToughness{decimal.NewFromFloat(4.5)},
MeltingPoint: DimensionItemNullTemperature{decimal.NullDecimal{decimal.NewFromInt(600), true}}, //загорается при 600 град Цельсия
}
bytes, err := json.Marshal(teststeel)
suite.NoError(err)
suite.Equal(
`{"name":"teststeel","material_flags":{"conducts_elictricity":true,"blocks_liquid":true,"acid_resistant":true,"blocks_gas":true,"flammable":false,"conducts_heat":true,"radiates":true},"density":"7800","fracture_toughness":"30","melting_point":"1400","boiling_point":"3200"}`,
string(bytes),
)
bytes, err = json.Marshal(testOakWood)
suite.NoError(err)
suite.Equal(`{"name":"testoakwood","material_flags":{"conducts_elictricity":false,"blocks_liquid":true,"acid_resistant":false,"blocks_gas":true,"flammable":true,"conducts_heat":false,"radiates":false},"density":"700","fracture_toughness":"4.5","melting_point":"600","boiling_point":null}`,
string(bytes),
)
bytes, err = json.Marshal(testOakWood)
suite.NoError(err)
suite.Equal(`{"name":"testoakwood","material_flags":{"conducts_elictricity":false,"blocks_liquid":true,"acid_resistant":false,"blocks_gas":true,"flammable":true,"conducts_heat":false,"radiates":false},"density":"700","fracture_toughness":"4.5","melting_point":"600","boiling_point":null}`,
string(bytes),
)
}
func (suite *IpTestSuite) TestMaterialDeserialization() {
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
mm, err := NewMaterialMap("../../..", logger)
suite.NoError(err)
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,
}
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 := Material{
Name: "steel",
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
loadedsteel := mm["steel"]
suite.Equalf(teststeel, *loadedsteel, "error: %s")
_ = mm
}

View File

@ -1,109 +1,116 @@
package itemprops
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"io/ioutil"
"os"
"path/filepath"
"strings"
"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"`
FractureToughness DimensionFractureToughness `json:"fracture_toughness"`
MeltingPoint DimensionItemNullTemperature `json:"melting_point,omitempty"`
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
Id string `json:"id"`
Name string `json:"name"`
Flags MaterialFlags `json:"material_flags"`
Density DimensionItemDensity `json:"density"`
FractureToughness DimensionFractureToughness `json:"fracture_toughness"`
MeltingPoint DimensionItemNullTemperature `json:"melting_point,omitempty"`
BoilingPoint DimensionItemNullTemperature `json:"boiling_point,omitempty"`
}
func (m Material) Unmarshal() {}
type MaterialFlags struct {
ConductsElictricity bool `json:"conducts_elictricity"`
BlocksLiquid bool `json:"blocks_liquid"`
AcidResistant bool `json:"acid_resistant"`
BlocksGas bool `json:"blocks_gas"`
Flammable bool `json:"flammable"`
ConductsHeat bool `json:"conducts_heat"`
Radiates bool `json:"radiates"`
ConductsElictricity bool `json:"conducts_elictricity"`
BlocksLiquid bool `json:"blocks_liquid"`
AcidResistant bool `json:"acid_resistant"`
BlocksGas bool `json:"blocks_gas"`
Flammable bool `json:"flammable"`
ConductsHeat bool `json:"conducts_heat"`
Radiates bool `json:"radiates"`
}
type MaterialMap map[string]Material
type MaterialMap map[string]*Material
type tt map[string]MaterialMap
func NewMaterialMap(path string, logger zerolog.Logger) (*MaterialMap, error) {
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
}
mm := make(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 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
})
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))
_ = 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 {
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
toReplace := item2.(map[string]interface{})["material_flags"]
if ref, ok := toReplace.(map[string]interface{})["$ref"]; ok {
rfs := strings.Split(ref.(string), "/")
referredFlag := rfs[len(rfs)-1]
item2.(map[string]interface{})["material_flags"] = flags[referredFlag]
}
bts, err := json.Marshal(item2)
if err != nil {
return nil, fmt.Errorf("Could not marshal back:%w", err)
}
mm[clause] = &Material{}
err = json.Unmarshal(bts, mm[clause])
if err != nil {
return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
}
}
}
}
return mm, err
}