当前位置:网站首页>Bedtime story | made a Bitmap and AST length system configuration

Bedtime story | made a Bitmap and AST length system configuration

2022-08-10 21:14:00 The user 9959267

“This article is explained by way of storytellingbitmap&AST的一个应用场景.This story takes place in an online game company,Main characters 左边:导师,名字不重要 右边:实习生,三多

Sanduo works as an intern in a game company,He was paddling one afternoon,Was called by the instructor:

Sanduo cursed in his heart,But the thought of myself having to pay rent again next month,Still full of tears, I opened the requirements document,The general meaning of the requirements document is as follows:

Sanduo began to brush his hair at the same time,Design the system on one side,干到晚上12点,Developed the first version of the system architecture diagram:

其中DB表的schema为:

字段名

类型

含义

id

bigint

自增主键

user_id

bigint

用户id

stat_date

date

统计日期

online_duration

int

The total amount of time the user has been online

fight_duration

int

The total time of the user fighting monsters

pet_duration

int

Total time spent with pets

deal_duration

int

Total transaction time

mining_duration

int

Total mining time

fight_hour_0

int

0-1The total time to hit monsters

fight_hour_1

int

1-2The total time to hit monsters

...

fight_hour_23

int

23-24The total time to hit monsters

pet_hour_0

int

0-1Total time spent on pets

pet_hour_1

int

1-2Total time spent on pets

...

pet_hour_23

Int

23-24点1-2Total time spent on pets

...

Other duration information is similar,忽略

So Sanduo showed the design to the instructor the next day:

So Sanduo redesigned the technical solution:

After Sanduo designed the technical scheme,就开始写代码,然后项目上线,一切顺利,直到几天后...

Sanduo's tutor just picked up the guy and drew a picture and then threw it to Sanduo for him to realize:


So Sanduo realized this time-length system,Because the system capability is very flexible,老板让PMSome very complex duration expressions are configured,It was given a friendly title by the players:monkey company~

The following code is aAST的最小demo,有兴趣的读者可以看看:

package main

import (
 "fmt"
 "reflect"
 "strconv"
 "strings"
)

const (
 Number   = 0
 Operator = 1
)

type Node struct {
 Type  int
 Value string
 Left  *Node
 Right *Node
}

// input: 1 + 4 - 2
// result:
//     -
//        /   \
//     +  2
//      /   \
//    1  4
func getAst(expr string) *Node {

 operator := make(map[string]int)
 operator["+"] = Operator
 operator["-"] = Operator

 nodeList := make([]Node, 0)
 var root *Node

 expr = strings.Trim(expr, " ")
 words := strings.Split(expr, " ")
 for _, word := range words {

  var node Node

  if _, ok := operator[word]; ok {
   node.Type = Operator
  } else {
   node.Type = Number
  }
  node.Value = word
  nodeList = append(nodeList, node)
 }

 for i := 0; i < len(nodeList); i++ {
  if root == nil {
   root = &nodeList[i]
   continue
  }

  switch nodeList[i].Type {
  case Operator:
   nodeList[i].Left = root
   root = &nodeList[i]
  case Number:
   root.Right = &nodeList[i]
  }
 }

 return root
}

func getResult(node *Node) string {
 switch node.Type {
 case Number:
  return node.Value
 case Operator:
  return calc(getResult(node.Left), getResult(node.Right), node.Value)
 }
 return ""
}

func calc(left, right string, operator string) string {
 leftVal, _ := TransToInt(left)
 rightVal, _ := TransToInt(right)
 val := 0
 switch operator {
 case "+":
  val = leftVal + rightVal
 case "-":
  val = leftVal - rightVal
 }
 return TransToString(val)
}


func main() {
 expr := `1 + 4 - 2 + 100 - 20 + 12 `
 //expr := ` 1 + 4 `

 ast := getAst(expr)
 result := getResult(ast)

 fmt.Println(result)
}

func TransToString(data interface{}) (res string) {
 val := reflect.ValueOf(data)
 return strconv.FormatInt(val.Int(), 10)
}

func TransToInt(data interface{}) (res int, err error) {
 return strconv.Atoi(strings.TrimSpace(data.(string)))
}

原网站

版权声明
本文为[The user 9959267]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102028264370.html