基于Cloud Firestore实现欧洲杯赛事预测占比统计的技术咨询
问题:欧洲杯预测应用的Firestore设计与统计优化
我是编程新手,正在开发一款模拟欧洲杯赛事的移动应用,用户需要逐个完成小组赛的比赛结果预测(共6个小组,每组4支球队,总计6场比赛)。我想用Cloud Firestore实现数据库功能,在用户提交某场比赛的预测结果后,展示其他用户对该场比赛的预测占比(即多少%的用户预测A队胜、B队胜或平局)。
我在ViewModel里写了存储预测结果的函数:
private val firestoreRef = FirebaseFirestore.getInstance() fun storePrediction( group: String, matchIndex: Int, prediction: EuroMatchResult ) { val matchRef = firestoreRef.collection("predictions").document(group).collection("matches").document("match$matchIndex") matchRef.set(prediction) }
现在有几个疑问:
- Firestore的集合与文档结构设计是否合理?
- 应该存储完整比赛结果,还是仅存储代表预测类型的数值(比如A队胜存1、B队胜存2、平局存3),再通过统计这些数值占比来计算结果?
- 我之前想过一种展示百分比的实现,但不太认可:
predictions.collect {prediction -> val total = prediction.size.toFloat() val teamAPercentage = (prediction.count {it.scoreA > it.scoreB} / total) * 100 val teamBPercentage = (prediction.count {it.scoreA < it.scoreB} / total) * 100 val drawPercentage = (prediction.count {it.scoreA == it.scoreB} / total) * 100 }
希望得到技术帮助。
内容的提问来源于stack exchange,提问作者john2115
相关产品推荐
相关产品推荐

