Daily Lucky Numbers:
5
19
21
26
38
44

Pickems help

Started by Fog, December 07, 2013, 09:32:32 PM

Previous topic - Next topic

Chen Zhen

#15
Take a look at this:


// NFL ~ Current week - last game as tie breaker
// last updated: Sep 01 3:45am
global $user_info, $scripturl, $smcFunc, $sourcedir;
require_once($sourcedir . '/Subs-Package.php');
db_extend('packages');
checkNflTable();
$lastGamesArray = array(
0 => array('',''),
1 => array('SD', 'ARI'),
2 => array('PHI', 'IND'),
3 => array('CHI', 'NYJ'),
4 => array('NE', 'KC'),
5 => array('SEA', 'WSH'),
6 => array('SF', 'STL'),
7 => array('HOU', 'PIT'),
8 => array('WSH', 'DAL'),
9 => array('IND', 'NYG'),
10 => array('CAR', 'PHI'),
11 => array('PIT', 'TEN'),
12 => array('BAL', 'NO'),
13 => array('MIA', 'NYJ'),
14 => array('ATL', 'GB'),
15 => array('NO', 'CHI'),
16 => array('DEN', 'CIN'),
17 => array('STL', 'SEA'),
);

$onset = strtotime('Sep 7, 2014');
$gameCutOff = 1;
$feed = 'http://www.nfl.com/liveupdate/scorestrip/scorestrip.json';
$json = fetch_web_data($feed) ? fetch_web_data(utf8_encode($feed)) : false;
$json = preg_replace(array('~,,~', '~,,~', '~,,~'), array(',"",', ',"",', ',"",'), $json);
$html = json_decode($json, true);
$html = !empty($html['ss']) ? $html['ss'] : array();
$nfl_games = array();
$data = array();
$gameCount = 0;
$gameData = array('day', 'time', 'type', 'a', 'away', 'away_score', 'home', 'home_score', 'b', 'c', 'id', 'd', 'code', 'year');
$dayArray = array('Tue', 'Wed', 'Thu', 'Fri');

if (!empty($html))
{
foreach ($html as $key => $game)
{
for ($i=0; $i <=13; $i++)
$nfl_games[$gameCount][$gameData[$i]] = $game[$i];

$gameCount++;
}
$week_final = $nfl_games[count($nfl_games) - 1];

$actualTotal = (is_numeric($week_final['away_score']) || is_numeric($week_final['home_score'])) ? (int)$week_final['home_score'] + (int)$week_final['away_score'] : '??';
date_default_timezone_set('EST');
$currentDay = date('D', time());
$queryWeek = (time() - $onset !== abs(time() - $onset) || time() - $onset > 604800) ? 604800 : 0;
$time = strtotime('this ' . $week_final['day'] . ', ' . $week_final['time'] . 'pm') + $queryWeek;
$weekCounter = ((round(($time - $onset) / 604800)) > 0 ? round(($time - $onset) / 604800) : 1);
$cutoff = strtotime('this ' . $nfl_games[$gameCutOff]['day'] . ', ' . $nfl_games[$gameCutOff]['time'] . 'pm') + $queryWeek;
$displayCutOff = date('D M d h:iA', $cutoff) . ' EST';
$displayTime = date('D M d h:iA', $time) . ' EST';
$notAllowed = (time() - $cutoff >= 0) ? true : false;

$currentWeekApi = getInt($week_final['code']);
$currentMatchUp = $lastGamesArray[$weekCounter];
$confirm =  (in_array($week_final['home'], $currentMatchUp) && in_array($week_final['away'], $currentMatchUp)) ? true : false;
}

if ((!empty($html)) && empty($notAllowed) && time() < ($time + 86400) && in_array($currentDay, $dayArray) && $confirm && stripos($week_final['code'], 'PRE') === false && (int)$currentWeekApi == $weekCounter)
{

if (isset($_REQUEST['points']) && isset($_REQUEST['submit']))
{
$smcFunc['db_query']('', "
DELETE FROM {db_prefix}nfl_tiebreakers
WHERE id_member = {int:member} AND week = {int:current}",
array('member' => $user_info['id'], 'current' => $weekCounter)
);
$smcFunc['db_insert']('insert', "
{db_prefix}nfl_tiebreakers",
array('week' => 'int', 'id_member' => 'int', 'logged' => 'int', 'points' => 'int', 'home' => 'string', 'away' => 'string'),
array($weekCounter, $user_info['id'], $cutoff, (int)$_REQUEST['points'], $week_final['home'], $week_final['away']),
array('week')
);

}

$request = $smcFunc['db_query']('', "
SELECT week, id_member, logged, points, home, away
FROM {db_prefix}nfl_tiebreakers
WHERE week = {int:week} AND id_member = {int:member}",
array('week' => $weekCounter, 'member' => $user_info['id'])
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
$data = array(
'week' => !empty($row['week']) ? (int)$row['week'] : 0,
'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
'points' => isset($row['points']) ? (int)$row['points'] : null,
'home' => !empty($row['home']) ? $row['home'] : '',
'away' => !empty($row['away']) ? $row['away'] : '',
);
}
$smcFunc['db_free_result']($request);

echo '
<form id="nfl_pick" name="nfl_pickem" action="' . currentPageNflPickem() . '" method="post">
<div class="cat_bar">
<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
</div>
<table>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">Week</td>
<td style="padding:0px 8px 0px 8px;">Game Date</td>
<td style="padding:0px 8px 0px 8px;">Away</td>
<td style="padding:0px 8px 0px 8px;">Home</td>
<td style="padding:0px 8px 0px 8px;">Username</td>
<td style="padding:0px 8px 0px 8px;">Tie Breaking Points Total</td>
<td colspan="2">&nbsp;</td>
</tr>
</table>
<table>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
<td style="padding:0px 8px 0px 8px;">Game Date</td>
<td style="padding:0px 8px 0px 8px;">Away</td>
<td style="padding:0px 8px 0px 8px;">Home</td>
<td style="padding:0px 8px 0px 8px;">Username</td>
<td style="padding:0px 8px 0px 8px;">Tie Breaking Points Total</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user_info['id'] . '">', $user_info['name'], '</a></td>
<td style="padding:0px 8px 0px 8px;text-align:center;">
<input name="points" type="number" maxlength="4" size="3" min="0" max="999" value="', (isset($data['points']) ? (int)$data['points'] : 0), '" />
', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
</td>
<td>
', (isset($data['points']) ? '<span style="color:green;">On Record' : '<span style="color:red;">Pending'), '</span>
</td>
<td>
<input class="submit" type="submit" name="submit" style="position:relative;left:100%;" />
</td>
</tr>
</table>
</form>
';
}
elseif (!empty($html) && $confirm && stripos($week_final['code'], 'PRE') === false)
{
$request = $smcFunc['db_query']('', "
SELECT week, id_member, logged, points, home, away
FROM {db_prefix}nfl_tiebreakers
WHERE week = {int:week} AND id_member = {int:member}",
array('week' => $weekCounter, 'member' => $user_info['id'])
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
$data = array(
'week' => !empty($row['week']) ? (int)$row['week'] : 0,
'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
'points' => isset($row['points']) ? (int)$row['points'] : null,
'home' => !empty($row['home']) ? $row['home'] : '',
'away' => !empty($row['away']) ? $row['away'] : '',
);
}
$smcFunc['db_free_result']($request);

echo '
<div style="display:inline;">
<div class="cat_bar">
<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
</div>
<table>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
<td style="padding:0px 8px 0px 8px;">Game Date</td>
<td style="padding:0px 8px 0px 8px;">Away</td>
<td style="padding:0px 8px 0px 8px;">Home</td>
<td style="padding:0px 8px 0px 8px;">Username</td>
<td style="padding:0px 8px 0px 8px;">Tie Breaking Points Total</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user_info['id'] . '">', $user_info['name'], '</a></td>
<td style="padding:0px 8px 0px 8px;text-align:center;">
', (isset($data['points']) ? $data['points'] : '???'), '
', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
</td>
<td>
', (isset($data['points']) ? '<span style="color:green;">On Record' : '<span style="color:red;">No Data'), '</span>
</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
';
}
elseif (stripos($week_final['code'], 'PRE') !== false)
echo 'Preseason games still in effect.';
else
echo 'No data at this time.';

