前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >睡前故事|用Bitmap与AST做一个配置化时长系统

睡前故事|用Bitmap与AST做一个配置化时长系统

作者头像
薯条的编程修养
发布2022-08-10 19:31:09
3700
发布2022-08-10 19:31:09
举报
文章被收录于专栏:薯条的编程修养

“本文通过编故事的方式讲解bitmap&AST的一个应用场景。这个故事发生在一个网络游戏公司里面,主要登场人物 左边:导师,名字不重要 右边:实习生,三多

三多在一家游戏公司做实习生,一天下午他正在划水,被导师叫了过去:

三多心里骂娘,但是一想到自己下个月又得交房租了,还是满含泪水打开了需求文档,需求文档大概的意思如下:

三多开始一边薅头发,一边设计系统,干到晚上12点,搞出了第一版系统架构图:

其中DB表的schema为:

字段名

类型

含义

id

bigint

自增主键

user_id

bigint

用户id

stat_date

date

统计日期

online_duration

int

用户在线时长总计

fight_duration

int

用户打怪时长总计

pet_duration

int

携带宠物时长总计

deal_duration

int

交易时长总计

mining_duration

int

挖矿时长总计

fight_hour_0

int

0-1点打怪时长总计

fight_hour_1

int

1-2点打怪时长总计

...

fight_hour_23

int

23-24点打怪时长总计

pet_hour_0

int

0-1点养宠物时长总计

pet_hour_1

int

1-2点养宠物时长总计

...

pet_hour_23

Int

23-24点1-2点养宠物时长总计

...

其他时长信息类似,忽略

于是三多第二天给导师看设计:

于是三多重新设计了技术方案:

三多把技术方案设计好后,就开始写代码,然后项目上线,一切顺利,直到几天后...

三多导师随便抄起家伙画了个图然后丢给三多让他去实现:


于是三多实现了这套时长系统,由于系统能力非常灵活,老板让PM配置了一些非常复杂的时长表达式,被玩家们赐予了亲切的称号:耍猴公司~

下面这段代码是个AST的最小demo,有兴趣的读者可以看看:

代码语言:javascript
复制
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)))
}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 薯条的编程修养 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档