module.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package module
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hash/crc32"
  6. )
  7. // Setting ModuleConf
  8. type Setting struct {
  9. Timestamp int64 `json:"timestamp"`
  10. CheckSum int64 `json:"check_sum"`
  11. Data json.RawMessage `json:"data"`
  12. }
  13. const (
  14. _playerID = 1
  15. _playerKey = "player"
  16. _webPlayerID = 2
  17. _webPlayerKey = "web_player"
  18. )
  19. var _moduleMap = map[string]int{
  20. _playerKey: _playerID,
  21. _webPlayerKey: _webPlayerID,
  22. }
  23. // VerifyModuleKey verify key
  24. func VerifyModuleKey(key string) int {
  25. return _moduleMap[key]
  26. }
  27. // Result get module message
  28. func Result(moduleKeyID int, data string) (rm json.RawMessage, checkSum int64, err error) {
  29. var (
  30. bs []byte
  31. )
  32. switch moduleKeyID {
  33. case _playerID:
  34. player := &Player{}
  35. playerSha1 := &PlayerSha1{}
  36. err = json.Unmarshal([]byte(data), player)
  37. if err != nil {
  38. return
  39. }
  40. bs, err = json.Marshal(player)
  41. if err != nil {
  42. return
  43. }
  44. rm = json.RawMessage(bs)
  45. // check_sum
  46. err = json.Unmarshal([]byte(data), playerSha1)
  47. if err != nil {
  48. return
  49. }
  50. bs, err = json.Marshal(playerSha1)
  51. if err != nil {
  52. return
  53. }
  54. checkSum = int64(crc32.ChecksumIEEE(bs))
  55. return
  56. case _webPlayerID:
  57. player := &WebPlayer{}
  58. playerSha1 := &WebPlayerSha1{}
  59. err = json.Unmarshal([]byte(data), player)
  60. if err != nil {
  61. return
  62. }
  63. bs, err = json.Marshal(player)
  64. if err != nil {
  65. return
  66. }
  67. rm = json.RawMessage(bs)
  68. // check_sum
  69. err = json.Unmarshal([]byte(data), playerSha1)
  70. if err != nil {
  71. return
  72. }
  73. bs, err = json.Marshal(playerSha1)
  74. if err != nil {
  75. return
  76. }
  77. checkSum = int64(crc32.ChecksumIEEE(bs))
  78. return
  79. }
  80. err = fmt.Errorf("module_key_id not found: %v", moduleKeyID)
  81. return
  82. }