-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
92 lines (71 loc) · 2.89 KB
/
main.lua
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
if not Roll then
Roll = require('custom.dreamDice.roll')
end
local lastRolls = {}
Roll:addSkills {
lb = 'Longblade',
sb = 'Shortblade',
}
local Format = string.format
local RegisterCommand = customCommandHooks.registerCommand
local SendSystemMessage = tes3mp.SendMessage
local Traceback = debug.traceback
local Players = Players
local Roll = Roll
local LoadedCells = LoadedCells
local darkMagenta = color.DarkMagenta
local green = color.Green
local red = color.Red
local yellow = color.Yellow
local InvalidRollCommandMessage = Format('%sInvalid roll command.\n%sExample: %s/roll %s2d2-4\n'
, red, darkMagenta, green, yellow)
local NoPreviousRollMessage = 'does not have a previous roll to retry!'
local LoadedCellAssertMessage = 'A player is in this cell, why wouldn\'t it be loaded?'
local NonStringCellDescriptionAssertMessage = 'Cannot send a message to a non-string cell description!'
local UnloggedPlayerRerollAssertMessage = 'Player must be logged in to reroll!'
local function sendMessageToVisitors(cellDescription, message)
local cell = LoadedCells[cellDescription]
assert(cell, Format("%s%s", LoadedCellAssertMessage, Traceback(3)))
local cellVisitors = cell.visitors
for _, visitorPid in ipairs(cellVisitors) do
SendSystemMessage(visitorPid, message, false)
end
end
local function sendMessage(message, sendToAllOrCellDesc, playerId)
if sendToAllOrCellDesc == true then
SendSystemMessage(playerId, message, true)
else
assert(type(sendToAllOrCellDesc) == 'string', NonStringCellDescriptionAssertMessage)
sendMessageToVisitors(sendToAllOrCellDesc, message)
end
end
local function roll(pid, cmd, sendToAll)
local player = Players[pid]
local rollAttempt = cmd[2]
if not player and player:IsLoggedIn() then return end
local rollMessage = InvalidRollCommandMessage
if rollAttempt then
local statName, statRoll = Roll:fromStatId(pid, rollAttempt)
local rollInput = statRoll or rollAttempt
---@type RollObject
local currentRoll = Roll(rollInput)
currentRoll:log(pid)
lastRolls[pid] = currentRoll
rollMessage = currentRoll:getResultMessage {
playerId = pid,
forStat = statName,
}
end
sendMessage(rollMessage, sendToAll or player.data.location.cell, pid)
end
local function reroll(pid, sendToAll)
local player = Players[pid]
assert(player and player:IsLoggedIn(), UnloggedPlayerRerollAssertMessage)
local message = lastRolls[pid] and lastRolls[pid]:getResultMessage { playerId = pid }
or Format("%s %s", Players[pid].accountName, NoPreviousRollMessage)
sendMessage(message, sendToAll or player.data.location.cell, pid)
end
RegisterCommand("roll", function(pid, cmd) roll(pid, cmd, false) end)
RegisterCommand("rollg", function(pid, cmd) roll(pid, cmd, true) end)
RegisterCommand("reroll", function(pid) reroll(pid, false) end)
RegisterCommand("rerollg", function(pid) reroll(pid, true) end)