Skip to content

Instantly share code, notes, and snippets.

@littlekfc
Last active August 28, 2018 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save littlekfc/eabe491fb0c56ebbffbed9262896cda4 to your computer and use it in GitHub Desktop.
Save littlekfc/eabe491fb0c56ebbffbed9262896cda4 to your computer and use it in GitHub Desktop.
knight haoyangmao
#include <eosiolib/currency.hpp>
#include <eosiolib/transaction.hpp>
#include <math.h>
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>
#include <eosiolib/types.hpp>
#include <string>
#pragma once
// Linear Congruential Generator
class random_gen {
public:
const uint32_t a = 1103515245;
const uint32_t c = 12345;
uint64_t seed = 0;
random_gen(account_name player) {
if (seed == 0) {
seed = tapos_block_prefix() + player;
}
}
uint32_t range(uint32_t to) {
seed = (a * seed + c) % 0x7fffffff;
return (uint32_t)(seed % to);
}
};
using eosio::permission_level;
using eosio::action;
class haoyangmao: public eosio::contract
{
public:
haoyangmao(account_name self)
: contract(self)
{
}
void trypetgacha() {
require_auth(_self);
random_gen random = random_gen(_self);
eosio::print(random.seed, '\n');
const uint32_t sum = 28960;
int pos = random.range(sum);
eosio::print(pos);
if (pos >= 28800 && pos < 28960) {
eosio::print("got it");
action(
permission_level{_self, N(active)},
N(eosknightsio), N(petgacha),
std::make_tuple(_self, (uint16_t)2, (uint8_t)1))
.send();
} else {
eosio_assert(false, std::to_string(pos).c_str());
}
}
};
EOSIO_ABI(haoyangmao, (trypetgacha))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment