Skip to content

Commit

Permalink
#4 Fixed a bug caused by the latest DCS update that caused major fram…
Browse files Browse the repository at this point in the history
…e drops

Fixed random colors again
Fixed viewer count
  • Loading branch information
fdsprod committed Dec 13, 2016
1 parent 8300404 commit 9738915
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
41 changes: 35 additions & 6 deletions lua/Scripts/Twitch2DCSGameGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local string = base.string
local math = base.math
local assert = base.assert
local pairs = base.pairs
local ipairs = base.ipairs

local lfs = require('lfs')
local net = require('net')
Expand All @@ -31,7 +32,7 @@ local TwitchClient = {
joinPartSkin = nil,
userSkins = {},
nextUserIndex = 1,
viewerCount = 0
userNames = {}
}
local TwitchClient_mt = { __index = TwitchClient }
local client = nil
Expand Down Expand Up @@ -72,7 +73,7 @@ function TwitchClient:getSkinForUser(user)

self.nextUserIndex = self.nextUserIndex + 1

if self.nextUserIndex > table.getn(self.config.skins) then
if self.nextUserIndex > #self.config.skins.messageColors then
self.nextUserIndex = 1
end

Expand All @@ -87,6 +88,30 @@ function TwitchClient:canLogin()
(self.config.oathToken ~= nil and self.config.oathToken ~= '')
end

function TwitchClient:addViewer(user)
if user == self.config.username then
return
end
local function hasValue (tab, val)
for index, value in ipairs (tab) do
-- We grab the first index of our sub-table instead
if value == val then
return true
end
end

return false
end
if not hasValue(client.userNames, user) then
table.insert(client.userNames,user)
end
client:updateTitle()
end

function TwitchClient:removeViewer(user)
table.remove(client.userNames, user)
end

function TwitchClient.onUISendMessage(args)
local username = client.config.username
local skin = client:getSkinForUser(username)
Expand All @@ -108,22 +133,26 @@ end

function TwitchClient.onUserPart(cmd)
client.ui:addMessage(client:getTimeStamp().." "..cmd.user.." left.", client.joinPartSkin)
client.viewerCount = client.viewerCount - 1
client:removeViewer(cmd.user)
end

function TwitchClient.onUserJoin(cmd)
if cmd.user == client.config.username then
return
end
client.ui:addMessage(client:getTimeStamp().." "..cmd.user.." joined.", client.joinPartSkin)
client.viewerCount = client.viewerCount + 1
client:updateTitle()
client:addViewer(cmd.user)
end

function TwitchClient.onUserMessage(cmd)
local skin = client:getSkinForUser(cmd.user)
client.ui:addMessage(client:getTimeStamp().." "..cmd.user..": "..cmd.param2, skin)
client:addViewer(cmd.user)
end

function TwitchClient:updateTitle()
client.ui:setTitle("Twitch Chat | "..client.viewerCount.." viewers")
local viewerCount = #client.userNames
client.ui:setTitle("Twitch Chat | "..viewerCount.." viewers")
end

function TwitchClient:connect()
Expand Down
50 changes: 44 additions & 6 deletions lua/Scripts/twitch/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ local ListBoxItem = require('ListBoxItem')
local Tools = require('tools')
local MulChat = require('mul_chat')
local tracer = require("twitch.tracer")
local utils = require('twitch.utils')
local utils = require('twitch.utils')
local Input = require('Input')

local modes = {
hidden = "hidden",
Expand All @@ -35,10 +36,10 @@ local UI = {
_currentWheelValue = 0,
_listStatics = {},
_listMessages = {},
_nextChatColorIndex = 1,
_currentMode = modes.read,
_x,
_y,
_isKeyboardLocked = false,
modes = modes
}
local UI_mt = { __index = UI }
Expand Down Expand Up @@ -210,9 +211,9 @@ function UI:new(hotkey, defaultMode, x, y)
ui:resizeEditMessage()

if ui._currentMode == ui.modes.write then
ui:readMode()
else
ui:writeMode()
else
ui:readMode()
end

ui.window:addPositionCallback(function() ui:positionCallback() end)
Expand Down Expand Up @@ -248,7 +249,7 @@ function UI:writeMode()
self.pDown:setVisible(true)
self.eMessage:setFocused(true)

DCS.banKeyboard(true)
self:lockKeyboardInput(true)

self:updateListM()
end
Expand Down Expand Up @@ -276,7 +277,7 @@ function UI:readMode()
self.pDown:setVisible(false)
self.eMessage:setFocused(false)

DCS.banKeyboard(false)
self:lockKeyboardInput(false)

self:updateListM()
end
Expand All @@ -289,6 +290,43 @@ function UI:nextMode()
end
end

function UI:lockKeyboardInput(lock)
if lock then
if not self._isKeyboardLocked then
-- блокируем все кнопки клавиатуры,
-- кроме кнопок управления чатом
local keyboardEvents = Input.getDeviceKeys(Input.getKeyboardDeviceName())
local inputActions = Input.getEnvTable().Actions

local removeCommandEvents = function(commandEvents)
for i, commandEvent in ipairs(commandEvents) do
-- из массива удаляем элементы с конца
for j = #keyboardEvents, 1, -1 do
if keyboardEvents[j] == commandEvent then
table.remove(keyboardEvents, j)

break
end
end
end
end

removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandChat))
removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandAllChat))
removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandFriendlyChat))
removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandChatShowHide))

DCS.lockKeyboardInput(keyboardEvents)
self._isKeyboardLocked = true
end
else
if self._isKeyboardLocked then
DCS.unlockKeyboardInput()
self._isKeyboardLocked = false
end
end
end

function UI:resize(w, h)
self.window:setBounds(self._x, self._y, 360, 455)
self.box:setBounds(0, 0, 360, 400)
Expand Down

0 comments on commit 9738915

Please sign in to comment.