Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use build tags to remove database from wasm, and pass db resources fr… #1781

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ sim/core/items/all_items.go: tools/generate_items/*.go $(call rwildcard,sim/core

.PHONY: test
test: $(OUT_DIR)/lib.wasm binary_dist/dist.go
go test ./...
go test --tags=with_db ./...

.PHONY: update-tests
update-tests:
Expand Down
7 changes: 3 additions & 4 deletions proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,19 @@ message Player {
TankDeathknight tank_deathknight = 32;
}

// Only used by the UI. Sim uses talents within the spec protos.
string talentsString = 17;

Glyphs glyphs = 28;

Profession profession1 = 29;
Profession profession2 = 30;

Cooldowns cooldowns = 19;

bool in_front_of_target = 23;
double distance_from_target = 33;

HealingModel healing_model = 27;

// Items/enchants/gems/etc to include in the database.
SimDatabase database = 35;
}

message Party {
Expand Down
4 changes: 4 additions & 0 deletions sim/core/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Character struct {
}

func NewCharacter(party *Party, partyIndex int, player *proto.Player) Character {
if player.Database != nil {
addToDatabase(player.Database)
}

character := Character{
Unit: Unit{
Type: PlayerUnit,
Expand Down
6 changes: 5 additions & 1 deletion sim/core/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)

var WITH_DB = false

var ItemsByID = map[int32]Item{}
var GemsByID = map[int32]Gem{}
var EnchantsByItemByID = map[proto.ItemType]map[int32]Enchant{}

func addToDatabase(newDB *proto.SimDatabase) {
for _, v := range newDB.Items {
if _, ok := ItemsByID[v.Id]; !ok {
ItemsByID[v.Id] = ItemFromProto(v)
item := ItemFromProto(v)
ItemsByID[v.Id] = item
AddItemToSets(item)
}
}

Expand Down
5 changes: 5 additions & 0 deletions sim/core/database_load.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Only include this file in the build when we specify the 'with_db' tag.
// Without the tag, the database will start out completely empty.
//go:build with_db

package core

import (
Expand All @@ -7,6 +11,7 @@ import (

func init() {
db := database.Load()
WITH_DB = true

simDB := &proto.SimDatabase{
Items: make([]*proto.SimItem, len(db.Items)),
Expand Down
44 changes: 25 additions & 19 deletions sim/core/item_effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func HasEnchantEffect(id int32) bool {
// Registers an ApplyEffect function which will be called before the Sim
// starts, for any Agent that is wearing the item.
func NewItemEffect(id int32, itemEffect ApplyEffect) {
if _, hasItem := ItemsByID[id]; !hasItem {
if _, hasGem := GemsByID[id]; !hasGem {
panic(fmt.Sprintf("No item with ID: %d", id))
if WITH_DB {
if _, hasItem := ItemsByID[id]; !hasItem {
if _, hasGem := GemsByID[id]; !hasGem {
panic(fmt.Sprintf("No item with ID: %d", id))
}
}
}

Expand All @@ -71,15 +73,17 @@ func NewItemEffect(id int32, itemEffect ApplyEffect) {
}

func NewEnchantEffect(id int32, enchantEffect ApplyEffect) {
found := false
for _, enchantsByID := range EnchantsByItemByID {
if _, ok := enchantsByID[id]; ok {
found = true
break
if WITH_DB {
found := false
for _, enchantsByID := range EnchantsByItemByID {
if _, ok := enchantsByID[id]; ok {
found = true
break
}
}
if !found {
panic(fmt.Sprintf("No enchant with ID: %d", id))
}
}
if !found {
panic(fmt.Sprintf("No enchant with ID: %d", id))
}

if HasEnchantEffect(id) {
Expand All @@ -90,15 +94,17 @@ func NewEnchantEffect(id int32, enchantEffect ApplyEffect) {
}

func AddWeaponEffect(id int32, weaponEffect ApplyWeaponEffect) {
found := false
for _, enchantsByID := range EnchantsByItemByID {
if _, ok := enchantsByID[id]; ok {
found = true
break
if WITH_DB {
found := false
for _, enchantsByID := range EnchantsByItemByID {
if _, ok := enchantsByID[id]; ok {
found = true
break
}
}
if !found {
panic(fmt.Sprintf("No enchant with ID: %d", id))
}
}
if !found {
panic(fmt.Sprintf("No enchant with ID: %d", id))
}
if HasWeaponEffect(id) {
panic(fmt.Sprintf("Cannot add multiple effects for one item: %d, %#v", id, weaponEffect))
Expand Down
21 changes: 16 additions & 5 deletions sim/core/item_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ func NewItemSet(setStruct ItemSet) *ItemSet {
}
}
}
if !foundName {
panic("No items found for set " + set.Name)
}
if len(set.AlternativeName) > 0 && !foundAlternativeName {
panic("No items found for set alternative " + set.AlternativeName)

if WITH_DB {
if !foundName {
panic("No items found for set " + set.Name)
}
if len(set.AlternativeName) > 0 && !foundAlternativeName {
panic("No items found for set alternative " + set.AlternativeName)
}
}

sets = append(sets, set)
Expand All @@ -84,6 +87,14 @@ func NewItemSet(setStruct ItemSet) *ItemSet {
return set
}

func AddItemToSets(item Item) {
for _, set := range sets {
if set.Name == item.SetName || set.AlternativeName == item.SetName {
set.Items[item.ID] = struct{}{}
}
}
}

func (character *Character) HasSetBonus(itemSet *ItemSet, numItems int32) bool {
if character.Env != nil && character.Env.IsFinalized() {
panic("HasSetBonus is very slow and should never be called after finalization. Try caching the value during construction instead!")
Expand Down
9 changes: 6 additions & 3 deletions sim/core/test_generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@ func (filter *ItemFilter) FindAllSets() []*ItemSet {
filteredSets := []*ItemSet{}

for _, set := range GetAllItemSets() {
firstItem := ItemsByID[set.ItemIDs()[0]]
if filter.Matches(firstItem, true) {
filteredSets = append(filteredSets, set)
itemIDs := set.ItemIDs()
if len(itemIDs) > 0 {
firstItem := ItemsByID[itemIDs[0]]
if filter.Matches(firstItem, true) {
filteredSets = append(filteredSets, set)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions ui/core/player.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
Class,
Cooldowns,
Consumes,
Cooldowns,
Faction,
GemColor,
Glyphs,
HandType,
Expand All @@ -12,8 +13,8 @@ import {
Race,
RaidTarget,
RangedWeaponType,
SimDatabase,
Spec,
Faction,
Stat,
WeaponType,
} from './proto/common.js';
Expand Down Expand Up @@ -794,13 +795,14 @@ export class Player<SpecType extends Spec> {
}

toProto(forExport?: boolean): PlayerProto {
const gear = this.getGear();
return withSpecProto(
this.spec,
PlayerProto.create({
name: this.getName(),
race: this.getRace(),
class: this.getClass(),
equipment: this.getGear().asSpec(),
equipment: gear.asSpec(),
consumes: this.getConsumes(),
bonusStats: this.getBonusStats().asArray(),
buffs: this.getBuffs(),
Expand All @@ -812,6 +814,7 @@ export class Player<SpecType extends Spec> {
inFrontOfTarget: this.getInFrontOfTarget(),
distanceFromTarget: this.getDistanceFromTarget(),
healingModel: this.getHealingModel(),
database: forExport ? SimDatabase.create() : gear.toDatabase(),
}),
this.getRotation(),
forExport ? this.specTypeFunctions.talentsCreate() : this.getTalents(),
Expand Down
27 changes: 26 additions & 1 deletion ui/core/proto_utils/gear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { GemColor } from '../proto/common.js';
import { ItemSlot } from '../proto/common.js';
import { ItemSpec } from '../proto/common.js';
import { Profession } from '../proto/common.js';
import { SimDatabase } from '../proto/common.js';
import { SimItem } from '../proto/common.js';
import { SimEnchant } from '../proto/common.js';
import { SimGem } from '../proto/common.js';
import { WeaponType } from '../proto/common.js';
import { equalsOrBothNull } from '../utils.js';
import { getEnumValues } from '../utils.js';
import { distinct, getEnumValues } from '../utils.js';
import { isBluntWeaponType, isSharpWeaponType } from '../proto_utils/utils.js';
import {
UIEnchant as Enchant,
Expand Down Expand Up @@ -235,4 +239,25 @@ export class Gear {
.map(ei => ei.getFailedProfessionRequirements(professions))
.flat();
}

toDatabase(): SimDatabase {
const equippedItems = this.asArray().filter(ei => ei != null) as Array<EquippedItem>;
return SimDatabase.create({
items: distinct(equippedItems.map(ei => Gear.itemToDB(ei.item))),
enchants: distinct(equippedItems.filter(ei => ei.enchant).map(ei => Gear.enchantToDB(ei.enchant!))),
gems: distinct(equippedItems.map(ei => ei.curGems(true).map(gem => Gear.gemToDB(gem))).flat()),
});
}

private static itemToDB(item: Item): SimItem {
return SimItem.fromJson(Item.toJson(item), { ignoreUnknownFields: true });
}

private static enchantToDB(enchant: Enchant): SimEnchant {
return SimEnchant.fromJson(Enchant.toJson(enchant), { ignoreUnknownFields: true });
}

private static gemToDB(gem: Gem): SimGem {
return SimGem.fromJson(Gem.toJson(gem), { ignoreUnknownFields: true });
}
}