From 25734935e6135de2f34c8e4bddeefdecb81089a8 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Thu, 30 Sep 2021 08:13:27 -0400 Subject: [PATCH] World record tracking --- database.go | 41 ++++++++++++++++++++++++++++++++++++++++- get_course_record.go | 30 ++++++++++++------------------ main.go | 1 + types.go | 10 ++++++++++ upload_course_record.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 upload_course_record.go diff --git a/database.go b/database.go index 2ad8bb9..f3b90ae 100644 --- a/database.go +++ b/database.go @@ -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, 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 // diff --git a/get_course_record.go b/get_course_record.go index dde3557..246246d 100644 --- a/get_course_record.go +++ b/get_course_record.go @@ -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() diff --git a/main.go b/main.go index 98274f0..6da045a 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/types.go b/types.go index 4c9622a..164bd3a 100644 --- a/types.go +++ b/types.go @@ -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 +} diff --git a/upload_course_record.go b/upload_course_record.go new file mode 100644 index 0000000..43e7440 --- /dev/null +++ b/upload_course_record.go @@ -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) +}