当前位置:网站首页>睡前故事|用Bitmap与AST做一个配置化时长系统
睡前故事|用Bitmap与AST做一个配置化时长系统
2022-08-10 20:28:00 【用户9959267】
“本文通过编故事的方式讲解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,有兴趣的读者可以看看:
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)))
}
边栏推荐
- 赎金信问题答记
- 什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
- OPPO Enco X2 迎来秋季产品升级 旗舰体验全面拉满
- Rider调试ASP.NET Core时报thread not gc-safe的解决方法
- 转铁蛋白修饰长春新碱-粉防己碱脂质体|转铁蛋白修饰共载紫杉醇和金雀异黄素脂质体(试剂)
- (10) Sequence and deserialization of image data
- 深度学习实战教程(一):感知器
- 多线程与高并发(五)—— 源码解析 ReentrantLock
- 铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
- Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
猜你喜欢
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
leetcode:45. 跳跃游戏II
C语言详解系列——关于调试那些事
idea插件 协议 。。 公司申请软件用
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
YOLOv3 SPP源码分析
一次由groovy引起的fullGC问题排查
[email protected] nanomimetic e"/>
Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e
leetcode 547.省份数量 并查集
图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳
随机推荐
C语言写数据库
电脑重装系统Win11格式化硬盘的详细方法
电脑为什么会蓝屏的原因
铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
mysql服务器参数设置
laya打包发布apk
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
leetcode:45. 跳跃游戏II
关于 NFT 版权保护的争议
The most complete GIS related software in history (CAD, FME, ArcGIS, ArcGISPro)
MySQL查询数据库中的表和字段
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
优雅退出在Golang中的实现
[SWPUCTF 2021 新生赛] web
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
【一致性hash】负载均衡器分发请求
转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体
Apple Font Lookup
“蔚来杯“2022牛客暑期多校训练营7 F