Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 347 348 [349] 350 351 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 886081 times)

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #5220 on: November 19, 2013, 08:59:36 am »

Isn't a foreign key just a column that is used to identify the row in another table?

Annoying syntax aside, I've never thought of SQL databases as fundamentally difficult concepts, it's just when working with a large database for me the difficulty came in comprehending the grandness of the scale of the thing and how to execute your queries and statements in a way that doesn't cause it to grind to a halt because of a bad index.

In-code this wasn't much of a problem, just keep it simple and profile any SQL you're even slightly unsure about (Microsoft SQL Server has good tools for that), but if ever a developer was called upon to investigate something that required doing SQL statements on the backup data that needed to join over multiple tables or databases and not necessarily on indexes...Well, needless to say fun times were had by all.
« Last Edit: November 19, 2013, 09:17:22 am by MorleyDev »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5221 on: November 19, 2013, 09:17:15 am »

Foreign key constraints are actually the hardest part of the beginners' chapter, which is where I stopped.

Same here, although I don't bother with formal foreign key constraints in my database tables.  In fact, I was under the impression that MySQL didn't even support such a thing for a very long time.  Maybe it was just certain storage engines?
MyIsam doesn't support them, but InnoDb does.

Isn't a foreign key just a column that is used to identify the row in another table?
Yes, but you can make them official by making a constraint for it, so it shouldn't have a value that's not in the other table, and you can cascade actions. I rarely use cascades as well, but sometimes cascading delete actions is very useful.

Nowadays I don't even write a lot of SQL anymore, it's all DAO or "Activerecord" stuff that does things like these for you:
Spoiler (click to show/hide)


Edit: in other news, I'm publishing MOAR FREE OPEN SOFTWAREZ (http://www.yiiframework.com/extension/backjob/)
« Last Edit: November 19, 2013, 09:20:41 am by Siquo »
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5222 on: November 19, 2013, 03:04:12 pm »

I made my first network multiplayer game!
Spoiler (click to show/hide)
Basically it's a game server where people can login with their clients and move a colored dot around, but damn if it isn't satisfying to have achieved. It took over 700 nontrivial lines of code (not counting the display code), and it works over LAN too.

In other happy news:
When I was 13, I programmed a platformer with Perl/Tk. It was strictly procedural, had no kind of object structure, didn't use references or pointers, and on top of it all, used only global variables (not even function parameters). Input was raw terminal input, so the protagonist's speed depended on the system's keyboard settings (character repeat speed/delay), and every creature was drawn by changing the coordinates of 64 rectangles on Tk's canvas. Even so, it was completely playable, had checkpoints, power-ups, different types of badguys, moving platforms and even a final boss.
Guess what I found in the furthest recesses of my mail account.
It's an early version, so it doesn't have checkpoints, platforms or a final boss, but all the other features are there. And yes, it's almost as bad as I remembered it (at least it uses some function parameters).

Here's a screenshot:
Spoiler (click to show/hide)
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5223 on: November 20, 2013, 02:06:28 pm »

Adventures in dreammaker:
Quote
<drsingh> . =  . ? ..() : .
<drsingh> a valid line of dream maker code
Quote
<MrPerson> . << . should also be valid and yet it's terrible
Quote
<drsingh> while (.) ..()
<Head> ./var/a = "cake",
<Head> ;
<chinsky> {;;}
Quote
<drsingh> var/\?=2
<drsingh>  \? += 3
Quote
<MrPerson> var/\\/\/\\\//varname

All of these are valid and will compile.

Quote
<drsingh> i am from the badcode tribe
<drsingh> this is the language of my people

Never try DM, kids. It will corrupt your soul.
« Last Edit: November 20, 2013, 03:07:11 pm by miauw62 »
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Maklak

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5224 on: November 20, 2013, 02:26:50 pm »

I was considering integrating my scripts into a big one, with several tables, checkboxes for which columns to display, links from table to table, detailed info when clicking on an ID and other fancy features. But apart from how much work this would require, I've hit a wall.

I could use cgi and continue to use the browser for user interface, but I don't want to tell end users to install www servers; installing perl is bad enough.
Setting up a free service where people could upload their RAWs is not only beyond me, but the users would also need to reupload files each time they change the RAWs.

I could try and use a GUI, but Perl::TK refuses to compile for Windows because "No X Server". If I have trouble with I, I don't want the end users to be forced to do it.

> Obligatory Python suggestion.
Well, I'm not desperate enough to stomach a language without curly braces.

I might switch to another scripting language with a GUI, but what? Tcl::Tk? I would prefer to stick with Perl.

So I'm out of ideas. Any suggestions?

