Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / unblockjam / level_tools / src / hash.go
hash.go60 lines · 933 BRaw
| 1 | package rush_conv |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "sort" |
| 9 | ) |
| 10 | |
| 11 | const ID_VERSION = 1 |
| 12 | |
| 13 | func BoardID(board Board) string { |
| 14 | blocks := slices.Clone(board.Blocks) |
| 15 | |
| 16 | sort.Slice(blocks, func(i, j int) bool { |
| 17 | a, b := blocks[i], blocks[j] |
| 18 | |
| 19 | if a.X != b.X { |
| 20 | return a.X < b.X |
| 21 | } |
| 22 | if a.Y != b.Y { |
| 23 | return a.Y < b.Y |
| 24 | } |
| 25 | if a.Width != b.Width { |
| 26 | return a.Width < b.Width |
| 27 | } |
| 28 | if a.Height != b.Height { |
| 29 | return a.Height < b.Height |
| 30 | } |
| 31 | return !a.Fixed && b.Fixed |
| 32 | }) |
| 33 | |
| 34 | h := sha256.New() |
| 35 | |
| 36 | h.Write([]byte{ |
| 37 | byte(board.Width), |
| 38 | byte(board.Height), |
| 39 | byte(board.Exit.X), |
| 40 | byte(board.Exit.Y), |
| 41 | }) |
| 42 | |
| 43 | for _, b := range blocks { |
| 44 | h.Write([]byte{ |
| 45 | byte(b.X), |
| 46 | byte(b.Y), |
| 47 | byte(b.Width), |
| 48 | byte(b.Height), |
| 49 | }) |
| 50 | |
| 51 | if b.Fixed { |
| 52 | h.Write([]byte{1}) |
| 53 | } else { |
| 54 | h.Write([]byte{0}) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | sum := h.Sum(nil) |
| 59 | return fmt.Sprintf("%d-%s", ID_VERSION, base64.RawURLEncoding.EncodeToString(sum[:12])) |
| 60 | } |