当前位置:网站首页>睡前故事|用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)))
}
边栏推荐
- [SemiDrive source code analysis] [MailBox inter-core communication] 52 - DCF Notify implementation principle analysis and code combat
- 电脑开不了机是什么原因?
- The most complete GIS related software in history (CAD, FME, ArcGIS, ArcGISPro)
- 铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
- 苹果字体查找
- Auto.js中APP应用相关指令
- 【毕业设计】基于Stm32的智能疫情防控门禁系统 - 单片机 嵌入式 物联网
- 组合导航精度分析
- 铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
- 不止跑路,拯救误操作rm -rf /*的小伙儿
猜你喜欢

leetcode 547.省份数量 并查集

Colocate Join :ClickHouse的一种高性能分布式join查询模型
[email protected] NPs)"/>转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)

Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes

【语义分割】2015-UNet MICCAI

Linux服务器安装Redis,详细步骤。

Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)

OPPO Enco X2 迎来秋季产品升级 旗舰体验全面拉满

leetcode:45. 跳跃游戏II

姜还是老的辣,看看老战哥的老底儿和严谨劲儿
随机推荐
电信保温杯笔记——《统计学习方法(第二版)——李航》第17章 潜在语义分析
"POJ 3666" Making the Grade problem solution (two methods)
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
"Distributed Microservice E-commerce" Topic (1) - Project Introduction
不止跑路,拯救误操作rm -rf /*的小伙儿
leetcode 84.柱状图中最大的矩形 单调栈应用
【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
C语言详解系列——关于调试那些事
铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
(十二)STM32——NVIC中断优先级管理
OPPO Enco X2 迎来秋季产品升级 旗舰体验全面拉满
转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体
三子棋的设计和代码
爬虫基本原理介绍、实现以及问题解决
哈工大软件构造Lab3(2022)
cordova 安装错误 Command failed: powershell 解决方法
金鱼哥RHCA回忆录:CL210OpenStack操作的故障排除--章节实验
Linux服务器安装Redis,详细步骤。
验证码倒计时自定义hooks
servlet映射路径匹配解析