> Python.
I'd rather not do it at all than suffer a language without curly braces. In any case, porting thousands of lines of code to another language takes time, so I'd rather stick with perl.
« Last Edit: November 20, 2013, 03:04:25 pm by Maklak »
Logged
Quote from: Omnicega
Since you seem to criticize most things harsher than concentrated acid, I'll take that as a compliment.
On mining Organics
Military guide for FoE mod.
Research: Crossbow with axe and shield.
Dropbox referral

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5225 on: November 20, 2013, 02:52:25 pm »

Obligatory Python suggestion. Different Linux distros may or may not package TkInter with their version of Python, but it's "standard" and should be easily installable if it's not already. The Windows build should come with everything already, as IDLE, the simplistic IDE that it ships with, is built using TkInter.

Really, it wouldn't have to be Python. If you like Tk, it's also the default for Ruby and, I assume, some others.
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5226 on: November 20, 2013, 03:02:32 pm »

I've been screwing around with JSON Spirit(it's a json parser), which required building an object library if I don't want to wait 5 minutes for it to shift through a shitton of template code whenever I compile my stuff.

So I started a static library project in Code::Blocks called json_spirit_obj. Changed main.c to main.cpp and compiled. The end result was a file called main.o in the place where it's supposed to put the compiled stuff, and libjson_spirit_obj.a next to the project file. I then went back to the thing I was trying to make work, to link whichever one of the two I needed to do stuff with, and noticed that I didn't need to to anything as it apparently found whatever it needed on its own.

I'm happy and all that it works. But I would still prefer it if I understood what the hell I was doing and why it worked.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #5227 on: November 20, 2013, 03:22:17 pm »

So my University project has a server and client side component. Now, deciding on the language for the client side implementation is easy. Done it. The server-side, I'm a little stuck.

It's a ReST API with the ability to get information about libraries. Name, dependencies, and a downloadable script that the client executes. It's my C++ dependency installer and manager idea I had awhile ago. So the actual server only needs to be one of those "wrappers around a Database" things. High read, low write. Must be hostable on Linux and Windows.

And the choice of what to implement that Server in? That's the tricky one. Basically the decision is in: C#, Scala or Node.js? Tricky one. I guess it's all coming down to the tools. I have the most experience with C# and WebApi (having used that one professionally for a year), but I'm leaning more towards Scala or Node.js.

I do like Scala more as a language, but Node.js is a nice technology even if I do have an initial "ugh, javascript?" reaction xD And given it's simplicity, it may be more suited to a server that's only jobs will be user authentication and database operations to return either xml, json or a lua file...

Library wise they're about equal, Scala does also let you use a lot of Java libraries and technologies though on top of the very nice functional Scala syntax. But linq.js brings more or less that same syntax to Javascript anyway.

Anybody here with Scala and/or node.js experience want to try and nudge me in either direction? :) Possible arguments include which language has better editing tools or is better suited to iterative test driven development driving the functionality of the API of the server from the needs of the native client application.
« Last Edit: November 20, 2013, 04:41:28 pm by MorleyDev »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5228 on: November 20, 2013, 05:40:37 pm »

I was considering integrating my scripts into a big one, with several tables, checkboxes for which columns to display, links from table to table, detailed info when clicking on an ID and other fancy features. But apart from how much work this would require, I've hit a wall.

I could use cgi and continue to use the browser for user interface, but I don't want to tell end users to install www servers; installing perl is bad enough.
Setting up a free service where people could upload their RAWs is not only beyond me, but the users would also need to reupload files each time they change the RAWs.

I could try and use a GUI, but Perl::TK refuses to compile for Windows because "No X Server". If I have trouble with I, I don't want the end users to be forced to do it.

> Obligatory Python suggestion.
Well, I'm not desperate enough to stomach a language without curly braces.

I might switch to another scripting language with a GUI, but what? Tcl::Tk? I would prefer to stick with Perl.

So I'm out of ideas. Any suggestions?

> Python.
I'd rather not do it at all than suffer a language without curly braces. In any case, porting thousands of lines of code to another language takes time, so I'd rather stick with perl.

Write a script that just generates HTML pages. You can view those in a browser without using a server.
Logged

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #5229 on: November 20, 2013, 05:50:23 pm »

I was considering integrating my scripts into a big one, with several tables, checkboxes for which columns to display, links from table to table, detailed info when clicking on an ID and other fancy features. But apart from how much work this would require, I've hit a wall.

I could use cgi and continue to use the browser for user interface, but I don't want to tell end users to install www servers; installing perl is bad enough.
Setting up a free service where people could upload their RAWs is not only beyond me, but the users would also need to reupload files each time they change the RAWs.

I could try and use a GUI, but Perl::TK refuses to compile for Windows because "No X Server". If I have trouble with I, I don't want the end users to be forced to do it.

> Obligatory Python suggestion.
Well, I'm not desperate enough to stomach a language without curly braces.

I might switch to another scripting language with a GUI, but what? Tcl::Tk? I would prefer to stick with Perl.

So I'm out of ideas. Any suggestions?

> Python.
I'd rather not do it at all than suffer a language without curly braces. In any case, porting thousands of lines of code to another language takes time, so I'd rather stick with perl.

If Python scares you too much you could try Lua.

But as MagmaMcFry mentioned, a plain HTML page (+CSS and JS) would be a nice solution, but if you can't stomach Python then I doubt you'd enjoy that.
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5230 on: November 20, 2013, 06:11:14 pm »

I came back from vacation last week to learn that we're finally going to start using source control at work.  Git, to be specific.  Great, I've been gently nudging that we should do this for like a year now.

Cue me and everyone else spending the next few days scratching our heads and bumbling around with various Windows and Mac Git clients and trying to get them to work with our IDEs.  There was also lots of, "Oh, this is really confusing now," and "This is a major pain in the groin."

I'm starting to see the benefits of source control already (it saved a file I destroyed today), but properly using it is still a long time coming here.  Rather than jump straight into using it for our production sites, we still edit the files directly there but push source changes to a Git repo as a means of tracking changes and keeping lightweight "backups" (we backup elsewhere regularly, so no need to worry about that at least).  There's no way this is going to stay synchronized for long.

The worst part is I have no idea how we're going to set this up so that we can use it efficiently.  We need to be able to test by doing as little more than saving a file and refreshing a browser as is possible.  Short of setting up web servers on each developer's machine and creating testing harnesses of sorts, I don't know how we'll achieve that.  I guess that would work in theory at least.  Pull the repo, make changes to anything you need to your local repo, testing on your machine, then push when it's all done.  It's going to be a pain to set up though.
Logged
Through pain, I find wisdom.

Maklak

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5231 on: November 20, 2013, 06:20:07 pm »