/* Current url */
function currentPageNflPickem($pageURL = 'http://')
{
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $pageURL;
}

function getInt($str)
{
preg_match("/([0-9]+[\.,]?)+/",$str,$matches);
return $matches[0];
}

/* Check NFL Table */
function checkNflTable()
{
global $smcFunc;

$smcFunc['db_create_table']('{db_prefix}nfl_tiebreakers',
array(
array(
'name' => 'week',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
'auto' => false
),
array(
'name' => 'id_member',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'logged',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'points',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'home',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'away',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'picks',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
),
array(
array(
'type' => 'primary',
'columns' => array('week')
),
),
array(),
'ignore'
);

$smcFunc['db_add_column']('{db_prefix}nfl_tiebreakers',
array(
'name' => 'picks',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(),
'ignore',
'fatal'
);

return;
}


I'm not 100% positive regarding how that nfl.com API works atm since I have never used it before.
What I mean is that I may have to change the code for when the regular season is starting.
I will keep an eye on it though.

That will let your users log their tie-breaker point total for the last game (ususally a Monday nighter) where the cutoff is the second game of the weekly schedule start time.  The JSON parse of the NFL.com API should have the correct match-up for you.

Fog

I have it in a Simple Portal PHP Block and looks fine...now I just need an input field and create a DB

Fog

UD, here's the files for the game I'm using with my members.

It's a stand alone site right now

http://www.ngsforums.com/ngspickems/index.php

Since there is no tiebreaker with the "phppickem-master" zip

Our members are asking if we could come up with one...

I seek your help.

You're the master....

If you tinker with this game, I have a sql file I can send you for the 2014 schedule.

Fog

I see an input box now..

I'll get you a screen shot tomorrow.

Chen Zhen

The block code has been edited.
The cut-off for editing the tie breaker is Friday evening of each week and the tie breaker game is the last one for the week.
It will display the score of the final game if it is completed.
A way of letting the admin display the user picks for all or specific week is pending (later today perhaps).
The example still shows an allowable edit for this week because it is a pre-season schedule that was entirely on Thursday whereas the regular season should not work like that (cutoff Fridays).

So you want this for something outside of SMF or is this being displayed on your site whereas I can edit your file to load the SMF infrastructure (SSI.php) ??  I will look at this file later today.

Fog

What I'd like to do is have the tie breaker on SMF in the form of a block or page or a package.

This way we retain traffic that we may lose if the tie breaker was displayed on the stand alone game.

Think about this too...

Can we reward someone shop money or battle money?  I have SA Shop up and running and I'm offering NFL Baseball Caps for 5000 Shop Dollars.

So say each week we had each member input a int that reflects what the total points scored in that game for that given week.  In theory nobody would be able to see what others picked.  Then after the game is played it would display the results of who got the closest and reward them a certain amount of Shop Money and or Battle Money.

This could be a game outside of the Stand Alone Game of Pickems I found but we want to use it in a fashion so if we have a tie on the weekly picks we could fall back to who won the tiebreaker.

I don't know if I told you this, VBullentin has that Pickems Game that I have on a zip as a package for their boards.



Chen Zhen

#21
I did not bother looking at that package you uploaded as of yet.
Instead I started developing my own application made specifically for SMF.
Right now it still needs a lot of work which I am going to have to do this week prior to the start of the regular season.
It will require testing once the regular season starts which you are more than welcome to volunteer.
Unfortunately because it requires testing during the NFL season to work out all issues it will not be ready to use as a stable package.

  However I will update the post in this thread with a block code that should allow your users to record the tie breaker.
I still have not put in a routine to let Admin look over the user entries since I have been working on the other dev project.
Hopefully I will have time to do that for you tomorrow.

Regards.

Fog

Let me know how I can help....

Thanks UD

Fog

This is what I see so far...

Here's a link to the page I'm testing it on:

http://ngsforums.com/forums/index.php?page=page2418


Fog

Things I would include:

Have to register for the game each season.

Reward players so they can spend it in Shop or playing Battle.

Auto Scoring.

Chen Zhen

#25
Prior to adding any way of having it interact with other mods I think it will be best to get the game itself working properly.

Here is a test page:
http://www.web-develop.ca/index.php?page=nfl_pool

Currently the user stats part is not completed.
It also needs some other functions added to it but it is a decent start.





Fog,

  Try out the newer code from THIS POST again.
It should allow user input now (up until Sunday at 1pm).

Fog

The dates seem to be off a week.

It appears that I can't pick games

But I can see where I've been able to login my tie breaker.


Chen Zhen

#27
I redid the time calculations so that the array containing part of the schedule (last game of each week) now contains the actual start times of the initial games and the final game.
It should work properly now, please test it.

Here is a copy of the code thus far which can be entered into a PHP block or page:
The array is set to ignore Thursday games so that users have more time to enter their picks.
Currently this can be changed manually since it is a page/block format.
When I change it to a mod then I will allow admin options for it.

Admin Mode allows actually setting the favorite and spreads whereas Game Mode allows the admin to play by making picks like other users.


// NFL ~ Current week - last game as tie breaker
// last updated: Sep 07 9:30pm ~ 2014
global $user_info, $scripturl, $smcFunc, $sourcedir, $context, $modSettings, $settings, $txt;
require_once($sourcedir . '/Subs-Package.php');
db_extend('packages');

if (empty($modSettings['nfl_installed']))
installNflTables();

$lastGamesArray = nfl_gameYear();
$feed = 'http://www.nfl.com/liveupdate/scorestrip/scorestrip.json';
$json = fetch_web_data($feed) ? fetch_web_data(utf8_encode($feed)) : false;
$json = preg_replace(array('~,,~', '~,,~', '~,,~'), array(',"",', ',"",', ',"",'), $json);
$html = json_decode($json, true);
$html = !empty($html['ss']) ? $html['ss'] : array();
$nfl_games = array();
$data = array();
$gameCount = 0;
$gameData = array('day', 'time', 'type', 'a', 'away', 'away_score', 'home', 'home_score', 'b', 'c', 'id', 'd', 'code', 'year');
$currentPage = currentPageNflPickem();
list($userName, $userNow, $picks, $user_picks, $weekArray, $spreads, $games, $nfl_pick) = array('Invalid User ID', 'Invalid User Id', array(), array(), array(), '', '', 0);

if (!empty($html))
{
$checkWeek = !empty($html[0]['12']) ? getInt($html[0]['12']) : 1;
$_SESSION['nfl_otherUser'] = !empty($_SESSION['nfl_otherUser']) ? (int)$_SESSION['nfl_otherUser'] : $user_info['id'];
$user = !empty($_REQUEST['member']) ? (int)$_REQUEST['member'] : ($_SESSION['nfl_otherUser'] != $user_info['id'] ? $_SESSION['nfl_otherUser'] : $user_info['id']);
$_SESSION['nfl_otherUser'] = $user;
$allowedEdit = $user == $user_info['id'] || $user_info['is_admin'] ? true : false;
$currentPage = stripos($currentPage, 'member=' . $user) === false ? $currentPage .= ';member=' . $user: $currentPage;
$placement = $lastGamesArray[$checkWeek][2];
$cutoff = $lastGamesArray[$checkWeek][$placement+4];
$gameCutOff = !empty($checkWeek) ? $lastGamesArray[$checkWeek][2] : 1;
$allowedView = time() - $cutoff == abs(time() - $cutoff) || $user_info['is_admin'] ? true : false;
$error = !empty($_REQUEST['error']) ? $_REQUEST['error'] : false;
$original = $html;
$context['nfl_mode'] = '';

if ((!empty($gameCutOff)) && count($html) > $gameCutOff)
{
for ($i=0; $i<$gameCutOff; $i++)
array_shift($html);
}

foreach ($html as $key => $game)
{
for ($i=0; $i <=13; $i++)
$nfl_games[$gameCount][$gameData[$i]] = !empty($game[$i]) ? $game[$i] : '';

$gameCount++;
}
$week_final = $nfl_games[count($nfl_games) - 1];

$actualTotal = (is_numeric($week_final['away_score']) || is_numeric($week_final['home_score'])) ? (int)$week_final['home_score'] + (int)$week_final['away_score'] : '??';
date_default_timezone_set('EST');
$myTime = time();
$time = $lastGamesArray[$checkWeek][3];
$displayCutOff = date('D M d h:iA', $cutoff) . ' EST';
$displayTime = date('D M d h:iA', $time) . ' EST';
$notAllowed = time() - $cutoff >= 0 ? true : false;
$currentWeekApi = getInt($week_final['code']);
$currentMatchUp = $lastGamesArray[$checkWeek];
$confirm =  (in_array($week_final['home'], $currentMatchUp) && in_array($week_final['away'], $currentMatchUp)) ? true : false;
$checkQueryStatus = $user_info['is_admin'] || $notAllowed ? true : false;

if (isset($_REQUEST['NflMember']) && isset($_REQUEST['submit']) && $checkQueryStatus)
{
$queryName = $smcFunc['htmlspecialchars']($_REQUEST['NflMember']);
$result =  $smcFunc['db_query']('', '
SELECT id_member, real_name, member_name
FROM {db_prefix}members
WHERE real_name = {string:member}
LIMIT 1',
array(
'member' => $smcFunc['htmltrim']($queryName),
)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
list($_SESSION['nfl_otherUser'], $user) = array($row['id_member'], $row['id_member']);
$userName = $row['real_name'];
$userNow = !empty($row['real_name']) ? 'Editing week ' . $checkWeek . ' picks for ' . $row['real_name'] : (!empty($row['member_name']) ? $row['member_name'] : 'Invalid User ID');
}

$smcFunc['db_free_result']($result);

}
elseif ($user != $user_info['id'])
{
$result =  $smcFunc['db_query']('', '
SELECT id_member, real_name, member_name
FROM {db_prefix}members
WHERE id_member = {int:member}
LIMIT 1',
array(
'member' => $user,
)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
list($_SESSION['nfl_otherUser'], $user) = array($row['id_member'], $row['id_member']);
$userName = $row['real_name'];
$userNow = !empty($row['real_name']) ? 'Editing week ' . $checkWeek . ' picks for ' . $row['real_name'] : (!empty($row['member_name']) ? $row['member_name'] : 'Invalid User ID');
}

$smcFunc['db_free_result']($result);
}
else
{
list($_SESSION['nfl_otherUser'], $user) = array($user_info['id'], $user_info['id']);
$userName = $user_info['name'];
$userNow = 'Editing week ' . $checkWeek . ' picks for ' . $user_info['name'];
}
}

if ((!empty($html)) && !$notAllowed && $confirm && stripos($week_final['code'], 'PRE') === false && $allowedEdit)
{
if (isset($_REQUEST['admin']))
$_SESSION['nfl_admin'] = !empty($_SESSION['nfl_admin']) ? false : true;
if (isset($_REQUEST['stats']))
$_SESSION['nfl_stats'] = !empty($_SESSION['nfl_stats']) ? false : true;

if (!$user_info['is_admin'])
list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);

$userNow = !empty($_SESSION['nfl_admin']) ? 'Editing spreads for week ' . $checkWeek : $userNow;
if (isset($_REQUEST['points']) && isset($_REQUEST['submit']) && empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']))
{
list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
$request = $smcFunc['db_query']('', "
SELECT id_member
FROM {db_prefix}nfl_weekly
WHERE id_member = {int:member}
LIMIT 1",
array('member' => $user)
);

$row = $smcFunc['db_fetch_row']($request);
for ($i=1; $i <=17; $i++)
$weekArray['week' . $i] = !empty($row['week' . $i]) ? $row['week' . $i] : '';

$smcFunc['db_free_result']($request);

$current = !empty($weekArray['week' . $checkWeek]) ? array_pop(explode('_', $weekArray['week' . $checkWeek])) : array();
$current = implode('_', $current);
for ($i=1; $i< count($nfl_games)+1; $i++)
$games .= !empty($_REQUEST['game_' . ($i-1)]) ? $_REQUEST['game_' . ($i-1)] . '_': (!empty($current) ? $current . '_' : '');

$games .= !empty($_REQUEST['points']) ? abs((int)$_REQUEST['points']) : '';
$weekArray['week' . $checkWeek] = trim($games, '_');

$smcFunc['db_query']('', "
DELETE FROM {db_prefix}nfl_weekly
WHERE id_member = {int:member}",
array('member' => $user, 'current' => $checkWeek)
);

$smcFunc['db_insert']('insert', "
{db_prefix}nfl_weekly",
array(
'id_member' => 'int',
'week1' => 'string',
'week2' => 'string',
'week3' => 'string',
'week4' => 'string',
'week5' => 'string',
'week6' => 'string',
'week7' => 'string',
'week8' => 'string',
'week9' => 'string',
'week10' => 'string',
'week11' => 'string',
'week12' => 'string',
'week13' => 'string',
'week14' => 'string',
'week15' => 'string',
'week16' => 'string',
'week17' => 'string'
),
array(
$user,
(!empty($weekArray['week1']) ? $weekArray['week1'] : ''),
(!empty($weekArray['week2']) ? $weekArray['week2'] : ''),
(!empty($weekArray['week3']) ? $weekArray['week3'] : ''),
(!empty($weekArray['week4']) ? $weekArray['week4'] : ''),
(!empty($weekArray['week5']) ? $weekArray['week5'] : ''),
(!empty($weekArray['week6']) ? $weekArray['week6'] : ''),
(!empty($weekArray['week7']) ? $weekArray['week7'] : ''),
(!empty($weekArray['week8']) ? $weekArray['week8'] : ''),
(!empty($weekArray['week9']) ? $weekArray['week9'] : ''),
(!empty($weekArray['week10']) ? $weekArray['week10'] : ''),
(!empty($weekArray['week11']) ? $weekArray['week11'] : ''),
(!empty($weekArray['week12']) ? $weekArray['week12'] : ''),
(!empty($weekArray['week13']) ? $weekArray['week13'] : ''),
(!empty($weekArray['week14']) ? $weekArray['week14'] : ''),
(!empty($weekArray['week15']) ? $weekArray['week15'] : ''),
(!empty($weekArray['week16']) ? $weekArray['week16'] : ''),
(!empty($weekArray['week17']) ? $weekArray['week17'] : '')
),
array('id_member')
);

}
elseif ($user_info['is_admin'] && isset($_REQUEST['submit']) && !empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']))
{
$_SESSION['nfl_stats'] = false;
for ($i=0; $i<count($html); $i++)
{
$team = !empty($_REQUEST['game_' . $i]) ? $_REQUEST['game_' . $i] : $html[$i+1]['home'];
$spreads .= $team . (!empty($_REQUEST['spread_' . $i]) ? $_REQUEST['spread_' . $i] : '0.5') . '_';

}

$spreads = trim($spreads, '_');
$logged = time();

$smcFunc['db_query']('', "
DELETE FROM {db_prefix}nfl_tiebreakers
WHERE week = {int:week}",
array('week' => $checkWeek)
);

$smcFunc['db_insert']('insert', "
{db_prefix}nfl_tiebreakers",
array(
'week' => 'int',
'logged' => 'int',
'home' => 'string',
'away' => 'string',
'picks' => 'string'
),
array(
$checkWeek,
$logged,
(!empty($week_final['home']) ? $week_final['home'] : ''),
(!empty($week_final['away']) ? $week_final['away'] : ''),
$spreads
),
array('week')
);
}

if ($user_info['is_admin'] && !empty($_SESSION['nfl_stats']))
{
$_SESSION['nfl_stats'] = true;
$_SESSION['nfl_admin'] = false;
nfl_user_stats($currentPage, $checkWeek);
}
else
{
$request = $smcFunc['db_query']('', "
SELECT n.week, w.id_member, n.logged, n.home, n.away, n.picks, {raw:week_num}
FROM {db_prefix}nfl_tiebreakers as n
LEFT JOIN {db_prefix}nfl_weekly as w ON (w.id_member = {int:member})
WHERE week = {int:week}
LIMIT 1",
array('week' => $checkWeek, 'member' => $user, 'week_num' => 'w.week' . $checkWeek)
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
$data = array(
'week' => !empty($row['week']) ? (int)$row['week'] : 0,
'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
'home' => !empty($row['home']) ? $row['home'] : '',
'away' => !empty($row['away']) ? $row['away'] : '',
'picks' => !empty($row['picks']) ? $row['picks'] : '',
);

$picks = !empty($row['picks']) ? explode('_', $row['picks']) : array();
$user_picks = !empty($row['week' . $checkWeek]) ? explode('_', $row['week' . $checkWeek]) : array();
$context['nfl_user_points'] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
array_pop($user_picks);
}
$smcFunc['db_free_result']($request);

if ((!empty($gameCutOff)) && count($picks) > count($html))
{
for ($i=count($picks); $i>count($html); $i--)
array_shift($picks);
}

if ((!empty($gameCutOff)) && count($user_picks) > count($html))
{
for ($i=count($user_picks); $i>count($html); $i--)
array_shift($user_picks);
}

foreach ($picks as $pickem)
{
$context['spread_' . $nfl_pick] = abs((int)getInt($pickem)) . '.5';
$pickem = trim(str_replace(range(0,9), '', $pickem), '.');
$context['game_' . $nfl_pick] = (!empty($pickem)) && $pickem === trim($nfl_games[$nfl_pick]['home']) ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
$nfl_pick++;
}
$nfl_pick = 0;
foreach ($user_picks as $user_pickem)
{
$context['user_game_' . $nfl_pick] = (!empty($user_pickem)) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
$nfl_pick++;
}

echo '
<script type="text/javascript"><!-- // --><![CDATA[
function NFL_confirmSubmit()
{
var agree=confirm("Do you confirm this action?");
if (agree)
return true ;
else
return false ;
}
// ]]></script>
<form id="nfl_pick" name="nfl_pick" action="' . $currentPage . '" method="post">
<div class="windowbg">
<div class="cat_bar">
<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
</div>
<table width="100%">
<tr>
<td class="centertext titlebg" colspan="5">
Week ', (int)$currentWeekApi, '<br />
</td>
</tr>
<tr>
<td class="centertext titlebg" colspan="5">
', $userNow, '<br />
</td>
</tr>
', $user_info['is_admin'] && empty($_SESSION['nfl_admin']) ? '
<tr>
<td class="centertext titlebg" colspan="5">
Game Mode<br />
</td>
</tr>' : ($user_info['is_admin'] && !empty($_SESSION['nfl_admin']) ? '
<tr>
<td class="centertext titlebg" colspan="5">
Admin Mode<br />
</td>
</tr>' : ''), '
<tr>
<td class="centertext" style="padding:8px 8px 0px 8px;">AWAY</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">HOME</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">DATE</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">Point Spread</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">Select Team</td>
</tr>
<tr>
<td colspan="5">&nbsp;</td>
</tr>';
foreach ($nfl_games as $key => $game)
{
$gameTime = date('D M d h:iA', (strtotime($game['day'] . ', ' . $game['time'] . 'pm'))) . ' EST';
$windowbg = (!empty($windowbg)) && $windowbg === 'windowbg' ? 'windowbg2' : 'windowbg';
echo '
<tr class="' . $windowbg . '">
<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['away'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['away'], '</td>
<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['home'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['home'], '</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">', $gameTime, '</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">
', !empty($_SESSION['nfl_admin']) ? '
<input name="spread_' . $key . '" type="number" maxlength="4" size="3" min="0" max="999" step="0.5" value="' . (isset($context['spread_' . $key]) ? abs((int)$context['spread_' . $key]) + 0.5 : 0.5) . '" />' : '
<span>' . (isset($context['spread_' . $key]) ? abs((int)$context['spread_' . $key]) + 0.5 : 0.5) . '</span>', '
</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">
<select name="game_' . $key . '">
<option ', ($user_info['is_guest'] ? 'disabled ' : ''), 'value="' . $game['away'] . '" ', (empty($_SESSION['nfl_admin']) && !empty($context['user_game_' . $key]) && $context['user_game_' . $key] === $game['away'] ? 'selected' : (!empty($_SESSION['nfl_admin']) && !empty($context['game_' . $key]) && $context['game_' . $key] === $game['away'] ? 'selected' : '')), '>', $game['away'], '</option>
<option ', ($user_info['is_guest'] ? 'disabled ' : ''), 'value="' . $game['home'] . '" ', (empty($_SESSION['nfl_admin']) && !empty($context['user_game_' . $key]) && $context['user_game_' . $key] === $game['home'] ? 'selected' : (!empty($_SESSION['nfl_admin']) && !empty($context['game_' . $key]) && $context['game_' . $key] === $game['home'] ? 'selected' : '')), '>', $game['home'], '</option>
</select>
</td>
</tr>';
}
echo '
</table>
<div class="windowbg"><br /></div>
<table width="100%">
<tr>
<td class="centertext titlebg" colspan="7">
Tie Breaker<br />
</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
<td style="padding:0px 8px 0px 8px;">Game Date</td>
<td style="padding:0px 8px 0px 8px;">Away</td>
<td style="padding:0px 8px 0px 8px;">Home</td>
<td style="padding:0px 8px 0px 8px;">Username</td>
<td style="padding:0px 8px 0px 8px;">Points Total</td>
<td colspan="2">Status</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user . '">', $userName, '</a></td>
<td style="padding:0px 8px 0px 8px;" class="left">
<input ', (!empty($_SESSION['nfl_admin']) ? 'disabled ' : ''), 'name="points" type="number" maxlength="4" size="3" min="0" max="999" value="', (isset($context['nfl_user_points']) ? (int)$context['nfl_user_points'] : 0), '" />
', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
</td>
<td>
', (isset($context['nfl_user_points']) && empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']) ? '<span style="color:green;">SAVED' : (empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']) ? '<span style="color:red;">Pending' : (!empty($_SESSION['nfl_stats']) ? 'Stats Mode' : 'Admin Mode'))), '</span>
</td>
</tr>
<tr>
<td style="border:0px;" colspan="7"><hr /><br /></td>
</tr>
<tr>
<td colspan="7">
', (!empty($error) ? '<span class="error">Warning: You are not permitted to view other user picks until after the cut-off time.</span><br /><br />' : '&nbsp;'), '
</td>
</tr>
<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
<td>
<input class="submit" type="submit" name="submit" value="Submit" onclick="var confirm = NFL_confirmSubmit(); if (confirm) {this.form.submit();} else {return false;}" />
</td>
<td colspan="4">
<div class="content">
<strong>Search Member:</strong>
<input type="text" name="NflMember" id="NflMember" value="" class="input_text" ' . (!empty($_SESSION['nfl_admin']) ? 'disabled ' : '') . '/>
<div id="NflQueryUserContainer"></div>
</div>
</td>
', empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="stats" value="View Overall" />
</td>' : (!empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="stats" value="Exit Overall" />
</td>' : '
<td>
&nbsp;
</td>'), '
', empty($_SESSION['nfl_admin']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="admin" value="Admin Mode" />
</td>' : (!empty($_SESSION['nfl_admin']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="admin" value="Game Mode" />
</td>' : '
<td>
&nbsp;
</td>'), '
</tr>
</table>
</div>
</form>
<script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/suggest.js?fin20"></script>
<script type="text/javascript"><!-- // --><![CDATA[
var oAddMemberSuggest = new smc_AutoSuggest({
sSelf: \'oAddMemberSuggest\',
sSessionId: \'', $context['session_id'], '\',
sSessionVar: \'', $context['session_var'], '\',
sSuggestId: \'to_suggest\',
sControlId: \'NflMember\',
sSearchType: \'member\',
sPostName: \'nfl_pick\',
sURLMask: \'', !empty($_REQUEST['page']) ? 'page=' . $smcFunc['htmlspecialchars']($_REQUEST['page']) : (!empty($context['current_action']) ? 'action=' . $context['current_action'] : ''), ';member=%item_id%\',
sTextDeleteItem: \'', $txt['autosuggest_delete_item'], '\',
bItemList: false,
sItemListContainerId: \'NflQueryUserContainer\'
});
// ]]></script>';
}
}
elseif (!empty($html) && $confirm && stripos($week_final['code'], 'PRE') === false && $allowedView)
{
if (isset($_REQUEST['stats']))
$_SESSION['nfl_stats'] = !empty($_SESSION['nfl_stats']) ? false : true;

if (!$user_info['is_admin'])
list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
$userNow = !empty($_SESSION['nfl_admin']) ? 'Viewing spreads for week ' . $checkWeek : $userNow;

if ($user_info['is_admin'] && !empty($_SESSION['nfl_stats']))
{
$_SESSION['nfl_stats'] = true;
$_SESSION['nfl_admin'] = false;
nfl_user_stats($currentPage, $checkWeek);
}
else
{
list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
$request = $smcFunc['db_query']('', "
SELECT n.week, w.id_member, n.logged, n.home, n.away, n.picks, w.{raw:week_num}
FROM {db_prefix}nfl_tiebreakers as n
LEFT JOIN {db_prefix}nfl_weekly as w ON (w.id_member = {int:member})
WHERE week = {int:week}
LIMIT 1",
array('week' => $checkWeek, 'member' => $user, 'week_num' => 'week' . (string)$checkWeek)
);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
$data = array(
'week' => !empty($row['week']) ? (int)$row['week'] : 0,
'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
'home' => !empty($row['home']) ? $row['home'] : '',
'away' => !empty($row['away']) ? $row['away'] : '',
'picks' => !empty($row['picks']) ? $row['picks'] : '',
);

$picks = !empty($row['picks']) ? explode('_', $row['picks']) : array();
$user_picks = !empty($row['week' . $checkWeek]) ? explode('_', $row['week' . $checkWeek]) : array();
$context['nfl_user_points'] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
array_pop($user_picks);
}
$smcFunc['db_free_result']($request);

if ((!empty($gameCutOff)) && count($picks) > count($html))
{
for ($i=count($picks); $i>count($html); $i--)
array_shift($picks);
}

if ((!empty($gameCutOff)) && count($user_picks) > count($html))
{
for ($i=count($user_picks); $i>count($html); $i--)
array_shift($user_picks);
}

foreach ($picks as $pickem)
{
$context['spread_' . $nfl_pick] = abs((int)getInt($pickem)) . '.5';
$pickem = trim(str_replace(range(0,9), '', $pickem), '.');
$context['game_' . $nfl_pick] = (!empty($pickem)) && $pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
$nfl_pick++;
}
$nfl_pick = 0;
foreach ($user_picks as $user_pickem)
{
$context['user_game_' . $nfl_pick] = (!empty($user_pickem) && !empty($nfl_games[$nfl_pick]['home'])) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : (!empty($nfl_games[$nfl_pick]['away']) ? $nfl_games[$nfl_pick]['away'] : '');
$nfl_pick++;
}

echo '
<script type="text/javascript"><!-- // --><![CDATA[
function NFL_confirmSubmit()
{
var agree=confirm("Do you confirm this action?");
if (agree)
return true ;
else
return false ;
}
// ]]></script>
<div style="display:inline;" class="windowbg">
<div class="cat_bar">
<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
</div>
<table width="100%">
<tr>
<td class="centertext titlebg" colspan="5">
Week ', (int)$currentWeekApi, '<br />
</td>
</tr>
<tr>
<td class="centertext titlebg" colspan="5">
', $userNow, '<br />
</td>
</tr>
<tr>
<td class="centertext" style="padding:8px 8px 0px 8px;">AWAY</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">HOME</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">DATE</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">Point Spread</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">Your Pick</td>
</tr>
<tr>
<td colspan="5">&nbsp;</td>
</tr>';
foreach ($nfl_games as $key => $game)
{
$gameTime = date('D M d h:iA', (strtotime($game['day'] . ', ' . $game['time'] . 'pm'))) . ' EST';
$windowbg = (!empty($windowbg)) && $windowbg === 'windowbg' ? 'windowbg2' : 'windowbg';
$fu = (!empty($context['game_' . $key]) && !empty($context['user_game_' . $key])) && $context['game_' . $key] !== $context['user_game_' . $key] ? '<span style="float:right;">[U]</span>' : '<span style="float:right;">[F]</span>';
echo '
<tr class="' . $windowbg . '">
<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['away'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['away'], '</td>
<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['home'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['home'], '</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">', $gameTime, '</td>
<td class="centertext" style="padding:8px 8px 0px 8px;">', (!empty($context['spread_' . $key]) ? $context['spread_' . $key] : '0.5'), '</td>
<td class="centertext alert" style="padding:8px 8px 0px 8px;">', (!empty($context['user_game_' . $key]) ? $context['user_game_' . $key] . ' ' . $fu: '???'), '</td>
</tr>';
}
echo '
</table>
<div><br /></div>
<table width="100%">
<tr>
<td class="centertext titlebg" colspan="7">
Tie Breaker<br />
</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
<td style="padding:0px 8px 0px 8px;">Game Date</td>
<td style="padding:0px 8px 0px 8px;">Away</td>
<td style="padding:0px 8px 0px 8px;">Home</td>
<td style="padding:0px 8px 0px 8px;">Username</td>
<td style="padding:0px 8px 0px 8px;">Points Total</td>
<td colspan="2">Status</td>
</tr>
<tr style="padding:3px;">
<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user . '">', $userName, '</a></td>
<td style="padding:0px 8px 0px 8px;" class="left">
', (isset($context['nfl_user_points']) ? $context['nfl_user_points'] : '???'), '
', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
</td>
<td>
', (isset($context['nfl_user_points']) ? '<span style="color:green;">SAVED' : '<span style="color:red;">No Data'), '</span>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td style="border:0px;" colspan="7"><hr /><br /></td>
</tr>
<tr>
<td colspan="7">
', (!empty($error) ? '<span class="error">Warning: You are not permitted to view other user picks until after the cut-off time.</span><br /><br />' : '&nbsp;'), '
</td>
</tr>
</table>
<form id="nfl_pick" name="nfl_pickem" action="' . $currentPage . '" method="post">
<table width="100%">
<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
<td>
<input class="submit" type="submit" name="submit" value="Submit" onclick="var confirm = NFL_confirmSubmit(); if (confirm) {this.form.submit();} else {return false;}" />
</td>
<td colspan="5">
<div class="content">
<strong>Search Member:</strong>
<input type="text" name="NflMember" id="NflMember" value="" class="input_text" ' . (!empty($_SESSION['nfl_admin']) ? 'disabled ' : '') . '/>
<div id="NflQueryUserContainer"></div>
</div>
</td>
', empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="stats" value="Overall" />
</td>' : (!empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
<td>
<input class="submit" type="submit" name="stats" value="Exit Overall" />
</td>' : '
<td>
&nbsp;
</td>'), '
</tr>
</table>
</form>
</div>
<script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/suggest.js?fin20"></script>
<script type="text/javascript"><!-- // --><![CDATA[
var oAddMemberSuggest = new smc_AutoSuggest({
sSelf: \'oAddMemberSuggest\',
sSessionId: \'', $context['session_id'], '\',
sSessionVar: \'', $context['session_var'], '\',
sSuggestId: \'to_suggest\',
sControlId: \'NflMember\',
sSearchType: \'member\',
sPostName: \'nfl_pick\',
sURLMask: \'', !empty($_REQUEST['page']) ? 'page=' . $smcFunc['htmlspecialchars']($_REQUEST['page']) : (!empty($context['current_action']) ? 'action=' . $context['current_action'] : ''), ';member=%item_id%\',
sTextDeleteItem: \'', $txt['autosuggest_delete_item'], '\',
bItemList: false,
sItemListContainerId: \'NflQueryUserContainer\'
});
// ]]></script>
';
}
}
elseif (!empty($html) && $confirm && stripos($week_final['code'], 'PRE') === false && !$allowedView)
{
$currentPage = currentPageNflPickem() . ';error=1;member=' . $user_info['id'];
list($_SESSION['nfl_stats'], $_REQUEST['exit_stats']) = array(false, false);
header('Location: ' . $currentPage);
die();
}
elseif ((!empty($week_final)) && stripos($week_final['code'], 'PRE') !== false)
echo 'Preseason games still in effect.';
else
echo 'No data at this time.';

/* Current url */
function currentPageNflPickem($pageURL = 'http://')
{
global $context, $modSettings, $scripturl;

if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

$urlArray = explode('/', $pageURL);

return $pageURL;
}

function getInt($str)
{
preg_match("/([0-9]+[\.,]?)+/",$str,$matches);
return !empty($matches[0]) ? $matches[0] : 0;
}

/* Create NFL Tables */
function installNflTables()
{
global $smcFunc, $modSettings;

$smcFunc['db_create_table']('{db_prefix}nfl_tiebreakers',
array(
array(
'name' => 'week',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
'auto' => false
),
array(
'name' => 'logged',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'home',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'away',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(
'name' => 'picks',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
),
array(
array(
'type' => 'primary',
'columns' => array('week')
),
),
array(),
'ignore'
);

$smcFunc['db_create_table']('{db_prefix}nfl_weekly',
array(
array(
'name' => 'id_member',
'type' => 'int',
'size' => 10,
'null' => false,
'unsigned' => true,
),
),
array(
array(
'type' => 'primary',
'columns' => array('id_member')
),
),
array(),
'ignore'
);

for ($i=1; $i<=17; $i++)
$smcFunc['db_add_column']('{db_prefix}nfl_weekly',
array(
'name' => 'week' . (string)$i,
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(),
'ignore',
'fatal'
);

$smcFunc['db_add_column']('{db_prefix}nfl_tiebreakers',
array(
'name' => 'picks',
'type' => 'varchar',
'size' => 255,
'null' => false,
'unsigned' => true,
),
array(),
'ignore',
'fatal'
);

// set the install flag
$setArray['nfl_installed'] = 1;
$modSettings['nfl_installed'] = 1;
updateSettings($setArray);

return;
}

function nfl_gameYear()
{
// 2014 weekly confirmation
$lastGamesArray = array(
0 => array('1', '1', '1', '1', '1', '1'),
1 => array('SD', 'ARI', '1', '1410232800', '1409880600', '1410112800'),
2 => array('PHI', 'IND', '1', '1410831000', '1410485100', '1410717600'),
3 => array('CHI', 'NYJ', '1', '1411435800', '1411089900', '1411322400'),
4 => array('NE', 'KC', '1', '1412040600', '1411694700', '1411927200'),
5 => array('SEA', 'WSH', '1', '1412645400', '1412299500', '1412532000'),
6 => array('SF', 'STL', '1', '1413250200', '1412904300', '1413136800'),
7 => array('HOU', 'PIT', '1', '1413855000', '1413509100', '1413741600'),
8 => array('WSH', 'DAL', '1', '1414459800', '1414113900', '1414346400'),
9 => array('IND', 'NYG', '1', '1415064600', '1414718700', '1414951200'),
10 => array('CAR', 'PHI', '1', '1415669400', '1415323500', '1415556000'),
11 => array('PIT', 'TEN', '1', '1416274200', '1415928300', '1416160800'),
12 => array('BAL', 'NO', '1', '1416879000', '1416533100', '1416765600'),
13 => array('MIA', 'NYJ', '3', '1417483800', '1417109100', '1417123800', '1417138200', '1417370400'),
14 => array('ATL', 'GB', '1', '1418088600', '1417742700', '1417975200'),
15 => array('NO', 'CHI', '1', '1418693400', '1418347500', '1418580000'),
16 => array('DEN', 'CIN', '1', '1419298200', '1418952300', '1419098400'),
17 => array('STL', 'SEA', '0', '1419801900', '1419789600', '1419789600'),
);

return $lastGamesArray;
}

function NFL_pool_pagination($content, $redirect, $count = 10)
{
/* PHP pagination - max 7 visible integers and 6 periods (all links) - current page encircled with square brackets
* This php pagination code was developed by Underdog copyright 2013, 2014
* http://web-develop.ca
* Licensed under the GNU Public License: http://www.gnu.org/licenses/gpl.html
*/

// This particular function is only used when opting an entire table else it is not necessary
global $context, $scripturl;

/*  Set the $context variables for the display template  */
$context['current_count'] = count($content);
$context['current_pages'] = ((int)($context['current_count'] / $count)) + 1;
$redirect = !empty($redirect) ? $redirect : $scripturl;
$maxPages = 1;

if (count($content) <= $count || $count < 2)
{
$context['current_pages'] = 0;
return $content;
}

// Did they enter an invalid page via the url?
if ((int)($context['current_count']/$count) == $context['current_count']/$count)
$maxPages = $context['current_count']/$count;
elseif($context['current_count'] %$count != 0)
$maxPages += ($context['current_count'] - ($context['current_count'] % $count)) / $count;

if ((int)$context['current_page']+1 > $maxPages)
redirectexit($redirect . 'current_page=' . $maxPages . ';#page_top');
elseif ((int)$context['current_page'] < 0)
redirectexit($redirect . 'current_page=1;#page_top');

// Now calculate the part of the array to display
if (($context['current_count'] / $count) == (int)($context['current_count'] / $count))
$context['current_pages'] = ($context['current_count'] / $count);

$context['current_showResults'] = array(((int)$context['current_page'] * $count), (((int)$context['current_page'] + 1) * $count) - 1);

if ((int)$context['current_page']+1 == (int)$context['current_pages'])
    $context['current_showResults'][1] = count($content);
else
    $context['current_showResults'][1] = (int)$context['current_page']*$count + ($count-1);

foreach($content as $key => $var)
{
$counter = array_search($key , array_keys($content));
if ($counter >= (int)$context['current_showResults'][0] && $counter <= (int)$context['current_showResults'][1])
$new_content[$key] = $var;
}

if (!empty($new_content))
$context['current_showResults'][1] = ((int)$context['current_page']*$count) + count($new_content);
else
$new_content = $content;

return $new_content;
}

function NFL_pool_pages($lang, $anchor, $link, $pages, $sort=false, $order=false)
{
/* PHP pagination - max 7 visible integers and 6 periods (all links) - current page encircled with square brackets
* This php pagination code was developed by Underdog copyright 2013, 2014
* http://web-develop.ca
* Licensed under the GNU Public License: http://www.gnu.org/licenses/gpl.html
*/
global $context;

$pageCount = 1;
$display = array('page' => false, 'pages' => '0');
$page = !empty($context['current_page']) ? (int)$context['current_page'] : 0;
$display['pages'] = !empty($pages) ? (int)$pages : 1;

if ($display['pages'] > 1)
{
    $display['page'] =  '
    <script type="text/javascript"><!-- // --><![CDATA[
function changeColor(s)
{
document.getElementById("link"+s).style.color = "red";
}
function changeColorBack(s)
{
document.getElementById("link"+s).style.color = "blue";
}
    // ]]></script>
    <span style="text-align:center;position:relative;width:99%;display:inline-block;">
' . $lang . '<br />';

    while ($pageCount < (int)$display['pages']+1)
    {
$current_page = (int)$page+1;
$total = (int)$display['pages'];

if ($pageCount == 1 || $pageCount == $total || $pageCount == $current_page || $pageCount == $current_page+1 ||
    $pageCount == $current_page+2 || $pageCount == $current_page-1 || $pageCount == $current_page-2)
{
    if ((int)$pageCount == (int)$page+1)
$display['page'] .= '
<a onclick="this.href=\'javascript: void(0)\';" onmouseout="changeColor('. $pageCount . ')" onmouseover="changeColorBack(' . $pageCount . ')" id="link' . $pageCount . '" style="color:red;text-decoration:none;" href="'. $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">[' . $pageCount . ']</a> ';
    else
$display['page'] .= '
<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">' . $pageCount . '</a> ';
}
elseif ($pageCount < $current_page-2 && $pageCount > $current_page-6)
$display['page'] .= '
<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">.</a> ';
elseif ($pageCount > $current_page+2 && $pageCount < $current_page+6)
$display['page'] .= '
<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">.</a> ';

$pageCount++;
    }

    $display['page'] .= '
    </span>';
}

return $display;
}

function nfl_user_stats($currentPage, $checkWeek)
{
global $user_info, $scripturl, $smcFunc;
$nfl_pick = 0;

if (isset($_REQUEST['exit_stats']))
{
list($_SESSION['nfl_stats'], $_REQUEST['exit_stats']) = array(false, false);
header('Location: ' . $currentPage);
die();
}

$timeAlotted = !$user_info['is_admin'] ? time() : time() + 691200;
$member = isset($_REQUEST['nfl_member']) ? abs((int)$_REQUEST['nfl_member']) : 0;
$week = isset($_REQUEST['nfl_week']) ? abs((int)$_REQUEST['nfl_week']) : $checkWeek;
$context['current_page'] = !empty($context['current_page']) ? (int)$context['current_page'] : 1;
$context['current_page'] = (!empty($_REQUEST['current_page']) ? (int)$_REQUEST['current_page'] : $context['current_page']) -1;
$limit = ($context['current_page'] + 1) * 10;
$spreads = array();

$request = $smcFunc['db_query']('', "
SELECT week, picks
FROM {db_prefix}nfl_tiebreakers
ORDER BY week",
array()
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$queryWeek = !empty($row['week']) ? (int)$row['week'] : 0;
$spreads['week' . $queryWeek] = !empty($row['picks']) ? $row['picks'] : array();
}
$smcFunc['db_free_result']($request);

foreach ($spreads as $weekCount => $pickem)
{
$weekFavs = explode('_', $pickem);
$weekSpreads[$weekCount] = array();
foreach ($weekFavs as $weekFav)
{
$spread = abs((int)getInt($pickem)) . '.5';
$team = trim(str_replace(range(0,9), '', $pickem), '.');
$weekSpreads[$weekCount][][$team] = $spread;
}

}

if ($member < 1)
{
$request = $smcFunc['db_query']('', "
SELECT u.id_member
FROM {db_prefix}nfl_weekly AS u
WHERE id_member >= {int:page}
ORDER BY id_member
LIMIT 10",
array('week' => $checkWeek, 'page' => $limit)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$memberQuery = !empty($row['id_member']) ? (int)$row['id_member'] : 0;
for ($i=1; $i <=17; $i++)
{
$data['user_' . $memberQuery]['week' . $i] = array(
'user_picks' => !empty($row['week' . $i]) ? explode('_', $row['week' . $i]) : array(),
'tie_points' => (int)getInt($user_picks[(count($user_picks) -1)]),
'picks' => !empty($row['week' . $i]) ? explode('_', $row['week' . $i]) : array(),
);
}
/*
$userPoints[] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
array_pop($user_picks);
*/
}
$smcFunc['db_free_result']($request);

/*
foreach ($user_picks as $user_pickem)
{
$context['user_game_' . $nfl_pick] = (!empty($user_pickem)) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
$nfl_pick++;
}
*/
}
else
{
$request = $smcFunc['db_query']('', "
SELECT u.id_member
FROM {db_prefix}nfl_weekly AS u
WHERE id_member = {int:member}
LIMIT 1",
array('week' => $checkWeek, 'member' => $member)
);
}

echo '
<div style="display:inline;" class="windowbg">
<div class="cat_bar">
<h3 class="centertext catbg">NFL Pool Overall Sheet</h3>
</div>
<form id="nfl_pick" name="nfl_pickem" action="' . $currentPage . '" method="post">
<table width="100%">
<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
', $user_info['is_admin'] ? '
<td colspan="6">
<input class="submit" type="submit" name="submit" value="Submit" />
</td>
' : '
<td colspan="6">
&nbsp;
</td>', '
<td style="float:right;">
<input class="submit" type="submit" name="exit_stats" value="Exit Overall" />
</td>
</tr>
</table>
</form>
</div>';
return;
}


It has incomplete bits in there for viewing user info (including pages) which is my next thing to work on.
More descriptive titles and some instruction is needed.
I also will add the ability for it to auto calculate the weekly winner and will have it give options for viewing picks as follows:

Admin only:
- specific user picks
- ability to manually enter picks for a user (prior to onset of first game)

General:
- view list of all user picks for the week after weekly deadline (< using pages)
- view overall tally of points (<- using pages or view all) ~ week winners will be highlighted
- ability to print both of the above

Chen Zhen


I updated the code block for the last post yet again with some changes.
It now allows admin/users to query other peoples individual picks from an auto-complete name input.
Admins can view them anytime but regular users have to wait until after the cut-off date.
This also allows Admins to edit other user data for whatever reason (ie. manually adding user picks).

Imo it might be best for me to convert this project to a mod now and create a board for it.
After I do that the next step will be the overall display.
2 templates are needed for it...
1) Display all participants picks in a graph (names on left ~ fav/und on top) whereas it will display F or U for each game
2) After each week has completed a list is updated containing total correct for each week + winner of each week is highlighted

Its a slow process since my time is limited although I have 17 weeks to do it.  ;D
I may just scrap the current API, use a file containing an array of the current season and find another API or do a page scrape for outcomes/scores.

  The current API contains scores only for the current week and then it flips to the next week schedule at a certain day/time as far as I know. I could be wrong about that and have to wait for the first week to complete to see how it functions.
That means in order to ensure it updated that scores automatically one (admin) would have to make sure to log in at a certain time and intiiate the code one way or another else create a crom job to do it. Either way I do not like the idea of it having to be done at a limited window of time.. it needs a source that can be accessed at any time for any given regular season week.  Once I get everything else done (to a point) then I am going to look into this.



Chen Zhen


Ok fixed it again for the cut-off date.. hopefully it works properly now concerning not allowing people to edit their picks after the first game on the list starts (we skip Thursday game so it is Sun. 1pm EST). Hopefully that API displays all weeks in the json array which will make it easier for me!!