statistics.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package gamelogic
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. "bet24.com/servers/games/luckyfruit_table/common"
  8. excelize "github.com/xuri/excelize/v2"
  9. )
  10. var cacheBuffer [][]string
  11. var excelFile *excelize.File
  12. type betAreaInfo struct {
  13. betArea int
  14. betAmount int
  15. }
  16. type Statistics struct {
  17. betAmount int
  18. winAmount int
  19. betArea []betAreaInfo
  20. winArea common.Fruit
  21. winOdd float64
  22. waterpool int
  23. }
  24. type StatisticsList struct {
  25. lists []Statistics
  26. lastLists []Statistics
  27. tableId int
  28. }
  29. func (sl *StatisticsList) initData(tableId int) {
  30. sl.lists = []Statistics{}
  31. sl.lastLists = []Statistics{}
  32. sl.tableId = tableId
  33. go sl.updateValue()
  34. }
  35. func (sl *StatisticsList) updateValue() {
  36. for {
  37. c, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  38. select {
  39. case <-c.Done():
  40. sl.lastLists = []Statistics{}
  41. sl.lastLists = append(sl.lastLists, sl.lists...)
  42. sl.lists = []Statistics{}
  43. sl.storeToExcel()
  44. sl.lastLists = []Statistics{}
  45. cancel()
  46. }
  47. }
  48. }
  49. func (sl *StatisticsList) storeToExcel() {
  50. if len(sl.lastLists) == 0 {
  51. return
  52. }
  53. for n := 0; n < len(sl.lastLists); n++ {
  54. str := []string{
  55. fmt.Sprintf("%d", sl.lastLists[n].betAmount),
  56. fmt.Sprintf("%d", sl.lastLists[n].winAmount),
  57. fmt.Sprintf("%v", sl.lastLists[n].betArea),
  58. fmt.Sprintf("%v", sl.lastLists[n].winArea),
  59. fmt.Sprintf("%v", sl.lastLists[n].winOdd),
  60. fmt.Sprintf("%d", sl.lastLists[n].waterpool),
  61. }
  62. cacheBuffer = append(cacheBuffer, str)
  63. }
  64. sl.saveFile()
  65. }
  66. func (sl *StatisticsList) saveFile() {
  67. var err error
  68. sheetName := "Sheet1"
  69. dirName := "./"
  70. fileName := fmt.Sprintf("luckfruit_table%d.xlsx", sl.tableId)
  71. excelFile = excelize.NewFile()
  72. excelFile.NewSheet(sheetName)
  73. // 设置列宽
  74. excelFile.SetColWidth(sheetName, "A", "A", 20)
  75. excelFile.SetColWidth(sheetName, "B", "B", 20)
  76. excelFile.SetColWidth(sheetName, "C", "C", 80)
  77. excelFile.SetColWidth(sheetName, "D", "D", 20)
  78. excelFile.SetColWidth(sheetName, "E", "E", 20)
  79. excelFile.SetColWidth(sheetName, "F", "F", 20)
  80. style, _ := excelFile.NewStyle(&excelize.Style{
  81. Alignment: &excelize.Alignment{
  82. Horizontal: "center",
  83. Vertical: "center",
  84. },
  85. })
  86. excelFile.SetCellStyle(sheetName, "A1", "F1", style)
  87. // 设置工作表的单元格值
  88. excelFile.SetCellValue(sheetName, "A1", "下注金额")
  89. excelFile.SetCellValue(sheetName, "B1", "盈利金额")
  90. excelFile.SetCellValue(sheetName, "C1", "投注区域")
  91. excelFile.SetCellValue(sheetName, "D1", "中奖水果")
  92. excelFile.SetCellValue(sheetName, "E1", "中奖倍数")
  93. excelFile.SetCellValue(sheetName, "F1", "当前水池")
  94. // 获取已有数据的行数
  95. rows, err := excelFile.GetRows(sheetName)
  96. if err != nil {
  97. return
  98. }
  99. rowIndex := len(rows)
  100. for _, row := range cacheBuffer {
  101. for colIndex, col := range row {
  102. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  103. excelFile.SetCellValue(sheetName, cellName, col)
  104. excelFile.SetCellStyle(sheetName, cellName, cellName, style)
  105. }
  106. rowIndex++
  107. }
  108. excelFile.SaveAs(filepath.Join(dirName, fileName))
  109. }