> Write a script that just generates HTML pages. You can view those in a browser without using a server.
>:( I already have writing html files as an option and it works pretty well and I've clearly said that in my question as well as in the linked thread. I do txt, csv, bbcode and wikicode tables too, but overall html is the most useful.
It is just that the tables are plain and if I want links in them (other than linking to part of the same page), I'd either need the links to talk to a web server or somehow use javascript to show and hide tables and do that on the client side. I'd rather not require users to install their own web servers and I don't know enough javascript to heavily modify a static web page when the user clicks on a link.

> If Python scares you too much you could try Lua.
Sigh. Is there a good an portable way to make an UI or something in Lua? Because if not, then it's just work with no benefits over perl.

I'm barely motivated as it is, so I guess, I'll just stick to ~1k lines of code scripts that each print a single table for now. Maybe add a shell script to generate several tables with different options with a single command. I'm still at least armour, materials and syndromes / interactions and a manual from being done anyway. I was looking for an elegant way to modularise and bind the scripts, but if it's too much work or too difficult, then screw that. Die hard fans, like Meph and myself, will use those scripts as they are anyway.

> Nope, not true. You can link to other files relative to your current HTML file in the filesystem [...] 
Hm, assuming I print several tables into some directory using a shell script, this is worth testing. I'd just need an option --link for all the scripts. Thanks for the idea.
« Last Edit: November 20, 2013, 06:55:24 pm by Maklak »
Logged
Quote from: Omnicega
Since you seem to criticize most things harsher than concentrated acid, I'll take that as a compliment.
On mining Organics
Military guide for FoE mod.
Research: Crossbow with axe and shield.
Dropbox referral

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5232 on: November 20, 2013, 06:40:45 pm »

It is just that the tables are plain and if I want links in them (other than linking to part of the same page), I'd either need the links to talk to a web server or somehow use javascript to show and hide tables and do that on the client side.
Nope, not true. You can link to other files relative to your current HTML file in the filesystem:
Code: [Select]
<a href="thispage.html#foo"></a>
<a href="folder/thatpage.html#bar"></a>
<a href="../otherpage.html#baz"></a>
Not sure whether the last one works, but you could just put all your files into one folder, that would make more sense anyway.
Logged

da_nang

  • Bay Watcher
  • Argonian Overlord
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5233 on: November 21, 2013, 05:15:03 pm »

Alright, I can't for the life of me figure out why the first thread always continues to starve the other threads in this dining philosopher program.
Code: [Select]
/* Eating philosophers
   philo.c
   Compile as gcc philo.c -o philo -lpthread
*/

/* Hey, thread 0! Stop being a dick and starving the other threads! */

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define NUMPHILO 6
#define PORTIONS 10
#define LEFT(n) (n+1)%NUMPHILO
#define RIGHT(n) (n+NUMPHILO-1)%NUMPHILO

int statusForks[NUMPHILO];
int histogram[NUMPHILO];
int platesFinished = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t data = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t forks[NUMPHILO];
int start = 0;

void *philo(void * n);
void ponder(int n);
void eat(int n);
void takeForks(int n);
void putForks(int n);

void *philo(void * n) {
  int * i = (int *) n;
  while(1) {
    if (start) {
      takeForks(*i);
      break;
    }
  }
}

void ponder(int n){
  printf("Philosopher %i is pondering his existence...\n", n);
}

void takeForks(int n){
  while(platesFinished < PORTIONS) {
    ponder(n);
    pthread_mutex_lock(forks+LEFT(n));
    if (statusForks[LEFT(n)] == 1) {
      statusForks[LEFT(n)] = 0;
      pthread_mutex_unlock(forks+LEFT(n));

      pthread_mutex_lock(forks + RIGHT(n));

      if (statusForks[RIGHT(n)] == 1) {
        statusForks[RIGHT(n)] = 0;
        pthread_mutex_unlock(forks+RIGHT(n));
        eat(n);
        putForks(n);
        pthread_yield();
      }
      else {
        pthread_mutex_unlock(forks+RIGHT(n));
        pthread_mutex_lock(forks+LEFT(n));
        statusForks[LEFT(n)] = 1;
        pthread_mutex_unlock(forks+LEFT(n));
      }
    }
    else {
      pthread_mutex_unlock(forks+LEFT(n));
    }
    pthread_yield();
  }
}

void eat(int n){
  pthread_mutex_lock(&data);
  if(platesFinished == PORTIONS) {
    printf("No more food for philosopher %i.\n", n);
  }
  else {
    printf("Philosopher %i is gorging on the food!\n", n);
    platesFinished++;
    histogram[n]++;
    printf("Philosopher %i is sated. For now...\n");
  }
  pthread_mutex_unlock(&data);
}

void putForks(int n){
  pthread_mutex_lock(forks + LEFT(n));
  pthread_mutex_lock(forks + RIGHT(n));
  statusForks[LEFT(n)] = 1;
  statusForks[RIGHT(n)] = 1;
  pthread_mutex_unlock(forks+RIGHT(n));
  pthread_mutex_unlock(forks+LEFT(n));
}

int main() {
  pthread_t philosophers[NUMPHILO];
  int i;
  for(i = 0; i<NUMPHILO; i++) {
    statusForks[i] = 1;
    histogram[i] = 0;
    pthread_mutex_init(&forks[i], NULL);
  }

  //Create philosophers
  for(i = 0; i<NUMPHILO; i++) {
    if (pthread_create(&philosophers[i], NULL,
                       philo, (void*) &i) != 0) {
      perror("Error creating thread: ");
      return 1;
    }
    printf("Philosopher %i created.\n", i);
  }

  //start the dining simulation
  start++;

  for(i = 0; i<NUMPHILO; i++) {
    pthread_join(philosophers[i], NULL);
  }

  for(i = 0; i<NUMPHILO; i++) {
    printf("Philosopher %i ate %i times\n", i, histogram[i]);
  }

  return 0;
}
Other than the scheduler being an asshole and favoring unblocked threads, I can't see how the treads remain starved.
Logged
"Deliver yesterday, code today, think tomorrow."
Ceterum censeo Unionem Europaeam esse delendam.
Future supplanter of humanity.

Maklak

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5234 on: November 21, 2013, 06:33:09 pm »

I suggest that you use gdb and examine what's going on in each thread.
Failing that, add more printfs and fflush(stdout).

Hm, with your definitions of LEFT and RIGHT, this isn't a dining philosopher problem per se, but instead you have two independent groups: {0, 2, 4} use forks {1, 3, 5} and {1, 3, 5} use forks {0, 2, 4}, but this shouldn't matter. 

> pthread_mutex_lock(forks+LEFT(n));
This should work, but I prefer notation
pthread_mutex_lock(&forks[LEFT(n)]);

I don't see where the problem is either. I can't debug it under Windows, but I've even added _sleep(100 + rand()%100) and the result is still the same: Only the first philosopher ever does anything. Furthermore, I never see any of them but zero ever pondering, so it looks like the first thread is executed until it exits, then the rest of them see there's no more food and join without ever pondering. This is odd.

> //Notice it's the same integer literal, same address? The value was always 0 when cast back to int* in the philo function.
> //Solution is to use a goddamn different pointer for each thread, malloc this stuff up.
But the value of i changes, so if this theory is true, every thread would have number 5. 
Still, in philo you could change one line to separate thread numbers.
  int i = *((int *) n);
But using another array is more foolproof.
« Last Edit: November 21, 2013, 07:07:48 pm by Maklak »
Logged
Quote from: Omnicega
Since you seem to criticize most things harsher than concentrated acid, I'll take that as a compliment.
On mining Organics
Military guide for FoE mod.
Research: Crossbow with axe and shield.
Dropbox referral
Pages: 1 ... 347 348 [349] 350 351 ... 796