Choose the correct item based on luck Lua - list

Lets say I have this table.
Config.LootboxesRewards = {
[1] = {
{name = 'a45amg', label = 'Mercedes A45 AMG ', amount = 1, type = 'car', luck = 3},
{name = '720s', label = '720s mclaren', amount = 1, type = 'car', luck = 20},
{name = 'bac2', label = 'bac2', amount = 1, type = 'car', luck = 20},
{name = 'm6prior', label = 'BMW M6', amount = 1, type = 'car', luck = 19},
{name = 'huracan', label = 'Lamborghini Huracan', amount = 1, type = 'car', luck = 19},
{name = 'yzfr6', label = 'Yamaha R6', amount = 1, type = 'car', luck = 19},
},
}
Based on that I would like to give the the player 1 item based on luck value on that table.
What is the best way to do it?

The simplest way is to index Config.LootboxesRewards[1] with math.random(#Config.LootboxesRewards[1])
This assumes that you just want to give your player a random item with uniform distribution. If you want to vary the chanc of getting particular items I suggest you start here:
https://en.wikipedia.org/wiki/Probability
Read:
https://www.lua.org/manual/5.4/manual.html#pdf-math.random
https://www.lua.org/manual/5.4/manual.html#pdf-math.randomseed

One simple solution is to put the values with a higher chance (higher luck value) more often in the loot-table than the items with a lower change (lower luck value).
You can still keep your table for convenience and pre-process the table like this:
local function gcd(a, b)
while a ~= b do
if a > b then
a = a - b
else
b = b - a
end
end
return a
end
local function gcd_tbl(values, value_getter)
if #values < 1 then
return nil
end
value_getter = value_getter or function(v) return v end
local result = value_getter(values[1])
for i = 2, #values do
result = gcd(result, value_getter(values[i]))
end
return result
end
local function process_rewards(tbl)
local result = {}
for id, rewards in pairs(tbl) do
result[id] = {}
local greatest_common_divisor = gcd_tbl(rewards, function(v) return v.luck end)
for _, reward in ipairs(rewards) do
for i = 1, reward.luck / greatest_common_divisor do
table.insert(result[id], reward)
end
end
end
return result
end
Config.LootboxesRewards = process_rewards({
[1] = {
{name = 'a45amg', label = 'Mercedes A45 AMG ', amount = 1, type = 'car', luck = 3},
{name = '720s', label = '720s mclaren', amount = 1, type = 'car', luck = 20},
{name = 'bac2', label = 'bac2', amount = 1, type = 'car', luck = 20},
{name = 'm6prior', label = 'BMW M6', amount = 1, type = 'car', luck = 19},
{name = 'huracan', label = 'Lamborghini Huracan', amount = 1, type = 'car', luck = 19},
{name = 'yzfr6', label = 'Yamaha R6', amount = 1, type = 'car', luck = 19},
}
})
You can then choose a random index from the table to find the reward:
function get_random_reward(lootbox_id)
local lootbox_rewards = Config.LootboxesRewards[lootbox_id]
if not lootbox_rewards then
return nil
end
return lootbox_rewards[math.random(#lootbox_rewards)]
end
get_random_reward(1)
EDIT:
If you wish to indicate the other way around (higher luck = less chance to drop), you could chance these 2 functions:
local function gcd_and_max_tbl(values, value_getter)
if #values < 1 then
return nil, nil
end
value_getter = value_getter or function(v) return v end
local value = value_getter(values[1])
local gcd_result, max_result = value, value
for i = 2, #values do
value = value_getter(values[i])
gcd_result = gcd(gcd_result, value)
max_result = math.max(max_result, value)
end
return gcd_result, max_result
end
local function process_rewards(tbl)
local result = {}
for id, rewards in pairs(tbl) do
result[id] = {}
local greatest_common_divisor, max_luck = gcd_and_max_tbl(rewards, function(v) return v.luck end)
local max_relevant_luck = max_luck / greatest_common_divisor
for _, reward in ipairs(rewards) do
for i = 1, max_relevant_luck - (reward.luck / greatest_common_divisor) + 1 do
table.insert(result[id], reward)
end
end
end
return result
end

if luck simply is how likely it is to be picked, so that a car with luck 10 has double chance to be picked than one with 5 you could do it like this
function pickBasedOnLuck(list)
local totalLuck = 0
for i, car in ipairs(list) do
totalLuck = totalLuck + car.luck
end
local pick = math.random(totalLuck)
for i, car in ipairs(list) do
if pick > car.luck then
pick = pick - car.luck
else
return car
end
end
end
so in your case it could be called like pickBasedOnLuck(Config.LootboxesRewards[1]).

Related

How do I return a negative value based on a separate cell?

I have a button that submits information and that works. All the numbers will be positive when entered by clients. The question at hand is:
If A4 = "Sell", cells A16, A18, and A20, return those numbers as negative values instead of positive. If A4 = "Buy", then return as is (positive).
Here's what I currently have. I don't know where to input this IF statement, or even how.
function SubmitBuy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("Front Sheet"); //Data entry sheet
var dataS = ss.getSheetByName("Front Sheet"); //Data Sheet
var values = [[formS.getRange("A2").getValue(),
formS.getRange("A4").getValue(),
formS.getRange("A6").getValue(),
formS.getRange("A8").getValue(),
formS.getRange("A10").getValue(),
formS.getRange("A12").getValue(),
formS.getRange("A16").getValue(),
formS.getRange("A18").getValue(),
formS.getRange("A20").getValue(),
formS.getRange("A28").getValue(), ]];
dataS.getRange(dataS.getLastRow() +1, 3, 1, 10 ).setValues(values);
ClearCells();
}
Get all of Column A's values and manipulate them using Array.map and Set:
function SubmitBuy() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const frontSheet = ss.getSheetByName('Front Sheet'); //Data entry sheet
const valuesA1A28 = frontSheet.getRange('A1:A28').getValues();
const rowsNeeded = [2, 4, 5, 8, 10, 12, 16, 18, 20];
const criteriaRow = 4;
const rowsToModify = new Set([16, 18, 20]);
const valuesNeeded = [
rowsNeeded.map((thisRow) => {
const thisValue = valuesA1A28[thisRow - 1][0];
if (
rowsToModify.has(thisRow) &&
valuesA1A28[criteriaRow - 1][0] === 'Sell'
)
return -thisValue;
else return thisValue;
}),
];
frontSheet
.getRange(frontSheet.getLastRow() + 1, 3, 1, 10)
.setValues(valuesNeeded);
ClearCells();
}

Getting "wrong number of bind variables" when I'm trying to write a Rails finder method

I’m using Rails 4.2.3. I’m having trouble passing a variable number of search criteria to my Rails finder method. I have
user = current_user
search_criteria = ["my_objects.user_id = ?"]
search_values = [user.id]
start_date = params[:start_date]
if start_date.present?
start_date_obj = Date.strptime(start_date, "%m/%d/%Y")
search_criteria.push("my_objects.start_date >= ?")
search_values.push(start_date_obj)
else
start_date = my_object.find_user_first_my_object_date(user)
#default_start_date = start_date.to_time.strftime("%m/%d/%Y") if start_date.present?
end
end_date = params[:end_date]
if end_date.present?
end_date_obj = Date.strptime(end_date, "%m/%d/%Y")
search_criteria.push("my_objects.end_date <= ?")
search_values.push(end_date_obj)
else
end_date = my_object.find_user_last_my_object_date(user)
#default_end_date = end_date.to_time.strftime("%m/%d/%Y") if end_date.present?
end
distance = params[:distance]
if distance.present?
distance_parts = distance.split(" ")
search_criteria.push("my_objects.distance = ?")
search_criteria.push("my_objects.distance_unit_id = ?")
search_values.push("#{distance_parts[0]}")
search_values.push("#{distance_parts[1]}")
end
#user_my_objects = MyObjectTime.joins(:my_object).where(search_criteria.join(" AND "), search_values)
.order("my_objects.day")
But this results in the error
wrong number of bind variables (1 for 5) in: my_objects.user_id = ? AND my_objects.start_date >= ? AND my_objects.end_date <= ? AND my_objects.distance = ? AND my_objects.distance_unit_id = ?
I think Rails is treating my “search_values” array as a single value but I want it to pass each value of the array as an argument into the search condition. How do I fix the above?
If I've read the question right, this boils down to
values = ['a', 'b', 'c']
SomeModel.where("foo = ? AND bar = ? AND baz = ?", values)
Throwing an error about an incorrect number of bind variables. To unpack an array and use its values as individual arguments you use the splat operator:
SomeModel.where("foo = ? AND bar = ? AND baz = ?", *values)

Getting selected items from a Tkinter listbox without using a listbox bind

I have 2 listboxes (which are connected so that items can move from one to the other) and at the end I would like to get all the entries in the second listbox by using a 'Ok' button (or simply closing the frame). I could add/remove values to a list every time an item is selected (as shown in the commented section of the code below) but I would rather have a single line of code along the lines of [master.selected.get(idx) for idx in master.selected.curselection()] in the close function but I am unable to get it working.
Code:
def measurementPopup(self,master):
self.chargeCarrier = StringVar()
self.massModifiers = StringVar()
self.chargeCarrier.set("[M+xH]")
def onselect1(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could enter the selected value to a buffer list (to be returned in the ok function).
master.selected.insert(END,value)
master.avail.delete(index)
def onselect2(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could remove the selected value from a buffer list (to be returned in the ok function).
master.selected.delete(index)
master.avail.insert(END,value)
def close(self):
# Here I would return the buffer list and close the window
master.measurementWindow = 0
top.destroy()
if master.measurementWindow == 1:
return
master.measurementWindow = 1
top = self.top = Toplevel()
top.protocol( "WM_DELETE_WINDOW", lambda: close(self))
self.charge = Label(top, text = "Charge", width = 10)
self.charge.grid(row = 0, column = 0, sticky = W)
self.min = Label(top, text = "Min", width = 5)
self.min.grid(row=0, column = 1, sticky = W)
self.minCharge = Spinbox(top, from_= 1, to = 3, width = 5)
self.minCharge.grid(row = 0, column = 2, sticky = W)
self.max = Label(top, text = "Max", width = 5)
self.max.grid(row = 0, column = 3, sticky = W)
self.maxCharge = Spinbox(top, from_ = 1, to=3, width=5)
self.maxCharge.grid(row = 0, column = 4, sticky = W)
self.chargeCarrier = OptionMenu(top, self.chargeCarrier, "[M+xH]", "[M+xNa]")
self.chargeCarrier.grid(row = 0, column = 5, sticky = W)
self.availMass = Label(top, text = "Available")
self.availMass.grid(row = 1, column = 1, sticky = W)
self.selectMass = Label(top, text = "Selected")
self.selectMass.grid(row = 1, column = 3, sticky = W)
self.massMod = Label(top, text = "Mass Mods")
self.massMod.grid(row = 2, column = 0, sticky = W)
self.avail = Listbox(top)
for i in UNITS:
if BLOCKS[i]['available'] == 1:
self.avail.insert(END,BLOCKS[i]['human_readable_name'])
self.avail.grid(row = 2, column = 1, columnspan = 2, sticky = W)
self.avail.bind('<<ListboxSelect>>',onselect1)
self.selected = Listbox(top)
self.selected.grid(row = 2, column = 3, columnspan = 2, sticky = W)
self.selected.bind('<<ListboxSelect>>',onselect2)
self.ok = Button(top,text = 'Ok',command = lambda: close(self))
self.ok.grid(row = 3, column = 0, sticky = W)
I have tried to use the following small snippet in the close function:
values = [master.selected.get(idx) for idx in master.selected.curselection()]
print ', '.join(values)
However, the for segment doesn't return anything. I would expect that this is due to the fact that nothing is actually selected but that I would need something opposite, along the lines of master.selected.allitems() (if it exists and if I understand it correctly).
Summary
How would one get all the items in 1 specific listbox?
The .get() function for the Listbox widget allows you to specify a range of items, which can be specified as 0 to END to return a tuple of all the items.
Example:
from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")
def close():
global l, root
items = l.get(0, END)
print(items)
root.destroy()
b = Button(root, text = "OK", command = close).pack()
root.mainloop()
I hope this helps, if it's not what you were looking for let me know in a comment and I can try expand my answer.

set a event listener collision for all image in a list.

i have a section that randomly spawns images from a one list and spawns them on screen and despawns them continously. the images that are spawned are put into a list called object. i trying to make is so that each image that is spawned on screen has a collision detection by the user sprite. however at the moment it only detects the very first raft that is spawned.
isOnRaft = 0
--Set log position and movement
local mRandom = math.random
local raft = {"Raft1" ,"Raft2"}
local objectTag = 0
local object = {}
function spawnlogright()
objectTag = objectTag + 1
local objIdx = mRandom(#raft)
local objName = raft[objIdx]
object[objectTag] = display.newImage(objName..".png")
object[objectTag].x = 416
object[objectTag].y = 72
object[objectTag].name = objectTag
transition.to(object[objectTag], {time = 10000, x = -96, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogright()
timer.performWithDelay(3500,spawnlogright,0)
function spawnlogright()
objectTag = objectTag + 1
local objIdx = mRandom(#raft)
local objName = raft[objIdx]
object[objectTag] = display.newImage(objName..".png")
object[objectTag].x = 416
object[objectTag].y = 168
object[objectTag].name = objectTag
transition.to(object[objectTag], {time = 10000, x = -96, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogright()
timer.performWithDelay(3500,spawnlogright,0)
function spawnlogleft()
objectTag = objectTag + 1
local objIdx = mRandom(#raft)
local objName = raft[objIdx]
object[objectTag] = display.newImage(objName..".png")
object[objectTag].x = -96
object[objectTag].y = 120
object[objectTag].name = objectTag
transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogleft()
timer.performWithDelay(3500,spawnlogleft,0)
function spawnlogleft()
objectTag = objectTag + 1
local objIdx = mRandom(#raft)
local objName = raft[objIdx]
object[objectTag] = display.newImage(objName..".png")
object[objectTag].x = -96
object[objectTag].y = 216
object[objectTag].name = objectTag
transition.to(object[objectTag], {time = 10000, x = 416, onComplete = function(obj) obj:removeSelf(); obj = nil; end})
physics.addBody( object[objectTag], "static", {isSensor = true})
end
spawnlogleft()
timer.performWithDelay(3500,spawnlogleft,0)
--while the frog is on the log...
function raftCollide(event)
if ( event.phase == "began" ) then
isOnRaft = isOnRaft + 1
print(isOnLog)
elseif ( event.phase == "ended" )then
isOnRaft = isOnRaft - 1
print(isOnLog)
end
end
--add event for 'walking on the log'
object[objectTag]:addEventListener("collision", raftCollide)
so i need the user sprite to detect all of the rafts that are spawned and add 1 to isOnRaft. this will negate the water death function. is there a way to add the collision detection to all of the raft or all of the entities within the object list.
any help would be appriacaited thanks.
Just replace the last line by:
for logTag, logObject in pairs(object) do
logObject:addEventListener("collision", raftCollide)
end
Also just an advice but do what you feel like with it: Try not to declare 2 functions with the same names in the same scope... You should change the name of the second spawnRight / spawnLeft functions to have a different name :)

TclError: wrong # args error

I have no idea what is wrong but I keep getting this
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call
return self.func(*args)
File "/Users/Zane/Desktop/Factorial GUI.py", line 72, in reveal2
self.text2.insert(0.0, message)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk/Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
TclError: wrong # args: should be ".22186144.22187184 insert index chars ?tagList chars tagList ...?"
here is my code:`
from Tkinter import*
class App(Frame):
def fac(self, n):
if n >= 0:
if n == 1 or n == 0:
return 1
else:
return n*self.fac(n-1)
else:
print('Error')
def per(self, n, r):
y = (self.fac(n)) / self.fac(n - r)
print (y)
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction1 = Label(self, text = "Factorial:")
self.instruction1.grid(row = 0, column = 0, columnspan = 1, sticky = W)
self.password1 = Entry(self)
self.password1.grid(row = 0, column = 1, sticky = W)
self.submit_button1 = Button(self, text ="Enter", command = self.reveal1)
self.submit_button1.grid(row = 2, column = 0, sticky = W)
self.text1 = Text(self, width = 30, height = 1, wrap = WORD)
self.text1.grid(row = 3, column = 0, columnspan = 2, sticky = W)
self.instruction2 = Label(self, text = "Permutation:")
self.instruction2.grid(row = 4, column = 0, columnspan = 1, sticky = W)
self.password2 = Entry(self)
self.password2.grid(row = 4, column = 1, sticky = W)
self.password3 = Entry(self)
self.password3.grid(row = 6, column = 1, sticky = W)
self.submit_button2 = Button(self, text ="Enter", command = self.reveal2)
self.submit_button2.grid(row = 7, column = 0, sticky = W)
self.text2 = Text(self, width = 30, height = 1, wrap = WORD)
self.text2.grid(row = 8, column = 0, columnspan = 2, sticky = W)
def reveal1(self):
y = int(self.password1.get())
message = self.fac(y)
self.text1.delete(0.0, END)
self.text1.insert(0.0, message)
def reveal2(self):
y = int(self.password2.get())
z = int(self.password3.get())
message = self.per(y, z)
self.text2.delete(0.0, END)
self.text2.insert(0.0, message)
root = Tk()
root.title('Factorial')
root.geometry("340x300")
app = App(root)
root.mainloop()
`
Almost the only way to get the error you say you get with the code you posted, is if the insert method is called when the data to insert is None. message comes from the result of per, but per returns None because you don't explicitly return anything else.
One of the first things to try when trying to debug is to check that the data you're sending to the failing function is what you think it is. You can do this in a very low-tech way by simply printing out the values being passed to the insert message. This instantly told me that message was None. Once I learned that, it's pretty simple to answer the question "why was it None?".