World record tracking

This commit is contained in:
Jonathan Barrow 2021-09-30 08:13:27 -04:00
parent 91c2e53115
commit 25734935e6
5 changed files with 94 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import (
"log"
"time"
"github.com/PretendoNetwork/nex-go"
"github.com/gocql/gocql"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
@ -59,9 +60,11 @@ func connectCassandra() {
creation_date bigint,
update_date bigint,
stars int,
world_record_first_pid int,
world_record_pid int,
world_record_creation_date bigint,
world_record_update_date bigint,
world_record int,
attempts int,
failures int,
completions int,
@ -69,7 +72,7 @@ func connectCassandra() {
flag int,
extra_data list<text>,
data_type smallint,
period smallint,
period smallint
)`).Exec(); err != nil {
log.Fatal(err)
}
@ -290,6 +293,42 @@ func insertBufferQueueData(dataID uint64, slot uint32, buffer []byte) {
}
}
func getCourseWorldRecord(dataID uint64) *CourseWorldRecord {
var worldRecordFirstPID uint32
var worldRecordPID uint32
var worldRecordCreatedTime uint64
var worldRecordUpdatedTime uint64
var worldRecord int32
_ = cassandraClusterSession.Query(`SELECT world_record_first_pid, world_record_pid, world_record_creation_date, world_record_update_date, world_record FROM pretendo_smm.courses WHERE data_id=?`, dataID).Scan(&worldRecordFirstPID, &worldRecordPID, &worldRecordCreatedTime, &worldRecordUpdatedTime, &worldRecord)
if worldRecordFirstPID == 0 {
return nil
}
return &CourseWorldRecord{
FirstPID: worldRecordFirstPID,
BestPID: worldRecordPID,
CreatedTime: nex.NewDateTime(worldRecordCreatedTime),
UpdatedTime: nex.NewDateTime(worldRecordUpdatedTime),
Score: worldRecord,
}
}
func updateCourseWorldRecord(courseID uint64, ownerPID uint32, score int32) {
now := uint64(time.Now().Unix())
if getCourseWorldRecord(courseID) == nil {
if err := cassandraClusterSession.Query(`UPDATE pretendo_smm.courses SET world_record_first_pid=?, world_record_creation_date=? WHERE data_id=?`, ownerPID, now, courseID).Exec(); err != nil {
log.Fatal(err)
}
}
if err := cassandraClusterSession.Query(`UPDATE pretendo_smm.courses SET world_record_pid=?, world_record_update_date=?, world_record=? WHERE data_id=?`, ownerPID, now, score, courseID).Exec(); err != nil {
log.Fatal(err)
}
}
//////////////////////////////
// //
// MongoDB database methods //

View File

@ -6,35 +6,29 @@ import (
)
func getCourseRecord(err error, client *nex.Client, callID uint32, param *nexproto.DataStoreGetCourseRecordParam) {
// TODO complete this
// Hard coded to always say the course creator holds the world record
worldRecord := getCourseWorldRecord(param.DataID)
/*
courseMetadata := getCourseMetadataByDataID(param.DataID)
rmcResponse := nex.NewRMCResponse(nexproto.DataStoreSMMProtocolID, callID)
if worldRecord == nil {
rmcResponse.SetError(0x00690004)
} else {
result := nexproto.NewDataStoreGetCourseRecordResult()
result.DataID = param.DataID
result.Slot = 0
result.FirstPID = courseMetadata.OwnerPID
result.BestPID = courseMetadata.OwnerPID
result.BestScore = 0
result.CreatedTime = nex.NewDateTime(0)
result.UpdatedTime = nex.NewDateTime(0)
result.Slot = param.Slot
result.FirstPID = worldRecord.FirstPID
result.BestPID = worldRecord.BestPID
result.BestScore = worldRecord.Score
result.CreatedTime = worldRecord.CreatedTime
result.UpdatedTime = worldRecord.UpdatedTime
rmcResponseStream := nex.NewStreamOut(nexServer)
rmcResponseStream.WriteStructure(result)
rmcResponseBody := rmcResponseStream.Bytes()
rmcResponse := nex.NewRMCResponse(nexproto.DataStoreSMMProtocolID, callID)
rmcResponse.SetSuccess(nexproto.DataStoreSMMMethodGetCourseRecord, rmcResponseBody)
rmcResponseBytes := rmcResponse.Bytes()
*/
rmcResponse := nex.NewRMCResponse(nexproto.DataStoreSMMProtocolID, callID)
rmcResponse.SetError(0x00690004)
}
rmcResponseBytes := rmcResponse.Bytes()

View File

@ -77,6 +77,7 @@ func main() {
dataStoreSmmServer.FollowingsLatestCourseSearchObject(followingsLatestCourseSearchObject)
dataStoreSmmServer.RecommendedCourseSearchObject(recommendedCourseSearchObject)
dataStoreSmmServer.SuggestedCourseSearchObject(suggestedCourseSearchObject)
dataStoreSmmServer.UploadCourseRecord(uploadCourseRecord)
dataStoreSmmServer.GetCourseRecord(getCourseRecord)
dataStoreSmmServer.GetApplicationConfigString(getApplicationConfigString)
dataStoreSmmServer.GetMetasWithCourseRecord(getMetasWithCourseRecord)

View File

@ -1,5 +1,7 @@
package main
import "github.com/PretendoNetwork/nex-go"
type CourseMetadata struct {
Stars uint32
DataID uint64
@ -11,3 +13,11 @@ type CourseMetadata struct {
DataType uint16
Period uint16
}
type CourseWorldRecord struct {
FirstPID uint32
BestPID uint32
CreatedTime *nex.DateTime
UpdatedTime *nex.DateTime
Score int32
}

31
upload_course_record.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
nex "github.com/PretendoNetwork/nex-go"
nexproto "github.com/PretendoNetwork/nex-protocols-go"
)
func uploadCourseRecord(err error, client *nex.Client, callID uint32, param *nexproto.DataStoreUploadCourseRecordParam) {
// STUBBED
// TODO complete this
updateCourseWorldRecord(param.DataID, client.PID(), param.Score)
rmcResponse := nex.NewRMCResponse(nexproto.DataStoreProtocolID, callID)
rmcResponse.SetSuccess(nexproto.DataStoreSMMMethodUploadCourseRecord, nil)
rmcResponseBytes := rmcResponse.Bytes()
responsePacket, _ := nex.NewPacketV1(client, nil)
responsePacket.SetVersion(1)
responsePacket.SetSource(0xA1)
responsePacket.SetDestination(0xAF)
responsePacket.SetType(nex.DataPacket)
responsePacket.SetPayload(rmcResponseBytes)
responsePacket.AddFlag(nex.FlagNeedsAck)
responsePacket.AddFlag(nex.FlagReliable)
nexServer.Send(responsePacket)
}