TransWikia.com

How to add current date and time in the table so we know when a game score was logged?

EOS.IO Asked by Ramesh Gopal on August 20, 2021

For a gaming leaderboard. players will be able to compete with each other through a leaderboard.

When a new score is added, it is inserted in to a table that is scoped to the game
Each game is limited to the top 10 scores, so when a new score is added, one has to be removed if there are already 10 items. This should also add the current date and time in the table so we know when a game score was logged.

One Answer

You can use eosio::current_time_point() to get time in microseconds in smart contract.

#include <eosio/system.hpp> //include system header

uint32_t now() {
   return (uint32_t) (eosio::current_time_point().sec_since_epoch());
}

Add a scoreTime field in your score table

struct [[eosio::table]] table {
   uint32_t scoreTime; //add scoreTime in your table
   .
   .
   //other table fields
};

Add score time while inserting new score in your table.

table.emplace(get_self(), [&]( auto& row ) {
   row.score = 10;
   row.scoreTime = now();
   .
   .
   //other table fields
});

Answered by Abdul Moiz on August 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP