This repository has been archived by the owner on Aug 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
handler.go
171 lines (156 loc) · 4.38 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package wechat_brain
import (
"bytes"
"encoding/json"
"log"
"math/rand"
"net/url"
"strconv"
"time"
)
var (
roomID string
)
func handleQuestionReq(bs []byte) {
values, _ := url.ParseQuery(string(bs))
roomID = values.Get("roomID")
}
//根据题目返回,进行答案搜索
func handleQuestionResp(bs []byte) (bsNew []byte, ansPos int) {
bsNew = bs
question := &Question{}
json.Unmarshal(bs, question)
question.CalData.RoomID = roomID
question.CalData.quizNum = strconv.Itoa(question.Data.Num)
//Get the answer from the db
answer := FetchQuestion(question)
var ret map[string]int
if answer == "" {
tx := time.Now()
ret = GetFromBaidu(question.Data.Quiz, question.Data.Options)
tx2 := time.Now()
log.Printf("Cost time %d ms\n", tx2.Sub(tx).Nanoseconds()/1e6)
}
question.CalData.TrueAnswer = answer
question.CalData.Answer = answer
SetQuestion(question)
ansPos = 0
respQuestion := &Question{}
json.Unmarshal(bs, respQuestion)
if question.CalData.TrueAnswer != "" {
for i, option := range respQuestion.Data.Options {
if option == question.CalData.TrueAnswer {
respQuestion.Data.Options[i] = option + "[标准答案]"
ansPos = i + 1
break
}
}
} else {
var max int = 0
for i, option := range respQuestion.Data.Options {
if ret[option] > 0 {
respQuestion.Data.Options[i] = option + "[" + strconv.Itoa(ret[option]) + "]"
if ret[option] > max {
max = ret[option]
ansPos = i + 1
}
}
}
}
bsNew, _ = json.Marshal(respQuestion)
var out bytes.Buffer
json.Indent(&out, bsNew, "", " ")
//log.Printf("Question answer predict => %v\n", out.String())
var answerItem string = "不知道"
if ansPos != 0 {
answerItem = respQuestion.Data.Options[ansPos-1]
} else {
//随机点击
ansPos = rand.Intn(4) + 1
}
log.Printf("Question answer predict =>\n 【题目】 %v\n 【正确答案】%v\n", respQuestion.Data.Quiz, answerItem)
//直接将答案返回在客户端,可能导致封号,所以只在服务端显示
if Mode == 0 {
//返回修改后的答案
return out.Bytes(), ansPos
} else {
//返回答案
return bs, ansPos
}
}
//hijack 提交请求
func handleChooseReq(bs []byte) (newBs []byte) {
newBs = bs
values, _ := url.ParseQuery(string(bs))
quizNum := values.Get("quizNum")
question := GetQuestion(roomID, quizNum)
if question == nil {
return
}
// var idx = -1
// for i, option := range question.Data.Options {
// if question.CalData.Answer == option {
// idx = i + 1 //TODO ?
// break
// }
// }
// if idx != -1 {
// values.Set("option", strconv.Itoa(idx))
// newBs = []byte(values.Encode())
// }
return
}
func handleChooseResponse(bs []byte) {
chooseResp := &ChooseResp{}
json.Unmarshal(bs, chooseResp)
//log.Println("response choose", roomID, chooseResp.Data.Num, string(bs))
question := GetQuestion(roomID, strconv.Itoa(chooseResp.Data.Num))
if question == nil {
log.Println("error in get question", chooseResp.Data.RoomID, chooseResp.Data.Num)
return
}
question.CalData.TrueAnswer = question.Data.Options[chooseResp.Data.Answer-1]
if chooseResp.Data.Yes {
question.CalData.TrueAnswer = question.Data.Options[chooseResp.Data.Option-1]
}
log.Printf("【保存数据】 %s, %s", question.Data.Quiz, question.CalData.TrueAnswer)
StoreQuestion(question)
}
type Question struct {
Data struct {
Quiz string `json:"quiz"`
Options []string `json:"options"`
Num int `json:"num"`
School string `json:"school"`
Type string `json:"type"`
Contributor string `json:"contributor"`
EndTime int `json:"endTime"`
CurTime int `json:"curTime"`
} `json:"data"`
Errcode int `json:"errcode"`
CalData struct {
RoomID string
quizNum string
Answer string
TrueAnswer string
} `json:"-"`
}
type ChooseResp struct {
Data struct {
UID int `json:"uid"`
Num int `json:"num"`
Answer int `json:"answer"`
Option int `json:"option"`
Yes bool `json:"yes"`
Score int `json:"score"`
TotalScore int `json:"totalScore"`
RowNum int `json:"rowNum"`
RowMult int `json:"rowMult"`
CostTime int `json:"costTime"`
RoomID int `json:"roomId"`
EnemyScore int `json:"enemyScore"`
EnemyAnswer int `json:"enemyAnswer"`
} `json:"data"`
Errcode int `json:"errcode"`
}
//roomID=476376430&quizNum=4&option=4&uid=26394007&t=1515326786076&sign=3592b9d28d045f3465206b4147ea872b