-
Notifications
You must be signed in to change notification settings - Fork 0
/
it_history.ttslua
95 lines (84 loc) · 2.69 KB
/
it_history.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
indicatorGUID = "15a702"
function onLoad()
indicator_block = getObjectFromGUID(indicatorGUID)
THING_HISTORY = {"Black"}
self.setName("Thing history")
end
function onUpdate()
players = Player.getPlayers()
player_with_joker = get_player_with_joker(players)
last_recorded_thing = get_last_element_of_indexed_table(THING_HISTORY)
if not player_has_been_thing_before(player_with_joker) then
printToAll(string.format("%s ~= %s!", player_with_joker, last_recorded_thing))
table.insert(THING_HISTORY, player_with_joker)
end
indicator_block.setColorTint(get_last_element_of_indexed_table(THING_HISTORY))
self.setDescription(format_thing_history(THING_HISTORY))
end
-- auxiliary functions
function format_thing_history(table)
-- consumes the thing-history table and produces
-- a formatted string representing it.
str = ""
for _, el in ipairs(table) do
str = str .. el .. "\n"
end
return str
end
function player_has_joker(player)
-- consumes a player and produces true if that
-- player has the Joker, false otherwise.
-- assumes that each player has only 1 Hand-zone
if player.getHandCount() < 1 then
return false
elseif player.getHandCount() > 1 then
printToAll("hands are misconfigured: ".. tostring(Player) .. " has 2")
return false
else
objects = player.getHandObjects(1)
return table_contains_joker(objects)
end
end
function table_contains_joker(table)
-- consumes a table and produces true if that table
-- contains the joker as a member, false otherwise.
if #table < 1 then
return false
end
for _, object in ipairs(table) do
if Global.call("is_a_joker", object) then
return true
end
end
return false
end
function get_player_with_joker(players)
-- consumes a table of players and produces the first player that
-- holds a joker.
for _, player in ipairs(players) do
if player_has_joker(player) then
return player.color
end
end
return "Black"
end
function get_last_element_of_indexed_table(table)
-- consumes a table and produces the table's last element
return table[#table]
end
function get_last_recorded_thing()
-- consumes nothing and produces a string representing the color of the
-- last recorded thing.
return get_last_element_of_indexed_table(THING_HISTORY)
end
function player_has_been_thing_before(color)
-- consumes a Player and produces true if the player's been a Thing before,
-- false otherwise.
for _, playerColor in ipairs(THING_HISTORY) do
if playerColor == color then
return true
end
end
return false
end
--