GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io"
  5. "strings"
  6. )
  7. type charInfo struct {
  8. Nicks []string `json:"nicks"`
  9. Name string `json:"name"`
  10. Author string `json:"player"`
  11. ShortName string `json:"first"`
  12. }
  13. func load(reader io.Reader) ([]charInfo, error) {
  14. data := make(map[string]interface{})
  15. err := json.NewDecoder(reader).Decode(&data)
  16. if err != nil {
  17. return nil, err
  18. }
  19. links := make(map[string]string, len(data))
  20. infos := make([]charInfo, 0, 64)
  21. for key, value := range data {
  22. if info, ok := value.(map[string]interface{}); ok {
  23. name := info["name"].(string)
  24. author := info["player"].(string)
  25. shortName, ok := info["first"].(string)
  26. if !ok {
  27. shortName = strings.SplitN(name, " ", 2)[0]
  28. }
  29. infos = append(infos, charInfo{
  30. Nicks: []string{key},
  31. Name: name,
  32. Author: author,
  33. ShortName: shortName,
  34. })
  35. } else if nick, ok := value.(string); ok {
  36. links[key] = nick
  37. }
  38. }
  39. for key, value := range links {
  40. for i := range infos {
  41. if infos[i].Nicks[0] == value {
  42. infos[i].Nicks = append(infos[i].Nicks, key)
  43. }
  44. }
  45. }
  46. return infos, nil
  47. }